all files / addon/mixins/ checkbox.js

77.27% Statements 34/44
73.68% Branches 28/38
100% Functions 4/4
77.27% Lines 34/44
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                    40× 40× 40×     40× 40×   37×   40×         40×   40× 40× 40× 40×     40×   40×   40×                               12×                                              
import Ember from 'ember';
import Base from './base';
 
/*
 * Checkbox Component Mixin
 */
var CheckboxMixin = Ember.Mixin.create(Base, {
  module: 'checkbox',
  classNames: ['ui', 'checkbox'],
 
  willInitSemantic(settings) {
    let owner = Ember.getOwner(this);
    let fastboot = owner.lookup('service:fastboot');
    Iif (fastboot && fastboot.get('isFastBoot')) {
      return;
    }
    this._super(...arguments);
    if (settings.onChange) {
      // Checkbox and radio both have an implementation for this
      settings.onChange = this.get('_onChange');
    }
    if (this._hasOwnProperty(this.attrs, 'readonly') || this.get('readonly') != null) {
      this.$().toggleClass('read-only', this.get('readonly'));
    }
  },
 
  didInitSemantic() {
    this._super(...arguments);
    // We need to fake that its bindable for checked and disabled
    this._setAttrBindable('checked');
    this._setAttrBindable('disabled');
    this._setAttrBindable('enabled');
    if (this.get('readonly') != null) {
      this.get('_settableAttrs').addObject('readonly');
    }
    // Init initial value set properties correctly
    if (this.get('checked') != null) {
      this.setSemanticAttr('checked', this.get('checked'));
    }
    if (this.get('disabled') != null) {
      this.setSemanticAttr('disabled', this.get('disabled'));
    }
    Iif (this.get('enabled') != null) {
      this.setSemanticAttr('enabled', this.get('enabled'));
    }
  },
 
  getSemanticAttr(attrName) {
    if (attrName === 'checked') {
      return this.execute('is checked');
    }
    Eif (attrName === 'disabled') {
      return this.execute('is disabled');
    }
    if (attrName === 'enabled') {
      return this.execute('is enabled');
    }
    return this._super(...arguments);
  },
 
  setSemanticAttr(attrName, attrValue) {
    // Handle checked
    if (attrName === 'checked') {
      Iif (attrValue) {
        return this.execute('set checked');
      }
      return this.execute('set unchecked');
    }
    // Handle disabled
    if (attrName === 'disabled') {
      if (attrValue) {
        return this.execute('set disabled');
      }
      return this.execute('set enabled');
    }
    // Handle enabled
    Iif (attrName === 'enabled') {
      if (attrValue) {
        return this.execute('set enabled');
      }
      return this.execute('set disabled');
    }
    // Handle readonly
    Eif (attrName === 'readonly') {
      // We need to add a class verses updating the property, since semantic is caching the value internall
      return this.$().toggleClass('read-only', attrValue);
    }
    // Default
    return this._super(...arguments);
  }
});
 
export default CheckboxMixin;