|
| 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 MdcBannerComponent extends Component { |
| 8 | + // #region Accessed Services |
| 9 | + // #endregion |
| 10 | + |
| 11 | + // #region Tracked Attributes |
| 12 | + @tracked open = false; |
| 13 | + |
| 14 | + @tracked centered = false; |
| 15 | + @tracked stacked = false; |
| 16 | + |
| 17 | + @tracked text = null; |
| 18 | + @tracked icon = null; |
| 19 | + |
| 20 | + @tracked primaryActionLabel = null; |
| 21 | + @tracked secondaryActionLabel = null; |
| 22 | + // #endregion |
| 23 | + |
| 24 | + // #region Constructor |
| 25 | + constructor() { |
| 26 | + super(...arguments); |
| 27 | + this.#debug?.(`constructor`); |
| 28 | + |
| 29 | + this.#controls.open = this?._open; |
| 30 | + } |
| 31 | + // #endregion |
| 32 | + |
| 33 | + // #region Lifecycle Hooks |
| 34 | + // #endregion |
| 35 | + |
| 36 | + // #region DOM Event Handlers |
| 37 | + @action |
| 38 | + fireActionEvent(action) { |
| 39 | + this.#debug?.(`fireActionEvent: ${action}`); |
| 40 | + |
| 41 | + this?._open?.({ |
| 42 | + open: false |
| 43 | + }); |
| 44 | + |
| 45 | + this?._fireEvent?.(action); |
| 46 | + } |
| 47 | + |
| 48 | + @action |
| 49 | + recalcStyles() { |
| 50 | + this.#debug?.(`recalcStyles: re-calculating styling`); |
| 51 | + if (!this.#element) return; |
| 52 | + |
| 53 | + // Step 1: Reset |
| 54 | + this.#element?.style?.removeProperty?.( |
| 55 | + '--mdc-banner-graphic-background-color' |
| 56 | + ); |
| 57 | + |
| 58 | + this.#element?.style?.removeProperty?.('--mdc-banner-graphic-color'); |
| 59 | + |
| 60 | + // Step 2: Style / Palette |
| 61 | + const paletteColour = `--mdc-theme-${this?.args?.palette ?? 'primary'}`; |
| 62 | + const textColour = `--mdc-theme-on-${this?.args?.palette ?? 'primary'}`; |
| 63 | + |
| 64 | + this.#element?.style?.setProperty?.( |
| 65 | + '--mdc-banner-graphic-background-color', |
| 66 | + `var(${paletteColour})` |
| 67 | + ); |
| 68 | + |
| 69 | + this.#element?.style?.setProperty?.( |
| 70 | + '--mdc-banner-graphic-color', |
| 71 | + `var(${textColour})` |
| 72 | + ); |
| 73 | + } |
| 74 | + |
| 75 | + @action |
| 76 | + storeElement(element) { |
| 77 | + this.#debug?.(`storeElement: `, element); |
| 78 | + this.#element = element; |
| 79 | + |
| 80 | + this?.recalcStyles?.(); |
| 81 | + this?._fireEvent?.('init'); |
| 82 | + } |
| 83 | + // #endregion |
| 84 | + |
| 85 | + // #region Computed Properties |
| 86 | + // #endregion |
| 87 | + |
| 88 | + // #region Private Methods |
| 89 | + @action |
| 90 | + _fireEvent(name) { |
| 91 | + this.#debug?.(`_fireEvent`); |
| 92 | + if (!this.#element) return; |
| 93 | + |
| 94 | + const thisEvent = new CustomEvent(name, { |
| 95 | + detail: { |
| 96 | + id: this.#element?.getAttribute?.('id'), |
| 97 | + controls: this.#controls, |
| 98 | + status: { |
| 99 | + open: this?.open, |
| 100 | + |
| 101 | + centered: this?.centered, |
| 102 | + stacked: this?.stacked, |
| 103 | + |
| 104 | + text: this?.text, |
| 105 | + icon: this?.icon, |
| 106 | + |
| 107 | + primaryActionLabel: this?.primaryActionLabel, |
| 108 | + secondaryActionLabel: this?.secondaryActionLabel |
| 109 | + } |
| 110 | + } |
| 111 | + }); |
| 112 | + |
| 113 | + this.#element?.dispatchEvent?.(thisEvent); |
| 114 | + } |
| 115 | + |
| 116 | + @action |
| 117 | + _open(options) { |
| 118 | + this.#debug?.(`_open: `, options); |
| 119 | + |
| 120 | + let shouldFire = false; |
| 121 | + |
| 122 | + if (this?.centered !== options?.centered) { |
| 123 | + this.centered = options?.centered ?? false; |
| 124 | + shouldFire ||= true; |
| 125 | + } |
| 126 | + |
| 127 | + if (this?.stacked !== options?.stacked) { |
| 128 | + this.stacked = options?.stacked ?? false; |
| 129 | + shouldFire ||= true; |
| 130 | + } |
| 131 | + |
| 132 | + if (this?.text !== options?.text) { |
| 133 | + this.text = options?.text; |
| 134 | + shouldFire ||= true; |
| 135 | + } |
| 136 | + |
| 137 | + if (this?.icon !== options?.icon) { |
| 138 | + this.icon = options?.icon; |
| 139 | + shouldFire ||= true; |
| 140 | + } |
| 141 | + |
| 142 | + if (this?.primaryActionLabel !== options?.primaryActionLabel) { |
| 143 | + this.primaryActionLabel = options?.primaryActionLabel; |
| 144 | + shouldFire ||= true; |
| 145 | + } |
| 146 | + |
| 147 | + if (this?.secondaryActionLabel !== options?.secondaryActionLabel) { |
| 148 | + this.secondaryActionLabel = options?.secondaryActionLabel; |
| 149 | + shouldFire ||= true; |
| 150 | + } |
| 151 | + |
| 152 | + if (this?.open !== options?.open) { |
| 153 | + this.open = options?.open ?? false; |
| 154 | + shouldFire ||= true; |
| 155 | + } |
| 156 | + |
| 157 | + if (!shouldFire) return; |
| 158 | + |
| 159 | + this?.recalcStyles?.(); |
| 160 | + this?._fireEvent?.('statuschange'); |
| 161 | + } |
| 162 | + // #endregion |
| 163 | + |
| 164 | + // #region Default Sub-components |
| 165 | + // #endregion |
| 166 | + |
| 167 | + // #region Private Attributes |
| 168 | + #debug = debugLogger('component:mdc-banner'); |
| 169 | + |
| 170 | + #element = null; |
| 171 | + #controls = {}; |
| 172 | + // #endregion |
| 173 | +} |
0 commit comments