Final fixes in variants
[vchess.git] / public / javascripts / components / problems.js
CommitLineData
4ecf423b 1Vue.component('my-problems', {
da06a6eb
BA
2 data: function () {
3 return {
45109880
BA
4 problems: problemArray, //initial value
5 newProblem: {
6 fen: V.GenRandInitFen(),
7 instructions: "",
8 solution: "",
9 stage: "nothing", //or "preview" after new problem is filled
10 },
da06a6eb
BA
11 };
12 },
4ecf423b 13 template: `
da06a6eb 14 <div>
7931e479
BA
15 <button @click="fetchProblems('backward')">Previous</button>
16 <button @click="fetchProblems('forward')">Next</button>
da06a6eb 17 <button @click="showNewproblemModal">New</button>
c794dbb8 18 <my-problem-summary v-on:show-problem="bubbleUp(p)"
b5fb8e69
BA
19 v-for="(p,idx) in sortedProblems"
20 v-bind:prob="p" v-bind:preview="false" v-bind:key="idx">
da06a6eb
BA
21 </my-problem-summary>
22 <input type="checkbox" id="modal-newproblem" class="modal">
23 <div role="dialog" aria-labelledby="newProblemTxt">
45109880 24 <div v-show="newProblem.stage=='nothing'" class="card newproblem-form">
da06a6eb
BA
25 <label for="modal-newproblem" class="modal-close"></label>
26 <h3 id="newProblemTxt">Add problem</h3>
45109880 27 <form @submit.prevent="previewNewProblem">
da06a6eb
BA
28 <fieldset>
29 <label for="newpbFen">Fen</label>
45109880 30 <input id="newpbFen" type="text" v-model="newProblem.fen"/>
da06a6eb
BA
31 </fieldset>
32 <fieldset>
45109880 33 <p class="emphasis">Safe HTML tags allowed</p>
da06a6eb 34 <label for="newpbInstructions">Instructions</label>
45109880 35 <textarea id="newpbInstructions" v-model="newProblem.instructions"
b5fb8e69 36 placeholder="Explain the problem here"></textarea>
da06a6eb 37 <label for="newpbSolution">Solution</label>
45109880 38 <textarea id="newpbSolution" v-model="newProblem.solution"
b5fb8e69 39 placeholder="How to solve the problem?"></textarea>
45109880 40 <button class="center-btn">Preview</button>
da06a6eb 41 </fieldset>
da06a6eb
BA
42 </form>
43 </div>
45109880 44 <div v-show="newProblem.stage=='preview'" class="card newproblem-preview">
b5fb8e69
BA
45 <label for="modal-newproblem" class="modal-close"></label>
46 <my-problem-summary v-bind:prob="newProblem" v-bind:preview="true">
47 </my-problem-summary>
48 <div class="col-sm-12 col-md-6 col-lg-3 col-lg-offset-3 topspace">
49 <button @click="sendNewProblem()">Send</button>
50 <button @click="newProblem.stage='nothing'">Cancel</button>
51 </div>
45109880 52 </div>
da06a6eb 53 </div>
4ecf423b
BA
54 </div>
55 `,
da06a6eb
BA
56 computed: {
57 sortedProblems: function() {
58 // Newest problem first
7931e479 59 return this.problems.sort((p1,p2) => { return p2.added - p1.added; });
da06a6eb
BA
60 },
61 mailErrProblem: function() {
62 return "mailto:contact@vchess.club?subject=[" + variant + " problems] error";
63 },
64 },
65 methods: {
c794dbb8
BA
66 // Propagate "show problem" event to parent component (my-variant)
67 bubbleUp: function(problem) {
68 this.$emit('show-problem', JSON.stringify(problem));
69 },
da06a6eb 70 fetchProblems: function(direction) {
7931e479
BA
71 return; //TODO: re-activate after server side is implemented (see routes/all.js)
72 if (this.problems.length == 0)
73 return; //what could we do?!
74 // Search for newest date (or oldest)
75 let last_dt = this.problems[0].added;
76 for (let i=0; i<this.problems.length; i++)
77 {
78 if ((direction == "forward" && this.problems[i].added > last_dt) ||
79 (direction == "backward" && this.problems[i].added < last_dt))
80 {
81 last_dt = this.problems[i].added;
82 }
83 }
84 ajax("/problems/" + variant, "GET", {
85 direction: direction,
86 last_dt: last_dt,
87 }, response => {
88 if (response.problems.length > 0)
89 this.problems = response.problems;
90 });
da06a6eb
BA
91 },
92 showNewproblemModal: function() {
93 document.getElementById("modal-newproblem").checked = true;
94 },
45109880
BA
95 previewNewProblem: function() {
96 if (!V.IsGoodFen(this.newProblem.fen))
7931e479 97 return alert("Bad FEN string");
45109880
BA
98 this.newProblem.stage = "preview";
99 },
100 sendNewProblem: function() {
101 // Send it to the server and close modal
7931e479 102 ajax("/problems/" + variant, "POST", {
45109880
BA
103 fen: this.newProblem.fen,
104 instructions: this.newProblem.instructions,
105 solution: this.newProblem.solution,
7931e479 106 }, response => {
b5fb8e69
BA
107 this.newProblem.added = Date.now();
108 this.problems.push(JSON.parse(JSON.stringify(this.newProblem)));
7931e479 109 document.getElementById("modal-newproblem").checked = false;
45109880 110 this.newProblem.stage = "nothing";
7931e479 111 });
da06a6eb
BA
112 },
113 },
4ecf423b 114})