Started code review + some fixes (unfinished)
[vchess.git] / client / src / views / Problems.vue
CommitLineData
89021f18
BA
1<template lang="pug">
2main
3 input#modalNewprob.modal(type="checkbox" @change="infoMsg=''")
4 div#newprobDiv(role="dialog" data-checkbox="modalNewprob")
604b951e 5 .card(@keyup.enter="sendProblem()")
89021f18 6 label#closeNewprob.modal-close(for="modalNewprob")
604b951e
BA
7 fieldset
8 label(for="selectVariant") {{ st.tr["Variant"] }}
9 select#selectVariant(
10 v-model="curproblem.vid"
11 @change="changeVariant(curproblem)"
12 )
13 option(
14 v-for="v in [emptyVar].concat(st.variants)"
15 :value="v.id"
16 :selected="curproblem.vid==v.id"
17 )
18 | {{ v.name }}
19 fieldset
604b951e
BA
20 input#inputFen(
21 type="text"
bd76b456 22 placeholder="FEN"
604b951e
BA
23 v-model="curproblem.fen"
24 @input="trySetDiagram(curproblem)"
25 )
bd76b456 26 #diagram(v-html="curproblem.diag")
604b951e 27 fieldset
bd76b456 28 textarea(
604b951e
BA
29 :placeholder="st.tr['Instructions']"
30 v-model="curproblem.instruction"
31 )
32 p(v-html="parseHtml(curproblem.instruction)")
33 fieldset
bd76b456 34 textarea(
604b951e
BA
35 :placeholder="st.tr['Solution']"
36 v-model="curproblem.solution"
37 )
38 p(v-html="parseHtml(curproblem.solution)")
39 button(@click="sendProblem()") {{ st.tr["Send"] }}
89021f18 40 #dialog.text-center {{ st.tr[infoMsg] }}
604b951e 41 .row(v-if="showOne")
2f258c37 42 .col-sm-12.col-md-10.col-md-offset-2
bd76b456 43 #topPage
2f258c37 44 span.vname {{ curproblem.vname }}
98fb976e 45 span.uname ({{ curproblem.uname }})
bd76b456
BA
46 button.marginleft(@click="backToList()") {{ st.tr["Back to list"] }}
47 button.nomargin(
604b951e
BA
48 v-if="st.user.id == curproblem.uid"
49 @click="editProblem(curproblem)"
50 )
51 | {{ st.tr["Edit"] }}
bd76b456 52 button.nomargin(
604b951e
BA
53 v-if="st.user.id == curproblem.uid"
54 @click="deleteProblem(curproblem)"
55 )
56 | {{ st.tr["Delete"] }}
bd76b456 57 p.clickable(
2f258c37 58 v-html="parseHtml(curproblem.instruction)"
bd76b456
BA
59 @click="curproblem.showSolution=!curproblem.showSolution"
60 )
604b951e
BA
61 | {{ st.tr["Show solution"] }}
62 p(
63 v-show="curproblem.showSolution"
64 v-html="parseHtml(curproblem.solution)"
65 )
66 .row(v-else)
89021f18 67 .col-sm-12.col-md-10.col-md-offset-1.col-lg-8.col-lg-offset-2
bd76b456 68 #controls
6808d7a1 69 button#newProblem(onClick="window.doClick('modalNewprob')")
bd76b456
BA
70 | {{ st.tr["New problem"] }}
71 label(for="checkboxMine") {{ st.tr["My problems"] }}
72 input#checkboxMine(
73 type="checkbox"
74 v-model="onlyMines"
604b951e 75 )
bd76b456
BA
76 label(for="selectVariant") {{ st.tr["Variant"] }}
77 select#selectVariant(v-model="selectedVar")
78 option(
79 v-for="v in [emptyVar].concat(st.variants)"
80 :value="v.id"
81 )
82 | {{ v.name }}
83 table
84 tr
85 th {{ st.tr["Variant"] }}
86 th {{ st.tr["Instructions"] }}
2f258c37 87 th {{ st.tr["Number"] }}
bd76b456 88 tr(
6808d7a1 89 v-for="p in problems"
bd76b456
BA
90 v-show="displayProblem(p)"
91 @click="setHrefPid(p)"
92 )
93 td {{ p.vname }}
2f258c37
BA
94 td {{ firstChars(p.instruction) }}
95 td {{ p.id }}
604b951e 96 BaseGame(v-if="showOne" :game="game" :vr="vr")
89021f18
BA
97</template>
98
99<script>
100import { store } from "@/store";
101import { ajax } from "@/utils/ajax";
102import { checkProblem } from "@/data/problemCheck";
103import { getDiagram } from "@/utils/printDiagram";
bd76b456
BA
104import { processModalClick } from "@/utils/modalClick";
105import { ArrayFun } from "@/utils/array";
604b951e 106import BaseGame from "@/components/BaseGame.vue";
89021f18
BA
107export default {
108 name: "my-problems",
604b951e 109 components: {
6808d7a1 110 BaseGame
604b951e 111 },
89021f18
BA
112 data: function() {
113 return {
604b951e 114 st: store.state,
89021f18
BA
115 emptyVar: {
116 vid: 0,
6808d7a1 117 vname: ""
89021f18 118 },
604b951e
BA
119 // Problem currently showed, or edited:
120 curproblem: {
121 id: 0, //used in case of edit
89021f18
BA
122 vid: 0,
123 fen: "",
604b951e 124 diag: "",
89021f18
BA
125 instruction: "",
126 solution: "",
6808d7a1 127 showSolution: false
89021f18 128 },
604b951e
BA
129 loadedVar: 0, //corresponding to loaded V
130 selectedVar: 0, //to filter problems based on variant
89021f18 131 problems: [],
604b951e
BA
132 onlyMines: false,
133 showOne: false,
89021f18 134 infoMsg: "",
604b951e
BA
135 vr: null, //"variant rules" object initialized from FEN
136 game: {
6808d7a1
BA
137 players: [{ name: "Problem" }, { name: "Problem" }],
138 mode: "analyze"
139 }
89021f18
BA
140 };
141 },
142 created: function() {
6808d7a1
BA
143 ajax("/problems", "GET", res => {
144 // Show newest problem first:
145 this.problems = res.problems.sort((p1, p2) => p2.added - p1.added);
604b951e 146 if (this.st.variants.length > 0)
bd76b456
BA
147 this.problems.forEach(p => this.setVname(p));
148 // Retrieve all problems' authors' names
149 let names = {};
150 this.problems.forEach(p => {
6808d7a1
BA
151 if (p.uid != this.st.user.id) names[p.uid] = "";
152 //unknwon for now
153 else p.uname = this.st.user.name;
bd76b456 154 });
2f258c37
BA
155 const showOneIfPid = () => {
156 const pid = this.$route.query["id"];
6808d7a1 157 if (pid) this.showProblem(this.problems.find(p => p.id == pid));
2f258c37 158 };
6808d7a1
BA
159 if (Object.keys(names).length > 0) {
160 ajax("/users", "GET", { ids: Object.keys(names).join(",") }, res2 => {
161 res2.users.forEach(u => {
162 names[u.id] = u.name;
163 });
164 this.problems.forEach(p => (p.uname = names[p.uid]));
165 showOneIfPid();
166 });
167 } else showOneIfPid();
89021f18
BA
168 });
169 },
bd76b456 170 mounted: function() {
6808d7a1
BA
171 document
172 .getElementById("newprobDiv")
173 .addEventListener("click", processModalClick);
bd76b456 174 },
604b951e
BA
175 watch: {
176 // st.variants changes only once, at loading from [] to [...]
6808d7a1 177 "st.variants": function() {
604b951e
BA
178 // Set problems vname (either all are set or none)
179 if (this.problems.length > 0 && this.problems[0].vname == "")
180 this.problems.forEach(p => this.setVname(p));
181 },
6808d7a1 182 $route: function(to) {
bd76b456 183 const pid = to.query["id"];
6808d7a1
BA
184 if (pid) this.showProblem(this.problems.find(p => p.id == pid));
185 else this.showOne = false;
186 }
98fb976e 187 },
89021f18 188 methods: {
604b951e
BA
189 setVname: function(prob) {
190 prob.vname = this.st.variants.find(v => v.id == prob.vid).name;
191 },
2f258c37
BA
192 firstChars: function(text) {
193 let preparedText = text
194 // Replace line jumps and <br> by spaces
6808d7a1
BA
195 .replace(/\n/g, " ")
196 .replace(/<br\/?>/g, " ")
2f258c37
BA
197 .replace(/<[^>]+>/g, "") //remove remaining HTML tags
198 .replace(/[ ]+/g, " ") //remove series of spaces by only one
199 .trim();
200 const maxLength = 32; //arbitrary...
201 if (preparedText.length > maxLength)
6808d7a1 202 return preparedText.substr(0, 32) + "...";
2f258c37
BA
203 return preparedText;
204 },
604b951e 205 copyProblem: function(p1, p2) {
6808d7a1 206 for (let key in p1) p2[key] = p1[key];
604b951e 207 },
bd76b456
BA
208 setHrefPid: function(p) {
209 // Change href => $route changes, watcher notices, call showProblem
210 const curHref = document.location.href;
211 document.location.href = curHref.split("?")[0] + "?id=" + p.id;
212 },
213 backToList: function() {
214 // Change href => $route change, watcher notices, reset showOne to false
215 document.location.href = document.location.href.split("?")[0];
216 },
604b951e
BA
217 resetCurProb: function() {
218 this.curproblem.id = 0;
219 this.curproblem.uid = 0;
220 this.curproblem.vid = "";
221 this.curproblem.vname = "";
222 this.curproblem.fen = "";
223 this.curproblem.diag = "";
224 this.curproblem.instruction = "";
225 this.curproblem.solution = "";
226 this.curproblem.showSolution = false;
89021f18 227 },
604b951e
BA
228 parseHtml: function(txt) {
229 return !txt.match(/<[/a-zA-Z]+>/)
230 ? txt.replace(/\n/g, "<br/>") //no HTML tag
231 : txt;
232 },
233 changeVariant: function(prob) {
234 this.setVname(prob);
6808d7a1
BA
235 this.loadVariant(prob.vid, () => {
236 // Set FEN if possible (might not be correct yet)
237 if (V.IsGoodFen(prob.fen)) this.setDiagram(prob);
238 });
604b951e
BA
239 },
240 loadVariant: async function(vid, cb) {
241 // Condition: vid is a valid variant ID
242 this.loadedVar = 0;
243 const variant = this.st.variants.find(v => v.id == vid);
89021f18
BA
244 const vModule = await import("@/variants/" + variant.name + ".js");
245 window.V = vModule.VariantRules;
604b951e
BA
246 this.loadedVar = vid;
247 cb();
248 },
249 trySetDiagram: function(prob) {
250 // Problem edit: FEN could be wrong or incomplete,
251 // variant could not be ready, or not defined
252 if (prob.vid > 0 && this.loadedVar == prob.vid && V.IsGoodFen(prob.fen))
253 this.setDiagram(prob);
89021f18 254 },
604b951e
BA
255 setDiagram: function(prob) {
256 // Condition: prob.fen is correct and global V is ready
257 const parsedFen = V.ParseFen(prob.fen);
258 const args = {
259 position: parsedFen.position,
6808d7a1 260 orientation: parsedFen.turn
604b951e
BA
261 };
262 prob.diag = getDiagram(args);
89021f18 263 },
604b951e 264 displayProblem: function(p) {
6808d7a1
BA
265 return (
266 (this.selectedVar == 0 || p.vid == this.selectedVar) &&
267 ((this.onlyMines && p.uid == this.st.user.id) ||
268 (!this.onlyMines && p.uid != this.st.user.id))
269 );
604b951e
BA
270 },
271 showProblem: function(p) {
6808d7a1
BA
272 this.loadVariant(p.vid, () => {
273 // The FEN is already checked at this stage:
274 this.vr = new V(p.fen);
275 this.game.vname = p.vname;
276 this.game.mycolor = this.vr.turn; //diagram orientation
277 this.game.fen = p.fen;
278 this.$set(this.game, "fenStart", p.fen);
279 this.copyProblem(p, this.curproblem);
280 this.showOne = true;
281 });
604b951e
BA
282 },
283 sendProblem: function() {
284 const error = checkProblem(this.curproblem);
6808d7a1
BA
285 if (error) {
286 alert(error);
287 return;
288 }
604b951e
BA
289 const edit = this.curproblem.id > 0;
290 this.infoMsg = "Processing... Please wait";
291 ajax(
292 "/problems",
293 edit ? "PUT" : "POST",
6808d7a1
BA
294 { prob: this.curproblem },
295 ret => {
296 if (edit) {
604b951e
BA
297 let editedP = this.problems.find(p => p.id == this.curproblem.id);
298 this.copyProblem(this.curproblem, editedP);
6808d7a1
BA
299 } //new problem
300 else {
604b951e
BA
301 let newProblem = Object.assign({}, this.curproblem);
302 newProblem.id = ret.id;
bd76b456
BA
303 newProblem.uid = this.st.user.id;
304 newProblem.uname = this.st.user.name;
604b951e
BA
305 this.problems = this.problems.concat(newProblem);
306 }
307 this.resetCurProb();
308 this.infoMsg = "";
309 }
310 );
311 },
312 editProblem: function(prob) {
6808d7a1 313 if (!prob.diag) this.setDiagram(prob); //possible because V is loaded at this stage
604b951e 314 this.copyProblem(prob, this.curproblem);
6808d7a1 315 window.doClick("modalNewprob");
604b951e
BA
316 },
317 deleteProblem: function(prob) {
6808d7a1
BA
318 if (confirm(this.st.tr["Are you sure?"])) {
319 ajax("/problems", "DELETE", { id: prob.id }, () => {
bd76b456
BA
320 ArrayFun.remove(this.problems, p => p.id == prob.id);
321 this.backToList();
322 });
323 }
6808d7a1
BA
324 }
325 }
89021f18
BA
326};
327</script>
328
329<style lang="sass" scoped>
bd76b456
BA
330[type="checkbox"].modal+div .card
331 max-width: 767px
332 max-height: 100%
333#inputFen
334 width: 100%
335textarea
336 width: 100%
337#diagram
338 margin: 0 auto
339 max-width: 400px
340#controls
341 margin: 0
342 width: 100%
343 text-align: center
344 & > *
345 margin: 0
2f258c37 346
bd76b456 347#topPage
2f258c37 348 span.vname
bd76b456 349 font-weight: bold
feae89d3 350 padding-left: var(--universal-margin)
2f258c37
BA
351 span.uname
352 padding-left: var(--universal-margin)
bd76b456
BA
353 margin: 0 auto
354 & > .nomargin
355 margin: 0
356 & > .marginleft
357 margin: 0 0 0 15px
358
2f258c37
BA
359@media screen and (max-width: 767px)
360 #topPage
361 text-align: center
89021f18 362</style>