Advertisement
Guest User

Untitled

a guest
May 25th, 2019
100
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.13 KB | None | 0 0
  1. /**
  2. * Add a character line.
  3. */
  4. function addCharacter() {
  5. const addChar = document.querySelectorAll( '[id^="add-tmp-char-"]' );
  6.  
  7. // Loop through added character inputs.
  8. for ( const tmpCharButton of addChar ) {
  9. // Add a click handler for this particular input.
  10. tmpCharButton.addEventListener( 'click', () => {
  11. const id = getId( tmpCharButton );
  12. const thisItem = document.getElementById( `character-${id}` )
  13. const charInput = document.getElementById( `addChar-${id}` );
  14. const charName = charInput ? charInput.value : false;
  15. const dexInput = document.getElementById( `charDex-${id}` );
  16. const dexVal = dexInput ? dexInput.value : false;
  17.  
  18. if ( charName && charName !== 'undefined' && charName !== '' ) {
  19. // Remove the old input and button.
  20. charInput.remove();
  21.  
  22. // Only remove the add button if we have a Dex value.
  23. if ( dexVal !== 'undefined' && dexVal !== '' ) {
  24. tmpCharButton.remove();
  25. } else {
  26. // We still need to add the dex. Change the button value to indicate that a Dex score is still needed.
  27. const buttonTxt = `${tmpCharButton.textContent} Dex`;
  28. tmpCharButton.textContent = buttonTxt;
  29. }
  30.  
  31. charSpan = document.createElement('span');
  32. charSpan.setAttribute( 'id', `addChar-${id}` );
  33. charSpan.classList.add('character-name');
  34. charSpan.textContent = charName;
  35. thisItem.appendChild( charSpan );
  36. }
  37.  
  38. if ( dexVal && dexVal !== 'undefined' && dexVal !== '' ) {
  39. // Remove the old input and button.
  40. dexInput.remove();
  41.  
  42. // Only remove the add button if we have a character name.
  43. if ( charName !== 'undefined' && charName !== '' ) {
  44. tmpCharButton.remove();
  45. } else {
  46. // We still need to add the character name. Change the button value to indicate that a name is still needed.
  47. const buttonTxt = `${tmpCharButton.textContent} Name`;
  48. tmpCharButton.textContent = buttonTxt;
  49. }
  50.  
  51. dexSpan = document.createElement('span');
  52. dexSpan.setAttribute( 'id', `charDex-${id}` );
  53. dexSpan.classList.add('character-init-bonus');
  54. dexSpan.textContent = `Initiative Bonus: ${getModifier( dexVal )}`;
  55. thisItem.appendChild( dexSpan );
  56. }
  57. } );
  58. }
  59. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement