|  | 
| 2 | 2 |  * @module ember-paper | 
| 3 | 3 |  */ | 
| 4 | 4 | import Focusable from './-focusable'; | 
|  | 5 | +import { tracked } from '@glimmer/tracking'; | 
| 5 | 6 | import { action } from '@ember/object'; | 
|  | 7 | +import { assert } from '@ember/debug'; | 
| 6 | 8 | 
 | 
| 7 | 9 | /** | 
| 8 | 10 |  * @class PaperButton | 
| 9 | 11 |  * @extends Focusable | 
| 10 | 12 |  */ | 
| 11 | 13 | export default class PaperButton extends Focusable { | 
|  | 14 | +  /** | 
|  | 15 | +   * Reference to the component's DOM element | 
|  | 16 | +   * @type {HTMLElement} | 
|  | 17 | +   */ | 
|  | 18 | +  @tracked element; | 
|  | 19 | +  /** | 
|  | 20 | +   * The parent this component is bound to. | 
|  | 21 | +   * @type {Boolean} | 
|  | 22 | +   */ | 
|  | 23 | +  parent; | 
|  | 24 | +  /** | 
|  | 25 | +   * Marks whether the component should register itself to the supplied parent | 
|  | 26 | +   * @type {Boolean} | 
|  | 27 | +   */ | 
|  | 28 | +  shouldRegister; | 
|  | 29 | + | 
|  | 30 | +  constructor(owner, args) { | 
|  | 31 | +    super(owner, args); | 
|  | 32 | + | 
|  | 33 | +    this.shouldRegister = this.args.shouldRegister || false; | 
|  | 34 | +    if (this.shouldRegister) { | 
|  | 35 | +      assert( | 
|  | 36 | +        'A parent component should be supplied to <PaperButton> when shouldRegister=true', | 
|  | 37 | +        this.args.parent | 
|  | 38 | +      ); | 
|  | 39 | +      this.parent = this.args.parent; | 
|  | 40 | +    } | 
|  | 41 | +  } | 
|  | 42 | + | 
|  | 43 | +  /** | 
|  | 44 | +   * Performs any required DOM setup. | 
|  | 45 | +   * @param element | 
|  | 46 | +   */ | 
|  | 47 | +  @action didInsertNode(element) { | 
|  | 48 | +    this.element = element; | 
|  | 49 | +    this.registerListeners(element); | 
|  | 50 | + | 
|  | 51 | +    if (this.shouldRegister) { | 
|  | 52 | +      this.parent.registerChild(this); | 
|  | 53 | +    } | 
|  | 54 | +  } | 
|  | 55 | + | 
|  | 56 | +  /** | 
|  | 57 | +   * Performs DOM updates based on tracked args. | 
|  | 58 | +   */ | 
|  | 59 | +  @action didUpdateNode() { | 
|  | 60 | +    // noop | 
|  | 61 | +  } | 
|  | 62 | + | 
|  | 63 | +  /** | 
|  | 64 | +   * Performs any required DOM teardown. | 
|  | 65 | +   * @param element | 
|  | 66 | +   */ | 
|  | 67 | +  @action willDestroyNode(element) { | 
|  | 68 | +    this.deregisterListeners(element); | 
|  | 69 | +  } | 
|  | 70 | + | 
|  | 71 | +  willDestroy() { | 
|  | 72 | +    super.willDestroy(...arguments); | 
|  | 73 | + | 
|  | 74 | +    if (this.shouldRegister) { | 
|  | 75 | +      this.parent.deregisterChild(this); | 
|  | 76 | +    } | 
|  | 77 | +  } | 
|  | 78 | + | 
| 12 | 79 |   get tag() { | 
| 13 | 80 |     if (this.args.href) { | 
| 14 | 81 |       return 'a'; | 
| @@ -41,4 +108,39 @@ export default class PaperButton extends Focusable { | 
| 41 | 108 | 
 | 
| 42 | 109 |     return this.args.bubbles; | 
| 43 | 110 |   } | 
|  | 111 | + | 
|  | 112 | +  // Proxiable Handlers | 
|  | 113 | + | 
|  | 114 | +  @action handleMouseDown(e) { | 
|  | 115 | +    super.handleMouseDown(e); | 
|  | 116 | + | 
|  | 117 | +    let parentComponent = this.parentComponent; | 
|  | 118 | +    if (parentComponent) { | 
|  | 119 | +      parentComponent.mouseActive = true; | 
|  | 120 | +      setTimeout(() => { | 
|  | 121 | +        if (parentComponent.isDestroyed) { | 
|  | 122 | +          return; | 
|  | 123 | +        } | 
|  | 124 | +        parentComponent.mouseActive = false; | 
|  | 125 | +      }, 100); | 
|  | 126 | +    } | 
|  | 127 | +  } | 
|  | 128 | + | 
|  | 129 | +  @action handleFocusIn(e) { | 
|  | 130 | +    super.handleFocusIn(e); | 
|  | 131 | + | 
|  | 132 | +    let parentComponent = this.parent; | 
|  | 133 | +    if (parentComponent && !parentComponent.mouseActive) { | 
|  | 134 | +      parentComponent.focused = true; | 
|  | 135 | +    } | 
|  | 136 | +  } | 
|  | 137 | + | 
|  | 138 | +  @action focusOut(e) { | 
|  | 139 | +    super.focusOut(e); | 
|  | 140 | + | 
|  | 141 | +    let parentComponent = this.parent; | 
|  | 142 | +    if (parentComponent) { | 
|  | 143 | +      parentComponent.focused = false; | 
|  | 144 | +    } | 
|  | 145 | +  } | 
| 44 | 146 | } | 
0 commit comments