]> BookStack Code Mirror - bookstack/blob - resources/js/components/entity-selector-popup.js
e7cb60b1f6c4c05c751033ebec4424b8de7fea1d
[bookstack] / resources / js / components / entity-selector-popup.js
1 /**
2  * Entity Selector Popup
3  * @extends {Component}
4  */
5 class EntitySelectorPopup {
6
7     setup() {
8         this.elem = this.$el;
9         this.selectButton = this.$refs.select;
10
11         window.EntitySelectorPopup = this;
12         this.selectorEl = this.$refs.selector;
13
14         this.callback = null;
15         this.selection = null;
16
17         this.selectButton.addEventListener('click', this.onSelectButtonClick.bind(this));
18         window.$events.listen('entity-select-change', this.onSelectionChange.bind(this));
19         window.$events.listen('entity-select-confirm', this.handleConfirmedSelection.bind(this));
20     }
21
22     show(callback) {
23         this.callback = callback;
24         this.elem.components.popup.show();
25         this.getSelector().focusSearch();
26     }
27
28     hide() {
29         this.elem.components.popup.hide();
30     }
31
32     getSelector() {
33         return this.selectorEl.components['entity-selector'];
34     }
35
36     onSelectButtonClick() {
37         this.handleConfirmedSelection(this.selection);
38     }
39
40     onSelectionChange(entity) {
41         this.selection = entity;
42         if (entity === null) {
43             this.selectButton.setAttribute('disabled', 'true');
44         } else {
45             this.selectButton.removeAttribute('disabled');
46         }
47     }
48
49     handleConfirmedSelection(entity) {
50         this.hide();
51         this.getSelector().reset();
52         if (this.callback && entity) this.callback(entity);
53     }
54 }
55
56 export default EntitySelectorPopup;