All files / public/controller/js view.js

94.28% Statements 33/35
92.85% Branches 13/14
93.33% Functions 14/15
100% Lines 31/31

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78      7x 7x 7x 7x         2x 2x 2x           1x         1x   1x 1x     1x 1x     1x 3x 3x 3x 3x 2x     1x 1x             2x 1x 1x     1x 1x           2x 1x     1x           5x 5x        
export default class View {
 
  constructor () {
    this.buttonStart = document.getElementById('start')
    this.buttonStop = document.getElementById('stop')
    this.buttons = () => Array.from(document.querySelectorAll('button'))
    this.ignoreButtons = new Set(['unassigned'])
  }
 
 
  changeCommandButtonsVisibility (unassigned) {
    Array.from(document.querySelectorAll('[name=command]')).forEach(button => {
      const fn = unassigned ? 'add' : 'remove'
      button.classList[fn]('unassigned')
    })
  }
 
 
  onLoad () {
    this.changeCommandButtonsVisibility(true)
  }
 
 
  setButtonsOnClick (onClick) {
    const actions = {
      start: () => {
        this.toggleButtonStart(true)
        this.changeCommandButtonsVisibility(false)
      },
      stop: () => {
        this.toggleButtonStart(false)
        this.changeCommandButtonsVisibility(true)
      }
    }
    this.buttons()
      .forEach(button => button.onclick = () => {
        Iif (this.unassignedButton(button) || !button.id) return
        onClick(button.id)
        if (actions[button.id]) {
          actions[button.id]()
        }
        else {
          this.toggleDisableButton(button.classList)
          setTimeout(() => this.toggleDisableButton(button.classList), 1000)
        }
      })
  }
 
 
  toggleButtonStart (active) {
    if (active) {
      this.buttonStart.classList.add('hidden')
      this.buttonStop.classList.remove('hidden')
    }
    else {
      this.buttonStop.classList.add('hidden')
      this.buttonStart.classList.remove('hidden')
    }
  }
 
 
  toggleDisableButton (classList) {
    if (classList.contains('active')) {
      classList.remove('active')
    }
    else {
      classList.add('active')
    }
  }
 
 
  unassignedButton (button) {
    const classes = Array.from(button.classList)
    return classes.find(className => this.ignoreButtons.has(className))
      ? true : false
  }
 
}