paritybit.ca

Unnamed repository; edit this file 'description' to name the repository.
Log | Files | Refs | README | LICENSE

commit e3e9ad02ff90397ac8aed1aea501f601bbbe7a4e
parent 27bb40fec9949c038acdcfaf120c7f55aa7e06d4
Author: Jake Bauer <jbauer@paritybit.ca>
Date:   Mon, 15 Apr 2019 23:20:03 -0400

Add auto-sorting on add and edit and add comments

Diffstat:
Mpublic/js/initiative.js | 86+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++--------------------
1 file changed, 65 insertions(+), 21 deletions(-)

diff --git a/public/js/initiative.js b/public/js/initiative.js @@ -1,15 +1,36 @@ -// initiative.js -// Created By: Jake Bauer -// 2019-04-15 +/*initiative.js +* Copyright (C) 2019 Jake Bauer +* +* This program is free software: you can redistribute it and/or modify +* it under the terms of the GNU General Public License as published by +* the Free Software Foundation, either version 3 of the License, or +* (at your option) any later version. +* +* This program is distributed in the hope that it will be useful, +* but WITHOUT ANY WARRANTY; without even the implied warranty of +* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +* GNU General Public License for more details. +* +* You should have received a copy of the GNU General Public License +* along with this program. If not, see <https://www.gnu.org/licenses/>. +*/ +// Global variable keeping track of all characters added to the list. let charArray = []; +/* draw() + * @brief Draws a character, their information, and the relevant buttons to the + * viewport. + * @input char The character to draw + * @input position That character's position in the charArray (used to give the + * div a corresponding id attribute); + */ function draw(char, position) { let list = document.getElementById("initiative-list"); let div = document.createElement('div'); div.setAttribute("class","initiative-div"); div.setAttribute("id", position); - list.insertBefore(div, list.childNodes[list.childNodes.length-3]); + list.insertBefore(div, list.childNodes[list.childNodes.length-1]); let charName = document.createElement('p'); let textNode = document.createTextNode(char.name); @@ -48,22 +69,11 @@ function draw(char, position) { removeBtn.appendChild(textNode); } -function addChar() { - let newChar = {}; - nameField = document.getElementById("char-name"); - initField = document.getElementById("init-val"); - newChar.name = nameField.value; - newChar.initiative = Number(initField.value); - nameField.value = null; - initField.value = null; - if (newChar.name == "" || newChar.initiative == null) { - alert("Please fill out both fields before adding a character."); - return; - } - draw(newChar, charArray.length); - charArray.push(newChar); -} - +/* compare() + * @brief Used by sortList() to determine the sorted order. Sorts by greatest to + * least initative value for each character. + * @see sortList() + */ function compare(a, b) { if (a.initiative < b.initiative) return 1; @@ -73,6 +83,10 @@ function compare(a, b) { return 0; } +/* sortList() + * @brief Sort the list of characters by their initative value + * @see compare() + */ function sortList() { // Sorts in order largest to smallest charArray.sort(compare); @@ -87,6 +101,31 @@ function sortList() { } } +/* addChar() + * @brief Add a character to the list and sort the list + */ +function addChar() { + let newChar = {}; + nameField = document.getElementById("char-name"); + initField = document.getElementById("init-val"); + newChar.name = nameField.value; + newChar.initiative = Number(initField.value); + nameField.value = null; + initField.value = null; + if (newChar.name == "" || newChar.initiative == null) { + alert("Please fill out both fields before adding a character."); + return; + } + draw(newChar, charArray.length); + charArray.push(newChar); + sortList(); +} + +/* editChar() + * @brief Edit a characters initiative value and re-sort + * @input Elem The "edit" button clicked used to identify which character + * to edit + */ function editChar(elem) { let newVal = prompt("Enter the new value:", "0"); if (newVal != null) { @@ -101,12 +140,18 @@ function editChar(elem) { break; } } + sortList(); } else { return; } } +/* removeChar() + * @brief Removes a character from the list of characters + * @input Elem The "remove" button clicked used to identify which character + * to remove + */ function removeChar(elem) { let charIndex = elem.parentNode.getAttribute("id"); for (let i = 0; i < charArray.length; i++) { @@ -121,7 +166,6 @@ function removeChar(elem) { } // Then re-draw them for (let i = 0; i < charArray.length; i++) { - console.log(i); draw(charArray[i], i); } }