X-Git-Url: https://git.auder.net/?a=blobdiff_plain;f=public%2Fjavascripts%2Fcomponents%2Fproblems.js;h=a95529234fbe9ebaa7766d984395661271424e9a;hb=b955c65b942d09d24b5c3bed0d755d4f2f8f71f1;hp=93b3353a8ba12ae769b21b9c273b0a362ca0b2e9;hpb=ff1d4c3f43d8333e9629a8e59606c234cb10869f;p=vchess.git diff --git a/public/javascripts/components/problems.js b/public/javascripts/components/problems.js index 93b3353a..a9552923 100644 --- a/public/javascripts/components/problems.js +++ b/public/javascripts/components/problems.js @@ -1,15 +1,21 @@ Vue.component('my-problems', { + props: ["probId","settings"], data: function () { return { userId: user.id, problems: [], //oldest first myProblems: [], //same, but only mine - display: "list", //or "myList" - curIdx: -1, //index in (current) problems array + singletons: [], //requested problems (using #num) + display: "others", //or "mine" + curProb: null, //(reference to) current displayed problem (if any) showSolution: false, + nomoreMessage: "", + mode: "analyze", //for game component + pbNum: 0, //to navigate directly to some problem // New problem (to upload), or existing problem to edit: modalProb: { id: 0, //defined if it's an edit + uid: 0, //...also fen: "", instructions: "", solution: "", @@ -17,48 +23,16 @@ Vue.component('my-problems', { }, }; }, - // TODO: problem edit, just fill modalProb + adjust AJAX call - // problem delete: just AJAX call + confirm + // NOTE: always modals first, because otherwise "scroll to the end" undesirable effect template: `
-
- - - -
-
-
-

{{ curProb.instructions }}

-
- -
-

- {{ translations["Show solution"] }} -

-

{{ curProb.solution }}

-
- - -
-
-
- - - - +
- +

{{ translate("Add a problem") }}

-
+
{{ translate("Safe HTML tags allowed") }}

+ :placeholder='translate("Describe the problem goal")'> + + :placeholder='translate("How to solve the problem?")'> +
- - + + +
- +
+ +
+
+ +

{{ nomoreMessage }}

+
+
+
+ + + +
+
+
+

{{ curProb.instructions }}

+
+ + +
+

+ {{ translate("Show solution") }} +

+

{{ curProb.solution }}

