@alepot55/chessboardjs 2.3.7 → 2.3.8

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.
@@ -1,5 +1,5 @@
1
1
  /**
2
- * Service for managing board setup and DOM operations
2
+ * BoardService - Handles board DOM setup, manipulation, and resource management
3
3
  * @module services/BoardService
4
4
  * @since 2.0.0
5
5
  */
@@ -11,45 +11,45 @@ import Square from '../components/Square.js';
11
11
 
12
12
  /**
13
13
  * Service responsible for board DOM manipulation and setup
14
- * @class
14
+ * @class BoardService
15
15
  */
16
16
  export class BoardService {
17
17
  /**
18
- * Creates a new BoardService instance
19
- * @param {ChessboardConfig} config - Board configuration
18
+ * Create a new BoardService instance
19
+ * @param {Object} config - Board configuration
20
20
  */
21
21
  constructor(config) {
22
+ /** @type {Object} */
22
23
  this.config = config;
24
+ /** @type {HTMLElement|null} */
23
25
  this.element = null;
26
+ /** @type {Object.<string, Square>} */
24
27
  this.squares = {};
25
28
  }
26
29
 
27
30
  /**
28
- * Builds the board DOM element and attaches it to the configured container
29
- * @throws {DOMError} When the container element cannot be found
31
+ * Build the board DOM element and attach it to the configured container
32
+ * @throws {DOMError} If the container element cannot be found
30
33
  */
31
34
  buildBoard() {
32
- console.log('BoardService.buildBoard: Looking for element with ID:', this.config.id_div);
33
-
34
35
  this.element = document.getElementById(this.config.id_div);
35
36
  if (!this.element) {
36
37
  throw new DOMError(ERROR_MESSAGES.invalid_id_div + this.config.id_div, this.config.id_div);
37
38
  }
38
-
39
39
  this.resize(this.config.size);
40
- this.element.className = "board";
40
+ this.element.className = 'board';
41
41
  }
42
42
 
43
43
  /**
44
- * Creates all 64 squares and adds them to the board
44
+ * Create all 64 squares and add them to the board
45
45
  * @param {Function} coordConverter - Function to convert row/col to real coordinates
46
46
  */
47
47
  buildSquares(coordConverter) {
48
+ if (!this.element) throw new DOMError('Board element not initialized', this.config.id_div);
48
49
  for (let row = 0; row < BOARD_SIZE.ROWS; row++) {
49
50
  for (let col = 0; col < BOARD_SIZE.COLS; col++) {
50
51
  const [squareRow, squareCol] = coordConverter(row, col);
51
52
  const square = new Square(squareRow, squareCol);
52
-
53
53
  this.squares[square.getId()] = square;
54
54
  this.element.appendChild(square.element);
55
55
  }
@@ -57,20 +57,17 @@ export class BoardService {
57
57
  }
58
58
 
59
59
  /**
60
- * Removes all squares from the board and cleans up their resources
61
- * Best practice: always destroy JS objects and DOM nodes, and clear references.
60
+ * Remove all squares from the board and clean up their resources
62
61
  */
63
62
  removeSquares() {
64
63
  for (const square of Object.values(this.squares)) {
65
- // Always call destroy to remove DOM and clear piece reference
66
64
  square.destroy();
67
65
  }
68
66
  this.squares = {};
69
67
  }
70
68
 
71
69
  /**
72
- * Removes all content from the board element
73
- * Best practice: clear DOM and force element to be re-fetched on next build.
70
+ * Remove all content from the board element
74
71
  */
75
72
  removeBoard() {
76
73
  if (this.element) {
@@ -80,9 +77,9 @@ export class BoardService {
80
77
  }
81
78
 
82
79
  /**
83
- * Resizes the board to the specified size
80
+ * Resize the board to the specified size
84
81
  * @param {number|string} value - Size in pixels or 'auto'
85
- * @throws {ValidationError} When size value is invalid
82
+ * @throws {ValidationError} If size value is invalid
86
83
  */
87
84
  resize(value) {
88
85
  if (value === 'auto') {
@@ -96,15 +93,13 @@ export class BoardService {
96
93
  }
97
94
 
98
95
  /**
99
- * Calculates the optimal size when 'auto' is specified
96
+ * Calculate the optimal size when 'auto' is specified
100
97
  * @private
101
98
  * @returns {number} Calculated size in pixels
102
99
  */
103
100
  _calculateAutoSize() {
104
- if (!this.element) return 400; // Default fallback
105
-
101
+ if (!this.element) return 400;
106
102
  const { offsetWidth, offsetHeight } = this.element;
107
-
108
103
  if (offsetWidth === 0) {
109
104
  return offsetHeight || 400;
110
105
  } else if (offsetHeight === 0) {
@@ -115,8 +110,8 @@ export class BoardService {
115
110
  }
116
111
 
117
112
  /**
118
- * Gets a square by its ID
119
- * @param {string} squareId - Square identifier (API pubblica)
113
+ * Get a square by its ID
114
+ * @param {string} squareId - Square identifier
120
115
  * @returns {Square|null} The square or null if not found
121
116
  */
122
117
  getSquare(squareId) {
@@ -124,26 +119,37 @@ export class BoardService {
124
119
  }
125
120
 
126
121
  /**
127
- * Highlight a square (solo oggetto)
122
+ * Highlight a square
128
123
  * @param {Square} square
129
124
  * @param {Object} [opts]
130
125
  */
131
126
  highlightSquare(square, opts = {}) {
132
- if (!square) throw new Error('highlightSquare richiede oggetto Square');
133
- // ... logica esistente ...
127
+ if (!square) throw new Error('highlightSquare requires a Square object');
128
+ // Implement highlight logic or call square.highlight() if available
129
+ if (typeof square.highlight === 'function') {
130
+ square.highlight(opts);
131
+ } else {
132
+ square.element.classList.add('highlighted');
133
+ }
134
134
  }
135
+
135
136
  /**
136
- * Dehighlight a square (solo oggetto)
137
+ * Remove highlight from a square
137
138
  * @param {Square} square
138
139
  * @param {Object} [opts]
139
140
  */
140
141
  dehighlightSquare(square, opts = {}) {
141
- if (!square) throw new Error('dehighlightSquare richiede oggetto Square');
142
- // ... logica esistente ...
142
+ if (!square) throw new Error('dehighlightSquare requires a Square object');
143
+ // Implement dehighlight logic or call square.dehighlight() if available
144
+ if (typeof square.dehighlight === 'function') {
145
+ square.dehighlight(opts);
146
+ } else {
147
+ square.element.classList.remove('highlighted');
148
+ }
143
149
  }
144
150
 
145
151
  /**
146
- * Gets all squares
152
+ * Get all squares
147
153
  * @returns {Object.<string, Square>} All squares indexed by ID
148
154
  */
149
155
  getAllSquares() {
@@ -151,7 +157,7 @@ export class BoardService {
151
157
  }
152
158
 
153
159
  /**
154
- * Applies a method to all squares
160
+ * Apply a method to all squares
155
161
  * @param {string} methodName - Name of the method to call on each square
156
162
  * @param {...*} args - Arguments to pass to the method
157
163
  */
@@ -164,7 +170,7 @@ export class BoardService {
164
170
  }
165
171
 
166
172
  /**
167
- * Cleans up all resources
173
+ * Clean up all resources
168
174
  */
169
175
  destroy() {
170
176
  this.removeSquares();
@@ -1,5 +1,5 @@
1
1
  /**
2
- * Service for managing coordinate conversions and board orientation
2
+ * CoordinateService - Handles coordinate conversions and board orientation logic
3
3
  * @module services/CoordinateService
4
4
  * @since 2.0.0
5
5
  */
@@ -10,19 +10,20 @@ import { ERROR_MESSAGES } from '../errors/messages.js';
10
10
 
11
11
  /**
12
12
  * Service responsible for coordinate conversions and board orientation
13
- * @class
13
+ * @class CoordinateService
14
14
  */
15
15
  export class CoordinateService {
16
16
  /**
17
- * Creates a new CoordinateService instance
18
- * @param {ChessboardConfig} config - Board configuration
17
+ * Create a new CoordinateService instance
18
+ * @param {Object} config - Board configuration
19
19
  */
20
20
  constructor(config) {
21
+ /** @type {Object} */
21
22
  this.config = config;
22
23
  }
23
24
 
24
25
  /**
25
- * Converts logical coordinates to real coordinates based on board orientation
26
+ * Convert logical coordinates to real coordinates based on board orientation
26
27
  * @param {number} row - Row index (0-7)
27
28
  * @param {number} col - Column index (0-7)
28
29
  * @returns {Array<number>} Real coordinates [row, col] (1-8)
@@ -30,18 +31,16 @@ export class CoordinateService {
30
31
  realCoord(row, col) {
31
32
  let realRow = row;
32
33
  let realCol = col;
33
-
34
34
  if (this.isWhiteOriented()) {
35
35
  realRow = 7 - row;
36
36
  } else {
37
37
  realCol = 7 - col;
38
38
  }
39
-
40
39
  return [realRow + 1, realCol + 1];
41
40
  }
42
41
 
43
42
  /**
44
- * Converts board coordinates to square ID
43
+ * Convert board coordinates to square ID
45
44
  * @param {number} row - Row index (0-7)
46
45
  * @param {number} col - Column index (0-7)
47
46
  * @returns {string} Square ID (e.g., 'e4')
@@ -49,7 +48,6 @@ export class CoordinateService {
49
48
  getSquareID(row, col) {
50
49
  row = parseInt(row);
51
50
  col = parseInt(col);
52
-
53
51
  if (this.isWhiteOriented()) {
54
52
  row = 8 - row;
55
53
  col = col + 1;
@@ -57,7 +55,6 @@ export class CoordinateService {
57
55
  row = row + 1;
58
56
  col = 8 - col;
59
57
  }
60
-
61
58
  if (col < 1 || col > 8 || row < 1 || row > 8) {
62
59
  throw new ValidationError(
63
60
  `Invalid board coordinates: row=${row}, col=${col}`,
@@ -65,13 +62,12 @@ export class CoordinateService {
65
62
  { row, col }
66
63
  );
67
64
  }
68
-
69
65
  const letter = BOARD_LETTERS[col - 1];
70
66
  return letter + row;
71
67
  }
72
68
 
73
69
  /**
74
- * Converts square ID to board coordinates
70
+ * Convert square ID to board coordinates
75
71
  * @param {string} squareId - Square ID (e.g., 'e4')
76
72
  * @returns {Array<number>} Board coordinates [row, col] (0-7)
77
73
  */
@@ -83,10 +79,8 @@ export class CoordinateService {
83
79
  squareId
84
80
  );
85
81
  }
86
-
87
82
  const letter = squareId[0];
88
83
  const number = parseInt(squareId[1]);
89
-
90
84
  const col = BOARD_LETTERS.indexOf(letter);
91
85
  if (col === -1) {
92
86
  throw new ValidationError(
@@ -95,7 +89,6 @@ export class CoordinateService {
95
89
  squareId
96
90
  );
97
91
  }
98
-
99
92
  if (number < 1 || number > 8) {
100
93
  throw new ValidationError(
101
94
  ERROR_MESSAGES.invalid_square + squareId,
@@ -103,9 +96,7 @@ export class CoordinateService {
103
96
  squareId
104
97
  );
105
98
  }
106
-
107
99
  let row, boardCol;
108
-
109
100
  if (this.isWhiteOriented()) {
110
101
  row = 8 - number;
111
102
  boardCol = col;
@@ -113,12 +104,11 @@ export class CoordinateService {
113
104
  row = number - 1;
114
105
  boardCol = 7 - col;
115
106
  }
116
-
117
107
  return [row, boardCol];
118
108
  }
119
109
 
120
110
  /**
121
- * Converts pixel coordinates to square ID
111
+ * Convert pixel coordinates to square ID
122
112
  * @param {number} x - X coordinate in pixels
123
113
  * @param {number} y - Y coordinate in pixels
124
114
  * @param {HTMLElement} boardElement - Board DOM element
@@ -126,21 +116,15 @@ export class CoordinateService {
126
116
  */
127
117
  pixelToSquareID(x, y, boardElement) {
128
118
  if (!boardElement) return null;
129
-
130
119
  const rect = boardElement.getBoundingClientRect();
131
120
  const { width, height } = rect;
132
-
133
- // Check if coordinates are within board bounds
134
121
  if (x < 0 || x >= width || y < 0 || y >= height) {
135
122
  return null;
136
123
  }
137
-
138
124
  const squareWidth = width / 8;
139
125
  const squareHeight = height / 8;
140
-
141
126
  const col = Math.floor(x / squareWidth);
142
127
  const row = Math.floor(y / squareHeight);
143
-
144
128
  try {
145
129
  return this.getSquareID(row, col);
146
130
  } catch (error) {
@@ -149,25 +133,21 @@ export class CoordinateService {
149
133
  }
150
134
 
151
135
  /**
152
- * Converts square ID to pixel coordinates
136
+ * Convert square ID to pixel coordinates
153
137
  * @param {string} squareId - Square ID (e.g., 'e4')
154
138
  * @param {HTMLElement} boardElement - Board DOM element
155
139
  * @returns {Object|null} Pixel coordinates {x, y} or null if invalid
156
140
  */
157
141
  squareIDToPixel(squareId, boardElement) {
158
142
  if (!boardElement) return null;
159
-
160
143
  try {
161
144
  const [row, col] = this.getCoordinatesFromSquareID(squareId);
162
145
  const rect = boardElement.getBoundingClientRect();
163
146
  const { width, height } = rect;
164
-
165
147
  const squareWidth = width / 8;
166
148
  const squareHeight = height / 8;
167
-
168
149
  const x = col * squareWidth;
169
150
  const y = row * squareHeight;
170
-
171
151
  return { x, y };
172
152
  } catch (error) {
173
153
  return null;
@@ -175,7 +155,7 @@ export class CoordinateService {
175
155
  }
176
156
 
177
157
  /**
178
- * Gets the center pixel coordinates of a square
158
+ * Get the center pixel coordinates of a square
179
159
  * @param {string} squareId - Square ID (e.g., 'e4')
180
160
  * @param {HTMLElement} boardElement - Board DOM element
181
161
  * @returns {Object|null} Center coordinates {x, y} or null if invalid
@@ -183,11 +163,9 @@ export class CoordinateService {
183
163
  getSquareCenter(squareId, boardElement) {
184
164
  const coords = this.squareIDToPixel(squareId, boardElement);
185
165
  if (!coords) return null;
186
-
187
166
  const rect = boardElement.getBoundingClientRect();
188
167
  const squareWidth = rect.width / 8;
189
168
  const squareHeight = rect.height / 8;
190
-
191
169
  return {
192
170
  x: coords.x + squareWidth / 2,
193
171
  y: coords.y + squareHeight / 2
@@ -195,7 +173,7 @@ export class CoordinateService {
195
173
  }
196
174
 
197
175
  /**
198
- * Calculates the distance between two squares
176
+ * Calculate the distance between two squares
199
177
  * @param {string} fromSquare - Source square ID
200
178
  * @param {string} toSquare - Target square ID
201
179
  * @returns {number} Distance between squares
@@ -204,10 +182,8 @@ export class CoordinateService {
204
182
  try {
205
183
  const [fromRow, fromCol] = this.getCoordinatesFromSquareID(fromSquare);
206
184
  const [toRow, toCol] = this.getCoordinatesFromSquareID(toSquare);
207
-
208
185
  const rowDiff = Math.abs(toRow - fromRow);
209
186
  const colDiff = Math.abs(toCol - fromCol);
210
-
211
187
  return Math.sqrt(rowDiff * rowDiff + colDiff * colDiff);
212
188
  } catch (error) {
213
189
  return 0;
@@ -215,7 +191,7 @@ export class CoordinateService {
215
191
  }
216
192
 
217
193
  /**
218
- * Checks if the board is oriented from white's perspective
194
+ * Check if the board is oriented from white's perspective
219
195
  * @returns {boolean} True if white-oriented
220
196
  */
221
197
  isWhiteOriented() {
@@ -223,7 +199,7 @@ export class CoordinateService {
223
199
  }
224
200
 
225
201
  /**
226
- * Checks if the board is oriented from black's perspective
202
+ * Check if the board is oriented from black's perspective
227
203
  * @returns {boolean} True if black-oriented
228
204
  */
229
205
  isBlackOriented() {
@@ -231,31 +207,31 @@ export class CoordinateService {
231
207
  }
232
208
 
233
209
  /**
234
- * Flips the board orientation
210
+ * Flip the board orientation
235
211
  */
236
212
  flipOrientation() {
237
213
  this.config.orientation = this.isWhiteOriented() ? 'b' : 'w';
238
214
  }
239
215
 
240
216
  /**
241
- * Sets the board orientation
242
- * @param {string} orientation - 'w' for white, 'b' for black
243
- * @throws {ValidationError} When orientation is invalid
217
+ * Set the board orientation
218
+ * @param {string} orientation - 'w', 'b', 'white', or 'black'
219
+ * @throws {ValidationError} If orientation is invalid
244
220
  */
245
221
  setOrientation(orientation) {
246
- if (orientation !== 'w' && orientation !== 'b') {
222
+ const normalized = orientation === 'white' ? 'w' : orientation === 'black' ? 'b' : orientation;
223
+ if (normalized !== 'w' && normalized !== 'b') {
247
224
  throw new ValidationError(
248
225
  ERROR_MESSAGES.invalid_orientation + orientation,
249
226
  'orientation',
250
227
  orientation
251
228
  );
252
229
  }
253
-
254
- this.config.orientation = orientation;
230
+ this.config.orientation = normalized;
255
231
  }
256
232
 
257
233
  /**
258
- * Gets the current orientation
234
+ * Get the current orientation
259
235
  * @returns {string} Current orientation ('w' or 'b')
260
236
  */
261
237
  getOrientation() {
@@ -263,50 +239,21 @@ export class CoordinateService {
263
239
  }
264
240
 
265
241
  /**
266
- * Sets the orientation
267
- * @param {string} orientation - New orientation ('w', 'b', 'white', 'black')
268
- */
269
- setOrientation(orientation) {
270
- // Normalize orientation
271
- const normalizedOrientation = orientation === 'white' ? 'w' :
272
- orientation === 'black' ? 'b' : orientation;
273
-
274
- if (normalizedOrientation !== 'w' && normalizedOrientation !== 'b') {
275
- throw new ValidationError(
276
- ERROR_MESSAGES.invalid_orientation + orientation,
277
- 'orientation',
278
- orientation
279
- );
280
- }
281
-
282
- this.config.orientation = normalizedOrientation;
283
- }
284
-
285
- /**
286
- * Flips the board orientation
287
- */
288
- flipOrientation() {
289
- this.config.orientation = this.isWhiteOriented() ? 'b' : 'w';
290
- }
291
-
292
- /**
293
- * Gets all square IDs in order
242
+ * Get all square IDs in order
294
243
  * @returns {Array<string>} Array of all square IDs
295
244
  */
296
245
  getAllSquareIDs() {
297
246
  const squares = [];
298
-
299
247
  for (let row = 0; row < 8; row++) {
300
248
  for (let col = 0; col < 8; col++) {
301
249
  squares.push(this.getSquareID(row, col));
302
250
  }
303
251
  }
304
-
305
252
  return squares;
306
253
  }
307
254
 
308
255
  /**
309
- * Gets squares in a specific rank (row)
256
+ * Get all squares in a specific rank (row)
310
257
  * @param {number} rank - Rank number (1-8)
311
258
  * @returns {Array<string>} Array of square IDs in the rank
312
259
  */
@@ -318,19 +265,16 @@ export class CoordinateService {
318
265
  rank
319
266
  );
320
267
  }
321
-
322
268
  const squares = [];
323
-
324
269
  for (let col = 0; col < 8; col++) {
325
270
  const row = this.isWhiteOriented() ? 8 - rank : rank - 1;
326
271
  squares.push(this.getSquareID(row, col));
327
272
  }
328
-
329
273
  return squares;
330
274
  }
331
275
 
332
276
  /**
333
- * Gets squares in a specific file (column)
277
+ * Get all squares in a specific file (column)
334
278
  * @param {string} file - File letter (a-h)
335
279
  * @returns {Array<string>} Array of square IDs in the file
336
280
  */
@@ -343,13 +287,10 @@ export class CoordinateService {
343
287
  file
344
288
  );
345
289
  }
346
-
347
290
  const squares = [];
348
-
349
291
  for (let row = 0; row < 8; row++) {
350
292
  squares.push(this.getSquareID(row, col));
351
293
  }
352
-
353
294
  return squares;
354
295
  }
355
296
  }