diff --git a/main.js b/main.js index 431b336..07bde2e 100644 --- a/main.js +++ b/main.js @@ -87,6 +87,55 @@ function setPec(pecTemplate=null, targetIndex=null) { targetPec.appendChild(pecTemplate.DOM); } +function repopBoards (current=0) { + //app.boards; + //app.currentBoards; + //app.menu; + // Normally we only want <, but we want an extra + // iteration here for the "add new board" element + for (var i = 0; i <= app.boards.length; i++) { + var boardTab = document.getElementById("board"+i); + if (null == boardTab) { + boardTab = document.createElement("span"); + boardTab.setAttribute("id", "board"+i); + boardTab.classList.add("board_tab"); + app.gui.menu.appendChild(boardTab); + } + // Clear event listeners + var oldTab = boardTab; + boardTab = oldTab.cloneNode(true); + oldTab.parentNode.replaceChild(boardTab, oldTab); + // not sure how memory safe the above lines are + var name = "Board "+i; + if (i == app.boards.length) { + name = "+"; + boardTab.addEventListener("click",(e)=>{ + var current = app.boards.findIndex(i=>i==app.currentBoard); + app.boards.push(new pecBoard()); + repopBoards(current); + repopGrid(app.currentBoard); + }); + } else { + if (current != i) { + boardTab.classList.remove("active_board"); + boardTab.addEventListener("click", (e)=>{ + var sourceID = ((e.currentTarget.getAttribute("id")).split("board"))[1]; + app.currentBoard = app.boards[sourceID]; + repopBoards(sourceID); + repopGrid(app.currentBoard); + }); + } else { + boardTab.classList.add("active_board"); + } + if (undefined != app.boards[i].name) { + name = app.boards[i].name; + } + } + boardTab.innerText = name; + console.log(boardTab); + } +} + function repopGrid (board=null) { if (null == board) { return systemMsg("repopGrid was not provided data",2); } if (!(board instanceof pecBoard)) { return systemMsg("repopGrid was not provided data of type pecBoard",2); } @@ -146,6 +195,7 @@ for (var x = 0; x < 16; x++) { app.boards = []; app.boards.push(new pecBoard()); app.currentBoard = app.boards[0]; +repopBoards(0); repopGrid(app.currentBoard); // Populate Library