]> git.sesse.net Git - remoteglot/commitdiff
Remove the spare piece functionality we do not use.
authorSteinar H. Gunderson <sgunderson@bigfoot.com>
Tue, 27 Dec 2022 23:54:10 +0000 (00:54 +0100)
committerSteinar H. Gunderson <sgunderson@bigfoot.com>
Tue, 27 Dec 2022 23:54:10 +0000 (00:54 +0100)
www/js/chessboard-0.3.0.js

index 477d778d7562ba81e6726159df84ae4d7548a777..333935930aaace2bfe6c3c972d680eb25abe24ad 100644 (file)
@@ -216,9 +216,6 @@ var CSS = {
   numeric: 'numeric-fc462',
   piece: 'piece-417db',
   row: 'row-5277c',
-  sparePieces: 'spare-pieces-7492f',
-  sparePiecesBottom: 'spare-pieces-bottom-ae20f',
-  sparePiecesTop: 'spare-pieces-top-4028b',
   square: 'square-55d63'
 };
 var CSSColor = {};
@@ -232,9 +229,7 @@ CSSColor['black'] = 'black-3c85d';
 // DOM elements
 var containerEl,
   boardEl,
-  draggedPieceEl,
-  sparePiecesTopEl,
-  sparePiecesBottomEl;
+  draggedPieceEl;
 
 // constructor return object
 var widget = {};
@@ -251,7 +246,6 @@ var BOARD_BORDER_SIZE = 2,
   DRAGGED_PIECE_LOCATION,
   DRAGGED_PIECE_SOURCE,
   DRAGGING_A_PIECE = false,
-  SPARE_PIECE_ELS_IDS = {},
   SQUARE_ELS_IDS = {},
   SQUARE_ELS_OFFSETS;
 
@@ -408,16 +402,6 @@ function expandConfig() {
     cfg.dropOffBoard = 'snapback';
   }
 
-  // default for sparePieces is false
-  if (cfg.sparePieces !== true) {
-    cfg.sparePieces = false;
-  }
-
-  // draggable must be true if sparePieces is enabled
-  if (cfg.sparePieces === true) {
-    cfg.draggable = true;
-  }
-
   // default piece theme is wikipedia
   if (cfg.hasOwnProperty('pieceTheme') !== true ||
       (typeof cfg.pieceTheme !== 'string' &&
@@ -505,15 +489,6 @@ function createElIds() {
       SQUARE_ELS_IDS[square] = square + '-' + createId();
     }
   }
-
-  // spare pieces
-  var pieces = 'KQRBNP'.split('');
-  for (var i = 0; i < pieces.length; i++) {
-    var whitePiece = 'w' + pieces[i];
-    var blackPiece = 'b' + pieces[i];
-    SPARE_PIECE_ELS_IDS[whitePiece] = whitePiece + '-' + createId();
-    SPARE_PIECE_ELS_IDS[blackPiece] = blackPiece + '-' + createId();
-  }
 }
 
 //------------------------------------------------------------------------------
@@ -523,18 +498,8 @@ function createElIds() {
 function buildBoardContainer() {
   var html = '<div class="' + CSS.chessboard + '">';
 
-  if (cfg.sparePieces === true) {
-    html += '<div class="' + CSS.sparePieces + ' ' +
-      CSS.sparePiecesTop + '"></div>';
-  }
-
   html += '<div class="' + CSS.board + '"></div>';
 
-  if (cfg.sparePieces === true) {
-    html += '<div class="' + CSS.sparePieces + ' ' +
-      CSS.sparePiecesBottom + '"></div>';
-  }
-
   html += '</div>';
 
   return html;
@@ -653,20 +618,6 @@ function buildPiece(piece, hidden, id) {
   return img;
 }
 
-function buildSparePieces(color) {
-  var pieces = ['wK', 'wQ', 'wR', 'wB', 'wN', 'wP'];
-  if (color === 'black') {
-    pieces = ['bK', 'bQ', 'bR', 'bB', 'bN', 'bP'];
-  }
-
-  var html = '';
-  for (var i = 0; i < pieces.length; i++) {
-    html += buildPiece(pieces[i], false, SPARE_PIECE_ELS_IDS[pieces[i]]);
-  }
-
-  return html;
-}
-
 //------------------------------------------------------------------------------
 // Animations
 //------------------------------------------------------------------------------
@@ -726,44 +677,6 @@ function animateSquareToSquare(src, dest, piece, completeFn) {
   });
 }
 
