Update npm packages
[vchess.git] / client / src / views / Analyse.vue
CommitLineData
7f3484bd 1<template lang="pug">
7aa548e7 2main
26d8a01a
BA
3 input#modalRules.modal(type="checkbox")
4 div#rulesDiv(
5 role="dialog"
6 data-checkbox="modalRules"
7 )
8 .card
9 label.modal-close(for="modalRules")
1c15969e
BA
10 a#variantNameInAnalyze(:href="'/#/variants/'+game.vname")
11 | {{ game.vname }}
26d8a01a 12 div(v-html="rulesContent")
63ca2b89
BA
13 .row
14 .col-sm-12
9a3049f3 15 .text-center
910d631b
BA
16 input#fen(
17 v-model="curFen"
725da57f 18 @input="adjustFenSize(); tryGotoFen()"
910d631b 19 )
910d631b 20 BaseGame(
b1e46b33 21 ref="basegame"
910d631b 22 :game="game"
8055eabd 23 @fenchange="setFen"
910d631b 24 )
7f3484bd
BA
25</template>
26
27<script>
28import BaseGame from "@/components/BaseGame.vue";
26d8a01a
BA
29import { processModalClick } from "@/utils/modalClick";
30import { replaceByDiag } from "@/utils/printDiagram";
7f3484bd 31import { store } from "@/store";
7f3484bd 32export default {
6808d7a1 33 name: "my-analyse",
5f918a27
BA
34 // TODO: game import ==> require some adjustments, like
35 // the ability to analyse from a list of moves...
7f3484bd 36 components: {
6808d7a1 37 BaseGame
7f3484bd
BA
38 },
39 // gameRef: to find the game in (potentially remote) storage
40 data: function() {
41 return {
42 st: store.state,
26d8a01a 43 rulesContent: "",
6808d7a1 44 gameRef: {
652f40de
BA
45 vname: "",
46 fen: ""
47 },
48 game: {
6808d7a1
BA
49 players: [{ name: "Analyse" }, { name: "Analyse" }],
50 mode: "analyze"
7f3484bd 51 },
6808d7a1 52 curFen: ""
7f3484bd
BA
53 };
54 },
b1e46b33
BA
55 watch: {
56 $route: function() {
57 this.initFromUrl();
a97bdbda 58 }
7f3484bd 59 },
b1e46b33
BA
60 created: function() {
61 this.initFromUrl();
62 },
26d8a01a
BA
63 mounted: function() {
64 document.getElementById("rulesDiv")
65 .addEventListener("click", processModalClick);
66 },
7f3484bd 67 methods: {
a97bdbda
BA
68 alertAndQuit: function(text, wrongVname) {
69 // Soon after component creation, st.tr might be uninitialized.
70 // Set a timeout to let a chance for the message to show translated.
2c5d7b20
BA
71 const newUrl =
72 "/variants" + (wrongVname ? "" : "/" + this.gameRef.vname);
a97bdbda
BA
73 setTimeout(() => {
74 alert(this.st.tr[text] || text);
75 this.$router.replace(newUrl);
76 }, 500);
77 },
b1e46b33
BA
78 initFromUrl: function() {
79 this.gameRef.vname = this.$route.params["vname"];
80 const routeFen = this.$route.query["fen"];
81 if (!routeFen) this.alertAndQuit("Missing FEN");
82 else {
83 this.gameRef.fen = routeFen.replace(/_/g, " ");
07052665
BA
84 // orientation is optional: taken from FEN if missing.
85 // NOTE: currently no internal usage of 'side', but could be used by
86 // manually settings the URL (TODO?).
b1e46b33
BA
87 const orientation = this.$route.query["side"];
88 this.initialize(orientation);
89 }
90 },
7ba4a5bc 91 initialize: async function(orientation) {
5157ce0b 92 // Obtain VariantRules object
a97bdbda
BA
93 await import("@/variants/" + this.gameRef.vname + ".js")
94 .then((vModule) => {
32f6285e 95 window.V = vModule[this.gameRef.vname + "Rules"];
a97bdbda
BA
96 if (!V.CanAnalyze)
97 // Late check, in case the user tried to enter URL by hand
98 this.alertAndQuit("Analysis disabled for this variant");
7ba4a5bc 99 else this.loadGame(orientation);
a97bdbda
BA
100 })
101 .catch((err) => { this.alertAndQuit("Mispelled variant name", true); });
26d8a01a
BA
102 this.rulesContent =
103 require(
104 "raw-loader!@/translations/rules/" +
105 this.gameRef.vname + "/" +
106 this.st.lang + ".pug"
8b3b2151
BA
107 ).default
108 .replace('export default "', "")
109 .replace(/";$/, "")
26d8a01a
BA
110 // Next two lines fix a weird issue after last update (2019-11)
111 .replace(/\\n/g, " ")
112 .replace(/\\"/g, '"')
26d8a01a
BA
113 .replace(/(fen:)([^:]*):/g, replaceByDiag);
114 },
7ba4a5bc 115 loadGame: function(orientation) {
652f40de 116 this.game.vname = this.gameRef.vname;
b1e46b33 117 this.game.fenStart = this.gameRef.fen;
652f40de 118 this.game.fen = this.gameRef.fen;
0234201f 119 this.game.score = "*"; //never change
5157ce0b
BA
120 this.curFen = this.game.fen;
121 this.adjustFenSize();
7ba4a5bc 122 this.game.mycolor = orientation || V.ParseFen(this.gameRef.fen).turn;
b1e46b33 123 this.$refs["basegame"].re_setVariables(this.game);
7f3484bd 124 },
8055eabd
BA
125 // Triggered by "fenchange" emitted in BaseGame:
126 setFen: function(fen) {
127 this.curFen = fen;
128 this.adjustFenSize();
129 },
5157ce0b
BA
130 adjustFenSize: function() {
131 let fenInput = document.getElementById("fen");
d54f6261 132 fenInput.style.width = (this.curFen.length+3) + "ch";
5157ce0b 133 },
725da57f 134 tryGotoFen: function() {
3a2a7b5f 135 if (V.IsGoodFen(this.curFen)) {
725da57f
BA
136 this.gameRef.fen = this.curFen;
137 this.loadGame();
138 }
6808d7a1
BA
139 }
140 }
7f3484bd
BA
141};
142</script>
d54f6261 143
26d8a01a
BA
144<style lang="sass">
145@import "@/styles/_board_squares_img.sass"
146@import "@/styles/_rules.sass"
147</style>
148
149<style lang="sass" scoped>
1c15969e
BA
150a#variantNameInAnalyze
151 color: var(--card-fore-color)
26d8a01a 152 text-align: center
26d8a01a 153 font-weight: bold
1c15969e
BA
154 font-size: calc(1rem * var(--heading-ratio))
155 line-height: 1.2
156 margin: calc(1.5 * var(--universal-margin))
26d8a01a
BA
157
158#rulesDiv > .card
159 padding: 5px 0
160 max-width: 50%
161 max-height: 100%
162 @media screen and (max-width: 1500px)
163 max-width: 67%
164 @media screen and (max-width: 1024px)
165 max-width: 85%
166 @media screen and (max-width: 767px)
167 max-width: 100%
168
d54f6261
BA
169input#fen
170 // Use a Monospace font for input FEN width adjustment
171 font-family: "Fira Code"
172</style>