3 input#modalRules.modal(type="checkbox")
6 data-checkbox="modalRules"
9 label.modal-close(for="modalRules")
10 a#variantNameInProblems(:href="'/#/variants/'+game.vname")
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")
46 :placeholder="st.tr['Instructions']"
47 v-model="curproblem.instruction"
49 p(v-html="parseHtml(curproblem.instruction)")
52 :placeholder="st.tr['Solution']"
53 v-model="curproblem.solution"
55 p(v-html="parseHtml(curproblem.solution)")
56 button(@click="sendProblem()") {{ st.tr["Send"] }}
57 #dialog.text-center {{ st.tr[infoMsg] }}
59 .col-sm-12.col-md-10.col-md-offset-1.col-lg-8.col-lg-offset-2
61 .button-group(v-if="canIedit(curproblem.uid)")
62 button(@click="editProblem(curproblem)") {{ st.tr["Edit"] }}
63 button(@click="deleteProblem(curproblem)") {{ st.tr["Delete"] }}
64 span.vname {{ curproblem.vname }}
65 span.uname ({{ curproblem.uname }})
66 button.marginleft(@click="backToList()") {{ st.tr["Back to list"] }}
67 button.nomargin(@click="gotoPrevNext(curproblem,1)")
68 | {{ st.tr["Previous_p"] }}
69 button.nomargin(@click="gotoPrevNext(curproblem,-1)")
70 | {{ st.tr["Next_p"] }}
71 p.oneInstructions.clickable(
72 v-html="parseHtml(curproblem.instruction)"
73 @click="curproblem.showSolution=!curproblem.showSolution"
75 | {{ st.tr["Show solution"] }}
77 v-show="curproblem.showSolution"
78 v-html="parseHtml(curproblem.solution)"
81 .col-sm-12.col-md-10.col-md-offset-1.col-lg-8.col-lg-offset-2
83 button#newProblem(@click="prepareNewProblem()")
84 | {{ st.tr["New problem"] }}
85 div#myProblems(v-if="st.user.id > 0")
86 label(for="checkboxMine") {{ st.tr["My problems"] }}
91 label(for="selectVariant") {{ st.tr["Variant"] }}
92 select#selectVariant(v-model="selectedVar")
94 v-for="v in [emptyVar].concat(st.variants)"
101 th {{ st.tr["Variant"] }}
102 th {{ st.tr["Instructions"] }}
103 th {{ st.tr["Number"] }}
105 v-for="p in problems[onlyMine ? 'mine' : 'others']"
106 v-show="onlyMine || !selectedVar || p.vid == selectedVar"
107 @click="setHrefPid(p)"
110 td {{ firstChars(p.instruction) }}
113 v-if="hasMore[onlyMine ? 'mine' : 'others']"
114 @click="loadMore(onlyMine ? 'mine' : 'others')"
116 | {{ st.tr["Load more"] }}
125 import { store } from "@/store";
126 import { ajax } from "@/utils/ajax";
127 import { checkProblem } from "@/data/problemCheck";
128 import params from "@/parameters";
129 import { getDiagram, replaceByDiag } from "@/utils/printDiagram";
130 import { processModalClick } from "@/utils/modalClick";
131 import { ArrayFun } from "@/utils/array";
132 import BaseGame from "@/components/BaseGame.vue";
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 setVname: function(prob) {
214 prob.vname = this.st.variants.find(v => v.id == prob.vid).name;
216 // Add vname and user names:
217 decorate: function(problems, callback) {
218 if (this.st.variants.length > 0)
219 problems.forEach(p => this.setVname(p));
220 // Retrieve all problems' authors' names
222 problems.forEach(p => {
223 if (p.uid != this.st.user.id) names[p.uid] = "";
224 else p.uname = this.st.user.name;
226 if (Object.keys(names).length > 0) {
231 data: { ids: Object.keys(names).join(",") },
233 res2.users.forEach(u => {
234 names[u.id] = u.name;
236 problems.forEach(p => {
238 p.uname = names[p.uid];
240 if (!!callback) callback();
244 } else if (!!callback) callback();
246 firstChars: function(text) {
247 let preparedText = text
248 // Replace line jumps and <br> by spaces
250 .replace(/<br\/?>/g, " ")
251 .replace(/<[^>]+>/g, "") //remove remaining HTML tags
252 .replace(/[ ]+/g, " ") //remove series of spaces by only one
254 const maxLength = 32; //arbitrary...
255 if (preparedText.length > maxLength)
256 return preparedText.substr(0, 32) + "...";
259 copyProblem: function(p1, p2) {
260 for (let key in p1) p2[key] = p1[key];
262 setHrefPid: function(p) {
263 // Change href => $route changes, watcher notices, call showProblem
264 const curHref = document.location.href;
265 document.location.href = curHref.split("?")[0] + "?id=" + p.id;
267 backToList: function() {
268 // Change href => $route change, watcher notices, reset showOne to false
269 document.location.href = document.location.href.split("?")[0];
271 resetCurProb: function() {
272 this.curproblem.id = 0;
273 this.curproblem.uid = 0;
274 this.curproblem.vid = "";
275 this.curproblem.vname = "";
276 this.curproblem.fen = "";
277 this.curproblem.diag = "";
278 this.curproblem.instruction = "";
279 this.curproblem.solution = "";
280 this.curproblem.showSolution = false;
282 parseHtml: function(txt) {
283 return !txt.match(/<[/a-zA-Z]+>/)
284 ? txt.replace(/\n/g, "<br/>") //no HTML tag
287 changeVariant: function(prob) {
289 this.loadVariant(prob.vid, () => {
290 // Set FEN if possible (might not be correct yet)
291 if (V.IsGoodFen(prob.fen)) this.setDiagram(prob);
295 loadVariant: async function(vid, cb) {
296 // Condition: vid is a valid variant ID
298 const variant = this.st.variants.find(v => v.id == vid);
299 await import("@/variants/" + variant.name + ".js")
301 window.V = vModule[variant.name + "Rules"];
302 this.loadedVar = vid;
305 // (AJAX) Request to get rules content (plain text, HTML)
308 "raw-loader!@/translations/rules/" +
310 this.st.lang + ".pug"
312 // Next two lines fix a weird issue after last update (2019-11)
313 .replace(/\\n/g, " ")
314 .replace(/\\"/g, '"')
315 .replace('module.exports = "', "")
317 .replace(/(fen:)([^:]*):/g, replaceByDiag);
319 trySetDiagram: function(prob) {
320 // Problem edit: FEN could be wrong or incomplete,
321 // variant could not be ready, or not defined
322 if (prob.vid > 0 && this.loadedVar == prob.vid && V.IsGoodFen(prob.fen))
323 this.setDiagram(prob);
326 setDiagram: function(prob) {
327 // Condition: prob.fen is correct and global V is ready
328 const parsedFen = V.ParseFen(prob.fen);
330 position: parsedFen.position,
331 orientation: parsedFen.turn
333 prob.diag = getDiagram(args);
335 showProblem: function(p_id) {
336 const processWhenWeHaveProb = () => {
337 this.loadVariant(p.vid, () => {
338 this.onlyMine = (p.uid == this.st.user.id);
339 // The FEN is already checked at this stage:
340 this.game.vname = p.vname;
341 this.game.mycolor = V.ParseFen(p.fen).turn; //diagram orientation
342 this.game.fenStart = p.fen;
343 this.game.fen = p.fen;
345 // $nextTick to be sure $refs["basegame"] exists
346 this.$nextTick(() => {
347 this.$refs["basegame"].re_setVariables(this.game); });
348 this.curproblem.showSolution = false; //in case of
349 this.copyProblem(p, this.curproblem);
353 if (typeof p_id == "object") p = p_id;
355 const problems = this.problems["others"].concat(this.problems["mine"]);
356 p = problems.find(prob => prob.id == p_id);
359 // Bad luck: problem not in list. Get from server
366 this.decorate([res.problem], () => {
368 const mode = (p.uid == this.st.user.id ? "mine" : "others");
369 this.problems[mode].push(p);
370 processWhenWeHaveProb();
375 } else processWhenWeHaveProb();
377 gotoPrevNext: function(prob, dir) {
378 const mode = (this.onlyMine ? "mine" : "others");
379 const problems = this.problems[mode];
380 const startIdx = problems.findIndex(p => p.id == prob.id);
381 const nextIdx = startIdx + dir;
382 if (nextIdx >= 0 && nextIdx < problems.length)
383 this.setHrefPid(problems[nextIdx]);
384 else if (this.hasMore[mode]) {
388 if (nbProbs > 0) this.gotoPrevNext(prob, dir);
389 else alert(this.st.tr["No more problems"]);
393 else alert(this.st.tr["No more problems"]);
395 prepareNewProblem: function() {
397 window.doClick("modalNewprob");
399 sendProblem: function() {
400 const error = checkProblem(this.curproblem);
402 alert(this.st.tr[error]);
405 const edit = this.curproblem.id > 0;
406 this.infoMsg = "Processing... Please wait";
409 edit ? "PUT" : "POST",
411 data: { prob: this.curproblem },
414 let editedP = this.problems["mine"]
415 .find(p => p.id == this.curproblem.id);
417 // I'm an admin and edit another user' problem
418 editedP = this.problems["others"]
419 .find(p => p.id == this.curproblem.id);
420 this.copyProblem(this.curproblem, editedP);
421 this.showProblem(editedP);
424 let newProblem = Object.assign({}, this.curproblem);
425 newProblem.id = ret.id;
426 newProblem.uid = this.st.user.id;
427 newProblem.uname = this.st.user.name;
428 this.problems["mine"] =
429 [newProblem].concat(this.problems["mine"]);
431 document.getElementById("modalNewprob").checked = false;
437 canIedit: function(puid) {
438 return params.devs.concat([puid]).includes(this.st.user.id);
440 editProblem: function(prob) {
441 // prob.diag might correspond to some other problem or be empty:
442 this.setDiagram(prob); //V is loaded at this stage
443 this.copyProblem(prob, this.curproblem);
444 window.doClick("modalNewprob");
446 deleteProblem: function(prob) {
447 if (confirm(this.st.tr["Are you sure?"])) {
452 data: { id: prob.id },
454 const mode = prob.uid == (this.st.user.id ? "mine" : "others");
455 ArrayFun.remove(this.problems[mode], p => p.id == prob.id);
462 loadMore: function(mode, cb) {
468 uid: this.st.user.id,
470 cursor: this.cursor[mode]
473 const L = res.problems.length;
475 this.cursor[mode] = res.problems[L - 1].added;
476 // Remove potential duplicates:
477 const pids = this.problems[mode].map(p => p.id);
478 ArrayFun.remove(res.problems, p => pids.includes(p.id), "all");
479 this.decorate(res.problems);
480 this.problems[mode] =
481 this.problems[mode].concat(res.problems)
482 // TODO: problems are alrady sorted, would just need to insert
483 // the current individual problem in list; more generally
484 // there is probably only one misclassified problem.
485 // (Unless the user navigated several times by URL to show a
486 // single problem...)
487 .sort((p1, p2) => p2.added - p1.added);
488 } else this.hasMore[mode] = false;
499 @import "@/styles/_board_squares_img.sass"
500 @import "@/styles/_rules.sass"
503 <style lang="sass" scoped>
504 [type="checkbox"].modal+div .card
512 @media screen and (max-width: 1500px)
514 @media screen and (max-width: 1024px)
516 @media screen and (max-width: 767px)
546 background-color: lightgreen
549 display: inline-block
554 padding-left: var(--universal-margin)
556 padding-left: var(--universal-margin)
563 @media screen and (max-width: 767px)
567 a#variantNameInProblems
568 color: var(--card-fore-color)
571 font-size: calc(1rem * var(--heading-ratio))
573 margin: calc(1.5 * var(--universal-margin))