|
| 1 | +import Component from '@glimmer/component'; |
| 2 | +import debugLogger from 'ember-debug-logger'; |
| 3 | + |
| 4 | +import { action } from '@ember/object'; |
| 5 | +import { tracked } from '@glimmer/tracking'; |
| 6 | + |
| 7 | +export default class MdcDrawerComponent extends Component { |
| 8 | + // #region Accessed Services |
| 9 | + // #endregion |
| 10 | + |
| 11 | + // #region Tracked Attributes |
| 12 | + @tracked isOpen = false; |
| 13 | + // #endregion |
| 14 | + |
| 15 | + // #region Constructor |
| 16 | + constructor() { |
| 17 | + super(...arguments); |
| 18 | + this.#debug(`constructor`); |
| 19 | + |
| 20 | + this.#controls.open = this?._open; |
| 21 | + this.#controls.close = this?._close; |
| 22 | + this.#controls.toggle = this?._toggle; |
| 23 | + this.#controls.name = this?.args?.name ?? 'mdc-sidebar'; |
| 24 | + |
| 25 | + if (this?.args?.open || this?.args?.locked) this.isOpen = true; |
| 26 | + } |
| 27 | + // #endregion |
| 28 | + |
| 29 | + // #region Lifecycle Hooks |
| 30 | + willDestroy() { |
| 31 | + this.#debug(`willDestroy`); |
| 32 | + |
| 33 | + if (this?.args?.controlElement) { |
| 34 | + this.#debug(`willDestroy: unregistering with control`); |
| 35 | + this?.args?.controlElement?.sidebarControls?.unregisterSidebar?.( |
| 36 | + this.#element |
| 37 | + ); |
| 38 | + } |
| 39 | + |
| 40 | + super.willDestroy(...arguments); |
| 41 | + } |
| 42 | + // #endregion |
| 43 | + |
| 44 | + // #region DOM Event Handlers |
| 45 | + @action |
| 46 | + onClickOutside(event) { |
| 47 | + this.#debug(`onClickOutside: `, event); |
| 48 | + |
| 49 | + const isEventOnControl = |
| 50 | + event.target === this?.args?.controlElement || |
| 51 | + this?.args?.controlElement?.contains?.(event.target); |
| 52 | + |
| 53 | + if (isEventOnControl) { |
| 54 | + this.#debug( |
| 55 | + `onClickOutside::isEventOnControl: ${isEventOnControl}` |
| 56 | + ); |
| 57 | + return; |
| 58 | + } |
| 59 | + |
| 60 | + this?._close?.(); |
| 61 | + } |
| 62 | + |
| 63 | + @action |
| 64 | + storeElement(element) { |
| 65 | + this.#debug(`storeElement: `, element); |
| 66 | + this.#element = element; |
| 67 | + |
| 68 | + this?.registerWithControl?.(); |
| 69 | + } |
| 70 | + |
| 71 | + @action |
| 72 | + registerWithControl() { |
| 73 | + this.#debug(`registerWithControl`); |
| 74 | + |
| 75 | + if (!this.#element) { |
| 76 | + this.#debug(`registerWithControl: element not in dom. aborting...`); |
| 77 | + return; |
| 78 | + } |
| 79 | + |
| 80 | + this?.args?.controlElement?.sidebarControls?.registerSidebar?.( |
| 81 | + this.#element, |
| 82 | + this.#controls |
| 83 | + ); |
| 84 | + } |
| 85 | + // #endregion |
| 86 | + |
| 87 | + // #region Computed Properties |
| 88 | + get headerComponent() { |
| 89 | + return this?._getComputedSubcomponent?.('header'); |
| 90 | + } |
| 91 | + |
| 92 | + get contentComponent() { |
| 93 | + return this?._getComputedSubcomponent?.('content'); |
| 94 | + } |
| 95 | + // #endregion |
| 96 | + |
| 97 | + // #region Private Methods |
| 98 | + @action |
| 99 | + _open() { |
| 100 | + this.#debug(`_open`); |
| 101 | + this.isOpen = true; |
| 102 | + } |
| 103 | + |
| 104 | + @action |
| 105 | + _close() { |
| 106 | + this.#debug(`_close`); |
| 107 | + |
| 108 | + if (this?.args?.locked) { |
| 109 | + this.#debug(`_close: sidebar is locked. aborting...`); |
| 110 | + return; |
| 111 | + } |
| 112 | + |
| 113 | + this.isOpen = false; |
| 114 | + } |
| 115 | + |
| 116 | + @action |
| 117 | + _toggle() { |
| 118 | + this.#debug(`_toggle`); |
| 119 | + if (this?.isOpen) this?._close?.(); |
| 120 | + else this?._open?.(); |
| 121 | + } |
| 122 | + |
| 123 | + _getComputedSubcomponent(componentName) { |
| 124 | + const subComponent = |
| 125 | + this?.args?.customComponents?.[componentName] ?? |
| 126 | + this.#subComponents?.[componentName]; |
| 127 | + this.#debug(`${componentName}-component`, subComponent); |
| 128 | + |
| 129 | + return subComponent; |
| 130 | + } |
| 131 | + // #endregion |
| 132 | + |
| 133 | + // #region Default Sub-components |
| 134 | + #subComponents = { |
| 135 | + header: 'mdc-drawer/header', |
| 136 | + content: 'mdc-drawer/content' |
| 137 | + }; |
| 138 | + // #endregion |
| 139 | + |
| 140 | + // #region Private Attributes |
| 141 | + #debug = debugLogger('component:mdc-drawer'); |
| 142 | + #element = null; |
| 143 | + |
| 144 | + #controls = {}; |
| 145 | + // #endregion |
| 146 | +} |
0 commit comments