|
| 1 | +import Component from '@glimmer/component'; |
| 2 | +import debugLogger from 'ember-debug-logger'; |
| 3 | + |
| 4 | +import { action } from '@ember/object'; |
| 5 | + |
| 6 | +export default class MdcLinearProgressComponent extends Component { |
| 7 | + // #region Accessed Services |
| 8 | + // #endregion |
| 9 | + |
| 10 | + // #region Tracked Attributes |
| 11 | + // #endregion |
| 12 | + |
| 13 | + // #region Constructor |
| 14 | + constructor() { |
| 15 | + super(...arguments); |
| 16 | + this.#debug?.(`constructor`); |
| 17 | + } |
| 18 | + // #endregion |
| 19 | + |
| 20 | + // #region Lifecycle Hooks |
| 21 | + // #endregion |
| 22 | + |
| 23 | + // #region DOM Event Handlers |
| 24 | + @action |
| 25 | + recalcStyles() { |
| 26 | + this.#debug?.(`recalcStyles: re-calculating styling`); |
| 27 | + if (!this.#element) return; |
| 28 | + |
| 29 | + this.#element |
| 30 | + ?.querySelector?.('span.mdc-linear-progress__bar-inner') |
| 31 | + ?.style?.removeProperty?.('--mdc-linear-progress-color'); |
| 32 | + |
| 33 | + const paletteColour = `--mdc-theme-${this?.args?.palette ?? 'primary'}`; |
| 34 | + this.#element |
| 35 | + ?.querySelector?.('span.mdc-linear-progress__bar-inner') |
| 36 | + ?.style?.setProperty?.( |
| 37 | + '--mdc-linear-progress-color', |
| 38 | + `var(${paletteColour})` |
| 39 | + ); |
| 40 | + } |
| 41 | + |
| 42 | + @action |
| 43 | + storeElement(element) { |
| 44 | + this.#debug?.(`storeElement: `, element); |
| 45 | + this.#element = element; |
| 46 | + |
| 47 | + this?.recalcStyles?.(); |
| 48 | + } |
| 49 | + // #endregion |
| 50 | + |
| 51 | + // #region Computed Properties |
| 52 | + get bufferValue() { |
| 53 | + if (this?.args?.indeterminate) { |
| 54 | + return 0; |
| 55 | + } |
| 56 | + |
| 57 | + let value = Number(this?.args?.bufferedValue) ?? this?.rangeLower; |
| 58 | + if (Number?.isNaN(value)) value = this?.rangeLower; |
| 59 | + |
| 60 | + if (value < this?.rangeLower) value = this?.rangeLower; |
| 61 | + if (value > this?.rangeUpper) value = this?.rangeUpper; |
| 62 | + |
| 63 | + const bufferValue = |
| 64 | + (value - this?.rangeLower) / (this?.rangeUpper - this?.rangeLower); |
| 65 | + |
| 66 | + this.#debug?.(`bufferValue: ${bufferValue}`); |
| 67 | + return bufferValue; |
| 68 | + } |
| 69 | + |
| 70 | + get displayValue() { |
| 71 | + if (this?.args?.indeterminate) { |
| 72 | + return 0; |
| 73 | + } |
| 74 | + |
| 75 | + let value = Number(this?.args?.progress) ?? this?.rangeLower; |
| 76 | + if (Number?.isNaN(value)) value = this?.rangeLower; |
| 77 | + |
| 78 | + if (value < this?.rangeLower) value = this?.rangeLower; |
| 79 | + if (value > this?.rangeUpper) value = this?.rangeUpper; |
| 80 | + |
| 81 | + const displayValue = |
| 82 | + (value - this?.rangeLower) / (this?.rangeUpper - this?.rangeLower); |
| 83 | + |
| 84 | + this.#debug?.(`displayValue: ${displayValue}`); |
| 85 | + return displayValue; |
| 86 | + } |
| 87 | + |
| 88 | + get maxValue() { |
| 89 | + let maxValue = Number(this?.args?.maxValue) ?? 1; |
| 90 | + if (Number?.isNaN(maxValue)) maxValue = 1; |
| 91 | + |
| 92 | + this.#debug?.(`maxValue: ${maxValue}`); |
| 93 | + return maxValue; |
| 94 | + } |
| 95 | + |
| 96 | + get minValue() { |
| 97 | + let minValue = Number(this?.args?.minValue) ?? 0; |
| 98 | + if (Number?.isNaN(minValue)) minValue = 0; |
| 99 | + |
| 100 | + this.#debug?.(`minValue: ${minValue}`); |
| 101 | + return minValue; |
| 102 | + } |
| 103 | + |
| 104 | + get label() { |
| 105 | + return this?.args?.label ?? 'linear progress bar'; |
| 106 | + } |
| 107 | + |
| 108 | + get rangeLower() { |
| 109 | + const rangeLower = |
| 110 | + (Math?.abs?.(this?.minValue + this?.maxValue) - |
| 111 | + Math?.abs?.(this?.minValue - this?.maxValue)) / |
| 112 | + 2; |
| 113 | + |
| 114 | + this.#debug?.(`rangeLower: ${rangeLower}`); |
| 115 | + return rangeLower; |
| 116 | + } |
| 117 | + |
| 118 | + get rangeUpper() { |
| 119 | + const rangeUpper = |
| 120 | + (Math?.abs?.(this?.minValue + this?.maxValue) + |
| 121 | + Math?.abs?.(this?.minValue - this?.maxValue)) / |
| 122 | + 2; |
| 123 | + |
| 124 | + this.#debug?.(`rangeUpper: ${rangeUpper}`); |
| 125 | + return rangeUpper; |
| 126 | + } |
| 127 | + // #endregion |
| 128 | + |
| 129 | + // #region Private Methods |
| 130 | + // #endregion |
| 131 | + |
| 132 | + // #region Default Sub-components |
| 133 | + // #endregion |
| 134 | + |
| 135 | + // #region Private Attributes |
| 136 | + #debug = debugLogger('component:mdc-linear-progress'); |
| 137 | + #element = null; |
| 138 | + #oneRem = 16; |
| 139 | + // #endregion |
| 140 | +} |
0 commit comments