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