Index page almost OK. Now work on variant page (main hall...)
[vchess.git] / public / javascripts / components / problems.js
1 Vue.component('my-problems', {
2 data: function () {
3 return {
4 problems: [],
5 newProblem: {
6 fen: "",
7 instructions: "",
8 solution: "",
9 stage: "nothing", //or "preview" after new problem is filled
10 },
11 };
12 },
13 template: `
14 <div class="col-sm-12 col-md-10 col-md-offset-1 col-lg-8 col-lg-offset-2">
15 <div id="problemControls" class="button-group">
16 <button :aria-label='translate("Load previous problems")' class="tooltip"
17 @click="fetchProblems('backward')">
18 <i class="material-icons">skip_previous</i>
19 </button>
20 <button :aria-label='translate("Add a problem")' class="tooltip"
21 @click="showNewproblemModal">
22 {{ translate("New") }}
23 </button>
24 <button :aria-label='translate("Load next problems")' class="tooltip"
25 @click="fetchProblems('forward')">
26 <i class="material-icons">skip_next</i>
27 </button>
28 </div>
29 <my-problem-summary v-on:show-problem="bubbleUp(p)"
30 v-for="(p,idx) in sortedProblems"
31 v-bind:prob="p" v-bind:preview="false" v-bind:key="idx">
32 </my-problem-summary>
33 <input type="checkbox" id="modal-newproblem" class="modal">
34 <div role="dialog" aria-labelledby="newProblemTxt">
35 <div v-show="newProblem.stage=='nothing'" class="card newproblem-form">
36 <label for="modal-newproblem" class="modal-close"></label>
37 <h3 id="newProblemTxt">{{ translate("Add a problem") }}</h3>
38 <form @submit.prevent="previewNewProblem">
39 <fieldset>
40 <label for="newpbFen">FEN</label>
41 <input id="newpbFen" type="text" v-model="newProblem.fen"
42 :placeholder='translate("Full FEN description")'/>
43 </fieldset>
44 <fieldset>
45 <p class="emphasis">{{ translate("Safe HTML tags allowed") }}</p>
46 <label for="newpbInstructions">{{ translate("Instructions") }}</label>
47 <textarea id="newpbInstructions" v-model="newProblem.instructions"
48 :placeholder='translate("Describe the problem goal")'></textarea>
49 <label for="newpbSolution">{{ translate("Solution") }}</label>
50 <textarea id="newpbSolution" v-model="newProblem.solution"
51 :placeholder='translate("How to solve the problem?")'></textarea>
52 <button class="center-btn">{{ translate("Preview") }}</button>
53 </fieldset>
54 </form>
55 </div>
56 <div v-show="newProblem.stage=='preview'" class="card newproblem-preview">
57 <label for="modal-newproblem" class="modal-close"></label>
58 <my-problem-summary v-bind:prob="newProblem" v-bind:preview="true">
59 </my-problem-summary>
60 <div class="button-group">
61 <button @click="newProblem.stage='nothing'">{{ translate("Cancel") }}</button>
62 <button @click="sendNewProblem()">{{ translate("Send") }}</button>
63 </div>
64 </div>
65 </div>
66 </div>
67 `,
68 computed: {
69 sortedProblems: function() {
70 // Newest problem first
71 return this.problems.sort((p1,p2) => { return p2.added - p1.added; });
72 },
73 },
74 created: function() {
75 // TODO: fetch most recent problems from server
76 },
77 methods: {
78 translate: function(text) {
79 return translations[text];
80 },
81 // TODO: obsolete:
82 // // Propagate "show problem" event to parent component (my-variant)
83 // bubbleUp: function(problem) {
84 // this.$emit('show-problem', JSON.stringify(problem));
85 // },
86 fetchProblems: function(direction) {
87 if (this.problems.length == 0)
88 return; //what could we do?!
89 // Search for newest date (or oldest)
90 let last_dt = this.problems[0].added;
91 for (let i=0; i<this.problems.length; i++)
92 {
93 if ((direction == "forward" && this.problems[i].added > last_dt) ||
94 (direction == "backward" && this.problems[i].added < last_dt))
95 {
96 last_dt = this.problems[i].added;
97 }
98 }
99 ajax("/problems/" + variant, "GET", {
100 direction: direction,
101 last_dt: last_dt,
102 }, response => {
103 if (response.problems.length > 0)
104 this.problems = response.problems;
105 });
106 },
107 showNewproblemModal: function() {
108 document.getElementById("modal-newproblem").checked = true;
109 },
110 previewNewProblem: function() {
111 if (!V.IsGoodFen(this.newProblem.fen))
112 return alert(translations["Bad FEN description"]);
113 if (this.newProblem.instructions.trim().length == 0)
114 return alert(translations["Empty instructions"]);
115 if (this.newProblem.solution.trim().length == 0)
116 return alert(translations["Empty solution"]);
117 this.newProblem.stage = "preview";
118 },
119 sendNewProblem: function() {
120 // Send it to the server and close modal
121 ajax("/problems/" + variant, "POST", {
122 fen: this.newProblem.fen,
123 instructions: this.newProblem.instructions,
124 solution: this.newProblem.solution,
125 }, response => {
126 this.newProblem.added = Date.now();
127 this.problems.push(JSON.parse(JSON.stringify(this.newProblem)));
128 document.getElementById("modal-newproblem").checked = false;
129 this.newProblem.stage = "nothing";
130 });
131 },
132 },
133 })
134
135 // TODO:
136 // possibilité de supprimer / éditer si peer ID reconnu comme celui du probleme (champ "uploader")
137 // --> côté serveur on vérifie un certain "secret"
138 // --> filtre possible "mes problèmes"