3 input#modalNewprob.modal(
5 @change="fenFocusIfOpened($event)"
9 data-checkbox="modalNewprob"
12 label#closeNewprob.modal-close(for="modalNewprob")
14 label(for="selectVariant") {{ st.tr["Variant"] }}
16 v-model="curproblem.vid"
17 @change="changeVariant(curproblem)"
20 v-for="v in [emptyVar].concat(st.variants)"
23 :selected="curproblem.vid==v.id"
30 v-model="curproblem.fen"
31 @input="trySetDiagram(curproblem)"
33 #diagram(v-html="curproblem.diag")
36 :placeholder="st.tr['Instructions']"
37 v-model="curproblem.instruction"
39 p(v-html="parseHtml(curproblem.instruction)")
42 :placeholder="st.tr['Solution']"
43 v-model="curproblem.solution"
45 p(v-html="parseHtml(curproblem.solution)")
46 button(@click="sendProblem()") {{ st.tr["Send"] }}
47 #dialog.text-center {{ st.tr[infoMsg] }}
49 .col-sm-12.col-md-10.col-md-offset-1.col-lg-8.col-lg-offset-2
51 .button-group(v-if="canIedit(curproblem.uid)")
52 button(@click="editProblem(curproblem)") {{ st.tr["Edit"] }}
53 button(@click="deleteProblem(curproblem)") {{ st.tr["Delete"] }}
54 span.vname {{ curproblem.vname }}
55 span.uname ({{ curproblem.uname }})
56 button.marginleft(@click="backToList()") {{ st.tr["Back to list"] }}
57 button.nomargin(@click="gotoPrevNext(curproblem,1)")
58 | {{ st.tr["Previous_p"] }}
59 button.nomargin(@click="gotoPrevNext(curproblem,-1)")
60 | {{ st.tr["Next_p"] }}
61 p.oneInstructions.clickable(
62 v-html="parseHtml(curproblem.instruction)"
63 @click="curproblem.showSolution=!curproblem.showSolution"
65 | {{ st.tr["Show solution"] }}
67 v-show="curproblem.showSolution"
68 v-html="parseHtml(curproblem.solution)"
71 .col-sm-12.col-md-10.col-md-offset-1.col-lg-8.col-lg-offset-2
73 button#newProblem(@click="prepareNewProblem()")
74 | {{ st.tr["New problem"] }}
75 div#myProblems(v-if="st.user.id > 0")
76 label(for="checkboxMine") {{ st.tr["My problems"] }}
81 label(for="selectVariant") {{ st.tr["Variant"] }}
82 select#selectVariant(v-model="selectedVar")
84 v-for="v in [emptyVar].concat(st.variants)"
91 th {{ st.tr["Variant"] }}
92 th {{ st.tr["Instructions"] }}
93 th {{ st.tr["Number"] }}
95 v-for="p in problems[onlyMine ? 'mine' : 'others']"
96 v-show="onlyMine || !selectedVar || p.vid == selectedVar"
97 @click="setHrefPid(p)"
100 td {{ firstChars(p.instruction) }}
103 v-if="hasMore[onlyMine ? 'mine' : 'others']"
104 @click="loadMore(onlyMine ? 'mine' : 'others')"
106 | {{ st.tr["Load more"] }}
115 import { store } from "@/store";
116 import { ajax } from "@/utils/ajax";
117 import { checkProblem } from "@/data/problemCheck";
118 import params from "@/parameters";
119 import { getDiagram } from "@/utils/printDiagram";
120 import { processModalClick } from "@/utils/modalClick";
121 import { ArrayFun } from "@/utils/array";
122 import BaseGame from "@/components/BaseGame.vue";
135 // Problem currently showed, or edited:
137 id: 0, //used in case of edit
145 loadedVar: 0, //corresponding to loaded V
146 selectedVar: 0, //to filter problems based on variant
147 problems: { "mine": [], "others": [] },
148 // timestamp of oldest showed problem:
150 mine: Number.MAX_SAFE_INTEGER,
151 others: Number.MAX_SAFE_INTEGER
153 // hasMore == TRUE: a priori there could be more problems to load
154 hasMore: { mine: true, others: true },
159 players: [{ name: "Problem" }, { name: "Problem" }],
164 created: function() {
165 const pid = this.$route.query["id"];
166 if (!!pid) this.showProblem(pid);
167 else this.loadMore("others", () => { this.loadMore("mine"); });
169 mounted: function() {
170 document.getElementById("newprobDiv")
171 .addEventListener("click", processModalClick);
174 // st.variants changes only once, at loading from [] to [...]
175 "st.variants": function() {
176 // Set problems vname (either all are set or none)
177 let problems = this.problems["others"].concat(this.problems["mine"]);
178 if (problems.length > 0 && problems[0].vname == "")
179 problems.forEach(p => this.setVname(p));
181 $route: function(to) {
182 const pid = to.query["id"];
183 if (!!pid) this.showProblem(pid);
185 if (this.cursor["others"] == Number.MAX_SAFE_INTEGER)
186 // Back from a single problem view at initial loading:
187 // problems lists are empty!
188 this.loadMore("others", () => { this.loadMore("mine"); });
189 this.showOne = false;
194 fenFocusIfOpened: function(event) {
195 if (event.target.checked) {
197 document.getElementById("inputFen").focus();
200 setVname: function(prob) {
201 prob.vname = this.st.variants.find(v => v.id == prob.vid).name;
203 // Add vname and user names:
204 decorate: function(problems, callback) {
205 if (this.st.variants.length > 0)
206 problems.forEach(p => this.setVname(p));
207 // Retrieve all problems' authors' names
209 problems.forEach(p => {
210 if (p.uid != this.st.user.id) names[p.uid] = "";
211 else p.uname = this.st.user.name;
213 if (Object.keys(names).length > 0) {
218 data: { ids: Object.keys(names).join(",") },
220 res2.users.forEach(u => {
221 names[u.id] = u.name;
223 problems.forEach(p => {
225 p.uname = names[p.uid];
227 if (!!callback) callback();
231 } else if (!!callback) callback();
233 firstChars: function(text) {
234 let preparedText = text
235 // Replace line jumps and <br> by spaces
237 .replace(/<br\/?>/g, " ")
238 .replace(/<[^>]+>/g, "") //remove remaining HTML tags
239 .replace(/[ ]+/g, " ") //remove series of spaces by only one
241 const maxLength = 32; //arbitrary...
242 if (preparedText.length > maxLength)
243 return preparedText.substr(0, 32) + "...";
246 copyProblem: function(p1, p2) {
247 for (let key in p1) p2[key] = p1[key];
249 setHrefPid: function(p) {
250 // Change href => $route changes, watcher notices, call showProblem
251 const curHref = document.location.href;
252 document.location.href = curHref.split("?")[0] + "?id=" + p.id;
254 backToList: function() {
255 // Change href => $route change, watcher notices, reset showOne to false
256 document.location.href = document.location.href.split("?")[0];
258 resetCurProb: function() {
259 this.curproblem.id = 0;
260 this.curproblem.uid = 0;
261 this.curproblem.vid = "";
262 this.curproblem.vname = "";
263 this.curproblem.fen = "";
264 this.curproblem.diag = "";
265 this.curproblem.instruction = "";
266 this.curproblem.solution = "";
267 this.curproblem.showSolution = false;
269 parseHtml: function(txt) {
270 return !txt.match(/<[/a-zA-Z]+>/)
271 ? txt.replace(/\n/g, "<br/>") //no HTML tag
274 changeVariant: function(prob) {
276 this.loadVariant(prob.vid, () => {
277 // Set FEN if possible (might not be correct yet)
278 if (V.IsGoodFen(prob.fen)) this.setDiagram(prob);
282 loadVariant: async function(vid, cb) {
283 // Condition: vid is a valid variant ID
285 const variant = this.st.variants.find(v => v.id == vid);
286 await import("@/variants/" + variant.name + ".js")
288 window.V = vModule[variant.name + "Rules"];
289 this.loadedVar = vid;
293 trySetDiagram: function(prob) {
294 // Problem edit: FEN could be wrong or incomplete,
295 // variant could not be ready, or not defined
296 if (prob.vid > 0 && this.loadedVar == prob.vid && V.IsGoodFen(prob.fen))
297 this.setDiagram(prob);
300 setDiagram: function(prob) {
301 // Condition: prob.fen is correct and global V is ready
302 const parsedFen = V.ParseFen(prob.fen);
304 position: parsedFen.position,
305 orientation: parsedFen.turn
307 prob.diag = getDiagram(args);
309 showProblem: function(p_id) {
310 const processWhenWeHaveProb = () => {
311 this.loadVariant(p.vid, () => {
312 this.onlyMine = (p.uid == this.st.user.id);
313 // The FEN is already checked at this stage:
314 this.game.vname = p.vname;
315 this.game.mycolor = V.ParseFen(p.fen).turn; //diagram orientation
316 this.game.fenStart = p.fen;
317 this.game.fen = p.fen;
319 // $nextTick to be sure $refs["basegame"] exists
320 this.$nextTick(() => {
321 this.$refs["basegame"].re_setVariables(this.game); });
322 this.curproblem.showSolution = false; //in case of
323 this.copyProblem(p, this.curproblem);
327 if (typeof p_id == "object") p = p_id;
329 const problems = this.problems["others"].concat(this.problems["mine"]);
330 p = problems.find(prob => prob.id == p_id);
333 // Bad luck: problem not in list. Get from server
340 this.decorate([res.problem], () => {
342 const mode = (p.uid == this.st.user.id ? "mine" : "others");
343 this.problems[mode].push(p);
344 processWhenWeHaveProb();
349 } else processWhenWeHaveProb();
351 gotoPrevNext: function(prob, dir) {
352 const mode = (this.onlyMine ? "mine" : "others");
353 const problems = this.problems[mode];
354 const startIdx = problems.findIndex(p => p.id == prob.id);
355 const nextIdx = startIdx + dir;
356 if (nextIdx >= 0 && nextIdx < problems.length)
357 this.setHrefPid(problems[nextIdx]);
358 else if (this.hasMore[mode]) {
362 if (nbProbs > 0) this.gotoPrevNext(prob, dir);
363 else alert(this.st.tr["No more problems"]);
367 else alert(this.st.tr["No more problems"]);
369 prepareNewProblem: function() {
371 window.doClick("modalNewprob");
373 sendProblem: function() {
374 const error = checkProblem(this.curproblem);
376 alert(this.st.tr[error]);
379 const edit = this.curproblem.id > 0;
380 this.infoMsg = "Processing... Please wait";
383 edit ? "PUT" : "POST",
385 data: { prob: this.curproblem },
388 let editedP = this.problems["mine"]
389 .find(p => p.id == this.curproblem.id);
391 // I'm an admin and edit another user' problem
392 editedP = this.problems["others"]
393 .find(p => p.id == this.curproblem.id);
394 this.copyProblem(this.curproblem, editedP);
395 this.showProblem(editedP);
398 let newProblem = Object.assign({}, this.curproblem);
399 newProblem.id = ret.id;
400 newProblem.uid = this.st.user.id;
401 newProblem.uname = this.st.user.name;
402 this.problems["mine"] =
403 [newProblem].concat(this.problems["mine"]);
405 document.getElementById("modalNewprob").checked = false;
411 canIedit: function(puid) {
412 return params.devs.concat([puid]).includes(this.st.user.id);
414 editProblem: function(prob) {
415 // prob.diag might correspond to some other problem or be empty:
416 this.setDiagram(prob); //V is loaded at this stage
417 this.copyProblem(prob, this.curproblem);
418 window.doClick("modalNewprob");
420 deleteProblem: function(prob) {
421 if (confirm(this.st.tr["Are you sure?"])) {
426 data: { id: prob.id },
428 const mode = prob.uid == (this.st.user.id ? "mine" : "others");
429 ArrayFun.remove(this.problems[mode], p => p.id == prob.id);
436 loadMore: function(mode, cb) {
442 uid: this.st.user.id,
444 cursor: this.cursor[mode]
447 const L = res.problems.length;
449 this.cursor[mode] = res.problems[L - 1].added;
450 // Remove potential duplicates:
451 const pids = this.problems[mode].map(p => p.id);
452 ArrayFun.remove(res.problems, p => pids.includes(p.id), "all");
453 this.decorate(res.problems);
454 this.problems[mode] =
455 this.problems[mode].concat(res.problems)
456 // TODO: problems are alrady sorted, would just need to insert
457 // the current individual problem in list; more generally
458 // there is probably only one misclassified problem.
459 // (Unless the user navigated several times by URL to show a
460 // single problem...)
461 .sort((p1, p2) => p2.added - p1.added);
462 } else this.hasMore[mode] = false;
473 @import "@/styles/_board_squares_img.sass"
476 <style lang="sass" scoped>
477 [type="checkbox"].modal+div .card
508 background-color: lightgreen
511 display: inline-block
516 padding-left: var(--universal-margin)
518 padding-left: var(--universal-margin)
525 @media screen and (max-width: 767px)