Skip to content

Commit

Permalink
refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
Artony4444 authored and Artony4444 committed Dec 23, 2023
1 parent c1dc0ca commit 0c0cf40
Show file tree
Hide file tree
Showing 7 changed files with 62 additions and 70 deletions.
11 changes: 6 additions & 5 deletions src/js/chess/objects/board.js → src/js/chess/board.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,11 @@ let c = color; // color class

class board
{
constructor(id, containerId, color = c.w)
constructor(id, color = c.w)
{
this.id = id;
this.squares = [];
this.container = document.getElementById(containerId); if(this.container == null) {console.log("invalid div id : \""+containerId+"\""); console.log("example :", `\n<div id="01234"></div>\n<script src="static-chess.js" divID="01234"></script>`); return;}
this.container = document.getElementById(id); if(this.container == null) console.log("invalid div id : \""+containerId+"\"");
this.color = color;

this.initialize();
Expand All @@ -20,7 +20,7 @@ class board
this.assemble();
}

/* ---------- variables ---------- */
/* ---------- initialize ---------- */

variables()
{
Expand Down Expand Up @@ -52,6 +52,7 @@ class board

testPlace(piece, intXY)
{
// no changes in ui
this.getSquare(intXY).piece = piece;
}

Expand Down Expand Up @@ -157,7 +158,7 @@ class board

updateFenState(FEN)
{
// FEN b KQkq e3 0 1
// FEN = ( b KQkq e3 0 1)
let fen = FEN.split(" ");
if(fen.length != 6) return;

Expand Down Expand Up @@ -222,4 +223,4 @@ class board
{
return get.int2str(intXY) == string.invalid;
}
}
}
2 changes: 1 addition & 1 deletion src/js/chess/objects/logic.js → src/js/chess/logic.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ class logic
this.variables();
}

/* ---------- variables ---------- */
/* ---------- initialize ---------- */

variables()
{
Expand Down
2 changes: 1 addition & 1 deletion src/js/chess/objects/pieces.js → src/js/chess/pieces.js
Original file line number Diff line number Diff line change
Expand Up @@ -274,4 +274,4 @@ class Piece
pieceName = pieceName.slice(0,1) == "w" ? "b" + pieceName.slice(1) : "w" + pieceName.slice(1);
return this.name2piece(pieceName);
}
}
}
File renamed without changes.
24 changes: 9 additions & 15 deletions src/js/main.js
Original file line number Diff line number Diff line change
@@ -1,16 +1,18 @@
let divId = document.currentScript.staticChess;

var brd = new board("board1", divId, color.w);




let id = document.currentScript.boardID;
var brd = new board(id, color.w);






document.getElementById("headBar").addEventListener("touchend", () =>
{
//brd.getBoard();
console.log(
document.querySelector("html").innerHTML
);
})


//brd.ui.boardCenterAnimation("CHECKMATE");
Expand All @@ -23,14 +25,6 @@ var brd = new board("board1", divId, color.w);

/*
document.getElementById("headBar").addEventListener("touchend", function getBoard()
{
brd.getBoard();
//console.log(brd.container.innerHTML);
});
/*
to add :-
● moves history
Expand Down
3 changes: 1 addition & 2 deletions src/js/ui/ui.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,7 @@ class ui
{
let parent = this.container;

let board = document.createElement('div');
board.id = boardId;
let board = document.createElement('div'); // board.id = boardId;
board.className = string.boardClass;

parent.innerHTML = this.boardTab;
Expand Down
90 changes: 44 additions & 46 deletions static-chess.js
Original file line number Diff line number Diff line change
@@ -1,85 +1,83 @@


// ---------- Import Files ---------- //
// ---------- Installing ---------- //


let dcs = document.currentScript;
let address = dcs.getAttribute("src").split("static-chess.js").join("");
let boardID = dcs.staticChess;
let boardID = "sc:"+parseInt(Math.random()*10000);


//---------- css
// ---------- css

let cssStyles = `
<link rel="stylesheet" href="`+address+`src/css/root.css">
<link rel="stylesheet" href="`+address+`src/css/blocks.css">
<link rel="stylesheet" href="`+address+`src/css/main.css">
<link rel="stylesheet" href="`+address+`src/css/chess.board.css">
<div static-chess="`+boardID+`">
<link rel="stylesheet" href="`+address+`src/css/root.css">
<link rel="stylesheet" href="`+address+`src/css/blocks.css">
<link rel="stylesheet" href="`+address+`src/css/main.css">
<link rel="stylesheet" href="`+address+`src/css/chess.board.css">
</div>
`;

let head = document.getElementsByTagName('head')[0];
head.insertAdjacentHTML("beforeend", cssStyles);


//---------- js
// ---------- board container

let div = document.createElement('div');
div.id = boardID;
div.staticChess = boardID;
dcs.replaceWith(div);
let container = document.createElement('div');
container.id = boardID;
container.setAttribute("static-chess", boardID);
container.className = "container";
dcs.replaceWith(container);


// ---------- js

let jsClassesSRC = [
let jsSRC = [
address+"src/js/tools/strings.js",
address+"src/js/tools/converter.js",
address+"src/js/ui/objects/drag-drop.js",
address+"src/js/ui/ui.js",
address+"src/js/chess/objects/pieces.js",
address+"src/js/chess/objects/square.js",
address+"src/js/chess/objects/logic.js",
address+"src/js/chess/objects/board.js",
address+"src/js/chess/pieces.js",
address+"src/js/chess/square.js",
address+"src/js/chess/logic.js",
address+"src/js/chess/board.js",
address+"src/js/main.js"
];

let loadedCount = 0;
let body = document.getElementsByTagName('body')[0];
let div = document.createElement('div');
div.setAttribute("static-chess", boardID);
div.className = "static-chess-script";
body.appendChild(div); // if error => use this format <div> <STATIC-CHESS-SCRIPT> </div> or (use static-chess inside body)

document.addEventListener("DOMContentLoaded", function() {loadScriptSequentially();});
document.addEventListener("DOMContentLoaded", addScript);
let loadedCount = 0;

function loadScriptSequentially()
function addScript()
{
if(loadedCount < jsClassesSRC.length)
if(loadedCount < jsSRC.length)
{
let src = jsClassesSRC[loadedCount];
let src = jsSRC[loadedCount];

let sc = document.createElement('script');
sc.type = "text/javascript";
sc.src = src;
div.staticChess = boardID;

sc.onload = function()
{
loadedCount++;
loadScriptSequentially();
};

div.appendChild(sc);
sc.insertAdjacentHTML("afterend", "\n ");
let script = document.createElement('script'); // script.type = "text/javascript";
script.src = src;
script.boardID = boardID;
script.onload = function() { loadedCount++; addScript(); };

div.appendChild(script);
script.insertAdjacentHTML("beforebegin", "\n ");
}
}


/*
<---- debug ---->
/* ---------- DEBUG
if(loadedCount == jsClassesSRC.length - 1)
if(loadedCount == jsSRC.length - 1)
{
//document.querySelectorAll('[staticChess]').forEach((e) => console.log(e));
console.log(document.getElementsByTagName('html')[0].innerHTML);
}
<---- changes ---->
body.appendChild >> div.appendChild
*/
------------- */

0 comments on commit 0c0cf40

Please sign in to comment.