]> BookStack Code Mirror - bookstack/blob - resources/js/components/entity-selector-popup.js
Merge pull request #3364 from BookStackApp/app_url_requests
[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         this.searchInput = this.$refs.searchInput;
11
12         window.EntitySelectorPopup = this;
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.searchInput.focus();
26     }
27
28     hide() {
29         this.elem.components.popup.hide();
30     }
31
32     onSelectButtonClick() {
33         this.hide();
34         if (this.selection !== null && this.callback) this.callback(this.selection);
35     }
36
37     onSelectionConfirm(entity) {
38         this.hide();
39         if (this.callback && entity) this.callback(entity);
40     }
41
42     onSelectionChange(entity) {
43         this.selection = entity;
44         if (entity === null) {
45             this.selectButton.setAttribute('disabled', 'true');
46         } else {
47             this.selectButton.removeAttribute('disabled');
48         }
49     }
50 }
51
52 export default EntitySelectorPopup;