]> BookStack Code Mirror - bookstack/blob - resources/js/components/entity-selector-popup.js
Updated entity-selector-popup to reset on selection
[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.onSelectionConfirm.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.hide();
38         if (this.selection !== null && this.callback) this.callback(this.selection);
39     }
40
41     onSelectionConfirm(entity) {
42         this.hide();
43         this.getSelector().reset();
44         if (this.callback && entity) this.callback(entity);
45     }
46
47     onSelectionChange(entity) {
48         this.selection = entity;
49         if (entity === null) {
50             this.selectButton.setAttribute('disabled', 'true');
51         } else {
52             this.selectButton.removeAttribute('disabled');
53         }
54     }
55 }
56
57 export default EntitySelectorPopup;