UNPKG

@wirelineio/shogiboardjsx

Version:

Shogiboardjsx is a shogiboard for React. Inspired by Chessboardjsx

156 lines (134 loc) 7.57 kB
function _typeof(obj) { if (typeof Symbol === "function" && typeof Symbol.iterator === "symbol") { _typeof = function _typeof(obj) { return typeof obj; }; } else { _typeof = function _typeof(obj) { return obj && typeof Symbol === "function" && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; }; } return _typeof(obj); } function _defineProperty(obj, key, value) { if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; } function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } } function _defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } } function _createClass(Constructor, protoProps, staticProps) { if (protoProps) _defineProperties(Constructor.prototype, protoProps); if (staticProps) _defineProperties(Constructor, staticProps); return Constructor; } function _possibleConstructorReturn(self, call) { if (call && (_typeof(call) === "object" || typeof call === "function")) { return call; } return _assertThisInitialized(self); } function _assertThisInitialized(self) { if (self === void 0) { throw new ReferenceError("this hasn't been initialised - super() hasn't been called"); } return self; } function _getPrototypeOf(o) { _getPrototypeOf = Object.setPrototypeOf ? Object.getPrototypeOf : function _getPrototypeOf(o) { return o.__proto__ || Object.getPrototypeOf(o); }; return _getPrototypeOf(o); } function _inherits(subClass, superClass) { if (typeof superClass !== "function" && superClass !== null) { throw new TypeError("Super expression must either be null or a function"); } subClass.prototype = Object.create(superClass && superClass.prototype, { constructor: { value: subClass, writable: true, configurable: true } }); if (superClass) _setPrototypeOf(subClass, superClass); } function _setPrototypeOf(o, p) { _setPrototypeOf = Object.setPrototypeOf || function _setPrototypeOf(o, p) { o.__proto__ = p; return o; }; return _setPrototypeOf(o, p); } import React, { Component } from 'react'; import Piece from './Piece'; import Square from './Square'; import Notation from './Notation'; import Shogiboard from './index'; import PhantomPiece from './PhantomPiece'; import Row from './Row'; var Board = /*#__PURE__*/ function (_Component) { _inherits(Board, _Component); function Board() { var _getPrototypeOf2; var _temp, _this; _classCallCheck(this, Board); for (var _len = arguments.length, args = new Array(_len), _key = 0; _key < _len; _key++) { args[_key] = arguments[_key]; } return _possibleConstructorReturn(_this, (_temp = _this = _possibleConstructorReturn(this, (_getPrototypeOf2 = _getPrototypeOf(Board)).call.apply(_getPrototypeOf2, [this].concat(args))), _this.setSquareCoordinates = function (x, y, square) { return _this.setState(_defineProperty({}, square, { x: x, y: y })); }, _this.getSquareCoordinates = function (sourceSquare, targetSquare) { return { sourceSquare: _this.state[sourceSquare], targetSquare: _this.state[targetSquare] }; }, _this.showPhantom = function (_ref) { var square = _ref.square, targetSquare = _ref.targetSquare, phantomPiece = _ref.phantomPiece; var isActivePiece = function isActivePiece(square, targetSquare) { return targetSquare && targetSquare === square; }; return phantomPiece && phantomPiece[targetSquare] && isActivePiece(square, targetSquare); }, _this.hasPiece = function (currentPosition, square) { return currentPosition && Object.keys(currentPosition) && Object.keys(currentPosition).includes(square); }, _temp)); } _createClass(Board, [{ key: "render", value: function render() { var _this2 = this; return React.createElement(Shogiboard.Consumer, null, function (context) { return React.createElement(Row, { width: context.width, boardStyle: context.boardStyle, orientation: context.orientation, boardId: context.id }, function (_ref2) { var square = _ref2.square, squareColor = _ref2.squareColor, col = _ref2.col, row = _ref2.row, alpha = _ref2.alpha; return React.createElement(Square, { key: "".concat(col).concat(row), width: context.width, square: square, squareColor: squareColor, setSquareCoordinates: _this2.setSquareCoordinates, lightSquareStyle: context.lightSquareStyle, darkSquareStyle: context.darkSquareStyle, roughSquare: context.roughSquare, onMouseOverSquare: context.onMouseOverSquare, onMouseOutSquare: context.onMouseOutSquare, onDragOverSquare: context.onDragOverSquare, dropSquareStyle: context.dropSquareStyle, id: context.id, screenWidth: context.screenWidth, screenHeight: context.screenHeight, squareStyles: context.squareStyles, onSquareClick: context.onSquareClick, onSquareRightClick: context.onSquareRightClick, wasSquareClicked: context.wasSquareClicked }, _this2.hasPiece(context.currentPosition, square) ? React.createElement(Piece, { pieces: context.pieces, square: square, piece: context.currentPosition[square], width: context.width, setPosition: context.setPosition, dropOffBoard: context.dropOffBoard, getSquareCoordinates: _this2.getSquareCoordinates, draggable: context.draggable, onDrop: context.onDrop, sourceSquare: context.sourceSquare, targetSquare: context.targetSquare, waitForTransition: context.waitForTransition, transitionDuration: context.transitionDuration, orientation: context.orientation, id: context.id, setTouchState: context.setTouchState, wasManuallyDropped: context.wasManuallyDropped, phantomPiece: context.phantomPiece, onPieceClick: context.onPieceClick, wasSquareClicked: context.wasSquareClicked, allowDrag: context.allowDrag }) : null, _this2.showPhantom({ square: square, targetSquare: context.targetSquare, phantomPiece: context.phantomPiece }) && React.createElement(PhantomPiece, { width: context.width, phantomPieceValue: context.phantomPiece[context.targetSquare], pieces: context.pieces, showNotation: context.showNotation, allowDrag: context.allowDrag }), context.showNotation && React.createElement(Notation, { row: row, col: col, alpha: alpha, orientation: context.orientation, width: context.width, lightSquareStyle: context.lightSquareStyle, darkSquareStyle: context.darkSquareStyle })); }); }); } }]); return Board; }(Component); export default Board;