|
| 1 | +import { Base } from '../partials'; |
| 2 | +import { escapeHTML } from '@ids-core/helpers/escape'; |
| 3 | + |
| 4 | +export class OverflowList extends Base { |
| 5 | + private _moreItemNode: HTMLDivElement; |
| 6 | + private _numberOfItems = 0; |
| 7 | + private _numberOfVisibleItems = 0; |
| 8 | + private _templates: Record<'item' | 'itemMore', string> = { |
| 9 | + item: '', |
| 10 | + itemMore: '', |
| 11 | + }; |
| 12 | + |
| 13 | + private _resizeObserver = new ResizeObserver(() => { |
| 14 | + this.resetState(); |
| 15 | + this.rerender(); |
| 16 | + }); |
| 17 | + |
| 18 | + constructor(container: HTMLDivElement) { |
| 19 | + super(container); |
| 20 | + |
| 21 | + this._templates = { |
| 22 | + item: this.getTemplate('item'), |
| 23 | + itemMore: this.getTemplate('item_more'), |
| 24 | + }; |
| 25 | + |
| 26 | + this.removeTemplate('item'); |
| 27 | + this.removeTemplate('item_more'); |
| 28 | + |
| 29 | + const moreItemNode = container.querySelector<HTMLDivElement>(':scope *:last-child'); |
| 30 | + |
| 31 | + if (!moreItemNode) { |
| 32 | + throw new Error('OverflowList: OverflowList elements are missing in the container.'); |
| 33 | + } |
| 34 | + |
| 35 | + this._moreItemNode = moreItemNode; |
| 36 | + |
| 37 | + this._numberOfItems = this.getItems(false, false).length; |
| 38 | + this._numberOfVisibleItems = this._numberOfItems; |
| 39 | + } |
| 40 | + |
| 41 | + private getItems(getOnlyVisible = false, withOverflow = true): HTMLDivElement[] { |
| 42 | + const itemsSelector = `:scope > *${getOnlyVisible ? ':not([hidden])' : ''}`; |
| 43 | + const items = Array.from(this._container.querySelectorAll<HTMLDivElement>(itemsSelector)); |
| 44 | + |
| 45 | + if (withOverflow) { |
| 46 | + return items; |
| 47 | + } |
| 48 | + |
| 49 | + return items.slice(0, -1); |
| 50 | + } |
| 51 | + |
| 52 | + private getTemplate(type: 'item' | 'item_more'): string { |
| 53 | + const templateNode = this._container.querySelector<HTMLTemplateElement>(`.ids-overflow-list__template[data-id="${type}"]`); |
| 54 | + |
| 55 | + if (!templateNode) { |
| 56 | + throw new Error(`OverflowList: Template of type "${type}" is missing in the container.`); |
| 57 | + } |
| 58 | + |
| 59 | + return templateNode.innerHTML.trim(); |
| 60 | + } |
| 61 | + |
| 62 | + private removeTemplate(type: 'item' | 'item_more') { |
| 63 | + const templateNode = this._container.querySelector<HTMLTemplateElement>(`.ids-overflow-list__template[data-id="${type}"]`); |
| 64 | + |
| 65 | + templateNode?.remove(); |
| 66 | + } |
| 67 | + |
| 68 | + private updateMoreItem() { |
| 69 | + const hiddenCount = this._numberOfItems - this._numberOfVisibleItems; |
| 70 | + |
| 71 | + if (hiddenCount > 0) { |
| 72 | + const tempMoreItem = document.createElement('div'); |
| 73 | + tempMoreItem.innerHTML = this._templates.itemMore.replace('{{ hidden_count }}', hiddenCount.toString()); |
| 74 | + |
| 75 | + if (!tempMoreItem.firstElementChild) { |
| 76 | + throw new Error('OverflowList: Error while creating more item element from template.'); |
| 77 | + } |
| 78 | + |
| 79 | + this._moreItemNode.replaceWith(tempMoreItem.firstElementChild); |
| 80 | + } else { |
| 81 | + this._moreItemNode.setAttribute('hidden', 'true'); |
| 82 | + } |
| 83 | + } |
| 84 | + |
| 85 | + private hideOverflowItems() { |
| 86 | + const itemsNodes = this.getItems(true, false); |
| 87 | + |
| 88 | + itemsNodes.slice(this._numberOfVisibleItems).forEach((itemNode) => { |
| 89 | + itemNode.setAttribute('hidden', 'true'); |
| 90 | + }); |
| 91 | + } |
| 92 | + |
| 93 | + private recalculateVisibleItems() { |
| 94 | + const itemsNodes = this.getItems(true); |
| 95 | + const { right: listRightPosition } = this._container.getBoundingClientRect(); |
| 96 | + const newNumberOfVisibleItems = itemsNodes.findIndex((itemNode) => { |
| 97 | + const { right: itemRightPosition } = itemNode.getBoundingClientRect(); |
| 98 | + |
| 99 | + return itemRightPosition > listRightPosition; |
| 100 | + }); |
| 101 | + |
| 102 | + if (newNumberOfVisibleItems === -1 || newNumberOfVisibleItems === this._numberOfItems) { |
| 103 | + return true; |
| 104 | + } |
| 105 | + |
| 106 | + if (newNumberOfVisibleItems === this._numberOfVisibleItems) { |
| 107 | + this._numberOfVisibleItems = newNumberOfVisibleItems - 1; // eslint-disable-line no-magic-numbers |
| 108 | + } else { |
| 109 | + this._numberOfVisibleItems = newNumberOfVisibleItems; |
| 110 | + } |
| 111 | + |
| 112 | + return false; |
| 113 | + } |
| 114 | + |
| 115 | + private initResizeListener() { |
| 116 | + this._resizeObserver.observe(this._container); |
| 117 | + } |
| 118 | + |
| 119 | + public resetState() { |
| 120 | + this._numberOfVisibleItems = this._numberOfItems; |
| 121 | + |
| 122 | + const itemsNodes = this.getItems(false); |
| 123 | + |
| 124 | + itemsNodes.forEach((itemNode) => { |
| 125 | + itemNode.removeAttribute('hidden'); |
| 126 | + }); |
| 127 | + } |
| 128 | + |
| 129 | + public rerender() { |
| 130 | + let stopRecalculating = true; |
| 131 | + do { |
| 132 | + stopRecalculating = this.recalculateVisibleItems(); |
| 133 | + |
| 134 | + this.hideOverflowItems(); |
| 135 | + this.updateMoreItem(); |
| 136 | + } while (!stopRecalculating); |
| 137 | + } |
| 138 | + |
| 139 | + private setItemsContainer(items: Record<string, string>[]) { |
| 140 | + const fragment = document.createDocumentFragment(); |
| 141 | + |
| 142 | + items.forEach((item) => { |
| 143 | + const filledItem = Object.entries(item).reduce((acc, [key, value]) => { |
| 144 | + const pattern = `{{ ${key} }}`; |
| 145 | + const escapedValue = escapeHTML(value); |
| 146 | + return acc.replaceAll(pattern, escapedValue); |
| 147 | + }, this._templates.item); |
| 148 | + const container = document.createElement('div'); |
| 149 | + |
| 150 | + container.innerHTML = filledItem; |
| 151 | + |
| 152 | + if (container.firstElementChild) { |
| 153 | + fragment.append(container.firstElementChild); |
| 154 | + } |
| 155 | + }); |
| 156 | + |
| 157 | + // Needs to use type assertion here as cloneNode returns a Node type https://github.com/microsoft/TypeScript/issues/283 |
| 158 | + this._moreItemNode = this._moreItemNode.cloneNode(true) as HTMLDivElement; // eslint-disable-line @typescript-eslint/no-unsafe-type-assertion |
| 159 | + |
| 160 | + fragment.append(this._moreItemNode); |
| 161 | + |
| 162 | + this._container.innerHTML = ''; |
| 163 | + this._container.appendChild(fragment); |
| 164 | + this._numberOfItems = items.length; |
| 165 | + } |
| 166 | + |
| 167 | + public setItems(items: Record<string, string>[]) { |
| 168 | + this.setItemsContainer(items); |
| 169 | + this.resetState(); |
| 170 | + this.rerender(); |
| 171 | + } |
| 172 | + |
| 173 | + public init() { |
| 174 | + super.init(); |
| 175 | + |
| 176 | + this.initResizeListener(); |
| 177 | + |
| 178 | + this.rerender(); |
| 179 | + } |
| 180 | +} |
0 commit comments