Fix ExtinctionRules position check, add prev/next buttons in problems page
[vchess.git] / client / src / views / Problems.vue
CommitLineData
89021f18
BA
1<template lang="pug">
2main
910d631b
BA
3 input#modalNewprob.modal(
4 type="checkbox"
5 @change="infoMsg=''"
6 )
7 div#newprobDiv(
8 role="dialog"
9 data-checkbox="modalNewprob"
10 )
866842c3 11 .card
89021f18 12 label#closeNewprob.modal-close(for="modalNewprob")
604b951e
BA
13 fieldset
14 label(for="selectVariant") {{ st.tr["Variant"] }}
15 select#selectVariant(
16 v-model="curproblem.vid"
17 @change="changeVariant(curproblem)"
18 )
19 option(
20 v-for="v in [emptyVar].concat(st.variants)"
21 :value="v.id"
22 :selected="curproblem.vid==v.id"
23 )
24 | {{ v.name }}
25 fieldset
604b951e
BA
26 input#inputFen(
27 type="text"
bd76b456 28 placeholder="FEN"
604b951e
BA
29 v-model="curproblem.fen"
30 @input="trySetDiagram(curproblem)"
31 )
bd76b456 32 #diagram(v-html="curproblem.diag")
604b951e 33 fieldset
bd76b456 34 textarea(
604b951e
BA
35 :placeholder="st.tr['Instructions']"
36 v-model="curproblem.instruction"
37 )
38 p(v-html="parseHtml(curproblem.instruction)")
39 fieldset
bd76b456 40 textarea(
604b951e
BA
41 :placeholder="st.tr['Solution']"
42 v-model="curproblem.solution"
43 )
44 p(v-html="parseHtml(curproblem.solution)")
45 button(@click="sendProblem()") {{ st.tr["Send"] }}
89021f18 46 #dialog.text-center {{ st.tr[infoMsg] }}
604b951e 47 .row(v-if="showOne")
866842c3 48 .col-sm-12.col-md-10.col-md-offset-1.col-lg-8.col-lg-offset-2
bd76b456 49 #topPage
d7c00f6a
BA
50 .button-group(v-if="st.user.id == curproblem.uid")
51 button(@click="editProblem(curproblem)") {{ st.tr["Edit"] }}
52 button(@click="deleteProblem(curproblem)") {{ st.tr["Delete"] }}
2f258c37 53 span.vname {{ curproblem.vname }}
98fb976e 54 span.uname ({{ curproblem.uname }})
bd76b456 55 button.marginleft(@click="backToList()") {{ st.tr["Back to list"] }}
d7c00f6a
BA
56 button.nomargin(@click="gotoPrevNext($event,curproblem,1)")
57 | {{ st.tr["Previous"] }}
58 button.nomargin(@click="gotoPrevNext($event,curproblem,-1)")
59 | {{ st.tr["Next"] }}
866842c3 60 p.oneInstructions.clickable(
2f258c37 61 v-html="parseHtml(curproblem.instruction)"
bd76b456
BA
62 @click="curproblem.showSolution=!curproblem.showSolution"
63 )
604b951e
BA
64 | {{ st.tr["Show solution"] }}
65 p(
66 v-show="curproblem.showSolution"
67 v-html="parseHtml(curproblem.solution)"
68 )
69 .row(v-else)
89021f18 70 .col-sm-12.col-md-10.col-md-offset-1.col-lg-8.col-lg-offset-2
bd76b456 71 #controls
f37790f7 72 button#newProblem(@click="prepareNewProblem()")
bd76b456
BA
73 | {{ st.tr["New problem"] }}
74 label(for="checkboxMine") {{ st.tr["My problems"] }}
75 input#checkboxMine(
76 type="checkbox"
77 v-model="onlyMines"
604b951e 78 )
bd76b456
BA
79 label(for="selectVariant") {{ st.tr["Variant"] }}
80 select#selectVariant(v-model="selectedVar")
81 option(
82 v-for="v in [emptyVar].concat(st.variants)"
83 :value="v.id"
84 )
85 | {{ v.name }}
9b35f4a9 86 table#tProblems
bd76b456
BA
87 tr
88 th {{ st.tr["Variant"] }}
89 th {{ st.tr["Instructions"] }}
2f258c37 90 th {{ st.tr["Number"] }}
bd76b456 91 tr(
6808d7a1 92 v-for="p in problems"
bd76b456
BA
93 v-show="displayProblem(p)"
94 @click="setHrefPid(p)"
95 )
96 td {{ p.vname }}
2f258c37
BA
97 td {{ firstChars(p.instruction) }}
98 td {{ p.id }}
910d631b
BA
99 BaseGame(
100 v-if="showOne"
101 :game="game"
102 :vr="vr"
103 )
89021f18
BA
104</template>
105
106<script>
107import { store } from "@/store";
108import { ajax } from "@/utils/ajax";
109import { checkProblem } from "@/data/problemCheck";
110import { getDiagram } from "@/utils/printDiagram";
bd76b456
BA
111import { processModalClick } from "@/utils/modalClick";
112import { ArrayFun } from "@/utils/array";
604b951e 113import BaseGame from "@/components/BaseGame.vue";
89021f18
BA
114export default {
115 name: "my-problems",
604b951e 116 components: {
6808d7a1 117 BaseGame
604b951e 118 },
89021f18
BA
119 data: function() {
120 return {
604b951e 121 st: store.state,
89021f18
BA
122 emptyVar: {
123 vid: 0,
6808d7a1 124 vname: ""
89021f18 125 },
604b951e
BA
126 // Problem currently showed, or edited:
127 curproblem: {
128 id: 0, //used in case of edit
89021f18
BA
129 vid: 0,
130 fen: "",
604b951e 131 diag: "",
89021f18
BA
132 instruction: "",
133 solution: "",
6808d7a1 134 showSolution: false
89021f18 135 },
604b951e
BA
136 loadedVar: 0, //corresponding to loaded V
137 selectedVar: 0, //to filter problems based on variant
89021f18 138 problems: [],
604b951e
BA
139 onlyMines: false,
140 showOne: false,
89021f18 141 infoMsg: "",
604b951e
BA
142 vr: null, //"variant rules" object initialized from FEN
143 game: {
6808d7a1
BA
144 players: [{ name: "Problem" }, { name: "Problem" }],
145 mode: "analyze"
146 }
89021f18
BA
147 };
148 },
149 created: function() {
6808d7a1
BA
150 ajax("/problems", "GET", res => {
151 // Show newest problem first:
152 this.problems = res.problems.sort((p1, p2) => p2.added - p1.added);
604b951e 153 if (this.st.variants.length > 0)
bd76b456
BA
154 this.problems.forEach(p => this.setVname(p));
155 // Retrieve all problems' authors' names
156 let names = {};
157 this.problems.forEach(p => {
6808d7a1 158 if (p.uid != this.st.user.id) names[p.uid] = "";
6808d7a1 159 else p.uname = this.st.user.name;
bd76b456 160 });
2f258c37
BA
161 const showOneIfPid = () => {
162 const pid = this.$route.query["id"];
6808d7a1 163 if (pid) this.showProblem(this.problems.find(p => p.id == pid));
2f258c37 164 };
6808d7a1
BA
165 if (Object.keys(names).length > 0) {
166 ajax("/users", "GET", { ids: Object.keys(names).join(",") }, res2 => {
167 res2.users.forEach(u => {
168 names[u.id] = u.name;
169 });
c9c0c57a
BA
170 this.problems.forEach(p => {
171 if (!p.uname)
172 p.uname = names[p.uid];
173 });
6808d7a1
BA
174 showOneIfPid();
175 });
176 } else showOneIfPid();
89021f18
BA
177 });
178 },
bd76b456 179 mounted: function() {
6808d7a1
BA
180 document
181 .getElementById("newprobDiv")
182 .addEventListener("click", processModalClick);
bd76b456 183 },
604b951e
BA
184 watch: {
185 // st.variants changes only once, at loading from [] to [...]
6808d7a1 186 "st.variants": function() {
604b951e
BA
187 // Set problems vname (either all are set or none)
188 if (this.problems.length > 0 && this.problems[0].vname == "")
189 this.problems.forEach(p => this.setVname(p));
190 },
6808d7a1 191 $route: function(to) {
bd76b456 192 const pid = to.query["id"];
6808d7a1
BA
193 if (pid) this.showProblem(this.problems.find(p => p.id == pid));
194 else this.showOne = false;
195 }
98fb976e 196 },
89021f18 197 methods: {
604b951e
BA
198 setVname: function(prob) {
199 prob.vname = this.st.variants.find(v => v.id == prob.vid).name;
200 },
2f258c37
BA
201 firstChars: function(text) {
202 let preparedText = text
203 // Replace line jumps and <br> by spaces
6808d7a1
BA
204 .replace(/\n/g, " ")
205 .replace(/<br\/?>/g, " ")
2f258c37
BA
206 .replace(/<[^>]+>/g, "") //remove remaining HTML tags
207 .replace(/[ ]+/g, " ") //remove series of spaces by only one
208 .trim();
209 const maxLength = 32; //arbitrary...
210 if (preparedText.length > maxLength)
6808d7a1 211 return preparedText.substr(0, 32) + "...";
2f258c37
BA
212 return preparedText;
213 },
604b951e 214 copyProblem: function(p1, p2) {
6808d7a1 215 for (let key in p1) p2[key] = p1[key];
604b951e 216 },
bd76b456
BA
217 setHrefPid: function(p) {
218 // Change href => $route changes, watcher notices, call showProblem
219 const curHref = document.location.href;
220 document.location.href = curHref.split("?")[0] + "?id=" + p.id;
221 },
222 backToList: function() {
223 // Change href => $route change, watcher notices, reset showOne to false
224 document.location.href = document.location.href.split("?")[0];
225 },
604b951e
BA
226 resetCurProb: function() {
227 this.curproblem.id = 0;
228 this.curproblem.uid = 0;
229 this.curproblem.vid = "";
230 this.curproblem.vname = "";
231 this.curproblem.fen = "";
232 this.curproblem.diag = "";
233 this.curproblem.instruction = "";
234 this.curproblem.solution = "";
235 this.curproblem.showSolution = false;
89021f18 236 },
604b951e
BA
237 parseHtml: function(txt) {
238 return !txt.match(/<[/a-zA-Z]+>/)
239 ? txt.replace(/\n/g, "<br/>") //no HTML tag
240 : txt;
241 },
242 changeVariant: function(prob) {
243 this.setVname(prob);
6808d7a1
BA
244 this.loadVariant(prob.vid, () => {
245 // Set FEN if possible (might not be correct yet)
246 if (V.IsGoodFen(prob.fen)) this.setDiagram(prob);
247 });
604b951e
BA
248 },
249 loadVariant: async function(vid, cb) {
250 // Condition: vid is a valid variant ID
251 this.loadedVar = 0;
252 const variant = this.st.variants.find(v => v.id == vid);
89021f18
BA
253 const vModule = await import("@/variants/" + variant.name + ".js");
254 window.V = vModule.VariantRules;
604b951e
BA
255 this.loadedVar = vid;
256 cb();
257 },
258 trySetDiagram: function(prob) {
259 // Problem edit: FEN could be wrong or incomplete,
260 // variant could not be ready, or not defined
261 if (prob.vid > 0 && this.loadedVar == prob.vid && V.IsGoodFen(prob.fen))
262 this.setDiagram(prob);
89021f18 263 },
604b951e
BA
264 setDiagram: function(prob) {
265 // Condition: prob.fen is correct and global V is ready
266 const parsedFen = V.ParseFen(prob.fen);
267 const args = {
268 position: parsedFen.position,
6808d7a1 269 orientation: parsedFen.turn
604b951e
BA
270 };
271 prob.diag = getDiagram(args);
89021f18 272 },
604b951e 273 displayProblem: function(p) {
6808d7a1 274 return (
866842c3 275 (!this.selectedVar || p.vid == this.selectedVar) &&
6808d7a1
BA
276 ((this.onlyMines && p.uid == this.st.user.id) ||
277 (!this.onlyMines && p.uid != this.st.user.id))
278 );
604b951e
BA
279 },
280 showProblem: function(p) {
6808d7a1
BA
281 this.loadVariant(p.vid, () => {
282 // The FEN is already checked at this stage:
283 this.vr = new V(p.fen);
284 this.game.vname = p.vname;
285 this.game.mycolor = this.vr.turn; //diagram orientation
286 this.game.fen = p.fen;
287 this.$set(this.game, "fenStart", p.fen);
288 this.copyProblem(p, this.curproblem);
289 this.showOne = true;
290 });
604b951e 291 },
d7c00f6a
BA
292 gotoPrevNext: function(e, prob, dir) {
293 const startIdx = this.problems.findIndex(p => p.id == prob.id);
294 let nextIdx = startIdx + dir;
295 while (
296 nextIdx >= 0 &&
297 nextIdx < this.problems.length &&
298 ((this.onlyMines && this.problems[nextIdx].uid != this.st.user.id) ||
299 (!this.onlyMines && this.problems[nextIdx].uid == this.st.user.id))
300 )
301 nextIdx += dir;
302 if (nextIdx >= 0 && nextIdx < this.problems.length)
303 this.setHrefPid(this.problems[nextIdx]);
304 else
305 alert(this.st.tr["No more problems"]);
306 },
f37790f7
BA
307 prepareNewProblem: function() {
308 this.resetCurProb();
309 window.doClick("modalNewprob");
310 },
604b951e
BA
311 sendProblem: function() {
312 const error = checkProblem(this.curproblem);
6808d7a1 313 if (error) {
866842c3 314 alert(this.st.tr[error]);
6808d7a1
BA
315 return;
316 }
604b951e
BA
317 const edit = this.curproblem.id > 0;
318 this.infoMsg = "Processing... Please wait";
319 ajax(
320 "/problems",
321 edit ? "PUT" : "POST",
6808d7a1
BA
322 { prob: this.curproblem },
323 ret => {
324 if (edit) {
604b951e
BA
325 let editedP = this.problems.find(p => p.id == this.curproblem.id);
326 this.copyProblem(this.curproblem, editedP);
c9c0c57a 327 this.showProblem(editedP);
0cd02605 328 }
6808d7a1 329 else {
604b951e
BA
330 let newProblem = Object.assign({}, this.curproblem);
331 newProblem.id = ret.id;
bd76b456
BA
332 newProblem.uid = this.st.user.id;
333 newProblem.uname = this.st.user.name;
57b3f30e 334 this.problems = [newProblem].concat(this.problems);
604b951e 335 }
b638a2e7 336 document.getElementById("modalNewprob").checked = false;
c9c0c57a 337 this.infoMsg = "";
604b951e
BA
338 }
339 );
340 },
341 editProblem: function(prob) {
bec548e7
BA
342 // prob.diag might correspond to some other problem or be empty:
343 this.setDiagram(prob); //V is loaded at this stage
604b951e 344 this.copyProblem(prob, this.curproblem);
6808d7a1 345 window.doClick("modalNewprob");
604b951e
BA
346 },
347 deleteProblem: function(prob) {
6808d7a1
BA
348 if (confirm(this.st.tr["Are you sure?"])) {
349 ajax("/problems", "DELETE", { id: prob.id }, () => {
bd76b456
BA
350 ArrayFun.remove(this.problems, p => p.id == prob.id);
351 this.backToList();
352 });
353 }
6808d7a1
BA
354 }
355 }
89021f18
BA
356};
357</script>
358
359<style lang="sass" scoped>
bd76b456
BA
360[type="checkbox"].modal+div .card
361 max-width: 767px
362 max-height: 100%
910d631b 363
bd76b456
BA
364#inputFen
365 width: 100%
910d631b 366
bd76b456
BA
367textarea
368 width: 100%
910d631b 369
bd76b456
BA
370#diagram
371 margin: 0 auto
372 max-width: 400px
910d631b 373
9b35f4a9
BA
374table#tProblems
375 max-height: 100%
376
bd76b456
BA
377#controls
378 margin: 0
379 width: 100%
380 text-align: center
381 & > *
382 margin: 0
2f258c37 383
866842c3
BA
384p.oneInstructions
385 margin: 0
386 padding: 2px 5px
387 background-color: lightgreen
388
bd76b456 389#topPage
2f258c37 390 span.vname
bd76b456 391 font-weight: bold
feae89d3 392 padding-left: var(--universal-margin)
2f258c37
BA
393 span.uname
394 padding-left: var(--universal-margin)
bd76b456
BA
395 margin: 0 auto
396 & > .nomargin
397 margin: 0
398 & > .marginleft
399 margin: 0 0 0 15px
400
2f258c37
BA
401@media screen and (max-width: 767px)
402 #topPage
403 text-align: center
89021f18 404</style>