All files action.js

8.7% Statements 2/23
20% Branches 2/10
11.11% Functions 1/9
8.7% Lines 2/23
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    1x       1x                                                                                                                                          
import debug from 'debug'
 
var log = debug('ddf.action')
 
class Action {
  constructor(conditions) {
    this.conditions = conditions || []
  }
 
  apply(form, field) {
    let method = 'apply'
 
    for (var i=0; i<this.conditions.length; i++) {
      if (!this.conditions[i].validate(form)) {
        method = 'unapply'
        break
      }
    }
 
    log(this, '.', method, '(', form, ',', field, ')')
    this[method](form, field)
  }
}
 
// Remove a field from a form.
class Remove extends Action {
  // Hide the field.
  apply(form, field) {
    form.fieldHide(field)
  }
 
  // Show the field.
  unapply(form, field) {
    form.fieldShow(field)
  }
}
 
// Remove given choices from a field.
class RemoveChoices extends Action {
  constructor(choices) {
    super()
    this.choices = choices
  }
 
  // Hide options which are not in this.choices from a field.
  apply(form, field) {
    choices = this.choices
 
    if (choices.indexOf(form.fieldValueGet(field)) >= 0) {
      form.fieldValueClear(field)
    }
 
    form.fieldGet(field).find('option').each(function() {
      if (choices.indexOf($(this).attr('value')) >= 0) {
        $(this).hide()
      }
    })
  }
 
  // Show options which are not in this.choices from a field.
  unapply(form, field) {
    choices = this.choices
    form.fieldGet(field).find('option').each(function() {
      if (!$(this).attr('value').indexOf(choices)) {
        $(this).show()
      }
    })
  }
}
 
export {
  Action,
  Remove,
  RemoveChoices
}