3 <TestComp :vname="variant.name"/>
8 // @ is an alias to /src
9 import TestComp from "@/components/TestComp.vue";
18 variant: {name: "Atomic", id: 3},
23 Vue.component('my-problems', {
24 props: ["probId","settings"],
28 problems: [], //oldest first
29 myProblems: [], //same, but only mine
30 singletons: [], //requested problems (using #num)
31 display: "others", //or "mine"
32 curProb: null, //(reference to) current displayed problem (if any)
35 mode: "analyze", //for game component
36 pbNum: 0, //to navigate directly to some problem
37 // New problem (to upload), or existing problem to edit:
39 id: 0, //defined if it's an edit
48 // NOTE: always modals first, because otherwise "scroll to the end" undesirable effect
50 <div class="col-sm-12 col-md-10 col-md-offset-1 col-lg-8 col-lg-offset-2">
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">
56 <h3 id="modalProblemTxt">{{ translate("Add a problem") }}</h3>
57 <form @submit.prevent="previewProblem()">
59 <label for="newpbFen">FEN</label>
60 <input id="newpbFen" type="text" v-model="modalProb.fen"
61 :placeholder='translate("Full FEN description")'/>
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")'>
69 <label for="newpbSolution">{{ translate("Solution") }}</label>
70 <textarea id="newpbSolution" v-model="modalProb.solution"
71 :placeholder='translate("How to solve the problem?")'>
73 <button class="center-btn">{{ translate("Preview") }}</button>
77 <div v-show="modalProb.preview" class="card newproblem-preview">
78 <label for="modal-newproblem" class="modal-close"
79 @click="modalProb.preview=false">
81 <my-problem-summary :prob="modalProb" :userid="userId" :preview="true">
83 <div class="button-group">
84 <button @click="modalProb.preview=false">{{ translate("Cancel") }}</button>
85 <button @click="sendProblem()">{{ translate("Send") }}</button>
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>
96 <div id="problemControls" class="button-group">
97 <button :aria-label='translate("Previous problem(s)")' class="tooltip"
98 @click="showNext('backward')"
100 <i class="material-icons">skip_previous</i>
102 <button v-if="!!userId" :aria-label='translate("Add a problem")'
103 class="tooltip" onClick="doClick('modal-newproblem')"
105 {{ translate("New") }}
107 <button :aria-label='translate("Next problem(s)")' class="tooltip"
108 @click="showNext('forward')"
110 <i class="material-icons">skip_next</i>
113 <div id="mainBoard" v-if="!!curProb">
114 <div id="instructions-div" class="section-content">
115 <p id="problem-instructions">{{ curProb.instructions }}</p>
117 <my-game :fen="curProb.fen" :mode="mode" :allowMovelist="true"
118 :settings="settings">
120 <div id="solution-div" class="section-content">
121 <h3 class="clickable" @click="showSolution = !showSolution">
122 {{ translate("Show solution") }}
124 <p id="problem-solution" v-show="showSolution">{{ curProb.solution }}</p>
126 <button @click="displayList">Back to list display</button>
129 <input type="text" placeholder="Type problem number" v-model="pbNum"/>
130 <button @click="() => showProblem(pbNum)">Show problem</button>
132 <button v-if="!!userId" @click="toggleListDisplay"
133 :class="{'only-mine':display=='mine'}"
137 <my-problem-summary v-show="!curProb"
138 v-on:edit-problem="editProblem(p)" v-on:delete-problem="deleteProblem(p.id)"
139 v-on:show-problem="() => showProblem(p.id)"
140 v-for="p in curProblems()" @click="curProb=p"
141 v-bind:prob="p" v-bind:userid="userId" v-bind:key="p.id">
142 </my-problem-summary>
147 this.showProblem(this.probId);
150 created: function() {
152 this.showProblem(this.probId);
157 translate: translate,
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;
164 showProblem: function(pid) {
165 location.hash = "#problems?id=" + pid;
166 for (let parray of [this.singletons,this.problems,this.myProblems])
168 const pIdx = parray.findIndex(p => p.id == pid);
171 this.curProb = parray[pIdx];
177 // Cannot find problem in current set; get from server, and add to singletons.
179 "/problems/" + variant.id + "/" + pid, //TODO: variant ID should not be required
182 if (!!response.problem)
184 this.singletons.push(response.problem);
185 this.curProb = response.problem;
186 this.display = (response.problem.uid == this.userId ? "mine" : "others");
189 this.noMoreProblems("Sorry, problem " + pid + " does not exist");
194 this.display = (this.curProb.uid == this.userId ? "mine" : "others");
196 curProblems: function() {
197 switch (this.display)
200 return this.problems;
202 return this.myProblems;
205 // TODO?: get 50 from server but only show 10 at a time (for example)
206 showNext: function(direction) {
209 if (!problems || problems.length == 0)
210 this.noMoreProblems("No more problems in this direction");
213 return this.fetchProblems(this.display, direction, nomorePb);
214 // Show next problem (older or newer):
215 let curProbs = this.curProblems();
216 // Try to find a neighbour problem in the direction, among current set
217 const neighbor = this.findClosestNeighbor(this.curProb, curProbs, direction);
220 this.curProb = neighbor;
223 // Boundary case: nothing in current set, need to fetch from server
224 const curSize = curProbs.length;
225 this.fetchProblems(this.display, direction, problems => {
226 if (problems.length > 0)
228 // Ok, found something:
230 this.findClosestNeighbor(this.curProb, curProbs, direction);
236 findClosestNeighbor: function(problem, probList, direction) {
237 let neighbor = undefined;
238 let smallestDistance = Number.MAX_SAFE_INTEGER;
239 for (let prob of probList)
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)))
247 smallestDistance = delta;
252 noMoreProblems: function(message) {
253 this.nomoreMessage = message;
254 let modalNomore = document.getElementById("modalNomore");
255 modalNomore.checked = true;
256 setTimeout(() => modalNomore.checked = false, 2000);
258 displayList: function() {
260 location.hash = "#problems";
261 // Fetch problems if first call (if #num, and then lists)
262 if (!this.listsInitialized)
265 toggleListDisplay: function() {
266 const displays = ["mine","others"];
267 const curIndex = displays.findIndex(item => item == this.display);
268 this.display = displays[1-curIndex];
270 fetchProblems: function(type, direction, cb) {
271 let problems = (type == "others" ? this.problems : this.myProblems);
272 // "last datetime" set at a value OK for an empty initial array
273 let last_dt = (direction=="forward" ? 0 : Number.MAX_SAFE_INTEGER);
274 if (problems.length > 0)
276 // Search for newest date (or oldest)
277 last_dt = problems[0].added;
278 for (let i=1; i<problems.length; i++)
280 if ((direction == "forward" && problems[i].added > last_dt) ||
281 (direction == "backward" && problems[i].added < last_dt))
283 last_dt = problems[i].added;
288 "/problems/" + variant.id,
292 direction: direction,
296 if (response.problems.length > 0)
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
302 (type == "mine" ? this.problems : this.myProblems);
303 if (otherArray.length == 0)
305 this.$forceUpdate(); //TODO...
308 cb(response.problems);
312 previewProblem: function() {
313 if (!V.IsGoodFen(this.modalProb.fen))
314 return alert(translations["Bad FEN description"]);
315 if (this.modalProb.instructions.trim().length == 0)
316 return alert(translations["Empty instructions"]);
317 if (this.modalProb.solution.trim().length == 0)
318 return alert(translations["Empty solution"]);
319 Vue.set(this.modalProb, "preview", true);
321 editProblem: function(prob) {
322 this.modalProb = prob;
323 Vue.set(this.modalProb, "preview", false);
324 document.getElementById("modal-newproblem").checked = true;
326 deleteProblem: function(pid) {
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);
338 sendProblem: function() {
339 // Send it to the server and close modal
341 "/problems/" + variant.id,
342 (this.modalProb.id > 0 ? "PUT" : "POST"),
345 document.getElementById("modal-newproblem").checked = false;
346 Vue.set(this.modalProb, "preview", false);
347 if (this.modalProb.id == 0)
349 this.myProblems.unshift({
353 fen: this.modalProb.fen,
354 instructions: this.modalProb.instructions,
355 solution: this.modalProb.solution,
357 if (!this.curProb && this.display != "mine")
358 this.display = "mine";
361 this.modalProb.id = 0;