3 input#modalRules.modal(type="checkbox")
6 data-checkbox="modalRules"
9 label.modal-close(for="modalRules")
10 a#variantNameInProblems(:href="'/#/variants/'+game.vname")
11 | {{ curproblem.vdisp }}
12 div(v-html="rulesContent")
13 input#modalNewprob.modal(
15 @change="fenFocusIfOpened($event)"
19 data-checkbox="modalNewprob"
22 label#closeNewprob.modal-close(for="modalNewprob")
24 label(for="selectVariant") {{ st.tr["Variant"] }}
26 v-model="curproblem.vid"
27 @change="changeVariant(curproblem)"
30 v-for="v in [emptyVar].concat(st.variants)"
33 :selected="curproblem.vid==v.id"
40 v-model="curproblem.fen"
41 @input="trySetDiagram(curproblem)"
43 #diagram(v-html="curproblem.diag")
45 textarea.instructions-edit(
46 :placeholder="st.tr['Instructions']"
47 @input="adjustHeight('instructions')"
48 v-model="curproblem.instruction"
50 .instructions(v-html="parseHtml(curproblem.instruction)")
52 textarea.solution-edit(
53 :placeholder="st.tr['Solution']"
54 @input="adjustHeight('solution')"
55 v-model="curproblem.solution"
57 .solution(v-html="parseHtml(curproblem.solution)")
58 button(@click="sendProblem()") {{ st.tr["Send"] }}
59 #dialog.text-center {{ st.tr[infoMsg] }}
61 .col-sm-12.col-md-10.col-md-offset-1.col-lg-8.col-lg-offset-2
63 .button-group(v-if="canIedit(curproblem.uid)")
64 button(@click="editProblem(curproblem)") {{ st.tr["Edit"] }}
65 button(@click="deleteProblem(curproblem)") {{ st.tr["Delete"] }}
66 span.vname {{ curproblem.vdisp }}
67 span.uname ({{ curproblem.uname }})
68 button.marginleft(@click="backToList()") {{ st.tr["Back to list"] }}
69 button.nomargin(@click="gotoPrevNext(curproblem,1)")
70 | {{ st.tr["Previous_p"] }}
71 button.nomargin(@click="gotoPrevNext(curproblem,-1)")
72 | {{ st.tr["Next_p"] }}
73 .instructions.oneInstructions.clickable(
74 v-html="parseHtml(curproblem.instruction)"
75 @click="curproblem.showSolution=!curproblem.showSolution"
77 | {{ st.tr["Show solution"] }}
79 v-show="curproblem.showSolution"
80 v-html="parseHtml(curproblem.solution)"
83 .col-sm-12.col-md-10.col-md-offset-1.col-lg-8.col-lg-offset-2
85 button#newProblem(@click="prepareNewProblem()")
86 | {{ st.tr["New problem"] }}
87 div#myProblems(v-if="st.user.id > 0")
88 label(for="checkboxMine") {{ st.tr["My problems"] }}
93 label(for="selectVariant") {{ st.tr["Variant"] }}
94 select#selectVariant(v-model="selectedVar")
96 v-for="v in [emptyVar].concat(st.variants)"
103 th {{ st.tr["Variant"] }}
104 th {{ st.tr["Instructions"] }}
105 th {{ st.tr["Number"] }}
107 v-for="p in problems[onlyMine ? 'mine' : 'others']"
108 v-show="onlyMine || !selectedVar || p.vid == selectedVar"
109 @click="setHrefPid(p)"
112 td {{ firstChars(p.instruction) }}
115 v-if="hasMore[onlyMine ? 'mine' : 'others']"
116 @click="loadMore(onlyMine ? 'mine' : 'others')"
118 | {{ st.tr["Load more"] }}
127 import { store } from "@/store";
128 import { ajax } from "@/utils/ajax";
129 import { checkProblem } from "@/data/problemCheck";
130 import params from "@/parameters";
131 import { getDiagram, replaceByDiag } from "@/utils/printDiagram";
132 import { processModalClick } from "@/utils/modalClick";
133 import { ArrayFun } from "@/utils/array";
134 import afterRawLoad from "@/utils/afterRawLoad";
135 import BaseGame from "@/components/BaseGame.vue";
144 emptyVar: { vid: 0 },
145 // Problem currently showed, or edited:
147 id: 0, //used in case of edit
155 loadedVar: 0, //corresponding to loaded V
156 selectedVar: 0, //to filter problems based on variant
157 problems: { "mine": [], "others": [] },
158 // timestamp of oldest showed problem:
160 mine: Number.MAX_SAFE_INTEGER,
161 others: Number.MAX_SAFE_INTEGER
163 // hasMore == TRUE: a priori there could be more problems to load
164 hasMore: { mine: true, others: true },
170 players: [{ name: "Problem" }, { name: "Problem" }],
175 created: function() {
176 const pid = this.$route.query["id"];
177 if (!!pid) this.showProblem(pid);
178 else this.loadMore("others", () => { this.loadMore("mine"); });
180 mounted: function() {
181 ["rulesDiv","newprobDiv"].forEach(eltName => {
182 document.getElementById(eltName)
183 .addEventListener("click", processModalClick)
187 // st.variants changes only once, at loading from [] to [...]
188 "st.variants": function() {
189 // Set problems vname (either all are set or none)
190 let problems = this.problems["others"].concat(this.problems["mine"]);
191 if (problems.length > 0 && problems[0].vname == "")
192 problems.forEach(p => this.setVname(p));
194 $route: function(to) {
195 const pid = to.query["id"];
196 if (!!pid) this.showProblem(pid);
198 if (this.cursor["others"] == Number.MAX_SAFE_INTEGER)
199 // Back from a single problem view at initial loading:
200 // problems lists are empty!
201 this.loadMore("others", () => { this.loadMore("mine"); });
202 this.showOne = false;
207 fenFocusIfOpened: function(event) {
208 if (event.target.checked) {
210 document.getElementById("inputFen").focus();
213 adjustHeight: function(elt) {
214 // https://stackoverflow.com/a/48460773
215 let t = document.querySelector("." + elt + "-edit");
217 t.style.height = (t.scrollHeight + 3) + "px";
219 setVname: function(prob) {
220 const variant = this.st.variants.find(v => v.id == prob.vid);
221 prob.vname = variant.name;
222 prob.vdisp = variant.display;
224 // Add vname and user names:
225 decorate: function(problems, callback) {
226 if (this.st.variants.length > 0)
227 problems.forEach(p => this.setVname(p));
228 // Retrieve all problems' authors' names
230 problems.forEach(p => {
231 if (p.uid != this.st.user.id) names[p.uid] = "";
232 else p.uname = this.st.user.name;
234 if (Object.keys(names).length > 0) {
239 data: { ids: Object.keys(names).join(",") },
241 res2.users.forEach(u => {
242 names[u.id] = u.name;
244 problems.forEach(p => {
246 p.uname = names[p.uid];
248 if (!!callback) callback();
253 else if (!!callback) callback();
255 firstChars: function(text) {
256 let preparedText = text
257 // Replace line jumps and <br> by spaces
259 .replace(/<br\/?>/g, " ")
260 .replace(/<[^>]+>/g, "") //remove remaining HTML tags
261 .replace(/[ ]+/g, " ") //remove series of spaces by only one
263 const maxLength = 32; //arbitrary...
264 if (preparedText.length > maxLength)
265 return preparedText.substr(0, 32) + "...";
268 copyProblem: function(p1, p2) {
269 for (let key in p1) p2[key] = p1[key];
271 setHrefPid: function(p) {
272 // Change href => $route changes, watcher notices, call showProblem
273 const curHref = document.location.href;
274 document.location.href = curHref.split("?")[0] + "?id=" + p.id;
276 backToList: function() {
277 // Change href => $route change, watcher notices, reset showOne to false
278 document.location.href = document.location.href.split("?")[0];
280 resetCurProb: function() {
281 this.curproblem.id = 0;
282 this.curproblem.uid = 0;
283 this.curproblem.vid = "";
284 this.curproblem.vname = "";
285 this.curproblem.vdisp = "";
286 this.curproblem.fen = "";
287 this.curproblem.diag = "";
288 this.curproblem.instruction = "";
289 this.curproblem.solution = "";
290 this.curproblem.showSolution = false;
292 parseHtml: function(txt) {
293 return !txt.match(/<[/a-zA-Z]+>/)
296 txt.replace(/\n\n/g, "<br/><div class='br'></div>")
297 .replace(/\n/g, "<br/>")
300 changeVariant: function(prob) {
302 this.loadVariant(prob.vid, () => {
303 // Set FEN if possible (might not be correct yet)
304 if (V.IsGoodFen(prob.fen)) this.setDiagram(prob);
308 loadVariant: async function(vid, cb) {
309 // Condition: vid is a valid variant ID
311 const variant = this.st.variants.find(v => v.id == vid);
312 await import("@/variants/" + variant.name + ".js")
314 window.V = vModule[variant.name + "Rules"];
315 this.loadedVar = vid;
321 "raw-loader!@/translations/rules/" + variant.name + "/" +
322 this.st.lang + ".pug"
324 ).replace(/(fen:)([^:]*):/g, replaceByDiag);
326 trySetDiagram: function(prob) {
327 // Problem edit: FEN could be wrong or incomplete,
328 // variant could not be ready, or not defined
329 if (prob.vid > 0 && this.loadedVar == prob.vid && V.IsGoodFen(prob.fen))
330 this.setDiagram(prob);
333 setDiagram: function(prob) {
334 // Condition: prob.fen is correct and global V is ready
335 const parsedFen = V.ParseFen(prob.fen);
337 position: parsedFen.position,
338 orientation: parsedFen.turn
340 prob.diag = getDiagram(args);
342 showProblem: function(p_id) {
343 const processWhenWeHaveProb = () => {
344 this.loadVariant(p.vid, () => {
345 this.onlyMine = (p.uid == this.st.user.id);
346 // The FEN is already checked at this stage:
347 this.game.vname = p.vname;
348 this.game.mycolor = V.ParseFen(p.fen).turn; //diagram orientation
349 this.game.fenStart = p.fen;
350 this.game.fen = p.fen;
352 // $nextTick to be sure $refs["basegame"] exists
353 this.$nextTick(() => {
354 this.$refs["basegame"].re_setVariables(this.game); });
355 this.curproblem.showSolution = false; //in case of
356 this.copyProblem(p, this.curproblem);
360 if (typeof p_id == "object") p = p_id;
362 const problems = this.problems["others"].concat(this.problems["mine"]);
363 p = problems.find(prob => prob.id == p_id);
366 // Bad luck: problem not in list. Get from server
373 this.decorate([res.problem], () => {
375 const mode = (p.uid == this.st.user.id ? "mine" : "others");
376 this.problems[mode].push(p);
377 processWhenWeHaveProb();
383 else processWhenWeHaveProb();
385 gotoPrevNext: function(prob, dir) {
386 const mode = (this.onlyMine ? "mine" : "others");
387 const problems = this.problems[mode];
388 const startIdx = problems.findIndex(p => p.id == prob.id);
389 const nextIdx = startIdx + dir;
390 if (nextIdx >= 0 && nextIdx < problems.length)
391 this.setHrefPid(problems[nextIdx]);
392 else if (this.hasMore[mode]) {
396 if (nbProbs > 0) this.gotoPrevNext(prob, dir);
397 else alert(this.st.tr["No more problems"]);
401 else alert(this.st.tr["No more problems"]);
403 prepareNewProblem: function() {
405 this.adjustHeight("instructions");
406 this.adjustHeight("solution");
407 window.doClick("modalNewprob");
409 sendProblem: function() {
410 const error = checkProblem(this.curproblem);
412 alert(this.st.tr[error]);
415 const edit = this.curproblem.id > 0;
416 this.infoMsg = "Processing... Please wait";
419 edit ? "PUT" : "POST",
421 data: { prob: this.curproblem },
424 let editedP = this.problems["mine"]
425 .find(p => p.id == this.curproblem.id);
427 // I'm an admin and edit another user' problem
428 editedP = this.problems["others"]
429 .find(p => p.id == this.curproblem.id);
430 this.copyProblem(this.curproblem, editedP);
431 this.showProblem(editedP);
434 let newProblem = Object.assign({}, this.curproblem);
435 newProblem.id = ret.id;
436 newProblem.uid = this.st.user.id;
437 newProblem.uname = this.st.user.name;
438 this.problems["mine"] =
439 [newProblem].concat(this.problems["mine"]);
441 document.getElementById("modalNewprob").checked = false;
447 canIedit: function(puid) {
448 return params.devs.concat([puid]).includes(this.st.user.id);
450 editProblem: function(prob) {
451 // prob.diag might correspond to some other problem or be empty:
452 this.setDiagram(prob); //V is loaded at this stage
453 this.copyProblem(prob, this.curproblem);
454 this.adjustHeight("instructions");
455 this.adjustHeight("solution");
456 window.doClick("modalNewprob");
458 deleteProblem: function(prob) {
459 if (confirm(this.st.tr["Are you sure?"])) {
464 data: { id: prob.id },
466 const mode = prob.uid == (this.st.user.id ? "mine" : "others");
467 ArrayFun.remove(this.problems[mode], p => p.id == prob.id);
474 loadMore: function(mode, cb) {
480 uid: this.st.user.id,
482 cursor: this.cursor[mode]
485 const L = res.problems.length;
487 this.cursor[mode] = res.problems[L - 1].added;
488 // Remove potential duplicates:
489 const pids = this.problems[mode].map(p => p.id);
490 ArrayFun.remove(res.problems, p => pids.includes(p.id), "all");
491 this.decorate(res.problems);
492 this.problems[mode] =
493 this.problems[mode].concat(res.problems)
494 // TODO: problems are already sorted, would just need to insert
495 // the current individual problem in list; more generally
496 // there is probably only one misclassified problem.
497 // (Unless the user navigated several times by URL to show a
498 // single problem...)
499 .sort((p1, p2) => p2.added - p1.added);
501 else this.hasMore[mode] = false;
512 @import "@/styles/_board_squares_img.sass"
513 @import "@/styles/_rules.sass"
514 .instructions, .solution
515 margin: 0 var(--universal-margin)
516 p, ul, ol, pre, table, h3, h4, h5, h6, blockquote
517 margin: var(--universal-margin) 0
523 <style lang="sass" scoped>
524 [type="checkbox"].modal+div .card
532 @media screen and (max-width: 1500px)
534 @media screen and (max-width: 1024px)
536 @media screen and (max-width: 767px)
570 background-color: lightgreen
573 display: inline-block
578 padding-left: var(--universal-margin)
580 padding-left: var(--universal-margin)
587 @media screen and (max-width: 767px)
591 a#variantNameInProblems
592 color: var(--card-fore-color)
595 font-size: calc(1rem * var(--heading-ratio))
597 margin: calc(1.5 * var(--universal-margin))