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) }}
106 import { store } from "@/store";
107 import { ajax } from "@/utils/ajax";
108 import { checkProblem } from "@/data/problemCheck";
109 import { getDiagram } from "@/utils/printDiagram";
110 import { processModalClick } from "@/utils/modalClick";
111 import { ArrayFun } from "@/utils/array";
112 import BaseGame from "@/components/BaseGame.vue";
125 // Problem currently showed, or edited:
127 id: 0, //used in case of edit
135 loadedVar: 0, //corresponding to loaded V
136 selectedVar: 0, //to filter problems based on variant
142 players: [{ name: "Problem" }, { name: "Problem" }],
147 created: function() {
148 ajax("/problems", "GET", res => {
149 // Show newest problem first:
150 this.problems = res.problems.sort((p1, p2) => p2.added - p1.added);
151 if (this.st.variants.length > 0)
152 this.problems.forEach(p => this.setVname(p));
153 // Retrieve all problems' authors' names
155 this.problems.forEach(p => {
156 if (p.uid != this.st.user.id) names[p.uid] = "";
157 else p.uname = this.st.user.name;
159 const showOneIfPid = () => {
160 const pid = this.$route.query["id"];
161 if (pid) this.showProblem(this.problems.find(p => p.id == pid));
163 if (Object.keys(names).length > 0) {
164 ajax("/users", "GET", { ids: Object.keys(names).join(",") }, res2 => {
165 res2.users.forEach(u => {
166 names[u.id] = u.name;
168 this.problems.forEach(p => {
170 p.uname = names[p.uid];
174 } else showOneIfPid();
177 mounted: function() {
179 .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 firstChars: function(text) {
206 let preparedText = text
207 // Replace line jumps and <br> by spaces
209 .replace(/<br\/?>/g, " ")
210 .replace(/<[^>]+>/g, "") //remove remaining HTML tags
211 .replace(/[ ]+/g, " ") //remove series of spaces by only one
213 const maxLength = 32; //arbitrary...
214 if (preparedText.length > maxLength)
215 return preparedText.substr(0, 32) + "...";
218 copyProblem: function(p1, p2) {
219 for (let key in p1) p2[key] = p1[key];
221 setHrefPid: function(p) {
222 // Change href => $route changes, watcher notices, call showProblem
223 const curHref = document.location.href;
224 document.location.href = curHref.split("?")[0] + "?id=" + p.id;
226 backToList: function() {
227 // Change href => $route change, watcher notices, reset showOne to false
228 document.location.href = document.location.href.split("?")[0];
230 resetCurProb: function() {
231 this.curproblem.id = 0;
232 this.curproblem.uid = 0;
233 this.curproblem.vid = "";
234 this.curproblem.vname = "";
235 this.curproblem.fen = "";
236 this.curproblem.diag = "";
237 this.curproblem.instruction = "";
238 this.curproblem.solution = "";
239 this.curproblem.showSolution = false;
241 parseHtml: function(txt) {
242 return !txt.match(/<[/a-zA-Z]+>/)
243 ? txt.replace(/\n/g, "<br/>") //no HTML tag
246 changeVariant: function(prob) {
248 this.loadVariant(prob.vid, () => {
249 // Set FEN if possible (might not be correct yet)
250 if (V.IsGoodFen(prob.fen)) this.setDiagram(prob);
253 loadVariant: async function(vid, cb) {
254 // Condition: vid is a valid variant ID
256 const variant = this.st.variants.find(v => v.id == vid);
257 const vModule = await import("@/variants/" + variant.name + ".js");
258 window.V = vModule.VariantRules;
259 this.loadedVar = vid;
262 trySetDiagram: function(prob) {
263 // Problem edit: FEN could be wrong or incomplete,
264 // variant could not be ready, or not defined
265 if (prob.vid > 0 && this.loadedVar == prob.vid && V.IsGoodFen(prob.fen))
266 this.setDiagram(prob);
268 setDiagram: function(prob) {
269 // Condition: prob.fen is correct and global V is ready
270 const parsedFen = V.ParseFen(prob.fen);
272 position: parsedFen.position,
273 orientation: parsedFen.turn
275 prob.diag = getDiagram(args);
277 displayProblem: function(p) {
279 (!this.selectedVar || p.vid == this.selectedVar) &&
280 ((this.onlyMines && p.uid == this.st.user.id) ||
281 (!this.onlyMines && p.uid != this.st.user.id))
284 showProblem: function(p) {
285 this.loadVariant(p.vid, () => {
286 // The FEN is already checked at this stage:
287 this.game.vname = p.vname;
288 this.game.mycolor = V.ParseFen(p.fen).turn; //diagram orientation
289 this.game.fen = p.fen;
290 this.$set(this.game, "fenStart", p.fen);
291 this.copyProblem(p, this.curproblem);
295 gotoPrevNext: function(e, prob, dir) {
296 const startIdx = this.problems.findIndex(p => p.id == prob.id);
297 let nextIdx = startIdx + dir;
300 nextIdx < this.problems.length &&
301 ((this.onlyMines && this.problems[nextIdx].uid != this.st.user.id) ||
302 (!this.onlyMines && this.problems[nextIdx].uid == this.st.user.id))
305 if (nextIdx >= 0 && nextIdx < this.problems.length)
306 this.setHrefPid(this.problems[nextIdx]);
308 alert(this.st.tr["No more problems"]);
310 prepareNewProblem: function() {
312 window.doClick("modalNewprob");
314 sendProblem: function() {
315 const error = checkProblem(this.curproblem);
317 alert(this.st.tr[error]);
320 const edit = this.curproblem.id > 0;
321 this.infoMsg = "Processing... Please wait";
324 edit ? "PUT" : "POST",
325 { prob: this.curproblem },
328 let editedP = this.problems.find(p => p.id == this.curproblem.id);
329 this.copyProblem(this.curproblem, editedP);
330 this.showProblem(editedP);
333 let newProblem = Object.assign({}, this.curproblem);
334 newProblem.id = ret.id;
335 newProblem.uid = this.st.user.id;
336 newProblem.uname = this.st.user.name;
337 this.problems = [newProblem].concat(this.problems);
339 document.getElementById("modalNewprob").checked = false;
344 editProblem: function(prob) {
345 // prob.diag might correspond to some other problem or be empty:
346 this.setDiagram(prob); //V is loaded at this stage
347 this.copyProblem(prob, this.curproblem);
348 window.doClick("modalNewprob");
350 deleteProblem: function(prob) {
351 if (confirm(this.st.tr["Are you sure?"])) {
352 ajax("/problems", "DELETE", { id: prob.id }, () => {
353 ArrayFun.remove(this.problems, p => p.id == prob.id);
362 <style lang="sass" scoped>
363 [type="checkbox"].modal+div .card
390 background-color: lightgreen
395 padding-left: var(--universal-margin)
397 padding-left: var(--universal-margin)
404 @media screen and (max-width: 767px)