-
-
Notifications
You must be signed in to change notification settings - Fork 2.4k
Expand file tree
/
Copy pathentity-selector-popup.ts
More file actions
68 lines (53 loc) · 2.22 KB
/
entity-selector-popup.ts
File metadata and controls
68 lines (53 loc) · 2.22 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
import {Component} from './component';
import {EntitySelector, EntitySelectorEntity, EntitySelectorSearchOptions} from "./entity-selector";
import {Popup} from "./popup";
export type EntitySelectorPopupCallback = (entity: EntitySelectorEntity) => void;
export class EntitySelectorPopup extends Component {
protected container!: HTMLElement;
protected selectButton!: HTMLElement;
protected selectorEl!: HTMLElement;
protected callback: EntitySelectorPopupCallback|null = null;
protected selection: EntitySelectorEntity|null = null;
setup() {
this.container = this.$el;
this.selectButton = this.$refs.select;
this.selectorEl = this.$refs.selector;
this.selectButton.addEventListener('click', this.onSelectButtonClick.bind(this));
window.$events.listen('entity-select-change', this.onSelectionChange.bind(this));
window.$events.listen('entity-select-confirm', this.handleConfirmedSelection.bind(this));
}
/**
* Show the selector popup.
*/
show(callback: EntitySelectorPopupCallback, searchOptions: Partial<EntitySelectorSearchOptions> = {}) {
this.callback = callback;
this.getSelector().configureSearchOptions(searchOptions);
this.getPopup().show();
this.getSelector().focusSearch();
}
hide() {
this.getPopup().hide();
}
getPopup(): Popup {
return window.$components.firstOnElement(this.container, 'popup') as Popup;
}
getSelector(): EntitySelector {
return window.$components.firstOnElement(this.selectorEl, 'entity-selector') as EntitySelector;
}
onSelectButtonClick() {
this.handleConfirmedSelection(this.selection);
}
onSelectionChange(entity: EntitySelectorEntity|{}) {
this.selection = (entity.hasOwnProperty('id') ? entity : null) as EntitySelectorEntity|null;
if (!this.selection) {
this.selectButton.setAttribute('disabled', 'true');
} else {
this.selectButton.removeAttribute('disabled');
}
}
handleConfirmedSelection(entity: EntitySelectorEntity|null): void {
this.hide();
this.getSelector().reset();
if (this.callback && entity) this.callback(entity);
}
}