|  | 
| 1 |  | -/* eslint-disable ember/no-classic-components, ember/no-get, ember/no-mixins, ember/require-tagless-components */ | 
| 2 | 1 | /** | 
| 3 | 2 |  * @module ember-paper | 
| 4 | 3 |  */ | 
|  | 4 | +import Focusable from './-focusable'; | 
| 5 | 5 | import { inject as service } from '@ember/service'; | 
| 6 |  | - | 
| 7 |  | -import { computed } from '@ember/object'; | 
| 8 |  | -import { not, and } from '@ember/object/computed'; | 
| 9 |  | -import Component from '@ember/component'; | 
|  | 6 | +import { action } from '@ember/object'; | 
|  | 7 | +import { guidFor } from '@ember/object/internals'; | 
| 10 | 8 | import { assert } from '@ember/debug'; | 
| 11 |  | -import FocusableMixin from 'ember-paper/mixins/focusable-mixin'; | 
| 12 |  | -import ProxiableMixin from 'ember-paper/mixins/proxiable-mixin'; | 
| 13 |  | -import { invokeAction } from 'ember-paper/utils/invoke-action'; | 
|  | 9 | + | 
| 14 | 10 | /** | 
| 15 | 11 |  * @class PaperCheckbox | 
| 16 |  | - * @extends Ember.Component | 
| 17 |  | - * @uses FocusableMixin | 
| 18 |  | - * @uses ProxiableMixin | 
|  | 12 | + * @extends Component | 
| 19 | 13 |  */ | 
| 20 |  | -export default Component.extend(FocusableMixin, ProxiableMixin, { | 
| 21 |  | -  tagName: 'md-checkbox', | 
| 22 |  | -  classNames: ['md-checkbox', 'md-default-theme'], | 
| 23 |  | -  classNameBindings: [ | 
| 24 |  | -    'isChecked:md-checked', | 
| 25 |  | -    'indeterminate:md-indeterminate', | 
| 26 |  | -    'warn:md-warn', | 
| 27 |  | -    'accent:md-accent', | 
| 28 |  | -    'primary:md-primary', | 
| 29 |  | -  ], | 
| 30 |  | - | 
| 31 |  | -  attributeBindings: [ | 
| 32 |  | -    'role:role', | 
| 33 |  | -    'ariaLabel:aria-label', | 
| 34 |  | -    'ariaChecked:aria-checked', | 
| 35 |  | -    'labelId:aria-labelledby', | 
| 36 |  | -  ], | 
| 37 |  | - | 
| 38 |  | -  /* FocusableMixin Overrides */ | 
| 39 |  | -  focusOnlyOnKey: true, | 
| 40 |  | - | 
| 41 |  | -  constants: service(), | 
| 42 |  | -  value: false, | 
| 43 |  | -  role: 'checkbox', | 
| 44 |  | -  notIndeterminate: not('indeterminate'), | 
| 45 |  | -  isChecked: and('notIndeterminate', 'value'), | 
| 46 |  | - | 
| 47 |  | -  ariaChecked: computed('isChecked', 'indeterminate', function () { | 
|  | 14 | +export default class PaperCheckbox extends Focusable { | 
|  | 15 | +  /** | 
|  | 16 | +   * Service containing media query breakpoints and constants | 
|  | 17 | +   */ | 
|  | 18 | +  @service constants; | 
|  | 19 | + | 
|  | 20 | +  /** | 
|  | 21 | +   * Reference to the component's DOM element | 
|  | 22 | +   * @type {HTMLElement} | 
|  | 23 | +   */ | 
|  | 24 | +  element; | 
|  | 25 | +  /** | 
|  | 26 | +   * The parent this component is bound to. | 
|  | 27 | +   * @type {Boolean} | 
|  | 28 | +   */ | 
|  | 29 | +  parent; | 
|  | 30 | +  /** | 
|  | 31 | +   * Marks whether the component should register itself to the supplied parent | 
|  | 32 | +   * @type {Boolean} | 
|  | 33 | +   */ | 
|  | 34 | +  shouldRegister; | 
|  | 35 | +  /** | 
|  | 36 | +   * Marks whether the component should skip being proxied. | 
|  | 37 | +   * @type {Boolean} | 
|  | 38 | +   */ | 
|  | 39 | +  skipProxy; | 
|  | 40 | +  /** | 
|  | 41 | +   * provides a globally unique component id for tracking bindings between aria | 
|  | 42 | +   * tags and labels. | 
|  | 43 | +   * @type {string} | 
|  | 44 | +   */ | 
|  | 45 | +  labelId; | 
|  | 46 | + | 
|  | 47 | +  /* Focusable Overrides */ | 
|  | 48 | +  focusOnlyOnKey = true; | 
|  | 49 | + | 
|  | 50 | +  constructor(owner, args) { | 
|  | 51 | +    super(owner, args); | 
|  | 52 | + | 
|  | 53 | +    this.labelId = `${guidFor(args)}-label`; | 
|  | 54 | +    this.shouldRegister = this.args.shouldRegister || false; | 
|  | 55 | +    this.skipProxy = this.args.skipProxy || false; | 
|  | 56 | + | 
|  | 57 | +    if (this.shouldRegister) { | 
|  | 58 | +      assert( | 
|  | 59 | +        'A parent component should be supplied to <PaperCheckbox> when shouldRegister=true', | 
|  | 60 | +        this.args.parentComponent | 
|  | 61 | +      ); | 
|  | 62 | +      this.parent = this.args.parentComponent; | 
|  | 63 | +    } | 
|  | 64 | + | 
|  | 65 | +    assert( | 
|  | 66 | +      '<PaperCheckbox> requires an `onChange` action or null for no action.', | 
|  | 67 | +      this.args.onChange !== undefined | 
|  | 68 | +    ); | 
|  | 69 | +  } | 
|  | 70 | + | 
|  | 71 | +  /** | 
|  | 72 | +   * Performs any required DOM setup. | 
|  | 73 | +   * @param element | 
|  | 74 | +   */ | 
|  | 75 | +  @action didInsertNode(element) { | 
|  | 76 | +    this.element = element; | 
|  | 77 | +    this.registerListeners(element); | 
|  | 78 | + | 
|  | 79 | +    if (this.shouldRegister) { | 
|  | 80 | +      this.parent.registerChild(this); | 
|  | 81 | +    } | 
|  | 82 | +  } | 
|  | 83 | + | 
|  | 84 | +  @action didUpdateNode() { | 
|  | 85 | +    // noop | 
|  | 86 | +  } | 
|  | 87 | + | 
|  | 88 | +  /** | 
|  | 89 | +   * Performs any required DOM teardown. | 
|  | 90 | +   * @param element | 
|  | 91 | +   */ | 
|  | 92 | +  @action willDestroyNode(element) { | 
|  | 93 | +    this.unregisterListeners(element); | 
|  | 94 | +  } | 
|  | 95 | + | 
|  | 96 | +  willDestroy() { | 
|  | 97 | +    super.willDestroy(...arguments); | 
|  | 98 | + | 
|  | 99 | +    if (this.shouldRegister) { | 
|  | 100 | +      this.parent.unregisterChild(this); | 
|  | 101 | +    } | 
|  | 102 | +  } | 
|  | 103 | + | 
|  | 104 | +  get indeterminate() { | 
|  | 105 | +    return this.args.indeterminate || false; | 
|  | 106 | +  } | 
|  | 107 | + | 
|  | 108 | +  get notIndeterminate() { | 
|  | 109 | +    return !this.indeterminate; | 
|  | 110 | +  } | 
|  | 111 | + | 
|  | 112 | +  get value() { | 
|  | 113 | +    return this.args.value || false; | 
|  | 114 | +  } | 
|  | 115 | + | 
|  | 116 | +  get isChecked() { | 
|  | 117 | +    return this.notIndeterminate && this.value; | 
|  | 118 | +  } | 
|  | 119 | + | 
|  | 120 | +  get ariaChecked() { | 
| 48 | 121 |     if (this.indeterminate) { | 
| 49 | 122 |       return 'mixed'; | 
| 50 | 123 |     } | 
| 51 | 124 | 
 | 
| 52 | 125 |     return this.isChecked ? 'true' : 'false'; | 
| 53 |  | -  }), | 
|  | 126 | +  } | 
| 54 | 127 | 
 | 
| 55 |  | -  labelId: computed('elementId', function () { | 
| 56 |  | -    return `${this.elementId}-label`; | 
| 57 |  | -  }), | 
|  | 128 | +  get bubbles() { | 
|  | 129 | +    return this.args.bubbles === undefined || this.args.bubbles; | 
|  | 130 | +  } | 
| 58 | 131 | 
 | 
| 59 |  | -  init() { | 
| 60 |  | -    this._super(...arguments); | 
| 61 |  | -    assert( | 
| 62 |  | -      '{{paper-checkbox}} requires an `onChange` action or null for no action.', | 
| 63 |  | -      this.onChange !== undefined | 
| 64 |  | -    ); | 
| 65 |  | -  }, | 
| 66 |  | - | 
| 67 |  | -  click() { | 
|  | 132 | +  @action onClick(e) { | 
|  | 133 | +    console.log('checkbox:onClick'); | 
| 68 | 134 |     if (!this.disabled) { | 
| 69 |  | -      invokeAction(this, 'onChange', !this.value); | 
|  | 135 | +      if (this.args.onChange) { | 
|  | 136 | +        this.args.onChange(!this.value); | 
|  | 137 | +      } | 
|  | 138 | + | 
|  | 139 | +      // Prevent bubbling, if specified. If undefined, the event will bubble. | 
|  | 140 | +      if (!this.bubbles) { | 
|  | 141 | +        e.stopPropagation(); | 
|  | 142 | +      } | 
| 70 | 143 |     } | 
| 71 |  | -    // Prevent bubbling, if specified. If undefined, the event will bubble. | 
| 72 |  | -    return this.bubbles; | 
| 73 |  | -  }, | 
|  | 144 | +  } | 
| 74 | 145 | 
 | 
| 75 |  | -  keyPress(ev) { | 
|  | 146 | +  @action onKeyPress(e) { | 
| 76 | 147 |     if ( | 
| 77 |  | -      ev.which === this.get('constants.KEYCODE.SPACE') || | 
| 78 |  | -      ev.which === this.get('constants.KEYCODE.ENTER') | 
|  | 148 | +      e.which === this.constants.KEYCODE.SPACE || | 
|  | 149 | +      e.which === this.constants.KEYCODE.ENTER | 
| 79 | 150 |     ) { | 
| 80 |  | -      ev.preventDefault(); | 
| 81 |  | -      this.click(); | 
|  | 151 | +      e.preventDefault(); | 
|  | 152 | +      this.onClick(); | 
| 82 | 153 |     } | 
| 83 |  | -  }, | 
|  | 154 | +  } | 
| 84 | 155 | 
 | 
| 85 | 156 |   processProxy() { | 
| 86 |  | -    invokeAction(this, 'onChange', !this.value); | 
| 87 |  | -  }, | 
| 88 |  | -}); | 
|  | 157 | +    console.log('checkbox:processProxy'); | 
|  | 158 | +    if (this.args.onChange) { | 
|  | 159 | +      this.args.onChange(!this.value); | 
|  | 160 | +    } | 
|  | 161 | +  } | 
|  | 162 | +} | 
0 commit comments