| 1 | Vue.component('my-problems', { |
| 2 | props: ["queryHash","settings"], |
| 3 | data: function () { |
| 4 | return { |
| 5 | userId: user.id, |
| 6 | problems: [], //oldest first |
| 7 | myProblems: [], //same, but only mine |
| 8 | singletons: [], //requested problems (using #num) |
| 9 | display: "others", //or "mine" |
| 10 | curProb: null, //(reference to) current displayed problem (if any) |
| 11 | showSolution: false, |
| 12 | nomoreMessage: "", |
| 13 | pbNum: 0, //to navigate directly to some problem |
| 14 | // New problem (to upload), or existing problem to edit: |
| 15 | modalProb: { |
| 16 | id: 0, //defined if it's an edit |
| 17 | uid: 0, //...also |
| 18 | fen: "", |
| 19 | instructions: "", |
| 20 | solution: "", |
| 21 | preview: false, |
| 22 | }, |
| 23 | }; |
| 24 | }, |
| 25 | template: ` |
| 26 | <div class="col-sm-12 col-md-10 col-md-offset-1 col-lg-8 col-lg-offset-2"> |
| 27 | <div id="problemControls" class="button-group"> |
| 28 | <button :aria-label='translate("Previous problem(s)")' class="tooltip" |
| 29 | @click="showNext('backward')" |
| 30 | > |
| 31 | <i class="material-icons">skip_previous</i> |
| 32 | </button> |
| 33 | <button v-if="!!userId" :aria-label='translate("Add a problem")' |
| 34 | class="tooltip" onClick="doClick('modal-newproblem')" |
| 35 | > |
| 36 | {{ translate("New") }} |
| 37 | </button> |
| 38 | <button :aria-label='translate("Next problem(s)")' class="tooltip" |
| 39 | @click="showNext('forward')" |
| 40 | > |
| 41 | <i class="material-icons">skip_next</i> |
| 42 | </button> |
| 43 | </div> |
| 44 | <div id="mainBoard" v-if="!!curProb"> |
| 45 | <div id="instructions-div" class="section-content"> |
| 46 | <p id="problem-instructions">{{ curProb.instructions }}</p> |
| 47 | </div> |
| 48 | <my-game :fen="curProb.fen" :mode="analyze" :allowMovelist="true" |
| 49 | :settings="settings" |
| 50 | > |
| 51 | </my-game> |
| 52 | <div id="solution-div" class="section-content"> |
| 53 | <h3 class="clickable" @click="showSolution = !showSolution"> |
| 54 | {{ translations["Show solution"] }} |
| 55 | </h3> |
| 56 | <p id="problem-solution" v-show="showSolution">{{ curProb.solution }}</p> |
| 57 | </div> |
| 58 | <button @click="displayList">Back to list display</button> |
| 59 | </div> |
| 60 | <div> |
| 61 | <input type="text" placeholder="Type problem number" v-model="pbNum"/> |
| 62 | <button @click="showProblem">Show problem</button> |
| 63 | </div> |
| 64 | <button v-if="!!userId" @click="toggleListDisplay" |
| 65 | :class="{'only-mine':display=='mine'}" |
| 66 | > |
| 67 | My problems (only) |
| 68 | </button> |
| 69 | <my-problem-summary v-show="!curProb" |
| 70 | v-on:edit-problem="editProblem(p)" v-on:delete-problem="deleteProblem(p.id)" |
| 71 | v-on:show-problem="() => showProblem(p.id)" |
| 72 | v-for="p in curProblems()" @click="curProb=p" |
| 73 | v-bind:prob="p" v-bind:userid="userId" v-bind:key="p.id"> |
| 74 | </my-problem-summary> |
| 75 | <input type="checkbox" id="modal-newproblem" class="modal"/> |
| 76 | <div role="dialog" aria-labelledby="modalProblemTxt"> |
| 77 | <div v-show="!modalProb.preview" class="card newproblem-form"> |
| 78 | <label for="modal-newproblem" class="modal-close"> |
| 79 | </label> |
| 80 | <h3 id="modalProblemTxt">{{ translate("Add a problem") }}</h3> |
| 81 | <form @submit.prevent="previewProblem()"> |
| 82 | <fieldset> |
| 83 | <label for="newpbFen">FEN</label> |
| 84 | <input id="newpbFen" type="text" v-model="modalProb.fen" |
| 85 | :placeholder='translate("Full FEN description")'/> |
| 86 | </fieldset> |
| 87 | <fieldset> |
| 88 | <p class="emphasis">{{ translate("Safe HTML tags allowed") }}</p> |
| 89 | <label for="newpbInstructions">{{ translate("Instructions") }}</label> |
| 90 | <textarea id="newpbInstructions" v-model="modalProb.instructions" |
| 91 | :placeholder='translate("Describe the problem goal")'> |
| 92 | </textarea> |
| 93 | <label for="newpbSolution">{{ translate("Solution") }}</label> |
| 94 | <textarea id="newpbSolution" v-model="modalProb.solution" |
| 95 | :placeholder='translate("How to solve the problem?")'> |
| 96 | </textarea> |
| 97 | <button class="center-btn">{{ translate("Preview") }}</button> |
| 98 | </fieldset> |
| 99 | </form> |
| 100 | </div> |
| 101 | <div v-show="modalProb.preview" class="card newproblem-preview"> |
| 102 | <label for="modal-newproblem" class="modal-close" |
| 103 | @click="modalProb.preview=false"> |
| 104 | </label> |
| 105 | <my-problem-summary :prob="modalProb" :userid="userId" :preview="true"> |
| 106 | </my-problem-summary> |
| 107 | <div class="button-group"> |
| 108 | <button @click="modalProb.preview=false">{{ translate("Cancel") }}</button> |
| 109 | <button @click="sendProblem()">{{ translate("Send") }}</button> |
| 110 | </div> |
| 111 | </div> |
| 112 | </div> |
| 113 | <input id="modalNomore" type="checkbox" class="modal"/> |
| 114 | <div role="dialog" aria-labelledby="nomoreMessage"> |
| 115 | <div class="card smallpad small-modal text-center"> |
| 116 | <label for="modalNomore" class="modal-close"></label> |
| 117 | <h3 id="nomoreMessage" class="section">{{ nomoreMessage }}</h3> |
| 118 | </div> |
| 119 | </div> |
| 120 | </div> |
| 121 | `, |
| 122 | watch: { |
| 123 | queryHash: function(newQhash) { |
| 124 | if (!!newQhash) |
| 125 | { |
| 126 | // New query hash = "id=42"; get 42 as problem ID |
| 127 | const pid = parseInt(newQhash.substr(2)); |
| 128 | this.showProblem(pid); |
| 129 | } |
| 130 | else |
| 131 | this.curProb = null; //(back to) list display |
| 132 | }, |
| 133 | }, |
| 134 | created: function() { |
| 135 | if (!!this.queryHash) |
| 136 | { |
| 137 | const pid = parseInt(this.queryHash.substr(2)); |
| 138 | this.showProblem(pid); |
| 139 | } |
| 140 | else |
| 141 | this.firstFetch(); |
| 142 | }, |
| 143 | methods: { |
| 144 | firstFetch: function() { |
| 145 | // Fetch most recent problems from server, for both lists |
| 146 | this.fetchProblems("others", "bacwkard"); |
| 147 | this.fetchProblems("mine", "bacwkard"); |
| 148 | this.listsInitialized = true; |
| 149 | }, |
| 150 | showProblem: function(num) { |
| 151 | const pid = num || this.pbNum; |
| 152 | location.hash = "#problems?id=" + pid; |
| 153 | for (let parray of [this.singletons,this.problems,this.myProblems]) |
| 154 | { |
| 155 | const pIdx = parray.findIndex(p => p.id == pid); |
| 156 | if (pIdx >= 0) |
| 157 | { |
| 158 | curProb = parray[pIdx]; |
| 159 | break; |
| 160 | } |
| 161 | } |
| 162 | if (!curProb) |
| 163 | { |
| 164 | // Cannot find problem in current set; get from server, and add to singletons. |
| 165 | ajax( |
| 166 | "/problems/" + variant.name + "/" + pid, //TODO: use variant._id ? |
| 167 | "GET", |
| 168 | response => { |
| 169 | if (!!response.problem) |
| 170 | { |
| 171 | this.singletons.push(response.problem); |
| 172 | this.curProb = response.problem; |
| 173 | } |
| 174 | else |
| 175 | this.noMoreProblems("Sorry, problem " + pid + " does not exist"); |
| 176 | } |
| 177 | ); |
| 178 | } |
| 179 | }, |
| 180 | translate: function(text) { |
| 181 | return translations[text]; |
| 182 | }, |
| 183 | curProblems: function() { |
| 184 | switch (this.display) |
| 185 | { |
| 186 | case "others": |
| 187 | return this.problems; |
| 188 | case "mine": |
| 189 | return this.myProblems; |
| 190 | } |
| 191 | }, |
| 192 | // TODO?: get 50 from server but only show 10 at a time (for example) |
| 193 | showNext: function(direction) { |
| 194 | if (!this.curProb) |
| 195 | return this.fetchProblems(this.display, direction); |
| 196 | // Show next problem (older or newer): |
| 197 | let curProbs = this.curProblems(); |
| 198 | // Try to find a neighbour problem in the direction, among current set |
| 199 | const neighbor = this.findClosestNeighbor(this.curProb, curProbs, direction); |
| 200 | if (!!neighbor) |
| 201 | { |
| 202 | this.curProb = neighbor; |
| 203 | return; |
| 204 | } |
| 205 | // Boundary case: nothing in current set, need to fetch from server |
| 206 | const curSize = curProbs.length; |
| 207 | this.fetchProblems(this.display, direction); |
| 208 | const newSize = curProbs.length; |
| 209 | if (curSize == newSize) //no problems found |
| 210 | return this.noMoreProblems("No more problems in this direction"); |
| 211 | // Ok, found something: |
| 212 | this.curProb = this.findClosestNeighbor(this.curProb, curProbs, direction); |
| 213 | }, |
| 214 | findClosestNeighbor: function(problem, probList, direction) { |
| 215 | let neighbor = undefined; |
| 216 | let smallestDistance = Number.MAX_SAFE_INTEGER; |
| 217 | for (let prob of probList) |
| 218 | { |
| 219 | const delta = Math.abs(prob.id - problem.id); |
| 220 | if (delta < smallestDistance && |
| 221 | ((direction == "backward" && prob.id < problem.id) |
| 222 | || (direction == "forward" && prob.id > problem.id))) |
| 223 | { |
| 224 | neighbor = prob; |
| 225 | smallestDistance = delta; |
| 226 | } |
| 227 | } |
| 228 | return neighbor; |
| 229 | }, |
| 230 | noMoreProblems: function(message) { |
| 231 | this.nomoreMessage = message; |
| 232 | let modalNomore = document.getElementById("modalNomore"); |
| 233 | modalNomore.checked = true; |
| 234 | setTimeout(() => modalNomore.checked = false, 2000); |
| 235 | }, |
| 236 | displayList: function() { |
| 237 | this.curProb = null; |
| 238 | location.hash = "#problems"; |
| 239 | // Fetch problems if first call (if #num, and then lists) |
| 240 | if (!this.listsInitialized) |
| 241 | this.firstFetch(); |
| 242 | }, |
| 243 | toggleListDisplay: function() { |
| 244 | const displays = ["mine","others"]; |
| 245 | const curIndex = displays.findIndex(item => item == this.display); |
| 246 | this.display = displays[1-curIndex]; |
| 247 | }, |
| 248 | fetchProblems: function(type, direction) { |
| 249 | let problems = (type == "others" ? this.problems : this.myProblems); |
| 250 | // "last datetime" set at a value OK for an empty initial array |
| 251 | let last_dt = (direction=="forward" ? 0 : Number.MAX_SAFE_INTEGER); |
| 252 | if (problems.length > 0) |
| 253 | { |
| 254 | // Search for newest date (or oldest) |
| 255 | last_dt = problems[0].added; |
| 256 | for (let i=1; i<problems.length; i++) |
| 257 | { |
| 258 | if ((direction == "forward" && problems[i].added > last_dt) || |
| 259 | (direction == "backward" && problems[i].added < last_dt)) |
| 260 | { |
| 261 | last_dt = problems[i].added; |
| 262 | } |
| 263 | } |
| 264 | } |
| 265 | ajax( |
| 266 | "/problems/" + variant.id, |
| 267 | "GET", |
| 268 | { |
| 269 | type: type, |
| 270 | direction: direction, |
| 271 | last_dt: last_dt, |
| 272 | }, |
| 273 | response => { |
| 274 | if (response.problems.length > 0) |
| 275 | { |
| 276 | Array.prototype.push.apply(problems, |
| 277 | response.problems.sort((p1,p2) => { return p2.added - p1.added; })); |
| 278 | // If one list is empty but not the other, show the non-empty |
| 279 | const otherArray = (type == "mine" ? this.problems : this.myProblems); |
| 280 | if (problems.length > 0 && otherArray.length == 0) |
| 281 | this.display = type; |
| 282 | } |
| 283 | } |
| 284 | ); |
| 285 | }, |
| 286 | previewProblem: function() { |
| 287 | if (!V.IsGoodFen(this.modalProb.fen)) |
| 288 | return alert(translations["Bad FEN description"]); |
| 289 | if (this.modalProb.instructions.trim().length == 0) |
| 290 | return alert(translations["Empty instructions"]); |
| 291 | if (this.modalProb.solution.trim().length == 0) |
| 292 | return alert(translations["Empty solution"]); |
| 293 | Vue.set(this.modalProb, "preview", true); |
| 294 | }, |
| 295 | editProblem: function(prob) { |
| 296 | this.modalProb = prob; |
| 297 | Vue.set(this.modalProb, "preview", false); |
| 298 | document.getElementById("modal-newproblem").checked = true; |
| 299 | }, |
| 300 | deleteProblem: function(pid) { |
| 301 | ajax( |
| 302 | "/problems/" + pid, |
| 303 | "DELETE", |
| 304 | response => { |
| 305 | // Delete problem from the list on client side |
| 306 | let problems = this.curProblems(); |
| 307 | const pIdx = problems.findIndex(p => p.id == pid); |
| 308 | problems.splice(pIdx, 1); |
| 309 | } |
| 310 | ); |
| 311 | }, |
| 312 | sendProblem: function() { |
| 313 | // Send it to the server and close modal |
| 314 | ajax( |
| 315 | "/problems/" + variant.id, |
| 316 | (this.modalProb.id > 0 ? "PUT" : "POST"), |
| 317 | this.modalProb, |
| 318 | response => { |
| 319 | document.getElementById("modal-newproblem").checked = false; |
| 320 | Vue.set(this.modalProb, "preview", false); |
| 321 | if (this.modalProb.id == 0) |
| 322 | { |
| 323 | this.myProblems.unshift({ |
| 324 | added: Date.now(), |
| 325 | id: response.id, |
| 326 | uid: user.id, |
| 327 | fen: this.modalProb.fen, |
| 328 | instructions: this.modalProb.instructions, |
| 329 | solution: this.modalProb.solution, |
| 330 | }); |
| 331 | } |
| 332 | else |
| 333 | this.modalProb.id = 0; |
| 334 | } |
| 335 | ); |
| 336 | }, |
| 337 | }, |
| 338 | }) |