Add statusControl and roleControl to ChessBuilder

In ChessServer, the ChessBuilder function and instantiation now take parameters for the statusControl and roleControl elements. This change improves flexibility by allowing these controls to be passed in from the invoking context, rather than being statically defined within the ChessBuilder function.
This commit is contained in:
Anduin Xue
2024-07-02 12:41:27 +00:00
parent 0fffb18029
commit ae4502a263
2 changed files with 8 additions and 4 deletions
@@ -11,7 +11,9 @@
window.addEventListener('DOMContentLoaded', async () => {
var playerId = getUserId();
var playerColor = await getPlayerColor(@Model.GameId);
let anduinChessBoard = new ChessBuilder(playerColor).default();
var statusControl = document.getElementById("status");
var roleControl = document.getElementById("role");
let anduinChessBoard = new ChessBuilder(playerColor, statusControl, roleControl).default();
anduinChessBoard.run(playerId, @Model.GameId);
document.body.addEventListener('pointerdown', (e) => {
@@ -16,8 +16,10 @@ import {
* let chessBoard = new ChessBuilder().default();
* ```
* @param {string} color currently color, w or b
* @param {HTMLElement} statusControl status control element
* @param {HTMLElement} roleControl role control element
*/
function ChessBuilder(color) {
function ChessBuilder(color, statusControl, roleControl) {
this.onDragStart = undefined;
this.onDrop = undefined;
this.onSnapEnd = undefined;
@@ -31,8 +33,8 @@ function ChessBuilder(color) {
anduinChessBoard.config.onDragStart = buildOnDragStart(anduinChessBoard);
anduinChessBoard.config.onDrop = buildOnDrop(anduinChessBoard);
anduinChessBoard.config.onSnapEnd = buildOnSnapEnd(anduinChessBoard);
anduinChessBoard.statusControl = document.getElementById("status");
anduinChessBoard.roleControl = document.getElementById("role");
anduinChessBoard.statusControl = statusControl;
anduinChessBoard.roleControl = roleControl;
return anduinChessBoard;
};