3 input#modalNewprob.modal(type="checkbox" @change="infoMsg=''")
4 div#newprobDiv(role="dialog" data-checkbox="modalNewprob")
5 .card(@keyup.enter="sendProblem()")
6 label#closeNewprob.modal-close(for="modalNewprob")
8 label(for="selectVariant") {{ st.tr["Variant"] }}
10 v-model="curproblem.vid"
11 @change="changeVariant(curproblem)"
14 v-for="v in [emptyVar].concat(st.variants)"
16 :selected="curproblem.vid==v.id"
23 v-model="curproblem.fen"
24 @input="trySetDiagram(curproblem)"
26 #diagram(v-html="curproblem.diag")
29 :placeholder="st.tr['Instructions']"
30 v-model="curproblem.instruction"
32 p(v-html="parseHtml(curproblem.instruction)")
35 :placeholder="st.tr['Solution']"
36 v-model="curproblem.solution"
38 p(v-html="parseHtml(curproblem.solution)")
39 button(@click="sendProblem()") {{ st.tr["Send"] }}
40 #dialog.text-center {{ st.tr[infoMsg] }}
42 .col-sm-12.col-md-10.col-md-offset-1.col-lg-8.col-lg-offset-2
44 span {{ curproblem.vname }}
45 button.marginleft(@click="backToList()") {{ st.tr["Back to list"] }}
47 v-if="st.user.id == curproblem.uid"
48 @click="editProblem(curproblem)"
52 v-if="st.user.id == curproblem.uid"
53 @click="deleteProblem(curproblem)"
55 | {{ st.tr["Delete"] }}
57 v-html="curproblem.uname + ' : ' + parseHtml(curproblem.instruction)"
58 @click="curproblem.showSolution=!curproblem.showSolution"
60 | {{ st.tr["Show solution"] }}
62 v-show="curproblem.showSolution"
63 v-html="parseHtml(curproblem.solution)"
66 .col-sm-12.col-md-10.col-md-offset-1.col-lg-8.col-lg-offset-2
68 button#newProblem(onClick="doClick('modalNewprob')")
69 | {{ st.tr["New problem"] }}
70 label(for="checkboxMine") {{ st.tr["My problems"] }}
75 label(for="selectVariant") {{ st.tr["Variant"] }}
76 select#selectVariant(v-model="selectedVar")
78 v-for="v in [emptyVar].concat(st.variants)"
84 th {{ st.tr["Variant"] }}
85 th {{ st.tr["Instructions"] }}
88 v-show="displayProblem(p)"
89 @click="setHrefPid(p)"
92 td(v-html="p.instruction")
93 BaseGame(v-if="showOne" :game="game" :vr="vr")
97 import { store } from "@/store";
98 import { ajax } from "@/utils/ajax";
99 import { checkProblem } from "@/data/problemCheck";
100 import { getDiagram } from "@/utils/printDiagram";
101 import { processModalClick } from "@/utils/modalClick";
102 import { ArrayFun } from "@/utils/array";
103 import BaseGame from "@/components/BaseGame.vue";
116 // Problem currently showed, or edited:
118 id: 0, //used in case of edit
126 loadedVar: 0, //corresponding to loaded V
127 selectedVar: 0, //to filter problems based on variant
132 vr: null, //"variant rules" object initialized from FEN
134 players:[{name:"Problem"},{name:"Problem"}],
139 created: function() {
140 ajax("/problems", "GET", (res) => {
141 this.problems = res.problems;
142 if (this.st.variants.length > 0)
143 this.problems.forEach(p => this.setVname(p));
144 // Retrieve all problems' authors' names
146 this.problems.forEach(p => {
147 if (p.uid != this.st.user.id)
148 names[p.uid] = ""; //unknwon for now
149 else { console.log("assign " + this.st.user.name);
150 p.uname = this.st.user.name; console.log(p); console.log(this.problems); }
152 if (Object.keys(name).length > 0)
156 { ids: Object.keys(names).join(",") },
158 res2.users.forEach(u => {names[u.id] = u.name});
159 this.problems.forEach(p => p.uname = names[p.uid]);
163 const pid = this.$route.query["id"];
165 this.showProblem(this.problems.find(p => p.id == pid));
168 mounted: function() {
169 document.getElementById("newprobDiv").addEventListener("click", processModalClick);
172 // st.variants changes only once, at loading from [] to [...]
173 "st.variants": function(variantArray) {
174 // Set problems vname (either all are set or none)
175 if (this.problems.length > 0 && this.problems[0].vname == "")
176 this.problems.forEach(p => this.setVname(p));
178 "$route": function(to, from) { console.log("ddddd");
179 const pid = to.query["id"];
181 this.showProblem(this.problems.find(p => p.id == pid));
187 setVname: function(prob) {
188 prob.vname = this.st.variants.find(v => v.id == prob.vid).name;
190 copyProblem: function(p1, p2) {
194 setHrefPid: function(p) {
195 // Change href => $route changes, watcher notices, call showProblem
196 const curHref = document.location.href;
197 document.location.href = curHref.split("?")[0] + "?id=" + p.id;
199 backToList: function() {
200 // Change href => $route change, watcher notices, reset showOne to false
201 document.location.href = document.location.href.split("?")[0];
203 resetCurProb: function() {
204 this.curproblem.id = 0;
205 this.curproblem.uid = 0;
206 this.curproblem.vid = "";
207 this.curproblem.vname = "";
208 this.curproblem.fen = "";
209 this.curproblem.diag = "";
210 this.curproblem.instruction = "";
211 this.curproblem.solution = "";
212 this.curproblem.showSolution = false;
214 parseHtml: function(txt) {
215 return !txt.match(/<[/a-zA-Z]+>/)
216 ? txt.replace(/\n/g, "<br/>") //no HTML tag
219 changeVariant: function(prob) {
224 // Set FEN if possible (might not be correct yet)
225 if (V.IsGoodFen(prob.fen))
226 this.setDiagram(prob);
230 loadVariant: async function(vid, cb) {
231 // Condition: vid is a valid variant ID
233 const variant = this.st.variants.find(v => v.id == vid);
234 const vModule = await import("@/variants/" + variant.name + ".js");
235 window.V = vModule.VariantRules;
236 this.loadedVar = vid;
239 trySetDiagram: function(prob) {
240 // Problem edit: FEN could be wrong or incomplete,
241 // variant could not be ready, or not defined
242 if (prob.vid > 0 && this.loadedVar == prob.vid && V.IsGoodFen(prob.fen))
243 this.setDiagram(prob);
245 setDiagram: function(prob) {
246 // Condition: prob.fen is correct and global V is ready
247 const parsedFen = V.ParseFen(prob.fen);
249 position: parsedFen.position,
250 orientation: parsedFen.turn,
252 prob.diag = getDiagram(args);
254 displayProblem: function(p) {
255 return ((this.selectedVar == 0 || p.vid == this.selectedVar) &&
256 ((this.onlyMines && p.uid == this.st.user.id)
257 || (!this.onlyMines && p.uid != this.st.user.id)));
259 showProblem: function(p) {
263 // The FEN is already checked at this stage:
264 this.vr = new V(p.fen);
265 this.game.vname = p.vname;
266 this.game.mycolor = this.vr.turn; //diagram orientation
267 this.game.fen = p.fen;
268 this.$set(this.game, "fenStart", p.fen);
269 this.copyProblem(p, this.curproblem);
274 sendProblem: function() {
275 const error = checkProblem(this.curproblem);
278 const edit = this.curproblem.id > 0;
279 this.infoMsg = "Processing... Please wait";
282 edit ? "PUT" : "POST",
283 {prob: this.curproblem},
287 let editedP = this.problems.find(p => p.id == this.curproblem.id);
288 this.copyProblem(this.curproblem, editedP);
292 let newProblem = Object.assign({}, this.curproblem);
293 newProblem.id = ret.id;
294 newProblem.uid = this.st.user.id;
295 newProblem.uname = this.st.user.name;
296 this.problems = this.problems.concat(newProblem);
303 editProblem: function(prob) {
305 this.setDiagram(prob); //possible because V is loaded at this stage
306 this.copyProblem(prob, this.curproblem);
307 doClick('modalNewprob');
309 deleteProblem: function(prob) {
310 if (confirm(this.st.tr["Are you sure?"]))
312 ajax("/problems", "DELETE", {id:prob.id}, () => {
313 ArrayFun.remove(this.problems, p => p.id == prob.id);
322 <style lang="sass" scoped>
323 [type="checkbox"].modal+div .card