Debugging problems page; TODO: hash navigation is wrong
[vchess.git] / public / javascripts / components / problems.js
CommitLineData
4ecf423b 1Vue.component('my-problems', {
582df349 2 props: ["queryHash","settings"],
da06a6eb
BA
3 data: function () {
4 return {
ff1d4c3f 5 userId: user.id,
81da2786 6 problems: [], //oldest first
ff1d4c3f 7 myProblems: [], //same, but only mine
936dc463
BA
8 singletons: [], //requested problems (using #num)
9 display: "others", //or "mine"
10 curProb: null, //(reference to) current displayed problem (if any)
ff1d4c3f 11 showSolution: false,
60d9063f 12 nomoreMessage: "",
936dc463 13 pbNum: 0, //to navigate directly to some problem
ff1d4c3f
BA
14 // New problem (to upload), or existing problem to edit:
15 modalProb: {
16 id: 0, //defined if it's an edit
badeb466 17 uid: 0, //...also
77fa6d1f 18 fen: "",
45109880
BA
19 instructions: "",
20 solution: "",
a9f262f3 21 preview: false,
45109880 22 },
da06a6eb
BA
23 };
24 },
4ecf423b 25 template: `
a5d56686
BA
26 <div class="col-sm-12 col-md-10 col-md-offset-1 col-lg-8 col-lg-offset-2">
27 <div id="problemControls" class="button-group">
badeb466
BA
28 <button :aria-label='translate("Previous problem(s)")' class="tooltip"
29 @click="showNext('backward')"
30 >
a5d56686
BA
31 <i class="material-icons">skip_previous</i>
32 </button>
badeb466
BA
33 <button v-if="!!userId" :aria-label='translate("Add a problem")'
34 class="tooltip" onClick="doClick('modal-newproblem')"
35 >
247356cd 36 {{ translate("New") }}
a5d56686 37 </button>
badeb466
BA
38 <button :aria-label='translate("Next problem(s)")' class="tooltip"
39 @click="showNext('forward')"
40 >
a5d56686
BA
41 <i class="material-icons">skip_next</i>
42 </button>
43 </div>
60d9063f 44 <div id="mainBoard" v-if="!!curProb">
ff1d4c3f 45 <div id="instructions-div" class="section-content">
badeb466 46 <p id="problem-instructions">{{ curProb.instructions }}</p>
ff1d4c3f 47 </div>
badeb466
BA
48 <my-game :fen="curProb.fen" :mode="analyze" :allowMovelist="true"
49 :settings="settings"
50 >
60d9063f 51 </my-game>
ff1d4c3f
BA
52 <div id="solution-div" class="section-content">
53 <h3 class="clickable" @click="showSolution = !showSolution">
54 {{ translations["Show solution"] }}
55 </h3>
badeb466 56 <p id="problem-solution" v-show="showSolution">{{ curProb.solution }}</p>
ff1d4c3f 57 </div>
badeb466 58 <button @click="displayList">Back to list display</button>
936dc463
BA
59 </div>
60 <div>
61 <input type="text" placeholder="Type problem number" v-model="pbNum"/>
badeb466 62 <button @click="showProblem">Show problem</button>
ff1d4c3f 63 </div>
badeb466
BA
64 <button v-if="!!userId" @click="toggleListDisplay"
65 :class="{'only-mine':display=='mine'}"
66 >
67 My problems (only)
8ef618ef 68 </button>
936dc463
BA
69 <my-problem-summary v-show="!curProb"
70 v-on:edit-problem="editProblem(p)" v-on:delete-problem="deleteProblem(p.id)"
badeb466
BA
71 v-on:show-problem="() => showProblem(p.id)"
72 v-for="p in curProblems()" @click="curProb=p"
8ef618ef 73 v-bind:prob="p" v-bind:userid="userId" v-bind:key="p.id">
da06a6eb 74 </my-problem-summary>
8ef618ef 75 <input type="checkbox" id="modal-newproblem" class="modal"/>
ff1d4c3f
BA
76 <div role="dialog" aria-labelledby="modalProblemTxt">
77 <div v-show="!modalProb.preview" class="card newproblem-form">
8ef618ef
BA
78 <label for="modal-newproblem" class="modal-close">
79 </label>
badeb466 80 <h3 id="modalProblemTxt">{{ translate("Add a problem") }}</h3>
8ef618ef 81 <form @submit.prevent="previewProblem()">
da06a6eb 82 <fieldset>
247356cd 83 <label for="newpbFen">FEN</label>
ff1d4c3f 84 <input id="newpbFen" type="text" v-model="modalProb.fen"
e081ffe3 85 :placeholder='translate("Full FEN description")'/>
da06a6eb
BA
86 </fieldset>
87 <fieldset>
badeb466
BA
88 <p class="emphasis">{{ translate("Safe HTML tags allowed") }}</p>
89 <label for="newpbInstructions">{{ translate("Instructions") }}</label>
ff1d4c3f 90 <textarea id="newpbInstructions" v-model="modalProb.instructions"
8ef618ef
BA
91 :placeholder='translate("Describe the problem goal")'>
92 </textarea>
badeb466 93 <label for="newpbSolution">{{ translate("Solution") }}</label>
ff1d4c3f 94 <textarea id="newpbSolution" v-model="modalProb.solution"
8ef618ef
BA
95 :placeholder='translate("How to solve the problem?")'>
96 </textarea>
badeb466 97 <button class="center-btn">{{ translate("Preview") }}</button>
da06a6eb 98 </fieldset>
da06a6eb
BA
99 </form>
100 </div>
ff1d4c3f 101 <div v-show="modalProb.preview" class="card newproblem-preview">
badeb466
BA
102 <label for="modal-newproblem" class="modal-close"
103 @click="modalProb.preview=false">
8ef618ef 104 </label>
badeb466 105 <my-problem-summary :prob="modalProb" :userid="userId" :preview="true">
8ef618ef 106 </my-problem-summary>
a5d56686 107 <div class="button-group">
badeb466
BA
108 <button @click="modalProb.preview=false">{{ translate("Cancel") }}</button>
109 <button @click="sendProblem()">{{ translate("Send") }}</button>
b5fb8e69 110 </div>
45109880 111 </div>
da06a6eb 112 </div>
936dc463
BA
113 <input id="modalNomore" type="checkbox" class="modal"/>
114 <div role="dialog" aria-labelledby="nomoreMessage">
115 <div class="card smallpad small-modal text-center">
116 <label for="modalNomore" class="modal-close"></label>
badeb466 117 <h3 id="nomoreMessage" class="section">{{ nomoreMessage }}</h3>
936dc463
BA
118 </div>
119 </div>
4ecf423b
BA
120 </div>
121 `,
582df349
BA
122 watch: {
123 queryHash: function(newQhash) {
124 if (!!newQhash)
125 {
126 // New query hash = "id=42"; get 42 as problem ID
127 const pid = parseInt(newQhash.substr(2));
128 this.showProblem(pid);
129 }
130 else
131 this.curProb = null; //(back to) list display
132 },
133 },
298c42e6 134 created: function() {
582df349
BA
135 if (!!this.queryHash)
136 {
137 const pid = parseInt(this.queryHash.substr(2));
138 this.showProblem(pid);
139 }
ff1d4c3f 140 else
936dc463 141 this.firstFetch();
298c42e6 142 },
da06a6eb 143 methods: {
936dc463
BA
144 firstFetch: function() {
145 // Fetch most recent problems from server, for both lists
146 this.fetchProblems("others", "bacwkard");
147 this.fetchProblems("mine", "bacwkard");
148 this.listsInitialized = true;
149 },
150 showProblem: function(num) {
151 const pid = num || this.pbNum;
badeb466
BA
152 location.hash = "#problems?id=" + pid;
153 for (let parray of [this.singletons,this.problems,this.myProblems])
154 {
155 const pIdx = parray.findIndex(p => p.id == pid);
156 if (pIdx >= 0)
157 {
158 curProb = parray[pIdx];
159 break;
160 }
161 }
162 if (!curProb)
936dc463
BA
163 {
164 // Cannot find problem in current set; get from server, and add to singletons.
165 ajax(
166 "/problems/" + variant.name + "/" + pid, //TODO: use variant._id ?
167 "GET",
168 response => {
169 if (!!response.problem)
170 {
171 this.singletons.push(response.problem);
172 this.curProb = response.problem;
173 }
174 else
175 this.noMoreProblems("Sorry, problem " + pid + " does not exist");
176 }
177 );
178 }
ff1d4c3f 179 },
247356cd
BA
180 translate: function(text) {
181 return translations[text];
182 },
ff1d4c3f
BA
183 curProblems: function() {
184 switch (this.display)
185 {
936dc463 186 case "others":
ff1d4c3f 187 return this.problems;
936dc463 188 case "mine":
ff1d4c3f
BA
189 return this.myProblems;
190 }
81da2786 191 },
8ef618ef 192 // TODO?: get 50 from server but only show 10 at a time (for example)
ff1d4c3f 193 showNext: function(direction) {
936dc463
BA
194 if (!this.curProb)
195 return this.fetchProblems(this.display, direction);
8ef618ef 196 // Show next problem (older or newer):
ff1d4c3f 197 let curProbs = this.curProblems();
936dc463
BA
198 // Try to find a neighbour problem in the direction, among current set
199 const neighbor = this.findClosestNeighbor(this.curProb, curProbs, direction);
200 if (!!neighbor)
ff1d4c3f 201 {
936dc463
BA
202 this.curProb = neighbor;
203 return;
ff1d4c3f 204 }
936dc463
BA
205 // Boundary case: nothing in current set, need to fetch from server
206 const curSize = curProbs.length;
207 this.fetchProblems(this.display, direction);
208 const newSize = curProbs.length;
209 if (curSize == newSize) //no problems found
210 return this.noMoreProblems("No more problems in this direction");
211 // Ok, found something:
212 this.curProb = this.findClosestNeighbor(this.curProb, curProbs, direction);
213 },
214 findClosestNeighbor: function(problem, probList, direction) {
215 let neighbor = undefined;
216 let smallestDistance = Number.MAX_SAFE_INTEGER;
217 for (let prob of probList)
ff1d4c3f 218 {
936dc463
BA
219 const delta = Math.abs(prob.id - problem.id);
220 if (delta < smallestDistance &&
221 ((direction == "backward" && prob.id < problem.id)
222 || (direction == "forward" && prob.id > problem.id)))
8ef618ef 223 {
936dc463
BA
224 neighbor = prob;
225 smallestDistance = delta;
8ef618ef 226 }
ff1d4c3f 227 }
936dc463
BA
228 return neighbor;
229 },
230 noMoreProblems: function(message) {
231 this.nomoreMessage = message;
232 let modalNomore = document.getElementById("modalNomore");
233 modalNomore.checked = true;
234 setTimeout(() => modalNomore.checked = false, 2000);
235 },
236 displayList: function() {
237 this.curProb = null;
badeb466 238 location.hash = "#problems";
936dc463
BA
239 // Fetch problems if first call (if #num, and then lists)
240 if (!this.listsInitialized)
241 this.firstFetch();
ff1d4c3f
BA
242 },
243 toggleListDisplay: function() {
badeb466
BA
244 const displays = ["mine","others"];
245 const curIndex = displays.findIndex(item => item == this.display);
246 this.display = displays[1-curIndex];
81da2786 247 },
936dc463
BA
248 fetchProblems: function(type, direction) {
249 let problems = (type == "others" ? this.problems : this.myProblems);
badeb466 250 // "last datetime" set at a value OK for an empty initial array
936dc463 251 let last_dt = (direction=="forward" ? 0 : Number.MAX_SAFE_INTEGER);
badeb466 252 if (problems.length > 0)
7931e479 253 {
936dc463
BA
254 // Search for newest date (or oldest)
255 last_dt = problems[0].added;
256 for (let i=1; i<problems.length; i++)
7931e479 257 {
badeb466
BA
258 if ((direction == "forward" && problems[i].added > last_dt) ||
259 (direction == "backward" && problems[i].added < last_dt))
936dc463 260 {
badeb466 261 last_dt = problems[i].added;
936dc463 262 }
7931e479
BA
263 }
264 }
936dc463 265 ajax(
582df349 266 "/problems/" + variant.id,
936dc463 267 "GET",
81da2786 268 {
936dc463
BA
269 type: type,
270 direction: direction,
271 last_dt: last_dt,
272 },
273 response => {
274 if (response.problems.length > 0)
275 {
276 Array.prototype.push.apply(problems,
badeb466 277 response.problems.sort((p1,p2) => { return p2.added - p1.added; }));
936dc463
BA
278 // If one list is empty but not the other, show the non-empty
279 const otherArray = (type == "mine" ? this.problems : this.myProblems);
280 if (problems.length > 0 && otherArray.length == 0)
281 this.display = type;
282 }
81da2786 283 }
936dc463 284 );
da06a6eb 285 },
8ef618ef 286 previewProblem: function() {
badeb466 287 if (!V.IsGoodFen(this.modalProb.fen))
e081ffe3 288 return alert(translations["Bad FEN description"]);
badeb466 289 if (this.modalProb.instructions.trim().length == 0)
6b5517b4 290 return alert(translations["Empty instructions"]);
badeb466 291 if (this.modalProb.solution.trim().length == 0)
6b5517b4 292 return alert(translations["Empty solution"]);
badeb466 293 Vue.set(this.modalProb, "preview", true);
45109880 294 },
936dc463
BA
295 editProblem: function(prob) {
296 this.modalProb = prob;
badeb466 297 Vue.set(this.modalProb, "preview", false);
936dc463
BA
298 document.getElementById("modal-newproblem").checked = true;
299 },
300 deleteProblem: function(pid) {
301 ajax(
badeb466 302 "/problems/" + pid,
936dc463
BA
303 "DELETE",
304 response => {
305 // Delete problem from the list on client side
306 let problems = this.curProblems();
307 const pIdx = problems.findIndex(p => p.id == pid);
308 problems.splice(pIdx, 1);
309 }
310 );
311 },
8ef618ef 312 sendProblem: function() {
45109880 313 // Send it to the server and close modal
8ef618ef 314 ajax(
582df349 315 "/problems/" + variant.id,
8ef618ef
BA
316 (this.modalProb.id > 0 ? "PUT" : "POST"),
317 this.modalProb,
318 response => {
319 document.getElementById("modal-newproblem").checked = false;
badeb466 320 Vue.set(this.modalProb, "preview", false);
8ef618ef
BA
321 if (this.modalProb.id == 0)
322 {
badeb466
BA
323 this.myProblems.unshift({
324 added: Date.now(),
325 id: response.id,
326 uid: user.id,
327 fen: this.modalProb.fen,
328 instructions: this.modalProb.instructions,
329 solution: this.modalProb.solution,
330 });
8ef618ef
BA
331 }
332 else
333 this.modalProb.id = 0;
334 }
335 );
336 },
da06a6eb 337 },
4ecf423b 338})