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