]> git.sesse.net Git - remoteglot/blobdiff - www/js/chessboard-0.3.0.js
Use proper variable scoping everywhere.
[remoteglot] / www / js / chessboard-0.3.0.js
index 851de8fc12eda325ed6233ffc20dfbfaee080172..451f1a9f4d17cf73ed93c0b52788ccfb2cda1577 100644 (file)
@@ -1,7 +1,8 @@
 /*!
- * chessboard.js v0.3.0
+ * chessboard.js v0.3.0+asn
  *
  * Copyright 2013 Chris Oakman
+ * Portions copyright 2022 Steinar H. Gunderson
  * Released under the MIT license
  * http://chessboardjs.com/license
  *
@@ -367,24 +368,6 @@ function checkDeps() {
     containerEl = containerElOrId;
   }
 
-  // JSON must exist
-  if (! window.JSON ||
-      typeof JSON.stringify !== 'function' ||
-      typeof JSON.parse !== 'function') {
-    window.alert('ChessBoard Error 1004: JSON does not exist. ' +
-      'Please include a JSON polyfill.\n\nExiting...');
-    return false;
-  }
-
-  // check for a compatible version of jQuery
-  if (! (typeof window.$ && $.fn && $.fn.jquery &&
-      compareSemVer($.fn.jquery, MINIMUM_JQUERY_VERSION) === true)) {
-    window.alert('ChessBoard Error 1005: Unable to find a valid version ' +
-      'of jQuery. Please include jQuery ' + MINIMUM_JQUERY_VERSION + ' or ' +
-      'higher on the page.\n\nExiting...');
-    return false;
-  }
-
   return true;
 }
 
@@ -743,7 +726,7 @@ function animateSquareToSquare(src, dest, piece, completeFn) {
   // animate the piece to the destination square
   animatedPieceEl.addEventListener('transitionend', complete, {once: true});
   requestAnimationFrame(() => {
-    animatedPieceEl.style.transitionProperty = 'top left';
+    animatedPieceEl.style.transitionProperty = 'top, left';
     animatedPieceEl.style.transitionDuration = cfg.moveSpeed + 'ms';
     animatedPieceEl.style.top = destSquarePosition.top + 'px';
     animatedPieceEl.style.left = destSquarePosition.left + 'px';
@@ -785,7 +768,7 @@ function animateSparePieceToSquare(piece, dest, completeFn) {
     duration: cfg.moveSpeed,
     complete: complete
   };
-  $(animatedPieceEl).animate(destOffset, opts);
+  //$(animatedPieceEl).animate(destOffset, opts);
 }
 
 function fadeIn(piece, onFinish) {
@@ -835,33 +818,35 @@ function doAnimations(a, oldPos, newPos) {
     }
   }
 
-  for (var i = 0; i < a.length; i++) {
-    // clear a piece
-    if (a[i].type === 'clear') {
-      document.getElementById(SQUARE_ELS_IDS[a[i].square]).querySelectorAll('.' + CSS.piece).forEach(
-        (piece) => fadeOut(piece, onFinish)
-      );
-    }
+  requestAnimationFrame(() => {  // Firefox workaround.
+    for (var i = 0; i < a.length; i++) {
+      // clear a piece
+      if (a[i].type === 'clear') {
+        document.getElementById(SQUARE_ELS_IDS[a[i].square]).querySelectorAll('.' + CSS.piece).forEach(
+          (piece) => fadeOut(piece, onFinish)
+        );
+      }
 
-    // add a piece (no spare pieces)
-    if (a[i].type === 'add' && cfg.sparePieces !== true) {
-      let square = document.getElementById(SQUARE_ELS_IDS[a[i].square]);
-      square.append(buildPiece(a[i].piece, true));
-      let piece = square.querySelector('.' + CSS.piece);
-      fadeIn(piece, onFinish);
-    }
+      // add a piece (no spare pieces)
+      if (a[i].type === 'add' && cfg.sparePieces !== true) {
+        let square = document.getElementById(SQUARE_ELS_IDS[a[i].square]);
+        square.append(buildPiece(a[i].piece, true));
+        let piece = square.querySelector('.' + CSS.piece);
+        fadeIn(piece, onFinish);
+      }
 
-    // add a piece from a spare piece
-    if (a[i].type === 'add' && cfg.sparePieces === true) {
-      animateSparePieceToSquare(a[i].piece, a[i].square, onFinish);
-    }
+      // add a piece from a spare piece
+      if (a[i].type === 'add' && cfg.sparePieces === true) {
+        animateSparePieceToSquare(a[i].piece, a[i].square, onFinish);
+      }
 
-    // move a piece
-    if (a[i].type === 'move') {
-      animateSquareToSquare(a[i].source, a[i].destination, a[i].piece,
-        onFinish);
+      // move a piece
+      if (a[i].type === 'move') {
+        animateSquareToSquare(a[i].source, a[i].destination, a[i].piece,
+          onFinish);
+      }
     }
-  }
+  });
 }
 
 // returns the distance between two squares
@@ -1135,7 +1120,7 @@ function snapbackDraggedPiece() {
   // animate the piece to the target square
   draggedPieceEl.addEventListener('transitionend', complete, {once: true});
   requestAnimationFrame(() => {
-    draggedPieceEl.style.transitionProperty = 'top left';
+    draggedPieceEl.style.transitionProperty = 'top, left';
     draggedPieceEl.style.transitionDuration = cfg.snapbackSpeed + 'ms';
     draggedPieceEl.style.top = sourceSquarePosition.top + 'px';
     draggedPieceEl.style.left = sourceSquarePosition.left + 'px';
@@ -1158,7 +1143,7 @@ function trashDraggedPiece() {
 
   // hide the dragged piece
   // FIXME: support this for non-jquery
-  $(draggedPieceEl).fadeOut(cfg.trashSpeed);
+  //$(draggedPieceEl).fadeOut(cfg.trashSpeed);
 
   // set state
   DRAGGING_A_PIECE = false;
@@ -1193,7 +1178,7 @@ function dropDraggedPieceOnSquare(square) {
   // snap the piece to the target square
   draggedPieceEl.addEventListener('transitionend', complete, {once: true});
   requestAnimationFrame(() => {
-    draggedPieceEl.style.transitionProperty = 'top left';
+    draggedPieceEl.style.transitionProperty = 'top, left';
     draggedPieceEl.style.transitionDuration = cfg.snapSpeed + 'ms';
     draggedPieceEl.style.top = targetSquarePosition.top + 'px';
     draggedPieceEl.style.left = targetSquarePosition.left + 'px';