cm-chessboard 3.17.6 → 3.17.9

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -44,6 +44,7 @@ function inputHandler(event) {
44
44
  position: "start",
45
45
  sprite: {url: "../assets/images/chessboard-sprite-staunty.svg"}
46
46
  })
47
+
47
48
  window.board.enableMoveInput(inputHandler)
48
49
  function inputHandler(event) {
49
50
  console.log(event)
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "cm-chessboard",
3
- "version": "3.17.6",
3
+ "version": "3.17.9",
4
4
  "description": "A JavaScript chessboard which is lightweight, ES6 module based, responsive, SVG rendered and without dependencies.",
5
5
  "keywords": [
6
6
  "chess",
@@ -41,11 +41,12 @@ export const FEN_EMPTY_POSITION = "8/8/8/8/8/8/8/8"
41
41
 
42
42
  export class Chessboard {
43
43
 
44
- constructor(element, props = {}) { // TODO rename element to context
45
- if (!element) {
46
- throw new Error("container element is " + element)
44
+ constructor(context, props = {}) {
45
+ if (!context) {
46
+ throw new Error("container element is " + context)
47
47
  }
48
- this.element = element
48
+ this.element = context
49
+ this.id = (Math.random() + 1).toString(36).substr(2, 6)
49
50
  let defaultProps = {
50
51
  position: "empty", // set as fen, "start" or "empty"
51
52
  orientation: COLOR.white, // white on bottom
@@ -57,8 +58,6 @@ export class Chessboard {
57
58
  aspectRatio: 1, // height/width. Set to `undefined`, if you want to define it only in the css.
58
59
  moveFromMarker: MARKER_TYPE.frame, // the marker used to mark the start square
59
60
  moveToMarker: MARKER_TYPE.frame, // the marker used to mark the square where the figure is moving to
60
- moveMarker: MARKER_TYPE.frame, // deprecated => moveFromMarker // TODO remove in future
61
- hoverMarker: MARKER_TYPE.frame // deprecated => moveToMarker // TODO remove in future
62
61
  },
63
62
  responsive: true, // resizes the board based on element size
64
63
  animationDuration: 300, // pieces animation duration in milliseconds
@@ -79,31 +78,20 @@ export class Chessboard {
79
78
  if (props.style) {
80
79
  Object.assign(this.props.style, props.style)
81
80
  }
82
- if(this.props.style.moveMarker !== MARKER_TYPE.frame) { // TODO remove in future
83
- console.warn("this.props.style.moveMarker is deprecated, use this.props.style.moveFromMarker")
84
- this.props.style.moveFromMarker = this.props.style.moveMarker
85
- }
86
- if(this.props.style.hoverMarker !== MARKER_TYPE.frame) { // TODO remove in future
87
- console.warn("this.props.style.hoverMarker is deprecated, use this.props.style.moveToMarker")
88
- this.props.style.moveToMarker = this.props.style.hoverMarker
89
- }
90
81
  if (this.props.style.aspectRatio) {
91
82
  this.element.style.height = (this.element.offsetWidth * this.props.style.aspectRatio) + "px"
92
83
  }
93
84
  this.state = new ChessboardState()
85
+ if (this.props.position === "start") {
86
+ this.state.setPosition(FEN_START_POSITION)
87
+ } else if (this.props.position === "empty" || this.props.position === undefined) {
88
+ this.state.setPosition(FEN_EMPTY_POSITION)
89
+ } else {
90
+ this.state.setPosition(this.props.position)
91
+ }
94
92
  this.state.orientation = this.props.orientation
95
-
96
93
  const viewType = this.props.accessible ? ChessboardViewAccessible : ChessboardView
97
- this.view = new viewType(this, (view) => {
98
- if (this.props.position === "start") {
99
- this.state.setPosition(FEN_START_POSITION)
100
- } else if (this.props.position === "empty" || this.props.position === undefined) {
101
- this.state.setPosition(FEN_EMPTY_POSITION)
102
- } else {
103
- this.state.setPosition(this.props.position)
104
- }
105
- view.redraw()
106
- })
94
+ this.view = new viewType(this)
107
95
  }
108
96
 
109
97
  // API //
@@ -264,7 +252,7 @@ export class Chessboard {
264
252
  this.element.addEventListener("mouseup", this.squareSelectListener)
265
253
  this.element.addEventListener("touchend", this.squareSelectListener)
266
254
  this.state.squareSelectEnabled = true
267
- this.view.setCursor()
255
+ this.view.visualizeInputState()
268
256
  }
269
257
 
270
258
  disableSquareSelect() {
@@ -273,7 +261,7 @@ export class Chessboard {
273
261
  this.element.removeEventListener("touchend", this.squareSelectListener)
274
262
  this.squareSelectListener = undefined
275
263
  this.state.squareSelectEnabled = false
276
- this.view.setCursor()
264
+ this.view.visualizeInputState()
277
265
  }
278
266
 
279
267
  }
@@ -60,7 +60,7 @@ export const SQUARE_COORDINATES = [
60
60
 
61
61
  export class ChessboardView {
62
62
 
63
- constructor(chessboard, callbackAfterCreation) {
63
+ constructor(chessboard) {
64
64
  this.animationRunning = false
65
65
  this.currentAnimation = undefined
66
66
  this.chessboard = chessboard
@@ -93,10 +93,10 @@ export class ChessboardView {
93
93
 
94
94
  this.createSvgAndGroups()
95
95
  this.updateMetrics()
96
- callbackAfterCreation(this)
97
96
  if (chessboard.props.responsive) {
98
97
  this.handleResize()
99
98
  }
99
+ this.redraw()
100
100
  }
101
101
 
102
102
  pointerDownHandler(e) {
@@ -194,7 +194,7 @@ export class ChessboardView {
194
194
  this.drawBoard()
195
195
  this.drawCoordinates()
196
196
  this.drawMarkers()
197
- this.setCursor()
197
+ this.visualizeInputState()
198
198
  this.drawPieces(this.chessboard.state.squares)
199
199
  }
200
200
 
@@ -413,7 +413,7 @@ export class ChessboardView {
413
413
  }
414
414
  this.chessboard.state.inputEnabled = true
415
415
  this.moveInputCallback = eventHandler
416
- this.setCursor()
416
+ this.visualizeInputState()
417
417
  }
418
418
 
419
419
  disableMoveInput() {
@@ -421,7 +421,7 @@ export class ChessboardView {
421
421
  this.chessboard.state.inputBlackEnabled = false
422
422
  this.chessboard.state.inputEnabled = false
423
423
  this.moveInputCallback = undefined
424
- this.setCursor()
424
+ this.visualizeInputState()
425
425
  }
426
426
 
427
427
  // callbacks //
@@ -465,7 +465,7 @@ export class ChessboardView {
465
465
 
466
466
  // Helpers //
467
467
 
468
- setCursor() {
468
+ visualizeInputState() {
469
469
  if (this.chessboard.state) { // fix https://github.com/shaack/cm-chessboard/issues/47
470
470
  if (this.chessboard.state.inputWhiteEnabled || this.chessboard.state.inputBlackEnabled || this.chessboard.state.squareSelectEnabled) {
471
471
  this.boardGroup.setAttribute("class", "board input-enabled")
@@ -7,17 +7,28 @@ import {ChessboardView, renderPieceTitle} from "./ChessboardView.js"
7
7
  import {COLOR} from "./Chessboard.js"
8
8
  import {piecesTranslations} from "./ChessboardView.js"
9
9
 
10
- let count = 0
11
-
12
-
13
10
  const hlTranslations = {
14
- en: {
15
- pieces_lists: "Pieces lists",
16
- board_as_table: "Chessboard as table"
17
- },
18
11
  de: {
19
12
  pieces_lists: "Figurenlisten",
20
- board_as_table: "Schachbrett als Tabelle"
13
+ board_as_table: "Schachbrett als Tabelle",
14
+ move_piece: "Figur bewegen",
15
+ from: "von",
16
+ to: "nach",
17
+ move: "bewegen",
18
+ input_white_enabled: "Eingabe Weiß aktiviert",
19
+ input_black_enabled: "Eingabe Schwarz aktiviert",
20
+ input_disabled: "Eingabe deaktiviert"
21
+ },
22
+ en: {
23
+ pieces_lists: "Pieces lists",
24
+ board_as_table: "Chessboard as table",
25
+ move_piece: "Move piece",
26
+ from: "from",
27
+ to: "to",
28
+ move: "move",
29
+ input_white_enabled: "Input white enabled",
30
+ input_black_enabled: "Input black enabled",
31
+ input_disabled: "Input disabled"
21
32
  }
22
33
  }
23
34
 
@@ -32,12 +43,50 @@ export class ChessboardViewAccessible extends ChessboardView {
32
43
  this.translations = piecesTranslations
33
44
  this.t = this.translations[this.lang]
34
45
  this.th = hlTranslations[this.lang]
35
- this.piecesListContainer = this.createElement(`<div class="cm-chessboard-content visually-hidden"><h3>${this.th.pieces_lists}</h3><div class="list"></div></div>`)
46
+
47
+ this.accessibleContainer = this.createElement(`<div class="cm-chessboard-content visually-hidden"></div>`)
48
+
49
+ this.movePieceFormContainer = this.createElement(`<div><h3>${this.th.move_piece}</h3><form>
50
+ <label for="move_piece_input_from_${chessboard.id}">${this.th.from}</label><input class="input-from" type="text" size="2" id="move_piece_input_from_${chessboard.id}"/>
51
+ <label for="move_piece_input_to_${chessboard.id}">${this.th.to}</label><input class="input-to" type="text" size="2" id="move_piece_input_to_${chessboard.id}"/>
52
+ <button type="button" class="button-move">${this.th.move}</button>
53
+ </form><div class="input-status" aria-live="polite"></div></div>`)
54
+ this.inputFrom = this.movePieceFormContainer.querySelector(".input-from")
55
+ this.inputTo = this.movePieceFormContainer.querySelector(".input-to")
56
+ this.moveButton = this.movePieceFormContainer.querySelector(".button-move")
57
+ this.moveButton.addEventListener("click", () => {
58
+ this.chessboard.movePiece(this.inputFrom.value, this.inputTo.value, true)
59
+ })
60
+ this.accessibleContainer.appendChild(this.movePieceFormContainer)
61
+
62
+ this.piecesListContainer = this.createElement(`<div><h3>${this.th.pieces_lists}</h3><div class="list"></div></div>`)
36
63
  this.piecesList = this.piecesListContainer.querySelector(".list")
37
- this.chessboard.element.appendChild(this.piecesListContainer)
38
- this.boardAsTableContainer = this.createElement(`<div class="cm-chessboard-content visually-hidden"><h3>${this.th.board_as_table}</h3><div class="table"></div></div>`)
64
+ this.accessibleContainer.appendChild(this.piecesListContainer)
65
+
66
+ this.boardAsTableContainer = this.createElement(`<div><h3>${this.th.board_as_table}</h3><div class="table"></div></div>`)
39
67
  this.boardAsTable = this.boardAsTableContainer.querySelector(".table")
40
- this.chessboard.element.appendChild(this.boardAsTableContainer)
68
+ this.accessibleContainer.appendChild(this.boardAsTableContainer)
69
+ this.chessboard.element.appendChild(this.accessibleContainer)
70
+ this.updateFormInputs()
71
+ }
72
+
73
+ visualizeInputState() {
74
+ super.visualizeInputState()
75
+ this.updateFormInputs()
76
+ }
77
+
78
+ updateFormInputs() {
79
+ if (this.inputFrom) {
80
+ if (this.chessboard.state.inputWhiteEnabled || this.chessboard.state.inputBlackEnabled) {
81
+ this.inputFrom.disabled = false
82
+ this.inputTo.disabled = false
83
+ this.moveButton.disabled = false
84
+ } else {
85
+ this.inputFrom.disabled = true
86
+ this.inputTo.disabled = true
87
+ this.moveButton.disabled = true
88
+ }
89
+ }
41
90
  }
42
91
 
43
92
  drawPieces(squares = this.chessboard.state.squares) {
@@ -59,12 +108,11 @@ export class ChessboardViewAccessible extends ChessboardView {
59
108
  listB += `<li class="list-inline-item">${renderPieceTitle(this.lang, piece.name)} ${piece.position}</li>`
60
109
  }
61
110
  }
62
- count++
63
111
  this.piecesList.innerHTML = `
64
- <h4 id="white-${count}">${this.t.colors_long.w}</h4>
65
- <ul aria-labelledby="white-${count}" class="list-inline">${listW}</ul>
66
- <h4 id="black-${count}">${this.t.colors_long.b}</h4>
67
- <ul aria-labelledby="black-${count}" class="list-inline">${listB}</ul>`
112
+ <h4 id="white_${this.chessboard.id}">${this.t.colors_long.w}</h4>
113
+ <ul aria-labelledby="white_${this.chessboard.id}" class="list-inline">${listW}</ul>
114
+ <h4 id="black_${this.chessboard.id}">${this.t.colors_long.b}</h4>
115
+ <ul aria-labelledby="black_${this.chessboard.id}" class="list-inline">${listB}</ul>`
68
116
  }
69
117
 
70
118
  redrawBoardAsTable() {
@@ -76,7 +124,7 @@ export class ChessboardViewAccessible extends ChessboardView {
76
124
  files.reverse()
77
125
  squares.reverse()
78
126
  }
79
- let html = `<table><caption>${this.t.board_as_table}</caption><tr><th></th>`
127
+ let html = `<table><caption>${this.th.board_as_table}</caption><tr><th></th>`
80
128
  for (const rank of ranks) {
81
129
  html += `<th scope='col'>${rank}</th>`
82
130
  }