+
+ +
+
+ + +
+ + +
`, - computed: { - sortedProblems: function() { - // Newest problem first - return this.curProblems.sort((a,b) => a.added - b.added); - }, - curProb: function() { - switch (this.display) - { - case "list": - return this.problems[this.curIdx]; - case "myList": - return this.myProblems[this.curIdx]; - } + watch: { + probId: function() { + this.showProblem(this.probId); }, }, created: function() { - if (location.hash.length > 0) - { - this.getOneProblem(location.hash.slice(1)); - this.curIdx = 0; //TODO: a bit more subtle, depending if it's my problem or not (set display) - } + if (!!this.probId) + this.showProblem(this.probId); else - { - // Fetch most recent problems from server - this.fetchProblems("backward"); //TODO: backward in time from the future. Second argument? - } + this.firstFetch(); }, methods: { - setCurIndex: function(idx) { - this.curIdx = idx; - location.hash = "#" + idx; + translate: translate, + firstFetch: function() { + // Fetch most recent problems from server, for both lists + this.fetchProblems("others", "bacwkard"); + this.fetchProblems("mine", "bacwkard"); + this.listsInitialized = true; }, - translate: function(text) { - return translations[text]; + showProblem: function(pid) { + location.hash = "#problems?id=" + pid; + for (let parray of [this.singletons,this.problems,this.myProblems]) + { + const pIdx = parray.findIndex(p => p.id == pid); + if (pIdx >= 0) + { + this.curProb = parray[pIdx]; + break; + } + } + if (!this.curProb) + { + // Cannot find problem in current set; get from server, and add to singletons. + ajax( + "/problems/" + variant.id + "/" + pid, //TODO: variant ID should not be required + "GET", + response => { + if (!!response.problem) + { + this.singletons.push(response.problem); + this.curProb = response.problem; + this.display = (response.problem.uid == this.userId ? "mine" : "others"); + } + else + this.noMoreProblems("Sorry, problem " + pid + " does not exist"); + } + ); + } + else + this.display = (this.curProb.uid == this.userId ? "mine" : "others"); }, curProblems: function() { switch (this.display) { - case "list": + case "others": return this.problems; - case "myList": + case "mine": return this.myProblems; } }, - // TODO: dans tous les cas si on n'affiche qu'un seul problème, - // le curseur ne doit se déplacer que d'une unité. + // TODO?: get 50 from server but only show 10 at a time (for example) showNext: function(direction) { - if (this.curIdx < 0) - this.fetchProblems(direction); + const nomorePb = + problems => { + if (!problems || problems.length == 0) + this.noMoreProblems("No more problems in this direction"); + }; + if (!this.curProb) + return this.fetchProblems(this.display, direction, nomorePb); + // Show next problem (older or newer): let curProbs = this.curProblems(); - if ((this.curIdx > 0 && direction=="backward") - || (this.curIdx < curProbs.length-1 && direction=="forward")) + // Try to find a neighbour problem in the direction, among current set + const neighbor = this.findClosestNeighbor(this.curProb, curProbs, direction); + if (!!neighbor) { - this.setCurIdx(this.curIdx + (direction=="forward" ? 1 : -1)); + this.curProb = neighbor; + return; } - else //at boundary + // Boundary case: nothing in current set, need to fetch from server + const curSize = curProbs.length; + this.fetchProblems(this.display, direction, problems => { + if (problems.length > 0) + { + // Ok, found something: + this.curProb = + this.findClosestNeighbor(this.curProb, curProbs, direction); + } + else + nomorePb(); + }); + }, + findClosestNeighbor: function(problem, probList, direction) { + let neighbor = undefined; + let smallestDistance = Number.MAX_SAFE_INTEGER; + for (let prob of probList) { - const curSize = curProbs.length; - this.fetchProblems(direction); - if (curProbs.length + const delta = Math.abs(prob.id - problem.id); + if (delta < smallestDistance && + ((direction == "backward" && prob.id < problem.id) + || (direction == "forward" && prob.id > problem.id))) + { + neighbor = prob; + smallestDistance = delta; + } } - else - this.setCurIndex(--this.curIdx); - - - if (this.curIdx == this.problems.length - 1) - this.fetchProblems("forward"); - else - this.curIdx++; - location.hash = this.curIdx; + return neighbor; + }, + noMoreProblems: function(message) { + this.nomoreMessage = message; + let modalNomore = document.getElementById("modalNomore"); + modalNomore.checked = true; + setTimeout(() => modalNomore.checked = false, 2000); + }, + displayList: function() { + this.curProb = null; + location.hash = "#problems"; + // Fetch problems if first call (if #num, and then lists) + if (!this.listsInitialized) + this.firstFetch(); }, toggleListDisplay: function() { - this.display = (this.display == "list" ? "myList" : "list"); + const displays = ["mine","others"]; + const curIndex = displays.findIndex(item => item == this.display); + this.display = displays[1-curIndex]; }, - // TODO: modal "there are no more problems" - fetchProblems: function(direction) { - const problems = if ... this.problems ... ou this.myProblems; - if (this.problems.length == 0) - return; //what could we do?! -------> ask problems older than MAX_NUMBER + backward - // Search for newest date (or oldest) - let last_dt = this.problems[0].added; - for (let i=0; i 0) { - if ((direction == "forward" && this.problems[i].added > last_dt) || - (direction == "backward" && this.problems[i].added < last_dt)) + // Search for newest date (or oldest) + last_dt = problems[0].added; + for (let i=1; i last_dt) || + (direction == "backward" && problems[i].added < last_dt)) + { + last_dt = problems[i].added; + } } } - ajax("/problems/" + variant.name, "GET", { //TODO: use variant._id ? - direction: direction, - last_dt: last_dt, - }, response => { - if (response.problems.length > 0) + ajax( + "/problems/" + variant.id, + "GET", { - this.problems = response.problems - .sort((p1,p2) => { return p1.added - p2.added; }); - this.setCurIndex(response.problems.length - 1); + type: type, + direction: direction, + last_dt: last_dt, + }, + response => { + if (response.problems.length > 0) + { + Array.prototype.push.apply(problems, response.problems.sort( + (p1,p2) => { return p2.added - p1.added; })); + // If one list is empty but not the other, show the non-empty + const otherArray = + (type == "mine" ? this.problems : this.myProblems); + if (otherArray.length == 0) + this.display = type; + this.$forceUpdate(); //TODO... + } + if (!!cb) + cb(response.problems); } - }); + ); }, - previewNewProblem: function() { - if (!V.IsGoodFen(this.newProblem.fen)) + previewProblem: function() { + if (!V.IsGoodFen(this.modalProb.fen)) return alert(translations["Bad FEN description"]); - if (this.newProblem.instructions.trim().length == 0) + if (this.modalProb.instructions.trim().length == 0) return alert(translations["Empty instructions"]); - if (this.newProblem.solution.trim().length == 0) + if (this.modalProb.solution.trim().length == 0) return alert(translations["Empty solution"]); - this.modalProb.preview = true; + Vue.set(this.modalProb, "preview", true); + }, + editProblem: function(prob) { + this.modalProb = prob; + Vue.set(this.modalProb, "preview", false); + document.getElementById("modal-newproblem").checked = true; }, - // TODO: adjust for update too - sendNewProblem: function() { + deleteProblem: function(pid) { + ajax( + "/problems/" + pid, + "DELETE", + response => { + // Delete problem from the list on client side + let problems = this.curProblems(); + const pIdx = problems.findIndex(p => p.id == pid); + problems.splice(pIdx, 1); + } + ); + }, + sendProblem: function() { // Send it to the server and close modal - ajax("/problems/" + variant.name, "POST", { //TODO: with variant._id ? - fen: this.newProblem.fen, - instructions: this.newProblem.instructions, - solution: this.newProblem.solution, - }, response => { - this.modalProb.added = Date.now(); - this.curProblems().push(JSON.parse(JSON.stringify(this.modalProb))); - document.getElementById("modal-newproblem").checked = false; - this.modalProb.preview = false; - }); + ajax( + "/problems/" + variant.id, + (this.modalProb.id > 0 ? "PUT" : "POST"), + this.modalProb, + response => { + document.getElementById("modal-newproblem").checked = false; + Vue.set(this.modalProb, "preview", false); + if (this.modalProb.id == 0) + { + this.myProblems.unshift({ + added: Date.now(), + id: response.id, + uid: user.id, + fen: this.modalProb.fen, + instructions: this.modalProb.instructions, + solution: this.modalProb.solution, + }); + if (!this.curProb && this.display != "mine") + this.display = "mine"; + } + else + this.modalProb.id = 0; + } + ); }, - // TODO: AJAX for problem deletion }, })