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"
20 label(for="inputFen") FEN
23 v-model="curproblem.fen"
24 @input="trySetDiagram(curproblem)"
26 div(v-html="curproblem.diag")
28 textarea#instructions(
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] }}
43 button#newProblem(onClick="doClick('modalNewprob')")
44 | {{ st.tr["New problem"] }}
46 .col-sm-12.col-md-10.col-md-offset-1.col-lg-8.col-lg-offset-2
48 button(@click="showOne=false") {{ st.tr["Back to list"] }}
50 v-if="st.user.id == curproblem.uid"
51 @click="editProblem(curproblem)"
55 v-if="st.user.id == curproblem.uid"
56 @click="deleteProblem(curproblem)"
58 | {{ st.tr["Delete"] }}
59 h4 {{ curproblem.vname }}
60 p(v-html="parseHtml(curproblem.instruction)")
61 h4(@click="curproblem.showSolution=!curproblem.showSolution")
62 | {{ st.tr["Show solution"] }}
64 v-show="curproblem.showSolution"
65 v-html="parseHtml(curproblem.solution)"
68 .col-sm-12.col-md-10.col-md-offset-1.col-lg-8.col-lg-offset-2
69 label(for="checkboxMine") {{ st.tr["My problems"] }}
74 label(for="selectVariant") {{ st.tr["Variant"] }}
75 select#selectVariant(v-model="selectedVar")
77 v-for="v in [emptyVar].concat(st.variants)"
83 v-show="displayProblem(p)"
84 @click="showProblem(p)"
88 p(v-html="p.instruction")
89 BaseGame(v-if="showOne" :game="game" :vr="vr")
93 // TODO: si showProblem(p), changer URL (ajouter problem ID)
94 // Et si au lancement l'URL comprend un pid, alors showOne=true et curproblem=...
95 // TODO: also style problem div (in the list, similar to variants page + clickable)
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 BaseGame from "@/components/BaseGame.vue";
114 // Problem currently showed, or edited:
116 id: 0, //used in case of edit
124 loadedVar: 0, //corresponding to loaded V
125 selectedVar: 0, //to filter problems based on variant
130 vr: null, //"variant rules" object initialized from FEN
132 players:[{name:"Problem"},{name:"Problem"}],
137 created: function() {
138 ajax("/problems", "GET", (res) => {
139 this.problems = res.problems;
140 if (this.st.variants.length > 0)
141 this.problems.forEach(p => this.setVname(p))
145 // st.variants changes only once, at loading from [] to [...]
146 "st.variants": function(variantArray) {
147 // Set problems vname (either all are set or none)
148 if (this.problems.length > 0 && this.problems[0].vname == "")
149 this.problems.forEach(p => this.setVname(p));
153 setVname: function(prob) {
154 prob.vname = this.st.variants.find(v => v.id == prob.vid).name;
156 copyProblem: function(p1, p2) {
160 resetCurProb: function() {
161 this.curproblem.id = 0;
162 this.curproblem.uid = 0;
163 this.curproblem.vid = "";
164 this.curproblem.vname = "";
165 this.curproblem.fen = "";
166 this.curproblem.diag = "";
167 this.curproblem.instruction = "";
168 this.curproblem.solution = "";
169 this.curproblem.showSolution = false;
171 parseHtml: function(txt) {
172 return !txt.match(/<[/a-zA-Z]+>/)
173 ? txt.replace(/\n/g, "<br/>") //no HTML tag
176 changeVariant: function(prob) {
181 // Set FEN if possible (might not be correct yet)
182 if (V.IsGoodFen(prob.fen))
183 this.setDiagram(prob);
187 loadVariant: async function(vid, cb) {
188 // Condition: vid is a valid variant ID
190 const variant = this.st.variants.find(v => v.id == vid);
191 const vModule = await import("@/variants/" + variant.name + ".js");
192 window.V = vModule.VariantRules;
193 this.loadedVar = vid;
196 trySetDiagram: function(prob) {
197 // Problem edit: FEN could be wrong or incomplete,
198 // variant could not be ready, or not defined
199 if (prob.vid > 0 && this.loadedVar == prob.vid && V.IsGoodFen(prob.fen))
200 this.setDiagram(prob);
202 setDiagram: function(prob) {
203 // Condition: prob.fen is correct and global V is ready
204 const parsedFen = V.ParseFen(prob.fen);
206 position: parsedFen.position,
207 orientation: parsedFen.turn,
209 prob.diag = getDiagram(args);
211 displayProblem: function(p) {
212 return ((this.selectedVar == 0 || p.vid == this.selectedVar) &&
213 ((this.onlyMines && p.uid == this.st.user.id)
214 || (!this.onlyMines && p.uid != this.st.user.id)));
216 showProblem: function(p) {
220 // The FEN is already checked at this stage:
221 this.vr = new V(p.fen);
222 this.game.vname = p.vname;
223 this.game.mycolor = this.vr.turn; //diagram orientation
224 this.game.fen = p.fen;
225 this.$set(this.game, "fenStart", p.fen);
226 this.copyProblem(p, this.curproblem);
231 sendProblem: function() {
232 const error = checkProblem(this.curproblem);
235 const edit = this.curproblem.id > 0;
236 this.infoMsg = "Processing... Please wait";
239 edit ? "PUT" : "POST",
240 {prob: this.curproblem},
244 let editedP = this.problems.find(p => p.id == this.curproblem.id);
245 this.copyProblem(this.curproblem, editedP);
249 let newProblem = Object.assign({}, this.curproblem);
250 newProblem.id = ret.id;
251 this.problems = this.problems.concat(newProblem);
258 editProblem: function(prob) {
260 this.setDiagram(prob); //possible because V is loaded at this stage
261 this.copyProblem(prob, this.curproblem);
262 doClick('modalNewprob');
264 deleteProblem: function(prob) {
265 if (confirm(this.st.tr["Are you sure?"]))
266 ajax("/problems", "DELETE", {pid:prob.id});
272 <style lang="sass" scoped>
275 margin: 10px auto 5px auto