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 id="problemControls" class="button-group">
51 <button :aria-label='translate("Previous problem(s)")' class="tooltip"
52 @click="showNext('backward')"
54 <i class="material-icons">skip_previous</i>
56 <button v-if="!!userId" :aria-label='translate("Add a problem")'
57 class="tooltip" onClick="doClick('modal-newproblem')"
59 {{ translate("New") }}
61 <button :aria-label='translate("Next problem(s)")' class="tooltip"
62 @click="showNext('forward')"
64 <i class="material-icons">skip_next</i>
67 <div id="mainBoard" v-if="!!curProb">
68 <div id="instructions-div" class="section-content">
69 <p id="problem-instructions">{{ curProb.instructions }}</p>
71 <my-game :fen="curProb.fen" :mode="mode" :allowMovelist="true"
74 <div id="solution-div" class="section-content">
75 <h3 class="clickable" @click="showSolution = !showSolution">
76 {{ translate("Show solution") }}
78 <p id="problem-solution" v-show="showSolution">{{ curProb.solution }}</p>
80 <button @click="displayList">Back to list display</button>
83 <input type="text" placeholder="Type problem number" v-model="pbNum"/>
84 <button @click="() => showProblem(pbNum)">Show problem</button>
86 <button v-if="!!userId" @click="toggleListDisplay"
87 :class="{'only-mine':display=='mine'}"
91 <my-problem-summary v-show="!curProb"
92 v-on:edit-problem="editProblem(p)" v-on:delete-problem="deleteProblem(p.id)"
93 v-on:show-problem="() => showProblem(p.id)"
94 v-for="p in curProblems()" @click="curProb=p"
95 v-bind:prob="p" v-bind:userid="userId" v-bind:key="p.id">
101 this.showProblem(this.probId);
104 created: function() {
106 this.showProblem(this.probId);
111 translate: translate,
112 firstFetch: function() {
113 // Fetch most recent problems from server, for both lists
114 this.fetchProblems("others", "bacwkard");
115 this.fetchProblems("mine", "bacwkard");
116 this.listsInitialized = true;
118 showProblem: function(pid) {
119 location.hash = "#problems?id=" + pid;
120 for (let parray of [this.singletons,this.problems,this.myProblems])
122 const pIdx = parray.findIndex(p => p.id == pid);
125 this.curProb = parray[pIdx];
131 // Cannot find problem in current set; get from server, and add to singletons.
133 "/problems/" + variant.id + "/" + pid, //TODO: variant ID should not be required
136 if (!!response.problem)
138 this.singletons.push(response.problem);
139 this.curProb = response.problem;
140 this.display = (response.problem.uid == this.userId ? "mine" : "others");
143 this.noMoreProblems("Sorry, problem " + pid + " does not exist");
148 this.display = (this.curProb.uid == this.userId ? "mine" : "others");
150 curProblems: function() {
151 switch (this.display)
154 return this.problems;
156 return this.myProblems;
159 // TODO?: get 50 from server but only show 10 at a time (for example)
160 showNext: function(direction) {
163 if (!problems || problems.length == 0)
164 this.noMoreProblems("No more problems in this direction");
167 return this.fetchProblems(this.display, direction, nomorePb);
168 // Show next problem (older or newer):
169 let curProbs = this.curProblems();
170 // Try to find a neighbour problem in the direction, among current set
171 const neighbor = this.findClosestNeighbor(this.curProb, curProbs, direction);
174 this.curProb = neighbor;
177 // Boundary case: nothing in current set, need to fetch from server
178 const curSize = curProbs.length;
179 this.fetchProblems(this.display, direction, problems => {
180 if (problems.length > 0)
182 // Ok, found something:
184 this.findClosestNeighbor(this.curProb, curProbs, direction);
190 findClosestNeighbor: function(problem, probList, direction) {
191 let neighbor = undefined;
192 let smallestDistance = Number.MAX_SAFE_INTEGER;
193 for (let prob of probList)
195 const delta = Math.abs(prob.id - problem.id);
196 if (delta < smallestDistance &&
197 ((direction == "backward" && prob.id < problem.id)
198 || (direction == "forward" && prob.id > problem.id)))
201 smallestDistance = delta;
206 noMoreProblems: function(message) {
207 this.nomoreMessage = message;
208 let modalNomore = document.getElementById("modalNomore");
209 modalNomore.checked = true;
210 setTimeout(() => modalNomore.checked = false, 2000);
212 displayList: function() {
214 location.hash = "#problems";
215 // Fetch problems if first call (if #num, and then lists)
216 if (!this.listsInitialized)
219 toggleListDisplay: function() {
220 const displays = ["mine","others"];
221 const curIndex = displays.findIndex(item => item == this.display);
222 this.display = displays[1-curIndex];
224 fetchProblems: function(type, direction, cb) {
225 let problems = (type == "others" ? this.problems : this.myProblems);
226 // "last datetime" set at a value OK for an empty initial array
227 let last_dt = (direction=="forward" ? 0 : Number.MAX_SAFE_INTEGER);
228 if (problems.length > 0)
230 // Search for newest date (or oldest)
231 last_dt = problems[0].added;
232 for (let i=1; i<problems.length; i++)
234 if ((direction == "forward" && problems[i].added > last_dt) ||
235 (direction == "backward" && problems[i].added < last_dt))
237 last_dt = problems[i].added;
242 "/problems/" + variant.id,
246 direction: direction,
250 if (response.problems.length > 0)
252 Array.prototype.push.apply(problems, response.problems.sort(
253 (p1,p2) => { return p2.added - p1.added; }));
254 // If one list is empty but not the other, show the non-empty
256 (type == "mine" ? this.problems : this.myProblems);
257 if (otherArray.length == 0)
259 this.$forceUpdate(); //TODO...
262 cb(response.problems);