1 import {Component} from './component';
2 import {EntitySelector, EntitySelectorEntity, EntitySelectorSearchOptions} from "./entity-selector";
3 import {Popup} from "./popup";
5 export type EntitySelectorPopupCallback = (entity: EntitySelectorEntity) => void;
7 export class EntitySelectorPopup extends Component {
9 protected container!: HTMLElement;
10 protected selectButton!: HTMLElement;
11 protected selectorEl!: HTMLElement;
13 protected callback: EntitySelectorPopupCallback|null = null;
14 protected selection: EntitySelectorEntity|null = null;
17 this.container = this.$el;
18 this.selectButton = this.$refs.select;
19 this.selectorEl = this.$refs.selector;
21 this.selectButton.addEventListener('click', this.onSelectButtonClick.bind(this));
22 window.$events.listen('entity-select-change', this.onSelectionChange.bind(this));
23 window.$events.listen('entity-select-confirm', this.handleConfirmedSelection.bind(this));
27 * Show the selector popup.
29 show(callback: EntitySelectorPopupCallback, searchOptions: Partial<EntitySelectorSearchOptions> = {}) {
30 this.callback = callback;
31 this.getSelector().configureSearchOptions(searchOptions);
32 this.getPopup().show();
34 this.getSelector().focusSearch();
38 this.getPopup().hide();
42 return window.$components.firstOnElement(this.container, 'popup') as Popup;
45 getSelector(): EntitySelector {
46 return window.$components.firstOnElement(this.selectorEl, 'entity-selector') as EntitySelector;
49 onSelectButtonClick() {
50 this.handleConfirmedSelection(this.selection);
53 onSelectionChange(entity: EntitySelectorEntity|{}) {
54 this.selection = (entity.hasOwnProperty('id') ? entity : null) as EntitySelectorEntity|null;
55 if (!this.selection) {
56 this.selectButton.setAttribute('disabled', 'true');
58 this.selectButton.removeAttribute('disabled');
62 handleConfirmedSelection(entity: EntitySelectorEntity|null): void {
64 this.getSelector().reset();
65 if (this.callback && entity) this.callback(entity);