all files / addon/components/ ui-radio.js

85.71% Statements 24/28
80% Branches 16/20
100% Functions 6/6
85.71% Lines 24/28
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                          36×   36×                     36× 36×       25× 25×       61× 61×     61×                               53×       61× 61×   21× 15×       40×            
import Ember from 'ember';
import Checkbox from '../mixins/checkbox';
import isPromise from 'ember-promise-tools/utils/is-promise';
import isFulfilled from 'ember-promise-tools/utils/is-fulfilled';
import getPromiseContent from 'ember-promise-tools/utils/get-promise-content';
import PromiseResolver from 'ember-promise-tools/mixins/promise-resolver';
 
export default Ember.Component.extend(Checkbox, PromiseResolver, {
  type: 'radio',
  classNames: ['radio'],
  ignorableAttrs: ['checked', 'label', 'disabled', 'value', 'current'],
 
  init() {
    this._super(...arguments);
 
    Iif (Ember.isBlank(this.get('name'))) {
      this.set('name', 'default');
      Ember.Logger.warn("The required component parameter of 'name' was not passed into the ui-radio component");
    }
  },
 
  // Internal wrapper for onchange, to pass through checked
  _onChange() {
    let value = this.get('value');
    return this.attrs.onChange(value, this);
  },
 
  didInitSemantic() {
    this._super(...arguments);
    this._inspectValueAndCurrent();
  },
 
  didUpdateAttrs() {
    this._super(...arguments);
    this._inspectValueAndCurrent();
  },
 
  _inspectValueAndCurrent() {
    let value = this.get('value');
    let current = this.get('current');
    // If either are a promise, we need to make sure both are resolved
    // Or wait for them to resolve
    if (isPromise(value) || isPromise(current)) {
 
      // This code is probably overkill, but i wanted to ensure that
      // if the promises are resolved we render as soon as possible instead of waiting
      // for the hash to resolve each time
      if (isPromise(value)) {
        Eif (!isFulfilled(value)) {
          return this.resolvePromise(Ember.RSVP.hash({ value, current }), this._checkValueAndCurrent);
        } else {
          value = getPromiseContent(value);
        }
      }
 
      Eif (isPromise(current)) {
        Eif (!isFulfilled(current)) {
          return this.resolvePromise(Ember.RSVP.hash({ value, current }), this._checkValueAndCurrent);
        } else {
          current = getPromiseContent(current);
        }
      }
    }
    // If we didn't return, the promises are either fulfilled or not promises
    this._checkValueAndCurrent({ value, current });
  },
 
  _checkValueAndCurrent(hash) {
    let isChecked = this.execute('is checked');
    if (this.areAttrValuesEqual('checked', hash.value, hash.current)) {
      // Value and current match, but radio isn't checked, return false
      if (!isChecked) {
        return this.execute('set checked');
      }
    } else {
      // Value and current don't match and radio is checked, return false
      if (isChecked) {
        return this.execute('set unchecked');
      }
    }
  }
});