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)"
22 :selected="curproblem.vid==v.id"
29 v-model="curproblem.fen"
30 @input="trySetDiagram(curproblem)"
32 #diagram(v-html="curproblem.diag")
35 :placeholder="st.tr['Instructions']"
36 v-model="curproblem.instruction"
38 p(v-html="parseHtml(curproblem.instruction)")
41 :placeholder="st.tr['Solution']"
42 v-model="curproblem.solution"
44 p(v-html="parseHtml(curproblem.solution)")
45 button(@click="sendProblem()") {{ st.tr["Send"] }}
46 #dialog.text-center {{ st.tr[infoMsg] }}
48 .col-sm-12.col-md-10.col-md-offset-1.col-lg-8.col-lg-offset-2
50 .button-group(v-if="canIedit(curproblem.uid)")
51 button(@click="editProblem(curproblem)") {{ st.tr["Edit"] }}
52 button(@click="deleteProblem(curproblem)") {{ st.tr["Delete"] }}
53 span.vname {{ curproblem.vname }}
54 span.uname ({{ curproblem.uname }})
55 button.marginleft(@click="backToList()") {{ st.tr["Back to list"] }}
56 button.nomargin(@click="gotoPrevNext($event,curproblem,1)")
57 | {{ st.tr["Previous_p"] }}
58 button.nomargin(@click="gotoPrevNext($event,curproblem,-1)")
59 | {{ st.tr["Next_p"] }}
60 p.oneInstructions.clickable(
61 v-html="parseHtml(curproblem.instruction)"
62 @click="curproblem.showSolution=!curproblem.showSolution"
64 | {{ st.tr["Show solution"] }}
66 v-show="curproblem.showSolution"
67 v-html="parseHtml(curproblem.solution)"
70 .col-sm-12.col-md-10.col-md-offset-1.col-lg-8.col-lg-offset-2
72 button#newProblem(@click="prepareNewProblem()")
73 | {{ st.tr["New problem"] }}
74 div#myProblems(v-if="st.user.id > 0")
75 label(for="checkboxMine") {{ st.tr["My problems"] }}
80 label(for="selectVariant") {{ st.tr["Variant"] }}
81 select#selectVariant(v-model="selectedVar")
83 v-for="v in [emptyVar].concat(st.variants)"
90 th {{ st.tr["Variant"] }}
91 th {{ st.tr["Instructions"] }}
92 th {{ st.tr["Number"] }}
94 v-for="p in problems[onlyMine ? 'mine' : 'others']"
95 v-show="onlyMine || !selectedVar || p.vid == selectedVar"
96 @click="setHrefPid(p)"
99 td {{ firstChars(p.instruction) }}
102 v-if="hasMore[onlyMine ? 'mine' : 'others']"
103 @click="loadMore(onlyMine ? 'mine' : 'others')"
105 | {{ st.tr["Load more"] }}
114 import { store } from "@/store";
115 import { ajax } from "@/utils/ajax";
116 import { checkProblem } from "@/data/problemCheck";
117 import { getDiagram } from "@/utils/printDiagram";
118 import { processModalClick } from "@/utils/modalClick";
119 import { ArrayFun } from "@/utils/array";
120 import BaseGame from "@/components/BaseGame.vue";
133 // Problem currently showed, or edited:
135 id: 0, //used in case of edit
143 loadedVar: 0, //corresponding to loaded V
144 selectedVar: 0, //to filter problems based on variant
145 problems: { "mine": [], "others": [] },
146 // timestamp of oldest showed problem:
148 mine: Number.MAX_SAFE_INTEGER,
149 others: Number.MAX_SAFE_INTEGER
151 // hasMore == TRUE: a priori there could be more problems to load
152 hasMore: { mine: true, others: true },
156 admins: [1], //hard-coded for now. TODO
158 players: [{ name: "Problem" }, { name: "Problem" }],
163 created: function() {
164 const pid = this.$route.query["id"];
165 if (!!pid) this.showProblem(pid);
166 else this.loadMore("others", () => { this.loadMore("mine"); });
168 mounted: function() {
169 document.getElementById("newprobDiv")
170 .addEventListener("click", processModalClick);
173 // st.variants changes only once, at loading from [] to [...]
174 "st.variants": function() {
175 // Set problems vname (either all are set or none)
176 let problems = this.problems["others"].concat(this.problems["mine"]);
177 if (problems.length > 0 && problems[0].vname == "")
178 problems.forEach(p => this.setVname(p));
180 $route: function(to) {
181 const pid = to.query["id"];
182 if (!!pid) this.showProblem(pid);
184 if (this.cursor["others"] == Number.MAX_SAFE_INTEGER)
185 // Back from a single problem view at initial loading:
186 // problems lists are empty!
187 this.loadMore("others", () => { this.loadMore("mine"); });
188 this.showOne = false;
193 fenFocusIfOpened: function(event) {
194 if (event.target.checked) {
196 document.getElementById("inputFen").focus();
199 setVname: function(prob) {
200 prob.vname = this.st.variants.find(v => v.id == prob.vid).name;
202 // Add vname and user names:
203 decorate: function(problems, callback) {
204 if (this.st.variants.length > 0)
205 problems.forEach(p => this.setVname(p));
206 // Retrieve all problems' authors' names
208 problems.forEach(p => {
209 if (p.uid != this.st.user.id) names[p.uid] = "";
210 else p.uname = this.st.user.name;
212 if (Object.keys(names).length > 0) {
217 data: { ids: Object.keys(names).join(",") },
219 res2.users.forEach(u => {
220 names[u.id] = u.name;
222 problems.forEach(p => {
224 p.uname = names[p.uid];
226 if (!!callback) callback();
230 } else if (!!callback) callback();
232 firstChars: function(text) {
233 let preparedText = text
234 // Replace line jumps and <br> by spaces
236 .replace(/<br\/?>/g, " ")
237 .replace(/<[^>]+>/g, "") //remove remaining HTML tags
238 .replace(/[ ]+/g, " ") //remove series of spaces by only one
240 const maxLength = 32; //arbitrary...
241 if (preparedText.length > maxLength)
242 return preparedText.substr(0, 32) + "...";
245 copyProblem: function(p1, p2) {
246 for (let key in p1) p2[key] = p1[key];
248 setHrefPid: function(p) {
249 // Change href => $route changes, watcher notices, call showProblem
250 const curHref = document.location.href;
251 document.location.href = curHref.split("?")[0] + "?id=" + p.id;
253 backToList: function() {
254 // Change href => $route change, watcher notices, reset showOne to false
255 document.location.href = document.location.href.split("?")[0];
257 resetCurProb: function() {
258 this.curproblem.id = 0;
259 this.curproblem.uid = 0;
260 this.curproblem.vid = "";
261 this.curproblem.vname = "";
262 this.curproblem.fen = "";
263 this.curproblem.diag = "";
264 this.curproblem.instruction = "";
265 this.curproblem.solution = "";
266 this.curproblem.showSolution = false;
268 parseHtml: function(txt) {
269 return !txt.match(/<[/a-zA-Z]+>/)
270 ? txt.replace(/\n/g, "<br/>") //no HTML tag
273 changeVariant: function(prob) {
275 this.loadVariant(prob.vid, () => {
276 // Set FEN if possible (might not be correct yet)
277 if (V.IsGoodFen(prob.fen)) this.setDiagram(prob);
281 loadVariant: async function(vid, cb) {
282 // Condition: vid is a valid variant ID
284 const variant = this.st.variants.find(v => v.id == vid);
285 await import("@/variants/" + variant.name + ".js")
287 window.V = vModule[variant.name + "Rules"];
288 this.loadedVar = vid;
292 trySetDiagram: function(prob) {
293 // Problem edit: FEN could be wrong or incomplete,
294 // variant could not be ready, or not defined
295 if (prob.vid > 0 && this.loadedVar == prob.vid && V.IsGoodFen(prob.fen))
296 this.setDiagram(prob);
299 setDiagram: function(prob) {
300 // Condition: prob.fen is correct and global V is ready
301 const parsedFen = V.ParseFen(prob.fen);
303 position: parsedFen.position,
304 orientation: parsedFen.turn
306 prob.diag = getDiagram(args);
308 showProblem: function(p_id) {
309 const processWhenWeHaveProb = () => {
310 this.loadVariant(p.vid, () => {
311 this.onlyMine = (p.uid == this.st.user.id);
312 // The FEN is already checked at this stage:
313 this.game.vname = p.vname;
314 this.game.mycolor = V.ParseFen(p.fen).turn; //diagram orientation
315 this.game.fenStart = p.fen;
316 this.game.fen = p.fen;
318 // $nextTick to be sure $refs["basegame"] exists
319 this.$nextTick(() => {
320 this.$refs["basegame"].re_setVariables(this.game); });
321 this.copyProblem(p, this.curproblem);
325 if (typeof p_id == "object") p = p_id;
327 const problems = this.problems["others"].concat(this.problems["mine"]);
328 p = problems.find(prob => prob.id == p_id);
331 // Bad luck: problem not in list. Get from server
338 this.decorate([res.problem], () => {
340 const mode = (p.uid == this.st.user.id ? "mine" : "others");
341 this.problems[mode].push(p);
342 processWhenWeHaveProb();
347 } else processWhenWeHaveProb();
349 gotoPrevNext: function(e, prob, dir) {
350 const mode = (this.onlyMine ? "mine" : "others");
351 const problems = this.problems[mode];
352 const startIdx = problems.findIndex(p => p.id == prob.id);
353 const nextIdx = startIdx + dir;
354 if (nextIdx >= 0 && nextIdx < problems.length)
355 this.setHrefPid(problems[nextIdx]);
356 else if (this.hasMore[mode]) this.loadMore(mode);
357 else alert(this.st.tr["No more problems"]);
359 prepareNewProblem: function() {
361 window.doClick("modalNewprob");
363 sendProblem: function() {
364 const error = checkProblem(this.curproblem);
366 alert(this.st.tr[error]);
369 const edit = this.curproblem.id > 0;
370 this.infoMsg = "Processing... Please wait";
373 edit ? "PUT" : "POST",
375 data: { prob: this.curproblem },
378 let editedP = this.problems["mine"]
379 .find(p => p.id == this.curproblem.id);
381 // I'm an admin and edit another user' problem
382 editedP = this.problems["others"]
383 .find(p => p.id == this.curproblem.id);
384 this.copyProblem(this.curproblem, editedP);
385 this.showProblem(editedP);
388 let newProblem = Object.assign({}, this.curproblem);
389 newProblem.id = ret.id;
390 newProblem.uid = this.st.user.id;
391 newProblem.uname = this.st.user.name;
392 this.problems["mine"] =
393 [newProblem].concat(this.problems["mine"]);
395 document.getElementById("modalNewprob").checked = false;
401 canIedit: function(puid) {
402 return this.admins.concat([puid]).includes(this.st.user.id);
404 editProblem: function(prob) {
405 // prob.diag might correspond to some other problem or be empty:
406 this.setDiagram(prob); //V is loaded at this stage
407 this.copyProblem(prob, this.curproblem);
408 window.doClick("modalNewprob");
410 deleteProblem: function(prob) {
411 if (confirm(this.st.tr["Are you sure?"])) {
416 data: { id: prob.id },
418 const mode = prob.uid == (this.st.user.id ? "mine" : "others");
419 ArrayFun.remove(this.problems[mode], p => p.id == prob.id);
426 loadMore: function(mode, cb) {
432 uid: this.st.user.id,
434 cursor: this.cursor[mode]
437 const L = res.problems.length;
439 this.cursor[mode] = res.problems[L - 1].added;
440 // Remove potential duplicates:
441 const pids = this.problems[mode].map(p => p.id);
442 ArrayFun.remove(res.problems, p => pids.includes(p.id), "all");
443 this.decorate(res.problems);
444 this.problems[mode] = this.problems[mode].concat(res.problems);
445 } else this.hasMore[mode] = false;
455 <style lang="sass" scoped>
456 [type="checkbox"].modal+div .card
487 background-color: lightgreen
490 display: inline-block
495 padding-left: var(--universal-margin)
497 padding-left: var(--universal-margin)
504 @media screen and (max-width: 767px)