-function animateSparePieceToSquare(piece, dest, completeFn) {
-  var srcOffset = offset(document.getelementById(SPARE_PIECE_ELS_IDS[piece]));
-  var destSquareEl = document.getElementById(SQUARE_ELS_IDS[dest]);
-  var destOffset = offset(destSquareEl);
-
-  // create the animate piece
-  var pieceId = createId();
-  document.body.append(buildPiece(piece, true, pieceId));
-  var animatedPieceEl = document.getElementById(pieceId);
-  animatedPieceEl.style.display = '';
-  animatedPieceEl.style.position = 'absolute';
-  animatedPieceEl.style.left = srcOffset.left;
-  animatedPieceEl.style.top = srcOffset.top;
-
-  // on complete
-  var complete = function() {
-    // add the "real" piece to the destination square
-    destSquareEl.querySelector('.' + CSS.piece).remove();
-    destSquareEl.append(buildPiece(piece));
-
-    // remove the animated piece
-    animatedPieceEl.remove();
-
-    // run complete function
-    if (typeof completeFn === 'function') {
-      completeFn();
-    }
-  };
-
-  // animate the piece to the destination square
-  // FIXME: support this for non-jquery
-  var opts = {
-    duration: cfg.moveSpeed,
-    complete: complete
-  };
-  //$(animatedPieceEl).animate(destOffset, opts);
-}
-
 function fadeIn(pieces, onFinish) {
   pieces.forEach((piece) => {
     piece.style.opacity = 0;
@@ -828,19 +741,14 @@ function doAnimations(a, oldPos, newPos) {
         );
       }
 
-      // add a piece (no spare pieces)
-      if (a[i].type === 'add' && cfg.sparePieces !== true) {
+      // add a piece
+      if (a[i].type === 'add') {
         let square = document.getElementById(SQUARE_ELS_IDS[a[i].square]);
         square.append(buildPiece(a[i].piece, true));
         let piece = square.querySelector('.' + CSS.piece);
         fadein_pieces.push(piece);
       }
 
-      // 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,
@@ -989,17 +897,6 @@ function drawPositionInstant() {
 function drawBoard() {
   boardEl.innerHTML = buildBoard(CURRENT_ORIENTATION);
   drawPositionInstant();
-
-  if (cfg.sparePieces === true) {
-    if (CURRENT_ORIENTATION === 'white') {
-      sparePiecesTopEl.innerHTML = buildSparePieces('black');
-      sparePiecesBottomEl.innerHTML = buildSparePieces('white');
-    }
-    else {
-      sparePiecesTopEl.innerHTML = buildSparePieces('white');
-      sparePiecesBottomEl.innerHTML = buildSparePieces('black');
-    }
-  }
 }
 
 // given a position and a set of moves, return a new position
@@ -1073,12 +970,6 @@ function removeSquareHighlights() {
 }
 
 function snapbackDraggedPiece() {
-  // there is no "snapback" for spare pieces
-  if (DRAGGED_PIECE_SOURCE === 'spare') {
-    trashDraggedPiece();
-    return;
-  }
-
   removeSquareHighlights();
 
   // animation complete
@@ -1182,14 +1073,7 @@ function beginDraggingPiece(source, piece, x, y) {
   DRAGGING_A_PIECE = true;
   DRAGGED_PIECE = piece;
   DRAGGED_PIECE_SOURCE = source;
-
-  // if the piece came from spare pieces, location is offboard
-  if (source === 'spare') {
-    DRAGGED_PIECE_LOCATION = 'offboard';
-  }
-  else {
-    DRAGGED_PIECE_LOCATION = source;
-  }
+  DRAGGED_PIECE_LOCATION = source;
 
   // capture the x, y coords of all squares in memory
   captureSquareOffsets();
@@ -1201,12 +1085,10 @@ function beginDraggingPiece(source, piece, x, y) {
   draggedPieceEl.style.left = (x - (SQUARE_SIZE / 2)) + 'px';
   draggedPieceEl.style.top = (y - (SQUARE_SIZE / 2)) + 'px';
 
-  if (source !== 'spare') {
-    // highlight the source square and hide the piece
-    let square = document.getElementById(SQUARE_ELS_IDS[source]);
-    square.classList.add(CSS.highlight1);
-    square.querySelector('.' + CSS.piece).style.display = 'none';
-  }
+  // highlight the source square and hide the piece
+  let square = document.getElementById(SQUARE_ELS_IDS[source]);
+  square.classList.add(CSS.highlight1);
+  square.querySelector('.' + CSS.piece).style.display = 'none';
 }
 
 function updateDraggedPiece(x, y) {
@@ -1257,16 +1139,6 @@ function stopDraggedPiece(location) {
     typeof cfg.onDrop === 'function') {
     var newPosition = deepCopy(CURRENT_POSITION);
 
-    // source piece is a spare piece and position is off the board
-    //if (DRAGGED_PIECE_SOURCE === 'spare' && location === 'offboard') {...}
-    // position has not changed; do nothing
-
-    // source piece is a spare piece and position is on the board
-    if (DRAGGED_PIECE_SOURCE === 'spare' && validSquare(location) === true) {
-      // add the piece to the board
-      newPosition[location] = DRAGGED_PIECE;
-    }
-
     // source piece was on the board and position is off the board
     if (validSquare(DRAGGED_PIECE_SOURCE) === true && location === 'offboard') {
       // remove the piece from the board
@@ -1469,12 +1341,6 @@ widget.resize = function() {
     draggedPieceEl.style.width = SQUARE_SIZE + 'px';
   }
 
-  // spare pieces
-  if (cfg.sparePieces === true) {
-    containerEl.querySelector('.' + CSS.sparePieces)
-      .style.paddingLeft = (SQUARE_SIZE + BOARD_BORDER_SIZE) + 'px';
-  }
-
   // redraw the board
   drawBoard();
 };
@@ -1533,33 +1399,6 @@ function touchstartSquare(e) {
     e.changedTouches[0].pageX, e.changedTouches[0].pageY);
 }
 
-function mousedownSparePiece(e) {
-  if (!e.target.matches('.' + CSS.sparePieces + ' .' + CSS.piece)) {
-    return;
-  }
-
-  // do nothing if sparePieces is not enabled
-  if (cfg.sparePieces !== true) return;
-
-  var piece = e.target.getAttribute('data-piece');
-
-  beginDraggingPiece('spare', piece, e.pageX, e.pageY);
-}
-
-function touchstartSparePiece(e) {
-  if (!e.target.matches('.' + CSS.sparePieces + ' .' + CSS.piece)) {
-    return;
-  }
-
-  // do nothing if sparePieces is not enabled
-  if (cfg.sparePieces !== true) return;
-
-  var piece = e.target.getAttribute('data-piece');
-
-  beginDraggingPiece('spare', piece,
-    e.changedTouches[0].pageX, e.changedTouches[0].pageY);
-}
-
 function mousemoveWindow(e) {
   // do nothing if we are not dragging a piece
   if (DRAGGING_A_PIECE !== true) return;
@@ -1675,7 +1514,6 @@ function addEvents() {
 
   // mouse drag pieces
   boardEl.addEventListener('mousedown', mousedownSquare);
-  containerEl.addEventListener('mousedown', mousedownSparePiece);
 
   // mouse enter / leave square
   boardEl.addEventListener('mouseenter', mouseenterSquare);
@@ -1687,7 +1525,6 @@ function addEvents() {
   // touch drag pieces
   if (isTouchDevice() === true) {
     boardEl.addEventListener('touchstart', touchstartSquare);
-    containerEl.addEventListener('touchstart', touchstartSparePiece);
     window.addEventListener('touchmove', touchmoveWindow);
     window.addEventListener('touchend', touchendWindow);
   }
@@ -1698,11 +1535,6 @@ function initDom() {
   containerEl.innerHTML = buildBoardContainer();
   boardEl = containerEl.querySelector('.' + CSS.board);
 
-  if (cfg.sparePieces === true) {
-    sparePiecesTopEl = containerEl.querySelector('.' + CSS.sparePiecesTop);
-    sparePiecesBottomEl = containerEl.querySelector('.' + CSS.sparePiecesBottom);
-  }
-
   // create the drag piece
   var draggedPieceId = createId();
   document.body.append(buildPiece('wP', true, draggedPieceId));