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