897babbe7035886ff889e384ff86fa1100e48868
1 Vue
.component('my-problems', {
5 problems: [], //oldest first
6 myProblems: [], //same, but only mine
7 singletons: [], //requested problems (using #num)
8 display: "others", //or "mine"
9 curProb: null, //(reference to) current displayed problem (if any)
11 pbNum: 0, //to navigate directly to some problem
12 // New problem (to upload), or existing problem to edit:
14 id: 0, //defined if it's an edit
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">
25 <button :aria-label='translate("Previous problem(s)")' class="tooltip" @click="showNext('backward')">
26 <i class="material-icons">skip_previous</i>
28 <button :aria-label='translate("Add a problem")' class="tooltip" onClick="doClick('modal-newproblem')">
29 {{ translate("New") }}
31 <button :aria-label='translate("Next problem(s)")' class="tooltip" @click="showNext('forward')">
32 <i class="material-icons">skip_next</i>
35 <div id="mainBoard" v-show="!!curProb">
36 <div id="instructions-div" class="section-content">
37 <p id="problem-instructions">
38 {{ curProb.instructions }}
41 <my-board :fen="curProb.fen"></my-board>
42 <div id="solution-div" class="section-content">
43 <h3 class="clickable" @click="showSolution = !showSolution">
44 {{ translations["Show solution"] }}
46 <p id="problem-solution" v-show="showSolution">
47 {{ curProb.solution }}
50 <button @click="displayList()">
51 <span>Back to list display</span>
55 <input type="text" placeholder="Type problem number" v-model="pbNum"/>
56 <button @click="showProblem()">
57 <span>Show problem</span>
60 <button v-if="!!userId" @click="toggleListDisplay()">
61 <span>My problems (only)</span>
63 <my-problem-summary v-show="!curProb"
64 v-on:edit-problem="editProblem(p)" v-on:delete-problem="deleteProblem(p.id)"
65 v-for="p in curProblems" @click="curProb=p"
66 v-bind:prob="p" v-bind:userid="userId" v-bind:key="p.id">
68 <input type="checkbox" id="modal-newproblem" class="modal"/>
69 <div role="dialog" aria-labelledby="modalProblemTxt">
70 <div v-show="!modalProb.preview" class="card newproblem-form">
71 <label for="modal-newproblem" class="modal-close">
73 <h3 id="modalProblemTxt">
74 {{ translate("Add a problem") }}
76 <form @submit.prevent="previewProblem()">
78 <label for="newpbFen">FEN</label>
79 <input id="newpbFen" type="text" v-model="modalProb.fen"
80 :placeholder='translate("Full FEN description")'/>
84 {{ translate("Safe HTML tags allowed") }}
86 <label for="newpbInstructions">
87 {{ translate("Instructions") }}
89 <textarea id="newpbInstructions" v-model="modalProb.instructions"
90 :placeholder='translate("Describe the problem goal")'>
92 <label for="newpbSolution">
93 {{ translate("Solution") }}
95 <textarea id="newpbSolution" v-model="modalProb.solution"
96 :placeholder='translate("How to solve the problem?")'>
98 <button class="center-btn">
99 {{ translate("Preview") }}
104 <div v-show="modalProb.preview" class="card newproblem-preview">
105 <label for="modal-newproblem" class="modal-close">
107 <my-problem-summary v-bind:prob="modalProb" v-bind:userid="userId">
108 </my-problem-summary>
109 <div class="button-group">
110 <button @click="modalProb.preview=false">
111 {{ translate("Cancel") }}
113 <button @click="sendProblem()">
114 {{ translate("Send") }}
119 <input id="modalNomore" type="checkbox" class="modal"/>
120 <div role="dialog" aria-labelledby="nomoreMessage">
121 <div class="card smallpad small-modal text-center">
122 <label for="modalNomore" class="modal-close"></label>
123 <h3 id="nomoreMessage" class="section">
130 created: function() {
131 if (location
.hash
.length
> 0)
132 this.showProblem(location
.hash
.slice(1));
137 firstFetch: function() {
138 // Fetch most recent problems from server, for both lists
139 this.fetchProblems("others", "bacwkard");
140 this.fetchProblems("mine", "bacwkard");
141 this.listsInitialized
= true;
143 showProblem: function(num
) {
144 const pid
= num
|| this.pbNum
;
145 location
.hash
= "#" + pid
;
146 const pIdx
= this.singletons
.findIndex(p
=> p
.id
== pid
);
148 curProb
= this.singletons
[pIdx
];
151 // Cannot find problem in current set; get from server, and add to singletons.
153 "/problems/" + variant
.name
+ "/" + pid
, //TODO: use variant._id ?
156 if (!!response
.problem
)
158 this.singletons
.push(response
.problem
);
159 this.curProb
= response
.problem
;
162 this.noMoreProblems("Sorry, problem " + pid
+ " does not exist");
167 translate: function(text
) {
168 return translations
[text
];
170 curProblems: function() {
171 switch (this.display
)
174 return this.problems
;
176 return this.myProblems
;
179 // TODO?: get 50 from server but only show 10 at a time (for example)
180 showNext: function(direction
) {
182 return this.fetchProblems(this.display
, direction
);
183 // Show next problem (older or newer):
184 let curProbs
= this.curProblems();
185 // Try to find a neighbour problem in the direction, among current set
186 const neighbor
= this.findClosestNeighbor(this.curProb
, curProbs
, direction
);
189 this.curProb
= neighbor
;
192 // Boundary case: nothing in current set, need to fetch from server
193 const curSize
= curProbs
.length
;
194 this.fetchProblems(this.display
, direction
);
195 const newSize
= curProbs
.length
;
196 if (curSize
== newSize
) //no problems found
197 return this.noMoreProblems("No more problems in this direction");
198 // Ok, found something:
199 this.curProb
= this.findClosestNeighbor(this.curProb
, curProbs
, direction
);
201 findClosestNeighbor: function(problem
, probList
, direction
) {
202 let neighbor
= undefined;
203 let smallestDistance
= Number
.MAX_SAFE_INTEGER
;
204 for (let prob
of probList
)
206 const delta
= Math
.abs(prob
.id
- problem
.id
);
207 if (delta
< smallestDistance
&&
208 ((direction
== "backward" && prob
.id
< problem
.id
)
209 || (direction
== "forward" && prob
.id
> problem
.id
)))
212 smallestDistance
= delta
;
217 noMoreProblems: function(message
) {
218 this.nomoreMessage
= message
;
219 let modalNomore
= document
.getElementById("modalNomore");
220 modalNomore
.checked
= true;
221 setTimeout(() => modalNomore
.checked
= false, 2000);
223 displayList: function() {
226 // Fetch problems if first call (if #num, and then lists)
227 if (!this.listsInitialized
)
230 toggleListDisplay: function() {
231 this.display
= (this.display
== "others" ? "mine" : "others");
233 fetchProblems: function(type
, direction
) {
234 let problems
= (type
== "others" ? this.problems : this.myProblems
);
235 let last_dt
= (direction
=="forward" ? 0 : Number
.MAX_SAFE_INTEGER
);
236 if (this.problems
.length
> 0)
238 // Search for newest date (or oldest)
239 last_dt
= problems
[0].added
;
240 for (let i
=1; i
<problems
.length
; i
++)
242 if ((direction
== "forward" && this.problems
[i
].added
> last_dt
) ||
243 (direction
== "backward" && this.problems
[i
].added
< last_dt
))
245 last_dt
= this.problems
[i
].added
;
250 "/problems/" + variant
.name
, //TODO: use variant._id ?
254 direction: direction
,
258 if (response
.problems
.length
> 0)
260 Array
.prototype.push
.apply(problems
,
261 response
.problems
.sort((p1
,p2
) => { return p1
.added
- p2
.added
; }));
262 // If one list is empty but not the other, show the non-empty
263 const otherArray
= (type
== "mine" ? this.problems : this.myProblems
);
264 if (problems
.length
> 0 && otherArray
.length
== 0)
270 previewProblem: function() {
271 if (!V
.IsGoodFen(this.newProblem
.fen
))
272 return alert(translations
["Bad FEN description"]);
273 if (this.newProblem
.instructions
.trim().length
== 0)
274 return alert(translations
["Empty instructions"]);
275 if (this.newProblem
.solution
.trim().length
== 0)
276 return alert(translations
["Empty solution"]);
277 this.modalProb
.preview
= true;
279 editProblem: function(prob
) {
280 this.modalProb
= prob
;
281 document
.getElementById("modal-newproblem").checked
= true;
283 deleteProblem: function(pid
) {
285 "/problems/" + variant
.name
+ "/" + pid
, //TODO: with variant.id ?
288 // Delete problem from the list on client side
289 let problems
= this.curProblems();
290 const pIdx
= problems
.findIndex(p
=> p
.id
== pid
);
291 problems
.splice(pIdx
, 1);
295 sendProblem: function() {
296 // Send it to the server and close modal
298 "/problems/" + variant
.name
, //TODO: with variant.id ?
299 (this.modalProb
.id
> 0 ? "PUT" : "POST"),
302 document
.getElementById("modal-newproblem").checked
= false;
303 if (this.modalProb
.id
== 0)
305 this.modalProb
.added
= Date
.now();
306 this.modalProb
.preview
= false;
307 this.myProblems
.push(JSON
.parse(JSON
.stringify(this.modalProb
)));
310 this.modalProb
.id
= 0;