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