@alepot55/chessboardjs 2.1.7 → 2.2.0
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.
- package/chessboard.bundle.js +18 -9
- package/chessboard.config.js +7 -5
- package/chessboard.js +71 -43
- package/chessboard.move.js +9 -21
- package/chessboard.piece.js +1 -1
- package/package.json +1 -1
package/chessboard.bundle.js
CHANGED
|
@@ -2587,6 +2587,7 @@ var Chessboard = (function () {
|
|
|
2587
2587
|
// -------------------
|
|
2588
2588
|
convertFen(position) {
|
|
2589
2589
|
if (typeof position === 'string') {
|
|
2590
|
+
|
|
2590
2591
|
if (this.validateFen(position)) return position;
|
|
2591
2592
|
else if (this.standard_positions[position]) return this.standard_positions[position];
|
|
2592
2593
|
else throw new Error('Invalid position -' + position);
|
|
@@ -2620,8 +2621,10 @@ var Chessboard = (function () {
|
|
|
2620
2621
|
}
|
|
2621
2622
|
|
|
2622
2623
|
setGame(position) {
|
|
2624
|
+
console.log(position);
|
|
2623
2625
|
const fen = this.convertFen(position);
|
|
2624
|
-
|
|
2626
|
+
console.log(fen);
|
|
2627
|
+
this.game = new Chess(fen === 'start' ? this.standard_positions['default'] : null);
|
|
2625
2628
|
}
|
|
2626
2629
|
|
|
2627
2630
|
// -------------------
|
|
@@ -2816,11 +2819,9 @@ var Chessboard = (function () {
|
|
|
2816
2819
|
updateSinglePiece(square, newPiece, updatedFlags, animation) {
|
|
2817
2820
|
if (!updatedFlags[square.id]) {
|
|
2818
2821
|
let lastMove = this.lastMove();
|
|
2819
|
-
console.log(lastMove);
|
|
2820
2822
|
|
|
2821
2823
|
if (lastMove?.promotion) {
|
|
2822
2824
|
if (lastMove['to'] === square.id) {
|
|
2823
|
-
console.log('Promotion detected');
|
|
2824
2825
|
|
|
2825
2826
|
let move = new Move(this.squares[lastMove['from']], square);
|
|
2826
2827
|
this.translatePiece(move, true, animation
|
|
@@ -3214,17 +3215,23 @@ var Chessboard = (function () {
|
|
|
3214
3215
|
return moves[moves.length - 1];
|
|
3215
3216
|
}
|
|
3216
3217
|
|
|
3217
|
-
flip(
|
|
3218
|
-
this.
|
|
3219
|
-
this.
|
|
3220
|
-
this.
|
|
3218
|
+
flip() {
|
|
3219
|
+
this.config.orientation = this.config.orientation === 'w' ? 'b' : 'w';
|
|
3220
|
+
this.destroy();
|
|
3221
|
+
this.initParams();
|
|
3222
|
+
this.build();
|
|
3221
3223
|
}
|
|
3222
3224
|
|
|
3223
3225
|
build() {
|
|
3224
|
-
if (this.board)
|
|
3226
|
+
if (this.board) this.destroy();
|
|
3225
3227
|
this.init();
|
|
3226
3228
|
}
|
|
3227
3229
|
|
|
3230
|
+
destroy() {
|
|
3231
|
+
this.removeSquares();
|
|
3232
|
+
this.removeBoard();
|
|
3233
|
+
}
|
|
3234
|
+
|
|
3228
3235
|
ascii() {
|
|
3229
3236
|
return this.game.ascii();
|
|
3230
3237
|
}
|
|
@@ -3317,7 +3324,8 @@ var Chessboard = (function () {
|
|
|
3317
3324
|
}
|
|
3318
3325
|
|
|
3319
3326
|
put(pieceId, squareId, animation = true) {
|
|
3320
|
-
const
|
|
3327
|
+
const [type, color] = pieceId.split('');
|
|
3328
|
+
const success = this.game.put({ type: type, color: color }, squareId);
|
|
3321
3329
|
if (success) this.updateBoardPieces(animation);
|
|
3322
3330
|
return success;
|
|
3323
3331
|
}
|
|
@@ -3397,4 +3405,5 @@ var Chessboard = (function () {
|
|
|
3397
3405
|
return Chessboard;
|
|
3398
3406
|
|
|
3399
3407
|
})();
|
|
3408
|
+
|
|
3400
3409
|
window.Chessboard.Chessboard = Chessboard;
|
package/chessboard.config.js
CHANGED
|
@@ -19,7 +19,8 @@ const transitionFunctions = {
|
|
|
19
19
|
'linear': 'linear',
|
|
20
20
|
'ease-in': 'ease-in',
|
|
21
21
|
'ease-out': 'ease-out',
|
|
22
|
-
'ease-in-out': 'ease-in-out'
|
|
22
|
+
'ease-in-out': 'ease-in-out',
|
|
23
|
+
'none': null
|
|
23
24
|
};
|
|
24
25
|
|
|
25
26
|
class ChessboardConfig {
|
|
@@ -73,9 +74,6 @@ class ChessboardConfig {
|
|
|
73
74
|
this.dropOffBoard = config.dropOffBoard;
|
|
74
75
|
this.size = config.size;
|
|
75
76
|
this.movableColors = config.movableColors;
|
|
76
|
-
this.moveAnimation = config.moveAnimation;
|
|
77
|
-
this.snapbackAnimation = config.snapbackAnimation;
|
|
78
|
-
this.fadeAnimation = config.fadeAnimation;
|
|
79
77
|
this.piecesPath = config.piecesPath;
|
|
80
78
|
this.onMove = config.onMove;
|
|
81
79
|
this.onMoveEnd = config.onMoveEnd;
|
|
@@ -85,6 +83,10 @@ class ChessboardConfig {
|
|
|
85
83
|
this.onDrop = config.onDrop;
|
|
86
84
|
this.onSnapbackEnd = config.onSnapbackEnd;
|
|
87
85
|
|
|
86
|
+
this.moveAnimation = this.setTransitionFunction(config.moveAnimation);
|
|
87
|
+
this.snapbackAnimation = this.setTransitionFunction(config.snapbackAnimation);
|
|
88
|
+
this.fadeAnimation = this.setTransitionFunction(config.fadeAnimation);
|
|
89
|
+
|
|
88
90
|
this.hints = this.setBoolean(config.hints);
|
|
89
91
|
this.clickable = this.setBoolean(config.clickable);
|
|
90
92
|
this.draggable = this.setBoolean(config.draggable);
|
|
@@ -137,7 +139,7 @@ class ChessboardConfig {
|
|
|
137
139
|
}
|
|
138
140
|
|
|
139
141
|
setTransitionFunction(value) {
|
|
140
|
-
if (transitionFunctions
|
|
142
|
+
if (Object.keys(transitionFunctions).indexOf(value) !== -1) return transitionFunctions[value];
|
|
141
143
|
throw new Error('Invalid transition function');
|
|
142
144
|
}
|
|
143
145
|
}
|
package/chessboard.js
CHANGED
|
@@ -65,7 +65,7 @@ class Chessboard {
|
|
|
65
65
|
}
|
|
66
66
|
|
|
67
67
|
initParams() {
|
|
68
|
-
this.
|
|
68
|
+
this.element = null;
|
|
69
69
|
this.squares = {};
|
|
70
70
|
this.promoting = false;
|
|
71
71
|
this.clicked = null;
|
|
@@ -77,12 +77,12 @@ class Chessboard {
|
|
|
77
77
|
// Board Setup
|
|
78
78
|
// -------------------
|
|
79
79
|
buildBoard() {
|
|
80
|
-
this.
|
|
81
|
-
if (!this.
|
|
80
|
+
this.element = document.getElementById(this.config.id_div);
|
|
81
|
+
if (!this.element) {
|
|
82
82
|
throw new Error(this.error_messages['invalid_id_div'] + this.config.id_div);
|
|
83
83
|
}
|
|
84
84
|
this.resize(this.config.size);
|
|
85
|
-
this.
|
|
85
|
+
this.element.className = "board";
|
|
86
86
|
}
|
|
87
87
|
|
|
88
88
|
buildSquares() {
|
|
@@ -94,19 +94,19 @@ class Chessboard {
|
|
|
94
94
|
let square = new Square(square_row, square_col);
|
|
95
95
|
this.squares[square.getId()] = square;
|
|
96
96
|
|
|
97
|
-
this.
|
|
97
|
+
this.element.appendChild(square.element);
|
|
98
98
|
}
|
|
99
99
|
}
|
|
100
100
|
}
|
|
101
101
|
|
|
102
102
|
removeBoard() {
|
|
103
103
|
|
|
104
|
-
this.
|
|
104
|
+
this.element.innerHTML = '';
|
|
105
105
|
}
|
|
106
106
|
|
|
107
107
|
removeSquares() {
|
|
108
108
|
for (const square of Object.values(this.squares)) {
|
|
109
|
-
this.
|
|
109
|
+
this.element.removeChild(square.element);
|
|
110
110
|
square.destroy();
|
|
111
111
|
|
|
112
112
|
}
|
|
@@ -116,12 +116,12 @@ class Chessboard {
|
|
|
116
116
|
resize(value) {
|
|
117
117
|
if (value === 'auto') {
|
|
118
118
|
let size;
|
|
119
|
-
if (this.
|
|
120
|
-
size = this.
|
|
121
|
-
} else if (this.
|
|
122
|
-
size = this.
|
|
119
|
+
if (this.element.offsetWidth === 0) {
|
|
120
|
+
size = this.element.offsetHeight;
|
|
121
|
+
} else if (this.element.offsetHeight === 0) {
|
|
122
|
+
size = this.element.offsetWidth;
|
|
123
123
|
} else {
|
|
124
|
-
size = Math.min(this.
|
|
124
|
+
size = Math.min(this.element.offsetWidth, this.element.offsetHeight);
|
|
125
125
|
}
|
|
126
126
|
this.resize(size);
|
|
127
127
|
} else if (typeof value !== 'number') {
|
|
@@ -137,7 +137,7 @@ class Chessboard {
|
|
|
137
137
|
// -------------------
|
|
138
138
|
convertFen(position) {
|
|
139
139
|
if (typeof position === 'string') {
|
|
140
|
-
|
|
140
|
+
if (position == 'start') return 'rnbqkbnr/pppppppp/8/8/8/8/PPPPPPPP/RNBQKBNR w KQkq - 0 1';
|
|
141
141
|
if (this.validateFen(position)) return position;
|
|
142
142
|
else if (this.standard_positions[position]) return this.standard_positions[position];
|
|
143
143
|
else throw new Error('Invalid position -' + position);
|
|
@@ -164,15 +164,17 @@ class Chessboard {
|
|
|
164
164
|
if (empty > 0) rowParts.push(empty);
|
|
165
165
|
parts.push(rowParts.join(''));
|
|
166
166
|
}
|
|
167
|
-
return parts.join('/')
|
|
167
|
+
return parts.join('/') + ' w KQkq - 0 1';;
|
|
168
168
|
} else {
|
|
169
169
|
throw new Error('Invalid position -' + position);
|
|
170
170
|
}
|
|
171
171
|
}
|
|
172
172
|
|
|
173
|
-
setGame(position) {
|
|
173
|
+
setGame(position, options = undefined) {
|
|
174
174
|
const fen = this.convertFen(position);
|
|
175
|
-
|
|
175
|
+
console.log(fen);
|
|
176
|
+
if (this.game) this.game.load(fen, options);
|
|
177
|
+
else this.game = new Chess(fen);
|
|
176
178
|
}
|
|
177
179
|
|
|
178
180
|
// -------------------
|
|
@@ -252,7 +254,7 @@ class Chessboard {
|
|
|
252
254
|
|
|
253
255
|
}
|
|
254
256
|
|
|
255
|
-
snapbackPiece(square, animate) {
|
|
257
|
+
snapbackPiece(square, animate = this.config.snapbackAnimation) {
|
|
256
258
|
let move = new Move(square, square);
|
|
257
259
|
this.translatePiece(move, false, animate);
|
|
258
260
|
}
|
|
@@ -263,11 +265,15 @@ class Chessboard {
|
|
|
263
265
|
updateBoardPieces(animation = false) {
|
|
264
266
|
let { updatedFlags, escapeFlags, movableFlags, pendingTranslations } = this.prepareBoardUpdateData();
|
|
265
267
|
|
|
268
|
+
let change = Object.values(updatedFlags).some(flag => !flag);
|
|
269
|
+
|
|
266
270
|
this.identifyPieceTranslations(updatedFlags, escapeFlags, movableFlags, pendingTranslations);
|
|
267
271
|
|
|
268
272
|
this.executePieceTranslations(pendingTranslations, escapeFlags, animation);
|
|
269
273
|
|
|
270
274
|
this.processRemainingPieceUpdates(updatedFlags, animation);
|
|
275
|
+
|
|
276
|
+
if (change) this.config.onChange(this.fen());
|
|
271
277
|
}
|
|
272
278
|
|
|
273
279
|
prepareBoardUpdateData() {
|
|
@@ -395,9 +401,8 @@ class Chessboard {
|
|
|
395
401
|
event.preventDefault();
|
|
396
402
|
|
|
397
403
|
if (!this.config.draggable || !piece) return;
|
|
398
|
-
if (!this.config.onDragStart(square, piece)) return;
|
|
399
404
|
|
|
400
|
-
let prec;
|
|
405
|
+
let prec, moved;
|
|
401
406
|
let from = square;
|
|
402
407
|
let to = square;
|
|
403
408
|
|
|
@@ -405,7 +410,7 @@ class Chessboard {
|
|
|
405
410
|
|
|
406
411
|
if (!this.canMove(from)) return;
|
|
407
412
|
if (!this.config.clickable) this.clicked = null;
|
|
408
|
-
if (this.onClick(from)) return;
|
|
413
|
+
if (this.onClick(from, true, true)) return;
|
|
409
414
|
|
|
410
415
|
img.style.position = 'absolute';
|
|
411
416
|
img.style.zIndex = 100;
|
|
@@ -419,10 +424,11 @@ class Chessboard {
|
|
|
419
424
|
};
|
|
420
425
|
|
|
421
426
|
const onMouseMove = (event) => {
|
|
427
|
+
if (!this.config.onDragStart(square, piece)) return;
|
|
422
428
|
if (!moveAt(event.pageX, event.pageY)) return;
|
|
423
429
|
|
|
424
|
-
const boardRect = this.
|
|
425
|
-
const { offsetWidth: boardWidth, offsetHeight: boardHeight } = this.
|
|
430
|
+
const boardRect = this.element.getBoundingClientRect();
|
|
431
|
+
const { offsetWidth: boardWidth, offsetHeight: boardHeight } = this.element;
|
|
426
432
|
const x = event.clientX - boardRect.left;
|
|
427
433
|
const y = event.clientY - boardRect.top;
|
|
428
434
|
|
|
@@ -457,9 +463,9 @@ class Chessboard {
|
|
|
457
463
|
this.allSquares('removeHint');
|
|
458
464
|
from.deselect();
|
|
459
465
|
this.remove(from);
|
|
460
|
-
} else if (!to || !this.onClick(to, true)) {
|
|
461
|
-
this.snapbackPiece(from
|
|
462
|
-
this.config.onSnapbackEnd(from, piece);
|
|
466
|
+
} else if (!to || !this.onClick(to, true, true)) {
|
|
467
|
+
this.snapbackPiece(from);
|
|
468
|
+
if (to !== from) this.config.onSnapbackEnd(from, piece);
|
|
463
469
|
}
|
|
464
470
|
};
|
|
465
471
|
|
|
@@ -486,14 +492,14 @@ class Chessboard {
|
|
|
486
492
|
if (this.config.clickable && (!piece || this.config.onlyLegalMoves)) this.onClick(square)
|
|
487
493
|
}
|
|
488
494
|
|
|
489
|
-
square.element.addEventListener("
|
|
495
|
+
square.element.addEventListener("mousedown", handleClick);
|
|
490
496
|
square.element.addEventListener("touch", handleClick);
|
|
491
497
|
}
|
|
492
498
|
}
|
|
493
499
|
|
|
494
|
-
onClick(square, animation = this.config.moveAnimation) {
|
|
495
|
-
|
|
496
|
-
if (
|
|
500
|
+
onClick(square, animation = this.config.moveAnimation, dragged = false) {
|
|
501
|
+
|
|
502
|
+
if (this.clicked === square) return false;
|
|
497
503
|
|
|
498
504
|
let from = this.clicked;
|
|
499
505
|
this.clicked = null;
|
|
@@ -512,19 +518,22 @@ class Chessboard {
|
|
|
512
518
|
if (!from) {
|
|
513
519
|
|
|
514
520
|
if (this.canMove(square)) {
|
|
515
|
-
|
|
516
|
-
|
|
521
|
+
if (this.config.clickable) {
|
|
522
|
+
square.select();
|
|
523
|
+
this.hintMoves(square);
|
|
524
|
+
}
|
|
517
525
|
this.clicked = square;
|
|
518
526
|
}
|
|
519
527
|
|
|
520
528
|
return false;
|
|
521
529
|
}
|
|
522
530
|
|
|
523
|
-
if (!this.canMove(from)) return false;
|
|
524
|
-
|
|
525
531
|
let move = new Move(from, square, promotion);
|
|
526
532
|
|
|
527
533
|
move.from.deselect();
|
|
534
|
+
|
|
535
|
+
if (!move.check()) return false;
|
|
536
|
+
|
|
528
537
|
this.allSquares("removeHint");
|
|
529
538
|
|
|
530
539
|
if (this.config.onlyLegalMoves && !move.isLegal(this.game)) return false;
|
|
@@ -579,7 +588,7 @@ class Chessboard {
|
|
|
579
588
|
return this.game.moves({ verbose: verb });
|
|
580
589
|
}
|
|
581
590
|
|
|
582
|
-
move(move, animation) {
|
|
591
|
+
move(move, animation = true) {
|
|
583
592
|
move = this.convertMove(move);
|
|
584
593
|
move.check();
|
|
585
594
|
|
|
@@ -750,20 +759,34 @@ class Chessboard {
|
|
|
750
759
|
}
|
|
751
760
|
|
|
752
761
|
setOrientation(color, animation = true) {
|
|
753
|
-
if (
|
|
754
|
-
this.config.orientation
|
|
755
|
-
|
|
762
|
+
if (['w', 'b'].includes(color)) {
|
|
763
|
+
if (color !== this.config.orientation) {
|
|
764
|
+
this.flip(animation);
|
|
765
|
+
}
|
|
756
766
|
} else {
|
|
757
767
|
throw new Error(this.error_messages['invalid_orientation'] + color);
|
|
758
768
|
}
|
|
759
769
|
}
|
|
760
770
|
|
|
771
|
+
highlight(squareId) {
|
|
772
|
+
let square = this.convertSquare(squareId);
|
|
773
|
+
square.check();
|
|
774
|
+
square.highlight();
|
|
775
|
+
}
|
|
776
|
+
|
|
777
|
+
dehighlight(squareId) {
|
|
778
|
+
let square = this.convertSquare(squareId);
|
|
779
|
+
square.check();
|
|
780
|
+
square.dehighlight();
|
|
781
|
+
}
|
|
782
|
+
|
|
761
783
|
lastMove() {
|
|
762
784
|
const moves = this.history({ verbose: true });
|
|
763
785
|
return moves[moves.length - 1];
|
|
764
786
|
}
|
|
765
787
|
|
|
766
788
|
flip() {
|
|
789
|
+
console.log(this.config.position);
|
|
767
790
|
this.config.orientation = this.config.orientation === 'w' ? 'b' : 'w';
|
|
768
791
|
this.destroy();
|
|
769
792
|
this.initParams();
|
|
@@ -771,7 +794,7 @@ class Chessboard {
|
|
|
771
794
|
}
|
|
772
795
|
|
|
773
796
|
build() {
|
|
774
|
-
if (this.
|
|
797
|
+
if (this.element) this.destroy();
|
|
775
798
|
this.init();
|
|
776
799
|
}
|
|
777
800
|
|
|
@@ -785,7 +808,12 @@ class Chessboard {
|
|
|
785
808
|
}
|
|
786
809
|
|
|
787
810
|
board() {
|
|
788
|
-
|
|
811
|
+
let dict = {};
|
|
812
|
+
for (let squareId in this.squares) {
|
|
813
|
+
let piece = this.getGamePieceId(squareId);
|
|
814
|
+
if (piece) dict[squareId] = piece;
|
|
815
|
+
}
|
|
816
|
+
return dict;
|
|
789
817
|
}
|
|
790
818
|
|
|
791
819
|
clear(options = {}, animation = true) {
|
|
@@ -800,7 +828,7 @@ class Chessboard {
|
|
|
800
828
|
get(squareId) {
|
|
801
829
|
const square = this.convertSquare(squareId);
|
|
802
830
|
square.check();
|
|
803
|
-
return square.piece;
|
|
831
|
+
return square.piece;
|
|
804
832
|
}
|
|
805
833
|
|
|
806
834
|
getCastlingRights(color) {
|
|
@@ -847,9 +875,9 @@ class Chessboard {
|
|
|
847
875
|
return this.game.isThreefoldRepetition();
|
|
848
876
|
}
|
|
849
877
|
|
|
850
|
-
load(
|
|
878
|
+
load(position, options = {}, animation = true) {
|
|
851
879
|
this.clearSquares();
|
|
852
|
-
this.
|
|
880
|
+
this.setGame(position, options);
|
|
853
881
|
this.updateBoardPieces(animation);
|
|
854
882
|
}
|
|
855
883
|
|
|
@@ -912,7 +940,7 @@ class Chessboard {
|
|
|
912
940
|
setHeader(key, value) {
|
|
913
941
|
return this.game.setHeader(key, value);
|
|
914
942
|
}
|
|
915
|
-
|
|
943
|
+
|
|
916
944
|
squareColor(squareId) {
|
|
917
945
|
return this.game.squareColor(squareId);
|
|
918
946
|
}
|
package/chessboard.move.js
CHANGED
|
@@ -21,27 +21,15 @@ class Move {
|
|
|
21
21
|
}
|
|
22
22
|
|
|
23
23
|
check() {
|
|
24
|
-
if (this.piece === null)
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
if (!(this.
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
if (
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
if (!(this.from instanceof Square)) {
|
|
34
|
-
throw new Error("Invalid move: from is not an instance of Square");
|
|
35
|
-
}
|
|
36
|
-
if (!(this.to instanceof Square)) {
|
|
37
|
-
throw new Error("Invalid move: to is not an instance of Square");
|
|
38
|
-
}
|
|
39
|
-
if (!this.to) {
|
|
40
|
-
throw new Error("Invalid move: to is null or undefined");
|
|
41
|
-
}
|
|
42
|
-
if (!this.from) {
|
|
43
|
-
throw new Error("Invalid move: from is null or undefined");
|
|
44
|
-
}
|
|
24
|
+
if (this.piece === null) return false;
|
|
25
|
+
if (!(this.piece instanceof Piece)) return false;
|
|
26
|
+
if (['q', 'r', 'b', 'n', null].indexOf(this.promotion) === -1) return false;
|
|
27
|
+
if (!(this.from instanceof Square)) return false;
|
|
28
|
+
if (!(this.to instanceof Square)) return false;
|
|
29
|
+
if (!this.to) return false;
|
|
30
|
+
if (!this.from) return false;
|
|
31
|
+
if (this.from === this.to) return false;
|
|
32
|
+
return true;
|
|
45
33
|
}
|
|
46
34
|
|
|
47
35
|
isLegal(game) {
|
package/chessboard.piece.js
CHANGED