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="st.user.id == 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"] }}
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 label(for="checkboxMine") {{ st.tr["My problems"] }}
79 label(for="selectVariant") {{ st.tr["Variant"] }}
80 select#selectVariant(v-model="selectedVar")
82 v-for="v in [emptyVar].concat(st.variants)"
88 th {{ st.tr["Variant"] }}
89 th {{ st.tr["Instructions"] }}
90 th {{ st.tr["Number"] }}
93 v-show="displayProblem(p)"
94 @click="setHrefPid(p)"
97 td {{ firstChars(p.instruction) }}
103 | {{ st.tr["Load more"] }}
112 import { store } from "@/store";
113 import { ajax } from "@/utils/ajax";
114 import { checkProblem } from "@/data/problemCheck";
115 import { getDiagram } from "@/utils/printDiagram";
116 import { processModalClick } from "@/utils/modalClick";
117 import { ArrayFun } from "@/utils/array";
118 import BaseGame from "@/components/BaseGame.vue";
131 // Problem currently showed, or edited:
133 id: 0, //used in case of edit
141 loadedVar: 0, //corresponding to loaded V
142 selectedVar: 0, //to filter problems based on variant
144 // timestamp of oldest showed problem:
145 cursor: Number.MAX_SAFE_INTEGER,
146 // hasMore == TRUE: a priori there could be more problems to load
152 players: [{ name: "Problem" }, { name: "Problem" }],
157 created: function() {
162 data: { cursor: this.cursor },
164 // The returned list is sorted from most recent to oldest
165 this.problems = res.problems;
166 const L = res.problems.length;
167 if (L > 0) this.cursor = res.problems[L - 1].added;
168 else this.hasMore = false;
169 const showOneIfPid = () => {
170 const pid = this.$route.query["id"];
171 if (!!pid) this.showProblem(this.problems.find(p => p.id == pid));
173 this.decorate(this.problems, showOneIfPid);
178 mounted: function() {
179 document.getElementById("newprobDiv")
180 .addEventListener("click", processModalClick);
183 // st.variants changes only once, at loading from [] to [...]
184 "st.variants": function() {
185 // Set problems vname (either all are set or none)
186 if (this.problems.length > 0 && this.problems[0].vname == "")
187 this.problems.forEach(p => this.setVname(p));
189 $route: function(to) {
190 const pid = to.query["id"];
191 if (!!pid) this.showProblem(this.problems.find(p => p.id == pid));
192 else this.showOne = false;
196 fenFocusIfOpened: function(event) {
197 if (event.target.checked) {
199 document.getElementById("inputFen").focus();
202 setVname: function(prob) {
203 prob.vname = this.st.variants.find(v => v.id == prob.vid).name;
205 // Add vname and user names:
206 decorate: function(problems, callback) {
207 if (this.st.variants.length > 0)
208 problems.forEach(p => this.setVname(p));
209 // Retrieve all problems' authors' names
211 problems.forEach(p => {
212 if (p.uid != this.st.user.id) names[p.uid] = "";
213 else p.uname = this.st.user.name;
215 if (Object.keys(names).length > 0) {
220 data: { ids: Object.keys(names).join(",") },
222 res2.users.forEach(u => {
223 names[u.id] = u.name;
225 problems.forEach(p => {
227 p.uname = names[p.uid];
229 if (!!callback) callback();
233 } else if (!!callback) callback();
235 firstChars: function(text) {
236 let preparedText = text
237 // Replace line jumps and <br> by spaces
239 .replace(/<br\/?>/g, " ")
240 .replace(/<[^>]+>/g, "") //remove remaining HTML tags
241 .replace(/[ ]+/g, " ") //remove series of spaces by only one
243 const maxLength = 32; //arbitrary...
244 if (preparedText.length > maxLength)
245 return preparedText.substr(0, 32) + "...";
248 copyProblem: function(p1, p2) {
249 for (let key in p1) p2[key] = p1[key];
251 setHrefPid: function(p) {
252 // Change href => $route changes, watcher notices, call showProblem
253 const curHref = document.location.href;
254 document.location.href = curHref.split("?")[0] + "?id=" + p.id;
256 backToList: function() {
257 // Change href => $route change, watcher notices, reset showOne to false
258 document.location.href = document.location.href.split("?")[0];
260 resetCurProb: function() {
261 this.curproblem.id = 0;
262 this.curproblem.uid = 0;
263 this.curproblem.vid = "";
264 this.curproblem.vname = "";
265 this.curproblem.fen = "";
266 this.curproblem.diag = "";
267 this.curproblem.instruction = "";
268 this.curproblem.solution = "";
269 this.curproblem.showSolution = false;
271 parseHtml: function(txt) {
272 return !txt.match(/<[/a-zA-Z]+>/)
273 ? txt.replace(/\n/g, "<br/>") //no HTML tag
276 changeVariant: function(prob) {
278 this.loadVariant(prob.vid, () => {
279 // Set FEN if possible (might not be correct yet)
280 if (V.IsGoodFen(prob.fen)) this.setDiagram(prob);
283 loadVariant: async function(vid, cb) {
284 // Condition: vid is a valid variant ID
286 const variant = this.st.variants.find(v => v.id == vid);
287 await import("@/variants/" + variant.name + ".js")
289 window.V = vModule[variant.name + "Rules"];
290 this.loadedVar = vid;
294 trySetDiagram: function(prob) {
295 // Problem edit: FEN could be wrong or incomplete,
296 // variant could not be ready, or not defined
297 if (prob.vid > 0 && this.loadedVar == prob.vid && V.IsGoodFen(prob.fen))
298 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 displayProblem: function(p) {
311 (!this.selectedVar || p.vid == this.selectedVar) &&
312 ((this.onlyMines && p.uid == this.st.user.id) ||
313 (!this.onlyMines && p.uid != this.st.user.id))
316 showProblem: function(p) {
317 this.loadVariant(p.vid, () => {
318 // The FEN is already checked at this stage:
319 this.game.vname = p.vname;
320 this.game.mycolor = V.ParseFen(p.fen).turn; //diagram orientation
321 this.game.fenStart = p.fen;
322 this.game.fen = p.fen;
324 // $nextTick to be sure $refs["basegame"] exists
325 this.$nextTick(() => {
326 this.$refs["basegame"].re_setVariables(this.game); });
327 this.copyProblem(p, this.curproblem);
330 gotoPrevNext: function(e, prob, dir) {
331 const startIdx = this.problems.findIndex(p => p.id == prob.id);
332 let nextIdx = startIdx + dir;
335 nextIdx < this.problems.length &&
336 ((this.onlyMines && this.problems[nextIdx].uid != this.st.user.id) ||
337 (!this.onlyMines && this.problems[nextIdx].uid == this.st.user.id))
340 if (nextIdx >= 0 && nextIdx < this.problems.length)
341 this.setHrefPid(this.problems[nextIdx]);
343 alert(this.st.tr["No more problems"]);
345 prepareNewProblem: function() {
347 window.doClick("modalNewprob");
349 sendProblem: function() {
350 const error = checkProblem(this.curproblem);
352 alert(this.st.tr[error]);
355 const edit = this.curproblem.id > 0;
356 this.infoMsg = "Processing... Please wait";
359 edit ? "PUT" : "POST",
361 data: { prob: this.curproblem },
364 let editedP = this.problems.find(p => p.id == this.curproblem.id);
365 this.copyProblem(this.curproblem, editedP);
366 this.showProblem(editedP);
369 let newProblem = Object.assign({}, this.curproblem);
370 newProblem.id = ret.id;
371 newProblem.uid = this.st.user.id;
372 newProblem.uname = this.st.user.name;
373 this.problems = [newProblem].concat(this.problems);
375 document.getElementById("modalNewprob").checked = false;
381 editProblem: function(prob) {
382 // prob.diag might correspond to some other problem or be empty:
383 this.setDiagram(prob); //V is loaded at this stage
384 this.copyProblem(prob, this.curproblem);
385 window.doClick("modalNewprob");
387 deleteProblem: function(prob) {
388 if (confirm(this.st.tr["Are you sure?"])) {
393 data: { id: prob.id },
395 ArrayFun.remove(this.problems, p => p.id == prob.id);
402 loadMore: function() {
407 data: { cursor: this.cursor },
409 const L = res.problems.length;
411 this.decorate(res.problems);
412 this.problems = this.problems.concat(res.problems);
413 this.cursor = res.problems[L - 1].added;
414 } else this.hasMore = false;
423 <style lang="sass" scoped>
424 [type="checkbox"].modal+div .card
455 background-color: lightgreen
460 padding-left: var(--universal-margin)
462 padding-left: var(--universal-margin)
469 @media screen and (max-width: 767px)