]> BookStack Code Mirror - bookstack/blob - resources/js/components/entity-selector-popup.js
0104eace7065373983792a525c501746616bc2c9
[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         window.EntitySelectorPopup = this;
11
12         this.callback = null;
13         this.selection = null;
14
15         this.selectButton.addEventListener('click', this.onSelectButtonClick.bind(this));
16         window.$events.listen('entity-select-change', this.onSelectionChange.bind(this));
17         window.$events.listen('entity-select-confirm', this.onSelectionConfirm.bind(this));
18     }
19
20     show(callback) {
21         this.callback = callback;
22         this.elem.components.popup.show();
23     }
24
25     hide() {
26         this.elem.components.popup.hide();
27     }
28
29     onSelectButtonClick() {
30         this.hide();
31         if (this.selection !== null && this.callback) this.callback(this.selection);
32     }
33
34     onSelectionConfirm(entity) {
35         this.hide();
36         if (this.callback && entity) this.callback(entity);
37     }
38
39     onSelectionChange(entity) {
40         this.selection = entity;
41         if (entity === null) {
42             this.selectButton.setAttribute('disabled', 'true');
43         } else {
44             this.selectButton.removeAttribute('disabled');
45         }
46     }
47 }
48
49 export default EntitySelectorPopup;