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