]> BookStack Code Mirror - bookstack/blob - resources/assets/js/components/page-picker.js
Fixes a corner case with exclamation in the ID.
[bookstack] / resources / assets / js / components / page-picker.js
1
2 class PagePicker {
3
4     constructor(elem) {
5         this.elem = elem;
6         this.input = elem.querySelector('input');
7         this.resetButton = elem.querySelector('[page-picker-reset]');
8         this.selectButton = elem.querySelector('[page-picker-select]');
9         this.display = elem.querySelector('[page-picker-display]');
10         this.defaultDisplay = elem.querySelector('[page-picker-default]');
11         this.buttonSep = elem.querySelector('span.sep');
12
13         this.value = this.input.value;
14         this.setupListeners();
15     }
16
17     setupListeners() {
18         // Select click
19         this.selectButton.addEventListener('click', event => {
20             window.EntitySelectorPopup.show(entity => {
21                 this.setValue(entity.id, entity.name);
22             });
23         });
24
25         this.resetButton.addEventListener('click', event => {
26             this.setValue('', '');
27         });
28     }
29
30     setValue(value, name) {
31         this.value = value;
32         this.input.value = value;
33         this.controlView(name);
34     }
35
36     controlView(name) {
37         let hasValue = this.value && this.value !== 0;
38         toggleElem(this.resetButton, hasValue);
39         toggleElem(this.buttonSep, hasValue);
40         toggleElem(this.defaultDisplay, !hasValue);
41         toggleElem(this.display, hasValue);
42         if (hasValue) {
43             let id = this.getAssetIdFromVal();
44             this.display.textContent = `#${id}, ${name}`;
45             this.display.href = window.baseUrl(`/link/${id}`);
46         }
47     }
48
49     getAssetIdFromVal() {
50         return Number(this.value);
51     }
52
53 }
54
55 function toggleElem(elem, show) {
56     let display = (elem.tagName === 'BUTTON' || elem.tagName === 'SPAN') ? 'inline-block' : 'block';
57     elem.style.display = show ? display : 'none';
58 }
59
60 module.exports = PagePicker;