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
26 <div class="col-sm-12 col-md-10 col-md-offset-1 col-lg-8 col-lg-offset-2">
27 <div id="problemControls" class="button-group">
28 <button :aria-label='translate("Previous problem(s)")' class="tooltip"
29 @click="showNext('backward')"
31 <i class="material-icons">skip_previous</i>
33 <button v-if="!!userId" :aria-label='translate("Add a problem")'
34 class="tooltip" onClick="doClick('modal-newproblem')"
36 {{ translate("New") }}
38 <button :aria-label='translate("Next problem(s)")' class="tooltip"
39 @click="showNext('forward')"
41 <i class="material-icons">skip_next</i>
44 <div id="mainBoard" v-if="!!curProb">
45 <div id="instructions-div" class="section-content">
46 <p id="problem-instructions">{{ curProb.instructions }}</p>
48 <my-game :fen="curProb.fen" :mode="analyze" :allowMovelist="true"
52 <div id="solution-div" class="section-content">
53 <h3 class="clickable" @click="showSolution = !showSolution">
54 {{ translations["Show solution"] }}
56 <p id="problem-solution" v-show="showSolution">{{ curProb.solution }}</p>
58 <button @click="displayList">Back to list display</button>
61 <input type="text" placeholder="Type problem number" v-model="pbNum"/>
62 <button @click="showProblem">Show problem</button>
64 <button v-if="!!userId" @click="toggleListDisplay"
65 :class="{'only-mine':display=='mine'}"
69 <my-problem-summary v-show="!curProb"
70 v-on:edit-problem="editProblem(p)" v-on:delete-problem="deleteProblem(p.id)"
71 v-on:show-problem="() => showProblem(p.id)"
72 v-for="p in curProblems()" @click="curProb=p"
73 v-bind:prob="p" v-bind:userid="userId" v-bind:key="p.id">
75 <input type="checkbox" id="modal-newproblem" class="modal"/>
76 <div role="dialog" aria-labelledby="modalProblemTxt">
77 <div v-show="!modalProb.preview" class="card newproblem-form">
78 <label for="modal-newproblem" class="modal-close">
80 <h3 id="modalProblemTxt">{{ translate("Add a problem") }}</h3>
81 <form @submit.prevent="previewProblem()">
83 <label for="newpbFen">FEN</label>
84 <input id="newpbFen" type="text" v-model="modalProb.fen"
85 :placeholder='translate("Full FEN description")'/>
88 <p class="emphasis">{{ translate("Safe HTML tags allowed") }}</p>
89 <label for="newpbInstructions">{{ translate("Instructions") }}</label>
90 <textarea id="newpbInstructions" v-model="modalProb.instructions"
91 :placeholder='translate("Describe the problem goal")'>
93 <label for="newpbSolution">{{ translate("Solution") }}</label>
94 <textarea id="newpbSolution" v-model="modalProb.solution"
95 :placeholder='translate("How to solve the problem?")'>
97 <button class="center-btn">{{ translate("Preview") }}</button>
101 <div v-show="modalProb.preview" class="card newproblem-preview">
102 <label for="modal-newproblem" class="modal-close"
103 @click="modalProb.preview=false">
105 <my-problem-summary :prob="modalProb" :userid="userId" :preview="true">
106 </my-problem-summary>
107 <div class="button-group">
108 <button @click="modalProb.preview=false">{{ translate("Cancel") }}</button>
109 <button @click="sendProblem()">{{ translate("Send") }}</button>
113 <input id="modalNomore" type="checkbox" class="modal"/>
114 <div role="dialog" aria-labelledby="nomoreMessage">
115 <div class="card smallpad small-modal text-center">
116 <label for="modalNomore" class="modal-close"></label>
117 <h3 id="nomoreMessage" class="section">{{ nomoreMessage }}</h3>
123 queryHash: function(newQhash
) {
126 // New query hash = "id=42"; get 42 as problem ID
127 const pid
= parseInt(newQhash
.substr(2));
128 this.showProblem(pid
);
131 this.curProb
= null; //(back to) list display
134 created: function() {
135 if (!!this.queryHash
)
137 const pid
= parseInt(this.queryHash
.substr(2));
138 this.showProblem(pid
);
144 firstFetch: function() {
145 // Fetch most recent problems from server, for both lists
146 this.fetchProblems("others", "bacwkard");
147 this.fetchProblems("mine", "bacwkard");
148 this.listsInitialized
= true;
150 showProblem: function(num
) {
151 const pid
= num
|| this.pbNum
;
152 location
.hash
= "#problems?id=" + pid
;
153 for (let parray
of [this.singletons
,this.problems
,this.myProblems
])
155 const pIdx
= parray
.findIndex(p
=> p
.id
== pid
);
158 curProb
= parray
[pIdx
];
164 // Cannot find problem in current set; get from server, and add to singletons.
166 "/problems/" + variant
.name
+ "/" + pid
, //TODO: use variant._id ?
169 if (!!response
.problem
)
171 this.singletons
.push(response
.problem
);
172 this.curProb
= response
.problem
;
175 this.noMoreProblems("Sorry, problem " + pid
+ " does not exist");
180 translate: function(text
) {
181 return translations
[text
];
183 curProblems: function() {
184 switch (this.display
)
187 return this.problems
;
189 return this.myProblems
;
192 // TODO?: get 50 from server but only show 10 at a time (for example)
193 showNext: function(direction
) {
195 return this.fetchProblems(this.display
, direction
);
196 // Show next problem (older or newer):
197 let curProbs
= this.curProblems();
198 // Try to find a neighbour problem in the direction, among current set
199 const neighbor
= this.findClosestNeighbor(this.curProb
, curProbs
, direction
);
202 this.curProb
= neighbor
;
205 // Boundary case: nothing in current set, need to fetch from server
206 const curSize
= curProbs
.length
;
207 this.fetchProblems(this.display
, direction
);
208 const newSize
= curProbs
.length
;
209 if (curSize
== newSize
) //no problems found
210 return this.noMoreProblems("No more problems in this direction");
211 // Ok, found something:
212 this.curProb
= this.findClosestNeighbor(this.curProb
, curProbs
, direction
);
214 findClosestNeighbor: function(problem
, probList
, direction
) {
215 let neighbor
= undefined;
216 let smallestDistance
= Number
.MAX_SAFE_INTEGER
;
217 for (let prob
of probList
)
219 const delta
= Math
.abs(prob
.id
- problem
.id
);
220 if (delta
< smallestDistance
&&
221 ((direction
== "backward" && prob
.id
< problem
.id
)
222 || (direction
== "forward" && prob
.id
> problem
.id
)))
225 smallestDistance
= delta
;
230 noMoreProblems: function(message
) {
231 this.nomoreMessage
= message
;
232 let modalNomore
= document
.getElementById("modalNomore");
233 modalNomore
.checked
= true;
234 setTimeout(() => modalNomore
.checked
= false, 2000);
236 displayList: function() {
238 location
.hash
= "#problems";
239 // Fetch problems if first call (if #num, and then lists)
240 if (!this.listsInitialized
)
243 toggleListDisplay: function() {
244 const displays
= ["mine","others"];
245 const curIndex
= displays
.findIndex(item
=> item
== this.display
);
246 this.display
= displays
[1-curIndex
];
248 fetchProblems: function(type
, direction
) {
249 let problems
= (type
== "others" ? this.problems : this.myProblems
);
250 // "last datetime" set at a value OK for an empty initial array
251 let last_dt
= (direction
=="forward" ? 0 : Number
.MAX_SAFE_INTEGER
);
252 if (problems
.length
> 0)
254 // Search for newest date (or oldest)
255 last_dt
= problems
[0].added
;
256 for (let i
=1; i
<problems
.length
; i
++)
258 if ((direction
== "forward" && problems
[i
].added
> last_dt
) ||
259 (direction
== "backward" && problems
[i
].added
< last_dt
))
261 last_dt
= problems
[i
].added
;
266 "/problems/" + variant
.id
,
270 direction: direction
,
274 if (response
.problems
.length
> 0)
276 Array
.prototype.push
.apply(problems
,
277 response
.problems
.sort((p1
,p2
) => { return p2
.added
- p1
.added
; }));
278 // If one list is empty but not the other, show the non-empty
279 const otherArray
= (type
== "mine" ? this.problems : this.myProblems
);
280 if (problems
.length
> 0 && otherArray
.length
== 0)
286 previewProblem: function() {
287 if (!V
.IsGoodFen(this.modalProb
.fen
))
288 return alert(translations
["Bad FEN description"]);
289 if (this.modalProb
.instructions
.trim().length
== 0)
290 return alert(translations
["Empty instructions"]);
291 if (this.modalProb
.solution
.trim().length
== 0)
292 return alert(translations
["Empty solution"]);
293 Vue
.set(this.modalProb
, "preview", true);
295 editProblem: function(prob
) {
296 this.modalProb
= prob
;
297 Vue
.set(this.modalProb
, "preview", false);
298 document
.getElementById("modal-newproblem").checked
= true;
300 deleteProblem: function(pid
) {
305 // Delete problem from the list on client side
306 let problems
= this.curProblems();
307 const pIdx
= problems
.findIndex(p
=> p
.id
== pid
);
308 problems
.splice(pIdx
, 1);
312 sendProblem: function() {
313 // Send it to the server and close modal
315 "/problems/" + variant
.id
,
316 (this.modalProb
.id
> 0 ? "PUT" : "POST"),
319 document
.getElementById("modal-newproblem").checked
= false;
320 Vue
.set(this.modalProb
, "preview", false);
321 if (this.modalProb
.id
== 0)
323 this.myProblems
.unshift({
327 fen: this.modalProb
.fen
,
328 instructions: this.modalProb
.instructions
,
329 solution: this.modalProb
.solution
,
333 this.modalProb
.id
= 0;