X-Git-Url: https://git.auder.net/?a=blobdiff_plain;f=client%2Fsrc%2Fviews%2FProblems.vue;h=3b53a1a0c7ed05da7cd2e526ae216ce17f969a9c;hb=b1e46b33d78bdf144a5603f82a6907901763abb9;hp=2d6ac6924a975c32d9189461d8f5c0beff7b1076;hpb=e71161fbfffe53b0f4b174e0467cdd98cc70b7b0;p=vchess.git diff --git a/client/src/views/Problems.vue b/client/src/views/Problems.vue index 2d6ac692..3b53a1a0 100644 --- a/client/src/views/Problems.vue +++ b/client/src/views/Problems.vue @@ -2,7 +2,7 @@ main input#modalNewprob.modal( type="checkbox" - @change="infoMsg=''" + @change="fenFocusIfOpened($event)" ) div#newprobDiv( role="dialog" @@ -56,7 +56,7 @@ main button.nomargin(@click="gotoPrevNext($event,curproblem,1)") | {{ st.tr["Previous"] }} button.nomargin(@click="gotoPrevNext($event,curproblem,-1)") - | {{ st.tr["Next"] }} + | {{ st.tr["Next_p"] }} p.oneInstructions.clickable( v-html="parseHtml(curproblem.instruction)" @click="curproblem.showSolution=!curproblem.showSolution" @@ -97,6 +97,7 @@ main td {{ firstChars(p.instruction) }} td {{ p.id }} BaseGame( + ref="basegame" v-if="showOne" :game="game" ) @@ -145,38 +146,50 @@ export default { }; }, created: function() { - ajax("/problems", "GET", res => { - // Show newest problem first: - this.problems = res.problems.sort((p1, p2) => p2.added - p1.added); - if (this.st.variants.length > 0) - this.problems.forEach(p => this.setVname(p)); - // Retrieve all problems' authors' names - let names = {}; - this.problems.forEach(p => { - if (p.uid != this.st.user.id) names[p.uid] = ""; - else p.uname = this.st.user.name; - }); - const showOneIfPid = () => { - const pid = this.$route.query["id"]; - if (pid) this.showProblem(this.problems.find(p => p.id == pid)); - }; - if (Object.keys(names).length > 0) { - ajax("/users", "GET", { ids: Object.keys(names).join(",") }, res2 => { - res2.users.forEach(u => { - names[u.id] = u.name; - }); + ajax( + "/problems", + "GET", + { + success: (res) => { + // Show newest problem first: + this.problems = res.problems.sort((p1, p2) => p2.added - p1.added); + if (this.st.variants.length > 0) + this.problems.forEach(p => this.setVname(p)); + // Retrieve all problems' authors' names + let names = {}; this.problems.forEach(p => { - if (!p.uname) - p.uname = names[p.uid]; + if (p.uid != this.st.user.id) names[p.uid] = ""; + else p.uname = this.st.user.name; }); - showOneIfPid(); - }); - } else showOneIfPid(); - }); + const showOneIfPid = () => { + const pid = this.$route.query["id"]; + if (!!pid) this.showProblem(this.problems.find(p => p.id == pid)); + }; + if (Object.keys(names).length > 0) { + ajax( + "/users", + "GET", + { + data: { ids: Object.keys(names).join(",") }, + success: (res2) => { + res2.users.forEach(u => { + names[u.id] = u.name; + }); + this.problems.forEach(p => { + if (!p.uname) + p.uname = names[p.uid]; + }); + showOneIfPid(); + } + } + ); + } else showOneIfPid(); + } + } + ); }, mounted: function() { - document - .getElementById("newprobDiv") + document.getElementById("newprobDiv") .addEventListener("click", processModalClick); }, watch: { @@ -188,11 +201,17 @@ export default { }, $route: function(to) { const pid = to.query["id"]; - if (pid) this.showProblem(this.problems.find(p => p.id == pid)); + if (!!pid) this.showProblem(this.problems.find(p => p.id == pid)); else this.showOne = false; } }, methods: { + fenFocusIfOpened: function(event) { + if (event.target.checked) { + this.infoMsg = ""; + document.getElementById("inputFen").focus(); + } + }, setVname: function(prob) { prob.vname = this.st.variants.find(v => v.id == prob.vid).name; }, @@ -280,10 +299,13 @@ export default { // The FEN is already checked at this stage: this.game.vname = p.vname; this.game.mycolor = V.ParseFen(p.fen).turn; //diagram orientation + this.game.fenStart = p.fen; this.game.fen = p.fen; - this.$set(this.game, "fenStart", p.fen); - this.copyProblem(p, this.curproblem); this.showOne = true; + // $nextTick to be sure $refs["basegame"] exists + this.$nextTick(() => { + this.$refs["basegame"].re_setVariables(this.game); }); + this.copyProblem(p, this.curproblem); }); }, gotoPrevNext: function(e, prob, dir) { @@ -316,22 +338,24 @@ export default { ajax( "/problems", edit ? "PUT" : "POST", - { prob: this.curproblem }, - ret => { - if (edit) { - let editedP = this.problems.find(p => p.id == this.curproblem.id); - this.copyProblem(this.curproblem, editedP); - this.showProblem(editedP); - } - else { - let newProblem = Object.assign({}, this.curproblem); - newProblem.id = ret.id; - newProblem.uid = this.st.user.id; - newProblem.uname = this.st.user.name; - this.problems = [newProblem].concat(this.problems); + { + data: { prob: this.curproblem }, + success: (ret) => { + if (edit) { + let editedP = this.problems.find(p => p.id == this.curproblem.id); + this.copyProblem(this.curproblem, editedP); + this.showProblem(editedP); + } + else { + let newProblem = Object.assign({}, this.curproblem); + newProblem.id = ret.id; + newProblem.uid = this.st.user.id; + newProblem.uname = this.st.user.name; + this.problems = [newProblem].concat(this.problems); + } + document.getElementById("modalNewprob").checked = false; + this.infoMsg = ""; } - document.getElementById("modalNewprob").checked = false; - this.infoMsg = ""; } ); }, @@ -343,10 +367,17 @@ export default { }, deleteProblem: function(prob) { if (confirm(this.st.tr["Are you sure?"])) { - ajax("/problems", "DELETE", { id: prob.id }, () => { - ArrayFun.remove(this.problems, p => p.id == prob.id); - this.backToList(); - }); + ajax( + "/problems", + "DELETE", + { + data: { id: prob.id }, + success: () => { + ArrayFun.remove(this.problems, p => p.id == prob.id); + this.backToList(); + } + } + ); } } }