Attempt to improve scrolling on smartphone
[vchess.git] / client / src / components / Board.vue
index b452808..5b8b922 100644 (file)
@@ -31,6 +31,7 @@ export default {
       circles: {}, //object of squares' ID --> true (TODO: use a set?)
       click: "",
       clickTime: 0,
+      initialized: 0,
       settings: store.state.settings
     };
   },
@@ -194,7 +195,9 @@ export default {
                     showCheck && lightSquare && incheckSq[ci][cj],
                   "incheck-dark":
                     showCheck && !lightSquare && incheckSq[ci][cj],
-                  "hover-highlight": this.vr.hoverHighlight(ci, cj)
+                  "hover-highlight":
+                    this.vr.hoverHighlight(
+                      [ci, cj], !this.analyze ? this.userColor : null)
                 },
                 attrs: {
                   id: getSquareId({ x: ci, y: cj })
@@ -297,7 +300,8 @@ export default {
         (!myReserveTop && !!this.vr.reserve[playingColor])
       );
       // Center reserves, assuming same number of pieces for each side:
-      const nbReservePieces = myReservePiecesArray.length;
+      const nbReservePieces =
+        Math.max(myReservePiecesArray.length, oppReservePiecesArray.length);
       const marginLeft =
         ((100 - nbReservePieces * (100 / reserveSquareNb)) / 2) + "%";
       if (hasReserveTop) {
@@ -460,6 +464,8 @@ export default {
         }
       };
     }
+    if (this.initialized == 1) this.$emit("rendered");
+    if (this.initialized <= 1) this.initialized++;
     return (
       h(
         "div",
@@ -544,15 +550,20 @@ export default {
       }
     },
     addArrow: function(arrow) {
-      this.arrows.push(arrow);
-      // Also add to DOM:
-      const boardElt = document.getElementById("gamePosition");
-      const squareWidth = boardElt.offsetWidth / V.size.y;
-      const bPos = boardElt.getBoundingClientRect();
-      const newArrow =
-        this.getSvgArrow(arrow, bPos.top, bPos.left, squareWidth);
-      document.getElementById("arrowCanvas")
-        .insertAdjacentElement("beforeend", newArrow);
+      const arrowIdx = this.arrows.findIndex(a => {
+        return (
+          a.start[0] == arrow.start[0] && a.start[1] == arrow.start[1] &&
+          a.end[0] == arrow.end[0] && a.end[1] == arrow.end[1]
+        );
+      });
+      if (arrowIdx >= 0)
+        // Erase the arrow
+        this.arrows.splice(arrowIdx, 1);
+      else
+        // Add to arrows vector:
+        this.arrows.push(arrow);
+      // NOTE: no need to draw here, will be re-draw
+      // by updated() hook callong re_setDrawings()
     },
     getSvgArrow: function(arrow, top, left, squareWidth) {
       const aStart =
@@ -575,11 +586,12 @@ export default {
       return path;
     },
     re_setDrawings: function() {
+      // Add some drawing on board (for some variants + arrows and circles)
+      const boardElt = document.getElementById("gamePosition");
+      if (!boardElt) return;
       // Remove current canvas, if any
       const curCanvas = document.getElementById("arrowCanvas");
       if (!!curCanvas) curCanvas.parentNode.removeChild(curCanvas);
-      // Add some drawing on board (for some variants + arrows and circles)
-      const boardElt = document.getElementById("gamePosition");
       const squareWidth = boardElt.offsetWidth / V.size.y;
       const bPos = boardElt.getBoundingClientRect();
       let svgArrows = [];
@@ -639,7 +651,6 @@ export default {
       document.getElementById("rootBoardElement").appendChild(arrowCanvas);
     },
     mousedown: function(e) {
-      e.preventDefault();
       if (!this.mobileBrowser && e.which != 3)
         // Cancel current drawing and circles, if any
         this.cancelResetArrows();
@@ -650,6 +661,7 @@ export default {
             document.getElementById("boardContainer").getBoundingClientRect();
           // NOTE: classList[0] is enough: 'piece' is the first assigned class
           const withPiece = (e.target.classList[0] == "piece");
+          if (withPiece) e.preventDefault();
           // Show possible moves if current player allowed to play
           const startSquare =
             getSquareFromId(withPiece ? e.target.parentNode.id : e.target.id);
@@ -693,6 +705,7 @@ export default {
       }
       else if (e.which == 3) {
         // Mouse right button
+        e.preventDefault();
         this.containerPos =
           document.getElementById("gamePosition").getBoundingClientRect();
         let elem = e.target;
@@ -700,6 +713,7 @@ export default {
         while (elem.tagName == "IMG") elem = elem.parentNode;
         this.startArrow = getSquareFromId(elem.id);
       }
+      else e.preventDefault();
     },
     mousemove: function(e) {
       if (!this.selectedPiece && !this.startArrow) return;
@@ -760,15 +774,17 @@ export default {
       }
     },
     mouseup: function(e) {
-      e.preventDefault();
       if (this.mobileBrowser || e.which == 1) {
         if (!this.selectedPiece) return;
+        e.preventDefault();
         // Drag'n drop. Selected piece is no longer needed:
         this.selectedPiece.parentNode.removeChild(this.selectedPiece);
         delete this.selectedPiece;
         this.selectedPiece = null;
         this.processMoveAttempt(e);
-      } else if (e.which == 3) {
+      }
+      else if (e.which == 3) {
+        e.preventDefault();
         if (!this.startArrow) return;
         // Mouse right button
         this.movingArrow = null;