1 import {Component} from './component';
3 function toggleElem(elem, show) {
4 elem.toggleAttribute('hidden', !show);
7 export class PagePicker extends Component {
10 this.input = this.$refs.input;
11 this.resetButton = this.$refs.resetButton;
12 this.selectButton = this.$refs.selectButton;
13 this.display = this.$refs.display;
14 this.defaultDisplay = this.$refs.defaultDisplay;
15 this.buttonSep = this.$refs.buttonSeperator;
17 this.selectorEndpoint = this.$opts.selectorEndpoint;
19 this.value = this.input.value;
20 this.setupListeners();
24 this.selectButton.addEventListener('click', this.showPopup.bind(this));
25 this.display.parentElement.addEventListener('click', this.showPopup.bind(this));
26 this.display.addEventListener('click', e => e.stopPropagation());
28 this.resetButton.addEventListener('click', () => {
29 this.setValue('', '');
34 /** @type {EntitySelectorPopup} * */
35 const selectorPopup = window.$components.first('entity-selector-popup');
36 selectorPopup.show(entity => {
37 this.setValue(entity.id, entity.name);
40 searchEndpoint: this.selectorEndpoint,
42 entityPermission: 'view',
46 setValue(value, name) {
48 this.input.value = value;
49 this.controlView(name);
53 const hasValue = this.value && this.value !== 0;
54 toggleElem(this.resetButton, hasValue);
55 toggleElem(this.buttonSep, hasValue);
56 toggleElem(this.defaultDisplay, !hasValue);
57 toggleElem(this.display, hasValue);
59 const id = this.getAssetIdFromVal();
60 this.display.textContent = `#${id}, ${name}`;
61 this.display.href = window.baseUrl(`/link/${id}`);
66 return Number(this.value);