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