1 Vue
.component('my-problems', {
4 problems: problemArray
, //initial value
6 fen: V
.GenRandInitFen(),
9 stage: "nothing", //or "preview" after new problem is filled
15 <button @click="fetchProblems('backward')">Previous</button>
16 <button @click="fetchProblems('forward')">Next</button>
17 <button @click="showNewproblemModal">New</button>
18 <my-problem-summary v-on:show-problem="bubbleUp(p)"
19 v-for="(p,idx) in sortedProblems" v-bind:prob="p" v-bind:key="idx">
21 <input type="checkbox" id="modal-newproblem" class="modal">
22 <div role="dialog" aria-labelledby="newProblemTxt">
23 <div v-show="newProblem.stage=='nothing'" class="card newproblem-form">
24 <label for="modal-newproblem" class="modal-close"></label>
25 <h3 id="newProblemTxt">Add problem</h3>
26 <form @submit.prevent="previewNewProblem">
28 <label for="newpbFen">Fen</label>
29 <input id="newpbFen" type="text" v-model="newProblem.fen"/>
32 <p class="emphasis">Safe HTML tags allowed</p>
33 <label for="newpbInstructions">Instructions</label>
34 <textarea id="newpbInstructions" v-model="newProblem.instructions"
35 placeholder="Explain the problem here"/>
36 <label for="newpbSolution">Solution</label>
37 <textarea id="newpbSolution" v-model="newProblem.solution"
38 placeholder="How to solve the problem?"/>
39 <button class="center-btn">Preview</button>
43 <div v-show="newProblem.stage=='preview'" class="card newproblem-preview">
44 // TODO: we don't want exactly the same display (-date +solution)
45 <my-problem-summary v-bind:prob="newProblem"></my-problem-summary>
46 <button @click="sendNewProblem()" class="center-btn">Send</button>
52 sortedProblems: function() {
53 // Newest problem first
54 return this.problems
.sort((p1
,p2
) => { return p2
.added
- p1
.added
; });
56 mailErrProblem: function() {
57 return "mailto:contact@vchess.club?subject=[" + variant
+ " problems] error";
61 // Propagate "show problem" event to parent component (my-variant)
62 bubbleUp: function(problem
) {
63 this.$emit('show-problem', JSON
.stringify(problem
));
65 fetchProblems: function(direction
) {
66 return; //TODO: re-activate after server side is implemented (see routes/all.js)
67 if (this.problems
.length
== 0)
68 return; //what could we do?!
69 // Search for newest date (or oldest)
70 let last_dt
= this.problems
[0].added
;
71 for (let i
=0; i
<this.problems
.length
; i
++)
73 if ((direction
== "forward" && this.problems
[i
].added
> last_dt
) ||
74 (direction
== "backward" && this.problems
[i
].added
< last_dt
))
76 last_dt
= this.problems
[i
].added
;
79 ajax("/problems/" + variant
, "GET", {
83 if (response
.problems
.length
> 0)
84 this.problems
= response
.problems
;
87 showNewproblemModal: function() {
88 document
.getElementById("modal-newproblem").checked
= true;
90 previewNewProblem: function() {
91 if (!V
.IsGoodFen(this.newProblem
.fen
))
92 return alert("Bad FEN string");
93 this.newProblem
.stage
= "preview";
95 sendNewProblem: function() {
96 // Send it to the server and close modal
97 ajax("/problems/" + variant
, "POST", {
98 fen: this.newProblem
.fen
,
99 instructions: this.newProblem
.instructions
,
100 solution: this.newProblem
.solution
,
102 document
.getElementById("modal-newproblem").checked
= false;
103 this.newProblem
.stage
= "nothing";