|
| 1 | +import { OdsPopover } from './ods-popover'; |
| 2 | +import { OdsComponentController } from '../../ods-component-controller'; |
| 3 | +import { OdsLogger } from '../../../logger/ods-logger'; |
| 4 | + |
| 5 | +/** |
| 6 | + * common controller logic for cmpnt component used by the different implementations. |
| 7 | + * it contains all the glue between framework implementation and the third party service. |
| 8 | + */ |
| 9 | +export class OdsPopoverController extends OdsComponentController<OdsPopover> { |
| 10 | + private readonly logger = new OdsLogger('OdsPopoverController'); |
| 11 | + |
| 12 | + constructor(component: OdsPopover) { |
| 13 | + super(component); |
| 14 | + } |
| 15 | + |
| 16 | + /** |
| 17 | + * Attributes validation documentation |
| 18 | + */ |
| 19 | + validateAttributes(): void { |
| 20 | + return; |
| 21 | + } |
| 22 | + |
| 23 | + handleTriggerClick(): void { |
| 24 | + this.logger.log('Click on trigger'); |
| 25 | + |
| 26 | + if (!this.component.surface) { |
| 27 | + return; |
| 28 | + } |
| 29 | + this.component.surface.opened = !this.component.surface.opened; |
| 30 | + } |
| 31 | + |
| 32 | + handleTriggerKey(event: KeyboardEvent): void { |
| 33 | + if((event.target as HTMLElement).tagName !== "OSDS-LINK" && (event.key === " " || event.key === "Enter")) { |
| 34 | + this.logger.log('Key on trigger'); |
| 35 | + |
| 36 | + if (!this.component.surface) { |
| 37 | + return; |
| 38 | + } |
| 39 | + this.component.surface.opened = !this.component.surface.opened; |
| 40 | + } |
| 41 | + |
| 42 | + if((event.target as HTMLElement).tagName !== "OSDS-LINK" && event.key === "Escape") { |
| 43 | + this.logger.log('EscapeKey on trigger'); |
| 44 | + if(this.component.surface) { |
| 45 | + this.component.surface.opened = false; |
| 46 | + } |
| 47 | + } |
| 48 | + } |
| 49 | + |
| 50 | + handleSurfaceKey(event: KeyboardEvent): void { |
| 51 | + if (event.key === "Escape") { |
| 52 | + this.logger.log('EscapeKey in surface'); |
| 53 | + if(this.component.surface) { |
| 54 | + this.component.surface.opened = false; |
| 55 | + } |
| 56 | + } |
| 57 | + } |
| 58 | + |
| 59 | + checkForClickOutside(event: any): void { |
| 60 | + if (this.component.el.contains(event.target) || this.component.surface === undefined || !this.component.surface.opened) { |
| 61 | + return; |
| 62 | + } else { |
| 63 | + this.logger.log('Click outside component while he is opened'); |
| 64 | + this.component.surface.close(); |
| 65 | + } |
| 66 | + } |
| 67 | + |
| 68 | + syncReferences(): void { |
| 69 | + if (this.component.surface && this.component.anchor) { |
| 70 | + this.component.surface.setAnchorElement(this.component.anchor); |
| 71 | + this.component.surface.setAnchorMargin( { |
| 72 | + top: 0, |
| 73 | + right: 0, |
| 74 | + bottom: 0, |
| 75 | + left: 0 |
| 76 | + }); |
| 77 | + } |
| 78 | + } |
| 79 | +} |
0 commit comments