@alepot55/chessboardjs 1.1.0 → 2.0.3

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.
@@ -0,0 +1,145 @@
1
+ const animationTime = {
2
+ 'fast': 200,
3
+ 'slow': 600,
4
+ 'normal': 400,
5
+ 'verySlow': 1000,
6
+ 'veryFast': 100
7
+ };
8
+
9
+ const boolValues = {
10
+ 'true': true,
11
+ 'false': false,
12
+ 'none': false,
13
+ 1: true,
14
+ 0: false
15
+ };
16
+
17
+ const transitionFunctions = {
18
+ 'ease': 'ease',
19
+ 'linear': 'linear',
20
+ 'ease-in': 'ease-in',
21
+ 'ease-out': 'ease-out',
22
+ 'ease-in-out': 'ease-in-out'
23
+ };
24
+
25
+ class ChessboardConfig {
26
+ constructor(settings) {
27
+ const defaults = {
28
+ id: 'board',
29
+ position: 'start',
30
+ orientation: 'w',
31
+ mode: 'normal',
32
+ size: 'auto',
33
+ draggable: true,
34
+ hints: true,
35
+ clickable: true,
36
+ movableColors: 'both',
37
+ moveHighlight: true,
38
+ overHighlight: true,
39
+ moveAnimation: 'ease',
40
+ moveTime: 'fast',
41
+ dropOffBoard: 'snapback',
42
+ snapbackTime: 'fast',
43
+ snapbackAnimation: 'ease',
44
+ fadeTime: 'fast',
45
+ fadeAnimation: 'ease',
46
+ ratio: 0.9,
47
+ piecesPath: 'https://cdn.jsdelivr.net/npm/@alepot55/chessboardjs/default_pieces',
48
+ onMove: () => true,
49
+ onMoveEnd: () => true,
50
+ onChange: () => true,
51
+ onDragStart: () => true,
52
+ onDragMove: () => true,
53
+ onDrop: () => true,
54
+ onSnapbackEnd: () => true,
55
+ whiteSquare: '#f0d9b5',
56
+ blackSquare: '#b58863',
57
+ highlight: 'yellow',
58
+ selectedSquareWhite: '#ababaa',
59
+ selectedSquareBlack: '#ababaa',
60
+ movedSquareWhite: '#f1f1a0',
61
+ movedSquareBlack: '#e9e981',
62
+ choiceSquare: 'white',
63
+ coverSquare: 'black',
64
+ hintColor: '#ababaa'
65
+ };
66
+
67
+ const config = Object.assign({}, defaults, settings);
68
+
69
+ this.id_div = config.id_div;
70
+ this.position = config.position;
71
+ this.orientation = config.orientation;
72
+ this.mode = config.mode;
73
+ this.dropOffBoard = config.dropOffBoard;
74
+ this.size = config.size;
75
+ this.movableColors = config.movableColors;
76
+ this.moveAnimation = config.moveAnimation;
77
+ this.snapbackAnimation = config.snapbackAnimation;
78
+ this.fadeAnimation = config.fadeAnimation;
79
+ this.piecesPath = config.piecesPath;
80
+ this.onMove = config.onMove;
81
+ this.onMoveEnd = config.onMoveEnd;
82
+ this.onChange = config.onChange;
83
+ this.onDragStart = config.onDragStart;
84
+ this.onDragMove = config.onDragMove;
85
+ this.onDrop = config.onDrop;
86
+ this.onSnapbackEnd = config.onSnapbackEnd;
87
+
88
+ this.hints = this.setBoolean(config.hints);
89
+ this.clickable = this.setBoolean(config.clickable);
90
+ this.draggable = this.setBoolean(config.draggable);
91
+ this.moveHighlight = this.setBoolean(config.moveHighlight);
92
+ this.overHighlight = this.setBoolean(config.overHighlight);
93
+
94
+ this.moveTime = this.setTime(config.moveTime)
95
+ this.snapbackTime = this.setTime(config.snapbackTime);
96
+ this.fadeTime = this.setTime(config.fadeTime);
97
+
98
+ this.setCSSProperty('pieceRatio', config.ratio);
99
+ this.setCSSProperty('whiteSquare', config.whiteSquare);
100
+ this.setCSSProperty('blackSquare', config.blackSquare);
101
+ this.setCSSProperty('highlightSquare', config.highlight);
102
+ this.setCSSProperty('selectedSquareWhite', config.selectedSquareWhite);
103
+ this.setCSSProperty('selectedSquareBlack', config.selectedSquareBlack);
104
+ this.setCSSProperty('movedSquareWhite', config.movedSquareWhite);
105
+ this.setCSSProperty('movedSquareBlack', config.movedSquareBlack);
106
+ this.setCSSProperty('choiceSquare', config.choiceSquare);
107
+ this.setCSSProperty('coverSquare', config.coverSquare);
108
+ this.setCSSProperty('hintColor', config.hintColor);
109
+
110
+ // Configure modes
111
+ if (this.mode === 'creative') {
112
+ this.onlyLegalMoves = false;
113
+ this.hints = false;
114
+ } else if (this.mode === 'normal') {
115
+ this.onlyLegalMoves = true;
116
+ }
117
+ }
118
+
119
+ setCSSProperty(property, value) {
120
+ document.documentElement.style.setProperty(`--${property}`, value);
121
+ }
122
+
123
+ setOrientation(orientation) {
124
+ this.orientation = orientation;
125
+ return this;
126
+ }
127
+
128
+ setTime(value) {
129
+ if (typeof value === 'number') return value;
130
+ if (value in animationTime) return animationTime[value];
131
+ throw new Error('Invalid time value');
132
+ }
133
+
134
+ setBoolean(value) {
135
+ if (typeof value === 'boolean') return value;
136
+ throw new Error('Invalid boolean value');
137
+ }
138
+
139
+ setTransitionFunction(value) {
140
+ if (transitionFunctions[value]) return transitionFunctions[value];
141
+ throw new Error('Invalid transition function');
142
+ }
143
+ }
144
+
145
+ export default ChessboardConfig;
package/chessboard.css CHANGED
@@ -1,106 +1,105 @@
1
- :root {
2
- --dimBoard: 600px;
3
- --pieceRatio: 0.9;
4
-
5
- --blackSquare: #b58863;
6
- --whiteSquare: #f0d9b5;
7
- --highlightSquare: yellow;
8
- --selectedSquareWhite: #ababaa;
9
- --selectedSquareBlack: #ababaa;
10
- --movedSquareBlack: #e9e981;
11
- --movedSquareWhite: #f1f1a0;
12
- --choiceSquare: white;
13
- --coverSquare: black;
14
- --hintColor: #ababaa;
15
- }
16
-
17
- .board {
18
- display: grid;
19
- grid-template: repeat(8, 1fr) / repeat(8, 1fr);
20
- width: var(--dimBoard);
21
- height: var(--dimBoard);
22
- background-color: var(--blackSquare);
23
- }
24
-
25
- .square {
26
- width: calc(var(--dimBoard) / 8);
27
- height: calc(var(--dimBoard) / 8);
28
- display: flex;
29
- justify-content: center;
30
- align-items: center;
31
- }
32
-
33
- .piece {
34
- position: absolute;
35
- z-index: 10;
36
- width: calc(calc(var(--dimBoard) / 8) * var(--pieceRatio));
37
- height: calc(calc(var(--dimBoard) / 8) * var(--pieceRatio));
38
-
39
- /* No selection */
40
- -webkit-user-select: none;
41
- /* Safari */
42
- -moz-user-select: none;
43
- /* Firefox */
44
- -ms-user-select: none;
45
- /* IE10+/Edge */
46
- user-select: none;
47
- /* Standard */
48
- }
49
-
50
- .choicable {
51
- z-index: 25;
52
- }
53
-
54
- .hint {
55
- background: var(--hintColor);
56
- width: calc(calc(var(--dimBoard) / 8) / 3.5);
57
- height: calc(calc(var(--dimBoard) / 8) / 3.5);
58
- position: absolute;
59
- z-index: 5;
60
- border-radius: 50%;
61
- opacity: 0.8;
62
- }
63
-
64
- .catchable {
65
- width: calc(calc(var(--dimBoard) / 8) * 0.9);
66
- height: calc(calc(var(--dimBoard) / 8) * 0.9);
67
- }
68
-
69
- .blackSquare {
70
- background: var(--blackSquare);
71
- }
72
-
73
- .whiteSquare {
74
- background: var(--whiteSquare);
75
- }
76
-
77
- .selectedSquareWhite {
78
- background: var(--selectedSquareWhite);
79
- }
80
-
81
- .selectedSquareBlack {
82
- background: var(--selectedSquareBlack);
83
- }
84
-
85
- .movedSquareBlack {
86
- background: var(--movedSquareBlack);
87
- }
88
-
89
- .movedSquareWhite {
90
- background: var(--movedSquareWhite);
91
- }
92
-
93
- .choice {
94
- background: var(--choiceSquare);
95
- z-index: 20;
96
- }
97
-
98
- .cover {
99
- background: var(--coverSquare);
100
- opacity: 0.5;
101
- z-index: 20;
102
- }
103
-
104
- .highlighted {
105
- box-shadow: inset 0 0 10px var(--highlightSquare);
1
+ :root {
2
+ --dimBoard: 600px;
3
+ --pieceRatio: 0.9;
4
+
5
+ --blackSquare: #b58863;
6
+ --whiteSquare: #f0d9b5;
7
+ --highlightSquare: yellow;
8
+ --selectedSquareWhite: #ababaa;
9
+ --selectedSquareBlack: #ababaa;
10
+ --movedSquareBlack: #e9e981;
11
+ --movedSquareWhite: #f1f1a0;
12
+ --choiceSquare: white;
13
+ --coverSquare: black;
14
+ --hintColor: #ababaa;
15
+ }
16
+
17
+ .board {
18
+ display: grid;
19
+ grid-template: repeat(8, 1fr) / repeat(8, 1fr);
20
+ width: var(--dimBoard);
21
+ height: var(--dimBoard);
22
+ }
23
+
24
+ .square {
25
+ width: calc(var(--dimBoard) / 8);
26
+ height: calc(var(--dimBoard) / 8);
27
+ display: flex;
28
+ justify-content: center;
29
+ align-items: center;
30
+ }
31
+
32
+ .piece {
33
+ position: absolute;
34
+ z-index: 10;
35
+ width: calc(calc(var(--dimBoard) / 8) * var(--pieceRatio));
36
+ height: calc(calc(var(--dimBoard) / 8) * var(--pieceRatio));
37
+
38
+ /* No selection */
39
+ -webkit-user-select: none;
40
+ /* Safari */
41
+ -moz-user-select: none;
42
+ /* Firefox */
43
+ -ms-user-select: none;
44
+ /* IE10+/Edge */
45
+ user-select: none;
46
+ /* Standard */
47
+ }
48
+
49
+ .choicable {
50
+ z-index: 50;
51
+ }
52
+
53
+ .hint {
54
+ background: var(--hintColor);
55
+ width: calc(calc(var(--dimBoard) / 8) / 3.5);
56
+ height: calc(calc(var(--dimBoard) / 8) / 3.5);
57
+ position: absolute;
58
+ z-index: 5;
59
+ border-radius: 50%;
60
+ opacity: 0.8;
61
+ }
62
+
63
+ .catchable {
64
+ width: calc(calc(var(--dimBoard) / 8) * 0.9);
65
+ height: calc(calc(var(--dimBoard) / 8) * 0.9);
66
+ }
67
+
68
+ .blackSquare {
69
+ background: var(--blackSquare);
70
+ }
71
+
72
+ .whiteSquare {
73
+ background: var(--whiteSquare);
74
+ }
75
+
76
+ .selectedSquareWhite {
77
+ background: var(--selectedSquareWhite);
78
+ }
79
+
80
+ .selectedSquareBlack {
81
+ background: var(--selectedSquareBlack);
82
+ }
83
+
84
+ .movedSquareBlack {
85
+ background: var(--movedSquareBlack);
86
+ }
87
+
88
+ .movedSquareWhite {
89
+ background: var(--movedSquareWhite);
90
+ }
91
+
92
+ .choice {
93
+ background: var(--choiceSquare);
94
+ z-index: 30;
95
+ }
96
+
97
+ .cover {
98
+ background: var(--coverSquare);
99
+ opacity: 0.5;
100
+ z-index: 25;
101
+ }
102
+
103
+ .highlighted {
104
+ box-shadow: inset 0 0 10px var(--highlightSquare);
106
105
  }