All files actions.js

85.29% Statements 29/34
68.75% Branches 11/16
80% Functions 8/10
85.29% Lines 29/34
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 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132    2x                   7x                 3x   3x 3x 2x 2x       3x 3x                                                     4x 4x         1x         2x                       1x 1x 1x         1x   1x       1x 2x 1x 1x 1x       1x   1x                       1x   1x 2x 1x                      
import debug from 'debug'
 
var log = debug('facond.action')
 
/**
 * Apply a reversible action on the DOM if conditions apply, unapply otherwise.
 */
class Action {
  /**
  * @param conditions List of :js:class:`Condition` objects
  */
  constructor(conditions) {
    this.conditions = conditions || []
  }
 
  /**
   * Call :js:method:`apply()` if conditions validate, :js:method:`unapply` otherwise.
   *
   * @param form :js:class:`Form` object to execute on.
   */
  execute(form) {
    let method = 'apply'
 
    for (var i=0; i<this.conditions.length; i++) {
      if (!this.conditions[i].validate(form)) {
        method = 'unapply'
        break
      }
    }
 
    log(this, '.', method, '(', this.field, ')')
    this[method](form)
  }
 
  /**
   * Modify the DOM, should save the state for :js:method:`unapply()`.
   */
  apply(form) {
    'not implemented'
  }
 
  /**
   * Restore the DOM prior to :js:method:`apply()` call.
   */
  unapply(form) {
    'not implemented'
  }
}
 
/**
 * Remove a field from a form.
 */
class RemoveField extends Action {
  /**
  * @param conditions List of :js:class:`Condition` objects
  * @param :js:class:`Field` Subject Field instance.
  */
  constructor(conditions, field) {
    super(conditions)
    this.field = field
  }
 
  // Hide the field.
  apply(form) {
    form.field(this.field).hide()
  }
 
  // Show the field.
  unapply(form) {
    form.field(this.field).show()
  }
}
 
/**
 * Remove given choices from a field.
 */
class RemoveChoices extends Action {
  /**
  * @param conditions List of :js:class:`Condition` objects
  */
  constructor(conditions, field, choices) {
    super(conditions)
    this.field = field
    this.choices = choices
  }
 
  // Hide options which are not in this.choices from a field.
  apply(form) {
    let field = form.field(this.field)
 
    Iif (this.choices.indexOf(field).value >= 0) {
      field.valueReset()
    }
 
    for (let i=0; i < field.element.options.length; i++) {
      if (this.choices.indexOf(field.element.options[i].value) >= 0) {
        let option = field.element.options[i]
        option.classList.add('facond-hide')
        option.selected = false
      }
    }
 
    Eif (!field.multiple) {
      // If selected value was removed, empty the field
      Iif (this.choices.indexOf(field.value) >= 0) {
        let empty = field.element.querySelector('option[value=""]')
        if (empty === undefined) {
          field.element.prepend('<option value=""></option>')
        }
        field.value = ''
      }
    }
  }
 
  // Show options which are not in this.choices from a field.
  unapply(form) {
    let  field = form.field(this.field)
 
    for (let i=0; i < field.element.options.length; i++) {
      if (!this.choices.indexOf(field.element.options[i].value)) {
        field.element.options[i].classList.remove('facond-hide')
      }
    }
  }
}
 
export {
  Action,
  RemoveChoices,
  RemoveField
}