All files / public/controller/js view.js

100% Statements 48/48
100% Branches 12/12
100% Functions 18/18
100% Lines 45/45

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 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112    17x 17x 17x   17x   17x   17x       1x 1x       2x   2x 2x   2x       1x               1x 1x 1x 1x   1x 1x         3x 3x   2x 1x 1x     1x                 1x   1x   1x 1x 1x           2x 1x 1x     1x               1x 1x   1x       2x   2x     2x 1x 1x 1x     1x 1x      
export default class View {
  constructor() {
    this.btnStart = document.getElementById('start') 
    this.btnStop = document.getElementById('stop') 
    this.buttons = () => Array.from(document.querySelectorAll('button'))
 
    this.ignoreButtons = new Set(['unassigned'])
    async function onBtnClick () {}
    this.onBtnClick = onBtnClick
 
    this.DISABLE_BTN_TIMEOUT = 500
  }
 
  onLoad() {
    this.changeCommandBtnsVisibility()
    this.btnStart.onclick = this.onStartClicked.bind(this)
  }
 
  changeCommandBtnsVisibility(hide = true) {
    Array.from(document.querySelectorAll('[name=command]'))
    .forEach(btn => {
      const fn = hide ? 'add' : 'remove'
      btn.classList[fn]('unassigned')
      function onClickReset() {}
      btn.onclick = onClickReset
    })
  }
  configureOnBtnClick(fn) {
    this.onBtnClick = fn
  }
 
  async onStartClicked({
    srcElement: {
      innerText
    }
  }) {
    const btnText =  innerText
    await this.onBtnClick(btnText)
    this.toggleBtnStart()
    this.changeCommandBtnsVisibility(false)
 
    this.buttons()
      .filter(btn => this.notIsUnassignedButton(btn))
      .forEach(this.setupBtnAction.bind(this))
  }
 
  setupBtnAction(btn) {
    const text = btn.innerText.toLowerCase()
    if(text.includes('start')) return;
 
    if(text.includes('stop')) {
      btn.onclick = this.onStopBtn.bind(this)
      return
    }
 
    btn.onclick = this.onCommandClick.bind(this)
  }
 
  async onCommandClick(btn) {
    const { 
      srcElement: {
        classList,
        innerText
      }
    } = btn
 
    this.toggleDisableCommandBtn(classList)
 
    await this.onBtnClick(innerText)
    setTimeout(
      () => this.toggleDisableCommandBtn(classList),
      this.DISABLE_BTN_TIMEOUT
    )
  }
 
  toggleDisableCommandBtn(classList) {
    if(!classList.contains('active')) {
      classList.add('active')
      return
    }
 
    classList.remove('active')
  }
 
  onStopBtn({
    srcElement: {
      innerText
    }
  }) {
    this.toggleBtnStart(false)
    this.changeCommandBtnsVisibility(true)
 
    return this.onBtnClick(innerText)
  }
 
  notIsUnassignedButton(btn) {
    const classes = Array.from(btn.classList)
 
    return !(!!classes.find(item => this.ignoreButtons.has(item)))
  }
  toggleBtnStart(active = true) {
    if(active) {
      this.btnStart.classList.add('hidden')
      this.btnStop.classList.remove('hidden')
      return;
    }
 
    this.btnStop.classList.add('hidden')
    this.btnStart.classList.remove('hidden')
 
  }
}