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 const vModule = await import("@/variants/" + variant.name + ".js");
288 window.V = vModule.VariantRules;
289 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);
298 setDiagram: function(prob) {
299 // Condition: prob.fen is correct and global V is ready
300 const parsedFen = V.ParseFen(prob.fen);
302 position: parsedFen.position,
303 orientation: parsedFen.turn
305 prob.diag = getDiagram(args);
307 displayProblem: function(p) {
309 (!this.selectedVar || p.vid == this.selectedVar) &&
310 ((this.onlyMines && p.uid == this.st.user.id) ||
311 (!this.onlyMines && p.uid != this.st.user.id))
314 showProblem: function(p) {
315 this.loadVariant(p.vid, () => {
316 // The FEN is already checked at this stage:
317 this.game.vname = p.vname;
318 this.game.mycolor = V.ParseFen(p.fen).turn; //diagram orientation
319 this.game.fenStart = p.fen;
320 this.game.fen = p.fen;
322 // $nextTick to be sure $refs["basegame"] exists
323 this.$nextTick(() => {
324 this.$refs["basegame"].re_setVariables(this.game); });
325 this.copyProblem(p, this.curproblem);
328 gotoPrevNext: function(e, prob, dir) {
329 const startIdx = this.problems.findIndex(p => p.id == prob.id);
330 let nextIdx = startIdx + dir;
333 nextIdx < this.problems.length &&
334 ((this.onlyMines && this.problems[nextIdx].uid != this.st.user.id) ||
335 (!this.onlyMines && this.problems[nextIdx].uid == this.st.user.id))
338 if (nextIdx >= 0 && nextIdx < this.problems.length)
339 this.setHrefPid(this.problems[nextIdx]);
341 alert(this.st.tr["No more problems"]);
343 prepareNewProblem: function() {
345 window.doClick("modalNewprob");
347 sendProblem: function() {
348 const error = checkProblem(this.curproblem);
350 alert(this.st.tr[error]);
353 const edit = this.curproblem.id > 0;
354 this.infoMsg = "Processing... Please wait";
357 edit ? "PUT" : "POST",
359 data: { prob: this.curproblem },
362 let editedP = this.problems.find(p => p.id == this.curproblem.id);
363 this.copyProblem(this.curproblem, editedP);
364 this.showProblem(editedP);
367 let newProblem = Object.assign({}, this.curproblem);
368 newProblem.id = ret.id;
369 newProblem.uid = this.st.user.id;
370 newProblem.uname = this.st.user.name;
371 this.problems = [newProblem].concat(this.problems);
373 document.getElementById("modalNewprob").checked = false;
379 editProblem: function(prob) {
380 // prob.diag might correspond to some other problem or be empty:
381 this.setDiagram(prob); //V is loaded at this stage
382 this.copyProblem(prob, this.curproblem);
383 window.doClick("modalNewprob");
385 deleteProblem: function(prob) {
386 if (confirm(this.st.tr["Are you sure?"])) {
391 data: { id: prob.id },
393 ArrayFun.remove(this.problems, p => p.id == prob.id);
400 loadMore: function() {
405 data: { cursor: this.cursor },
407 const L = res.problems.length;
409 this.decorate(res.problems);
410 this.problems = this.problems.concat(res.problems);
411 this.cursor = res.problems[L - 1].added;
412 } else this.hasMore = false;
421 <style lang="sass" scoped>
422 [type="checkbox"].modal+div .card
453 background-color: lightgreen
458 padding-left: var(--universal-margin)
460 padding-left: var(--universal-margin)
467 @media screen and (max-width: 767px)