paritybit.ca

Files for paritybit.ca.
git clone https://git.jaderune.net/jbauer/paritybit.ca
Log | Files | Refs | README | LICENSE

initiative.js (4929B)


      1 /*initiative.js
      2 * Copyright (C) 2019,2022  Jake Bauer
      3 * Licensed under the ISC License.
      4 */
      5 
      6 // Global variable keeping track of all characters added to the list.
      7 let charArray = [];
      8 
      9 /* draw()
     10  * @brief Draws a character, their information, and the relevant buttons to the
     11  *          viewport.
     12  * @input char The character to draw
     13  * @input position That character's position in the charArray (used to give the
     14  *                  div a corresponding id attribute);
     15  */
     16 function draw(char, position) {
     17     let list = document.getElementById("initiative-list");
     18     let div = document.createElement('div');
     19     div.setAttribute("class","initiative-div");
     20     div.setAttribute("id", position);
     21     list.insertBefore(div, list.childNodes[list.childNodes.length-1]);
     22 
     23     let charName = document.createElement('p');
     24     let textNode = document.createTextNode(char.name + ": " + char.initiative);
     25     charName.appendChild(textNode);
     26     div.appendChild(charName);
     27 
     28     let editBtn = document.createElement('button');
     29     let pencil = document.createElement('strong');
     30     textNode = document.createTextNode("✎");
     31     pencil.setAttribute("class", "green-pencil");
     32     pencil.appendChild(textNode);
     33     editBtn.setAttribute("class", "in-edit-btn");
     34     editBtn.setAttribute("onClick", "editChar(this)");
     35     div.appendChild(editBtn);
     36     editBtn.appendChild(pencil);
     37     textNode = document.createTextNode(" Edit");
     38     editBtn.appendChild(textNode);
     39 
     40     let removeBtn = document.createElement('button');
     41     let x = document.createElement('strong');
     42     textNode = document.createTextNode("X");
     43     x.setAttribute("class", "red-x");
     44     x.appendChild(textNode);
     45     removeBtn.setAttribute("class", "in-rem-btn");
     46     removeBtn.setAttribute("onClick", "removeChar(this)");
     47     div.appendChild(removeBtn);
     48     removeBtn.appendChild(x);
     49     textNode = document.createTextNode(" Remove");
     50     removeBtn.appendChild(textNode);
     51 }
     52 
     53 /* compare()
     54  * @brief Used by sortList() to determine the sorted order. Sorts by greatest to
     55  *          least initative value for each character.
     56  * @see sortList()
     57  */
     58 function compare(a, b) {
     59     if (a.initiative < b.initiative)
     60         return 1;
     61     if (a.initiative > b.initiative)
     62         return -1;
     63     else
     64         return 0;
     65 }
     66 
     67 /* sortList()
     68  * @brief Sort the list of characters by their initative value
     69  * @see compare()
     70  */
     71 function sortList() {
     72     // Sorts in order largest to smallest
     73     charArray.sort(compare);
     74     // Delete all divs of class initiative-div
     75     for (let i = 0; i < charArray.length; i++) {
     76         let element = document.getElementById(i);
     77         element.parentNode.removeChild(element);
     78     }
     79     // Then re-draw them
     80     for (let i = 0; i < charArray.length; i++) {
     81         draw(charArray[i], i);
     82     }
     83 }
     84 
     85 /* addChar()
     86  * @brief Add a character to the list and sort the list
     87  */
     88 function addChar() {
     89     let newChar = {};
     90     nameField = document.getElementById("char-name");
     91     initField = document.getElementById("init-val");
     92     newChar.name = nameField.value;
     93     newChar.initiative = Number(initField.value);
     94     nameField.value = null;
     95     initField.value = null;
     96     if (newChar.name == "" || newChar.initiative == null) {
     97         alert("Please fill out both fields before adding a character.");
     98         return;
     99     }
    100     draw(newChar, charArray.length);
    101     charArray.push(newChar);
    102     sortList();
    103 }
    104 
    105 /* editChar()
    106  * @brief Edit a characters initiative value and re-sort
    107  * @input Elem The "edit" button clicked used to identify which character
    108  *          to edit
    109  */
    110 function editChar(elem) {
    111     let newVal = prompt("Enter the new value:", "0");
    112     if (newVal != null) {
    113         // Edit the value in the array of characters
    114         let charIndex = elem.parentNode.getAttribute("id");
    115         charArray[charIndex].initiative = Number(newVal);
    116         // Modify the number displayed for that character
    117         let parentNode = elem.parentNode;
    118         for (let i = 0; i < elem.parentNode.childNodes.length; i++) {
    119             if (parentNode.childNodes[i].className == "in-val") {
    120                 parentNode.childNodes[i].innerHTML = newVal;
    121                 break;
    122             }
    123         }
    124         sortList();
    125     }
    126     else {
    127         return;
    128     }
    129 }
    130 
    131 /* removeChar()
    132  * @brief Removes a character from the list of characters
    133  * @input Elem The "remove" button clicked used to identify which character
    134  *          to remove
    135  */
    136 function removeChar(elem) {
    137     let charIndex = elem.parentNode.getAttribute("id");
    138     for (let i = 0; i < charArray.length; i++) {
    139         if (i == charIndex) {
    140             charArray.splice(i,1);
    141         }
    142     }
    143     // Delete all divs of class initiative-div
    144     for (let i = 0; i <= charArray.length; i++) {
    145         let element = document.getElementById(i);
    146         element.parentNode.removeChild(element);
    147     }
    148     // Then re-draw them
    149     for (let i = 0; i < charArray.length; i++) {
    150         draw(charArray[i], i);
    151     }
    152 }