|
1 |
| -export class Cards { |
2 |
| - |
3 |
| - static Init() { |
4 |
| - if (typeof document !== 'undefined') document.addEventListener("DOMContentLoaded", () => { |
5 |
| - document.body.addEventListener('click', e => { |
6 |
| - const trigger = <HTMLElement>e.target; |
7 |
| - |
8 |
| - const card: HTMLElement = trigger.closest('.card'); |
9 |
| - if (!card) return; |
10 |
| - |
11 |
| - const cardReveal = <HTMLElement|null>Array.from(card.children).find(elem => elem.classList.contains('card-reveal')); |
12 |
| - if (!cardReveal) return; |
13 |
| - const initialOverflow = getComputedStyle(card).overflow; |
14 |
| - |
15 |
| - // Close Card |
16 |
| - const closeArea = cardReveal.querySelector('.card-reveal .card-title'); |
17 |
| - if (trigger === closeArea || closeArea.contains(trigger)) { |
18 |
| - const duration = 225; |
19 |
| - cardReveal.style.transition = `transform ${duration}ms ease`; //easeInOutQuad |
20 |
| - cardReveal.style.transform = 'translateY(0)'; |
21 |
| - setTimeout(() => { |
22 |
| - cardReveal.style.display = 'none'; |
23 |
| - card.style.overflow = initialOverflow; |
24 |
| - }, duration); |
25 |
| - }; |
26 |
| - |
27 |
| - // Reveal Card |
28 |
| - const activators = card.querySelectorAll('.activator'); |
29 |
| - activators.forEach(activator => { |
30 |
| - if (trigger === activator || activator.contains(trigger)) { |
31 |
| - card.style.overflow = 'hidden'; |
32 |
| - cardReveal.style.display = 'block'; |
33 |
| - setTimeout(() => { |
34 |
| - const duration = 300; |
35 |
| - cardReveal.style.transition = `transform ${duration}ms ease`; //easeInOutQuad |
36 |
| - cardReveal.style.transform = 'translateY(-100%)'; |
37 |
| - }, 1); |
38 |
| - } |
39 |
| - }); |
40 |
| - |
41 |
| - }); |
42 |
| - }); |
| 1 | +import { Utils } from './utils'; |
| 2 | +import { Component, BaseOptions, InitElements, MElement, Openable } from './component'; |
| 3 | + |
| 4 | +export interface CardsOptions extends BaseOptions { |
| 5 | + onOpen: (el: Element) => void; |
| 6 | + onClose: (el: Element) => void; |
| 7 | + inDuration: number; |
| 8 | + outDuration: number; |
| 9 | +} |
| 10 | + |
| 11 | +const _defaults: CardsOptions = { |
| 12 | + onOpen: null, |
| 13 | + onClose: null, |
| 14 | + inDuration: 225, |
| 15 | + outDuration: 300 |
| 16 | +}; |
| 17 | + |
| 18 | +export class Cards extends Component<CardsOptions> implements Openable { |
| 19 | + isOpen: boolean = false; |
| 20 | + private readonly cardReveal: HTMLElement | null; |
| 21 | + private readonly initialOverflow: string; |
| 22 | + private _activators: HTMLElement[] | null; |
| 23 | + private cardRevealClose: HTMLElement | null; |
| 24 | + |
| 25 | + constructor(el: HTMLElement, options: Partial<CardsOptions>) { |
| 26 | + super(el, options, Cards); |
| 27 | + (this.el as any).M_Cards = this; |
| 28 | + |
| 29 | + this.options = { |
| 30 | + ...Cards.defaults, |
| 31 | + ...options |
| 32 | + }; |
| 33 | + |
| 34 | + this.cardReveal = <HTMLElement | null>Array.from(this.el.children).find(elem => elem.classList.contains('card-reveal')); |
| 35 | + |
| 36 | + if (this.cardReveal) { |
| 37 | + this.initialOverflow = getComputedStyle(this.el).overflow; |
| 38 | + this._activators = Array.from(this.el.querySelectorAll('.activator')); |
| 39 | + this._activators.forEach((el: HTMLElement) => el.tabIndex = 0); |
| 40 | + this.cardRevealClose = this.cardReveal.querySelector('.card-reveal .card-title .close'); |
| 41 | + this.cardRevealClose.tabIndex = -1; |
| 42 | + this.cardReveal.ariaExpanded = 'false'; |
| 43 | + this._setupEventHandlers(); |
| 44 | + } |
| 45 | + } |
| 46 | + |
| 47 | + static get defaults(): CardsOptions { |
| 48 | + return _defaults; |
| 49 | + } |
| 50 | + |
| 51 | + /** |
| 52 | + * Initializes instance of Cards. |
| 53 | + * @param el HTML element. |
| 54 | + * @param options Component options. |
| 55 | + */ |
| 56 | + static init(el: HTMLElement, options?: Partial<CardsOptions>): Cards; |
| 57 | + /** |
| 58 | + * Initializes instances of Cards. |
| 59 | + * @param els HTML elements. |
| 60 | + * @param options Component options. |
| 61 | + */ |
| 62 | + static init(els: InitElements<MElement>, options?: Partial<CardsOptions>): Cards[]; |
| 63 | + /** |
| 64 | + * Initializes instances of Cards. |
| 65 | + * @param els HTML elements. |
| 66 | + * @param options Component options. |
| 67 | + */ |
| 68 | + static init(els: HTMLElement | InitElements<MElement>, options?: Partial<CardsOptions>): Cards | Cards[] { |
| 69 | + return super.init(els, options, Cards); |
| 70 | + } |
43 | 71 |
|
| 72 | + static getInstance(el: HTMLElement): Cards { |
| 73 | + return (el as any).M_Cards; |
44 | 74 | }
|
| 75 | + |
| 76 | + /** |
| 77 | + * {@inheritDoc} |
| 78 | + */ |
| 79 | + destroy() { |
| 80 | + this._removeEventHandlers(); |
| 81 | + this._activators = []; |
| 82 | + } |
| 83 | + |
| 84 | + _setupEventHandlers = () => { |
| 85 | + this._activators.forEach((el: HTMLElement) => { |
| 86 | + el.addEventListener('click', this._handleClickInteraction); |
| 87 | + el.addEventListener('keypress', this._handleKeypressEvent); |
| 88 | + }); |
| 89 | + }; |
| 90 | + |
| 91 | + _removeEventHandlers = () => { |
| 92 | + this._activators.forEach((el: HTMLElement) => { |
| 93 | + el.removeEventListener('click', this._handleClickInteraction); |
| 94 | + el.removeEventListener('keypress', this._handleKeypressEvent); |
| 95 | + }); |
| 96 | + }; |
| 97 | + |
| 98 | + _handleClickInteraction = () => { |
| 99 | + this._handleRevealEvent(); |
| 100 | + }; |
| 101 | + |
| 102 | + _handleKeypressEvent: (e: KeyboardEvent) => void = (e: KeyboardEvent) => { |
| 103 | + if (Utils.keys.ENTER.includes(e.key)) { |
| 104 | + this._handleRevealEvent(); |
| 105 | + } |
| 106 | + }; |
| 107 | + |
| 108 | + _handleRevealEvent = () => { |
| 109 | + // Reveal Card |
| 110 | + this._activators.forEach((el: HTMLElement) => el.tabIndex = -1); |
| 111 | + this.open(); |
| 112 | + }; |
| 113 | + |
| 114 | + _setupRevealCloseEventHandlers = () => { |
| 115 | + this.cardRevealClose.addEventListener('click', this.close); |
| 116 | + this.cardRevealClose.addEventListener('keypress', this._handleKeypressCloseEvent); |
| 117 | + }; |
| 118 | + |
| 119 | + _removeRevealCloseEventHandlers = () => { |
| 120 | + this.cardRevealClose.addEventListener('click', this.close); |
| 121 | + this.cardRevealClose.addEventListener('keypress', this._handleKeypressCloseEvent); |
| 122 | + }; |
| 123 | + |
| 124 | + _handleKeypressCloseEvent: (e: KeyboardEvent) => void = (e: KeyboardEvent) => { |
| 125 | + if (Utils.keys.ENTER.includes(e.key)) { |
| 126 | + this.close(); |
| 127 | + } |
| 128 | + }; |
| 129 | + |
| 130 | + /** |
| 131 | + * Show card reveal. |
| 132 | + */ |
| 133 | + open: () => void = () => { |
| 134 | + if (this.isOpen) return; |
| 135 | + this.isOpen = true; |
| 136 | + this.el.style.overflow = 'hidden'; |
| 137 | + this.cardReveal.style.display = 'block'; |
| 138 | + this.cardReveal.ariaExpanded = 'true'; |
| 139 | + this.cardRevealClose.tabIndex = 0; |
| 140 | + setTimeout(() => { |
| 141 | + this.cardReveal.style.transition = `transform ${this.options.outDuration}ms ease`; //easeInOutQuad |
| 142 | + this.cardReveal.style.transform = 'translateY(-100%)'; |
| 143 | + }, 1); |
| 144 | + if (typeof this.options.onOpen === 'function') { |
| 145 | + this.options.onOpen.call(this); |
| 146 | + } |
| 147 | + this._setupRevealCloseEventHandlers(); |
| 148 | + }; |
| 149 | + |
| 150 | + /** |
| 151 | + * Hide card reveal. |
| 152 | + */ |
| 153 | + close: () => void = () => { |
| 154 | + if (!this.isOpen) return; |
| 155 | + this.isOpen = false; |
| 156 | + this.cardReveal.style.transition = `transform ${this.options.inDuration}ms ease`; //easeInOutQuad |
| 157 | + this.cardReveal.style.transform = 'translateY(0)'; |
| 158 | + setTimeout(() => { |
| 159 | + this.cardReveal.style.display = 'none'; |
| 160 | + this.cardReveal.ariaExpanded = 'false'; |
| 161 | + this._activators.forEach((el: HTMLElement) => el.tabIndex = 0); |
| 162 | + this.cardRevealClose.tabIndex = -1; |
| 163 | + this.el.style.overflow = this.initialOverflow; |
| 164 | + }, this.options.inDuration); |
| 165 | + if (typeof this.options.onClose === 'function') { |
| 166 | + this.options.onClose.call(this); |
| 167 | + } |
| 168 | + this._removeRevealCloseEventHandlers(); |
| 169 | + }; |
45 | 170 | }
|
0 commit comments