]> BookStack Code Mirror - bookstack/blob - resources/js/components/entity-selector-popup.js
Merge pull request #4728 from BookStackApp/friendlier_buttons
[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, searchText = '') {
19         this.callback = callback;
20         this.getPopup().show();
21
22         if (searchText) {
23             this.getSelector().searchText(searchText);
24         }
25
26         this.getSelector().focusSearch();
27     }
28
29     hide() {
30         this.getPopup().hide();
31     }
32
33     /**
34      * @returns {Popup}
35      */
36     getPopup() {
37         return window.$components.firstOnElement(this.container, 'popup');
38     }
39
40     /**
41      * @returns {EntitySelector}
42      */
43     getSelector() {
44         return window.$components.firstOnElement(this.selectorEl, 'entity-selector');
45     }
46
47     onSelectButtonClick() {
48         this.handleConfirmedSelection(this.selection);
49     }
50
51     onSelectionChange(entity) {
52         this.selection = entity;
53         if (entity === null) {
54             this.selectButton.setAttribute('disabled', 'true');
55         } else {
56             this.selectButton.removeAttribute('disabled');
57         }
58     }
59
60     handleConfirmedSelection(entity) {
61         this.hide();
62         this.getSelector().reset();
63         if (this.callback && entity) this.callback(entity);
64     }
65
66 }