Seemingly failed attempt at 'componentifying VariantRules'
[vchess.git] / client / src / views / Rules.vue
index 1702b50..fd66846 100644 (file)
@@ -1,6 +1,6 @@
 <template lang="pug">
 .row
-  .col-sm-12 col-md-10 col-md-offset-1 col-lg-8 col-lg-offset-2
+  .col-sm-12.col-md-10.col-md-offset-1.col-lg-8.col-lg-offset-2
     .button-group
       button(@click="display='rules'") Read the rules
       button(v-show="!gameInProgress" @click="watchComputerGame")
@@ -9,8 +9,8 @@
         | Beat the computer!
       button(v-show="gameInProgress" @click="stopGame")
         | Stop game
-    div(v-show="display=='rules'" v-html="content" class="section-content")
-    Game(v-show="display=='computer'" :mycolor="mycolor" :gid-or-fen="fen"
+    VariantRules(v-show="display=='rules'" :vname="variant.name")
+    Game(v-show="display=='computer'" :gid-or-fen="fen"
       :mode="mode" :sub-mode="subMode" :variant="variant"
       @computer-think="gameInProgress=false" @game-over="stopGame")
 </template>
 <script>
 import Game from "@/components/Game.vue";
 import { store } from "@/store";
+import VariantRules from "@/components/VariantRules";
 export default {
   name: 'my-rules',
   components: {
     Game,
+    VariantRules,
   },
   data: function() {
     return {
       st: store.state,
-      variant: null,
-      content: "",
+      variant: {id: 0, name: "_unknown"}, //...yet
       display: "rules",
       mode: "computer",
       subMode: "", //'auto' for game CPU vs CPU
@@ -36,32 +37,28 @@ export default {
       fen: "",
     };
   },
-  created: function() {
-    const vname = this.$route.params["vname"];
-    const idxOfVar = this.st.variants.indexOf(e => e.name == vname);
-    this.variant = this.st.variants[idxOfVar];
+  watch: {
+    $route: function(newRoute) {
+      this.tryChangeVariant(newRoute.params["vname"]);
+    },
   },
-  mounted: function() {
-    // Method to replace diagrams in loaded HTML
-    const replaceByDiag = (match, p1, p2) => {
-      const args = this.parseFen(p2);
-      return getDiagram(args);
-    };
-    // (AJAX) Request to get rules content (plain text, HTML)
-    this.content =
-      require("raw-loader!pug-plain-loader!@/rules/" +
-        this.$route.params["vname"] + "/" + this.st.lang + ".pug")
-      .replace(/(fen:)([^:]*):/g, replaceByDiag);
+  created: async function() {
+    // NOTE: variant cannot be set before store is initialized
+    this.tryChangeVariant(this.$route.params["vname"]);
   },
   methods: {
-    parseFen(fen) {
-      const fenParts = fen.split(" ");
-      return {
-        position: fenParts[0],
-        marks: fenParts[1],
-        orientation: fenParts[2],
-        shadow: fenParts[3],
-      };
+    tryChangeVariant: async function(vname) {
+      if (vname == "_unknown")
+        return;
+      if (this.st.variants.length > 0)
+      {
+        const idxOfVar = this.st.variants.findIndex(e => e.name == vname);
+        this.variant = this.st.variants[idxOfVar];
+      }
+      else
+        this.variant.name = vname;
+      const vModule = await import("@/variants/" + vname + ".js");
+      window.V = vModule.VariantRules;
     },
     startGame: function() {
       if (this.gameInProgress)