1 Vue
.component('my-problems', {
2 props: ["queryHash","settings"],
6 problems: [], //oldest first
7 myProblems: [], //same, but only mine
8 singletons: [], //requested problems (using #num)
9 display: "others", //or "mine"
10 curProb: null, //(reference to) current displayed problem (if any)
13 pbNum: 0, //to navigate directly to some problem
14 // New problem (to upload), or existing problem to edit:
16 id: 0, //defined if it's an edit
25 <div class="col-sm-12 col-md-10 col-md-offset-1 col-lg-8 col-lg-offset-2">
26 <div id="problemControls" class="button-group">
27 <button :aria-label='translate("Previous problem(s)")' class="tooltip" @click="showNext('backward')">
28 <i class="material-icons">skip_previous</i>
30 <button :aria-label='translate("Add a problem")' class="tooltip" onClick="doClick('modal-newproblem')">
31 {{ translate("New") }}
33 <button :aria-label='translate("Next problem(s)")' class="tooltip" @click="showNext('forward')">
34 <i class="material-icons">skip_next</i>
37 <div id="mainBoard" v-if="!!curProb">
38 <div id="instructions-div" class="section-content">
39 <p id="problem-instructions">
40 {{ curProb.instructions }}
43 <my-game :fen="curProb.fen" :mode="analyze" :allowMovelist="true" :settings="settings">
45 <div id="solution-div" class="section-content">
46 <h3 class="clickable" @click="showSolution = !showSolution">
47 {{ translations["Show solution"] }}
49 <p id="problem-solution" v-show="showSolution">
50 {{ curProb.solution }}
53 <button @click="displayList()">
54 <span>Back to list display</span>
58 <input type="text" placeholder="Type problem number" v-model="pbNum"/>
59 <button @click="showProblem()">
60 <span>Show problem</span>
63 <button v-if="!!userId" @click="toggleListDisplay()">
64 <span>My problems (only)</span>
66 <my-problem-summary v-show="!curProb"
67 v-on:edit-problem="editProblem(p)" v-on:delete-problem="deleteProblem(p.id)"
68 v-for="p in curProblems" @click="curProb=p"
69 v-bind:prob="p" v-bind:userid="userId" v-bind:key="p.id">
71 <input type="checkbox" id="modal-newproblem" class="modal"/>
72 <div role="dialog" aria-labelledby="modalProblemTxt">
73 <div v-show="!modalProb.preview" class="card newproblem-form">
74 <label for="modal-newproblem" class="modal-close">
76 <h3 id="modalProblemTxt">
77 {{ translate("Add a problem") }}
79 <form @submit.prevent="previewProblem()">
81 <label for="newpbFen">FEN</label>
82 <input id="newpbFen" type="text" v-model="modalProb.fen"
83 :placeholder='translate("Full FEN description")'/>
87 {{ translate("Safe HTML tags allowed") }}
89 <label for="newpbInstructions">
90 {{ translate("Instructions") }}
92 <textarea id="newpbInstructions" v-model="modalProb.instructions"
93 :placeholder='translate("Describe the problem goal")'>
95 <label for="newpbSolution">
96 {{ translate("Solution") }}
98 <textarea id="newpbSolution" v-model="modalProb.solution"
99 :placeholder='translate("How to solve the problem?")'>
101 <button class="center-btn">
102 {{ translate("Preview") }}
107 <div v-show="modalProb.preview" class="card newproblem-preview">
108 <label for="modal-newproblem" class="modal-close">
110 <my-problem-summary v-bind:prob="modalProb" v-bind:userid="userId">
111 </my-problem-summary>
112 <div class="button-group">
113 <button @click="modalProb.preview=false">
114 {{ translate("Cancel") }}
116 <button @click="sendProblem()">
117 {{ translate("Send") }}
122 <input id="modalNomore" type="checkbox" class="modal"/>
123 <div role="dialog" aria-labelledby="nomoreMessage">
124 <div class="card smallpad small-modal text-center">
125 <label for="modalNomore" class="modal-close"></label>
126 <h3 id="nomoreMessage" class="section">
134 queryHash: function(newQhash
) {
137 // New query hash = "id=42"; get 42 as problem ID
138 const pid
= parseInt(newQhash
.substr(2));
139 this.showProblem(pid
);
142 this.curProb
= null; //(back to) list display
145 created: function() {
146 if (!!this.queryHash
)
148 const pid
= parseInt(this.queryHash
.substr(2));
149 this.showProblem(pid
);
155 firstFetch: function() {
156 // Fetch most recent problems from server, for both lists
157 this.fetchProblems("others", "bacwkard");
158 this.fetchProblems("mine", "bacwkard");
159 this.listsInitialized
= true;
161 showProblem: function(num
) {
162 const pid
= num
|| this.pbNum
;
163 location
.hash
= "#" + pid
;
164 const pIdx
= this.singletons
.findIndex(p
=> p
.id
== pid
);
166 curProb
= this.singletons
[pIdx
];
169 // Cannot find problem in current set; get from server, and add to singletons.
171 "/problems/" + variant
.name
+ "/" + pid
, //TODO: use variant._id ?
174 if (!!response
.problem
)
176 this.singletons
.push(response
.problem
);
177 this.curProb
= response
.problem
;
180 this.noMoreProblems("Sorry, problem " + pid
+ " does not exist");
185 translate: function(text
) {
186 return translations
[text
];
188 curProblems: function() {
189 switch (this.display
)
192 return this.problems
;
194 return this.myProblems
;
197 // TODO?: get 50 from server but only show 10 at a time (for example)
198 showNext: function(direction
) {
200 return this.fetchProblems(this.display
, direction
);
201 // Show next problem (older or newer):
202 let curProbs
= this.curProblems();
203 // Try to find a neighbour problem in the direction, among current set
204 const neighbor
= this.findClosestNeighbor(this.curProb
, curProbs
, direction
);
207 this.curProb
= neighbor
;
210 // Boundary case: nothing in current set, need to fetch from server
211 const curSize
= curProbs
.length
;
212 this.fetchProblems(this.display
, direction
);
213 const newSize
= curProbs
.length
;
214 if (curSize
== newSize
) //no problems found
215 return this.noMoreProblems("No more problems in this direction");
216 // Ok, found something:
217 this.curProb
= this.findClosestNeighbor(this.curProb
, curProbs
, direction
);
219 findClosestNeighbor: function(problem
, probList
, direction
) {
220 let neighbor
= undefined;
221 let smallestDistance
= Number
.MAX_SAFE_INTEGER
;
222 for (let prob
of probList
)
224 const delta
= Math
.abs(prob
.id
- problem
.id
);
225 if (delta
< smallestDistance
&&
226 ((direction
== "backward" && prob
.id
< problem
.id
)
227 || (direction
== "forward" && prob
.id
> problem
.id
)))
230 smallestDistance
= delta
;
235 noMoreProblems: function(message
) {
236 this.nomoreMessage
= message
;
237 let modalNomore
= document
.getElementById("modalNomore");
238 modalNomore
.checked
= true;
239 setTimeout(() => modalNomore
.checked
= false, 2000);
241 displayList: function() {
244 // Fetch problems if first call (if #num, and then lists)
245 if (!this.listsInitialized
)
248 toggleListDisplay: function() {
249 this.display
= (this.display
== "others" ? "mine" : "others");
251 fetchProblems: function(type
, direction
) {
252 let problems
= (type
== "others" ? this.problems : this.myProblems
);
253 let last_dt
= (direction
=="forward" ? 0 : Number
.MAX_SAFE_INTEGER
);
254 if (this.problems
.length
> 0)
256 // Search for newest date (or oldest)
257 last_dt
= problems
[0].added
;
258 for (let i
=1; i
<problems
.length
; i
++)
260 if ((direction
== "forward" && this.problems
[i
].added
> last_dt
) ||
261 (direction
== "backward" && this.problems
[i
].added
< last_dt
))
263 last_dt
= this.problems
[i
].added
;
268 "/problems/" + variant
.id
,
272 direction: direction
,
276 if (response
.problems
.length
> 0)
278 Array
.prototype.push
.apply(problems
,
279 response
.problems
.sort((p1
,p2
) => { return p1
.added
- p2
.added
; }));
280 // If one list is empty but not the other, show the non-empty
281 const otherArray
= (type
== "mine" ? this.problems : this.myProblems
);
282 if (problems
.length
> 0 && otherArray
.length
== 0)
288 previewProblem: function() {
289 if (!V
.IsGoodFen(this.newProblem
.fen
))
290 return alert(translations
["Bad FEN description"]);
291 if (this.newProblem
.instructions
.trim().length
== 0)
292 return alert(translations
["Empty instructions"]);
293 if (this.newProblem
.solution
.trim().length
== 0)
294 return alert(translations
["Empty solution"]);
295 this.modalProb
.preview
= true;
297 editProblem: function(prob
) {
298 this.modalProb
= prob
;
299 document
.getElementById("modal-newproblem").checked
= true;
301 deleteProblem: function(pid
) {
303 "/problems/" + variant
.id
+ "/" + pid
,
306 // Delete problem from the list on client side
307 let problems
= this.curProblems();
308 const pIdx
= problems
.findIndex(p
=> p
.id
== pid
);
309 problems
.splice(pIdx
, 1);
313 sendProblem: function() {
314 // Send it to the server and close modal
316 "/problems/" + variant
.id
,
317 (this.modalProb
.id
> 0 ? "PUT" : "POST"),
320 document
.getElementById("modal-newproblem").checked
= false;
321 if (this.modalProb
.id
== 0)
323 this.modalProb
.added
= Date
.now();
324 this.modalProb
.preview
= false;
325 this.myProblems
.push(JSON
.parse(JSON
.stringify(this.modalProb
)));
328 this.modalProb
.id
= 0;