| 1 | Vue.component('my-problems', { |
| 2 | data: function () { |
| 3 | return { |
| 4 | problems: problemArray, //initial value |
| 5 | newProblem: { |
| 6 | fen: V.GenRandInitFen(), |
| 7 | instructions: "", |
| 8 | solution: "", |
| 9 | stage: "nothing", //or "preview" after new problem is filled |
| 10 | }, |
| 11 | }; |
| 12 | }, |
| 13 | template: ` |
| 14 | <div> |
| 15 | <button @click="fetchProblems('backward')">Previous</button> |
| 16 | <button @click="fetchProblems('forward')">Next</button> |
| 17 | <button @click="showNewproblemModal">New</button> |
| 18 | <my-problem-summary v-on:show-problem="bubbleUp(p)" |
| 19 | v-for="(p,idx) in sortedProblems" v-bind:prob="p" v-bind:key="idx"> |
| 20 | </my-problem-summary> |
| 21 | <input type="checkbox" id="modal-newproblem" class="modal"> |
| 22 | <div role="dialog" aria-labelledby="newProblemTxt"> |
| 23 | <div v-show="newProblem.stage=='nothing'" class="card newproblem-form"> |
| 24 | <label for="modal-newproblem" class="modal-close"></label> |
| 25 | <h3 id="newProblemTxt">Add problem</h3> |
| 26 | <form @submit.prevent="previewNewProblem"> |
| 27 | <fieldset> |
| 28 | <label for="newpbFen">Fen</label> |
| 29 | <input id="newpbFen" type="text" v-model="newProblem.fen"/> |
| 30 | </fieldset> |
| 31 | <fieldset> |
| 32 | <p class="emphasis">Safe HTML tags allowed</p> |
| 33 | <label for="newpbInstructions">Instructions</label> |
| 34 | <textarea id="newpbInstructions" v-model="newProblem.instructions" |
| 35 | placeholder="Explain the problem here"/> |
| 36 | <label for="newpbSolution">Solution</label> |
| 37 | <textarea id="newpbSolution" v-model="newProblem.solution" |
| 38 | placeholder="How to solve the problem?"/> |
| 39 | <button class="center-btn">Preview</button> |
| 40 | </fieldset> |
| 41 | </form> |
| 42 | </div> |
| 43 | <div v-show="newProblem.stage=='preview'" class="card newproblem-preview"> |
| 44 | // TODO: we don't want exactly the same display (-date +solution) |
| 45 | <my-problem-summary v-bind:prob="newProblem"></my-problem-summary> |
| 46 | <button @click="sendNewProblem()" class="center-btn">Send</button> |
| 47 | </div> |
| 48 | </div> |
| 49 | </div> |
| 50 | `, |
| 51 | computed: { |
| 52 | sortedProblems: function() { |
| 53 | // Newest problem first |
| 54 | return this.problems.sort((p1,p2) => { return p2.added - p1.added; }); |
| 55 | }, |
| 56 | mailErrProblem: function() { |
| 57 | return "mailto:contact@vchess.club?subject=[" + variant + " problems] error"; |
| 58 | }, |
| 59 | }, |
| 60 | methods: { |
| 61 | // Propagate "show problem" event to parent component (my-variant) |
| 62 | bubbleUp: function(problem) { |
| 63 | this.$emit('show-problem', JSON.stringify(problem)); |
| 64 | }, |
| 65 | fetchProblems: function(direction) { |
| 66 | return; //TODO: re-activate after server side is implemented (see routes/all.js) |
| 67 | if (this.problems.length == 0) |
| 68 | return; //what could we do?! |
| 69 | // Search for newest date (or oldest) |
| 70 | let last_dt = this.problems[0].added; |
| 71 | for (let i=0; i<this.problems.length; i++) |
| 72 | { |
| 73 | if ((direction == "forward" && this.problems[i].added > last_dt) || |
| 74 | (direction == "backward" && this.problems[i].added < last_dt)) |
| 75 | { |
| 76 | last_dt = this.problems[i].added; |
| 77 | } |
| 78 | } |
| 79 | ajax("/problems/" + variant, "GET", { |
| 80 | direction: direction, |
| 81 | last_dt: last_dt, |
| 82 | }, response => { |
| 83 | if (response.problems.length > 0) |
| 84 | this.problems = response.problems; |
| 85 | }); |
| 86 | }, |
| 87 | showNewproblemModal: function() { |
| 88 | document.getElementById("modal-newproblem").checked = true; |
| 89 | }, |
| 90 | previewNewProblem: function() { |
| 91 | if (!V.IsGoodFen(this.newProblem.fen)) |
| 92 | return alert("Bad FEN string"); |
| 93 | this.newProblem.stage = "preview"; |
| 94 | }, |
| 95 | sendNewProblem: function() { |
| 96 | // Send it to the server and close modal |
| 97 | ajax("/problems/" + variant, "POST", { |
| 98 | fen: this.newProblem.fen, |
| 99 | instructions: this.newProblem.instructions, |
| 100 | solution: this.newProblem.solution, |
| 101 | }, response => { |
| 102 | document.getElementById("modal-newproblem").checked = false; |
| 103 | this.newProblem.stage = "nothing"; |
| 104 | }); |
| 105 | }, |
| 106 | }, |
| 107 | }) |