2017-08-28 08:38:32 -04:00
|
|
|
|
|
|
|
class PagePicker {
|
|
|
|
|
|
|
|
constructor(elem) {
|
|
|
|
this.elem = elem;
|
|
|
|
this.input = elem.querySelector('input');
|
|
|
|
this.resetButton = elem.querySelector('[page-picker-reset]');
|
|
|
|
this.selectButton = elem.querySelector('[page-picker-select]');
|
|
|
|
this.display = elem.querySelector('[page-picker-display]');
|
|
|
|
this.defaultDisplay = elem.querySelector('[page-picker-default]');
|
|
|
|
this.buttonSep = elem.querySelector('span.sep');
|
|
|
|
|
|
|
|
this.value = this.input.value;
|
|
|
|
this.setupListeners();
|
|
|
|
}
|
|
|
|
|
|
|
|
setupListeners() {
|
2018-09-20 10:27:30 -04:00
|
|
|
this.selectButton.addEventListener('click', this.showPopup.bind(this));
|
|
|
|
this.display.parentElement.addEventListener('click', this.showPopup.bind(this));
|
2017-08-28 08:38:32 -04:00
|
|
|
|
|
|
|
this.resetButton.addEventListener('click', event => {
|
|
|
|
this.setValue('', '');
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2018-09-20 10:27:30 -04:00
|
|
|
showPopup() {
|
|
|
|
window.EntitySelectorPopup.show(entity => {
|
|
|
|
this.setValue(entity.id, entity.name);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2017-08-28 08:38:32 -04:00
|
|
|
setValue(value, name) {
|
|
|
|
this.value = value;
|
|
|
|
this.input.value = value;
|
|
|
|
this.controlView(name);
|
|
|
|
}
|
|
|
|
|
|
|
|
controlView(name) {
|
|
|
|
let hasValue = this.value && this.value !== 0;
|
|
|
|
toggleElem(this.resetButton, hasValue);
|
|
|
|
toggleElem(this.buttonSep, hasValue);
|
|
|
|
toggleElem(this.defaultDisplay, !hasValue);
|
|
|
|
toggleElem(this.display, hasValue);
|
|
|
|
if (hasValue) {
|
|
|
|
let id = this.getAssetIdFromVal();
|
|
|
|
this.display.textContent = `#${id}, ${name}`;
|
|
|
|
this.display.href = window.baseUrl(`/link/${id}`);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
getAssetIdFromVal() {
|
|
|
|
return Number(this.value);
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
function toggleElem(elem, show) {
|
|
|
|
let display = (elem.tagName === 'BUTTON' || elem.tagName === 'SPAN') ? 'inline-block' : 'block';
|
|
|
|
elem.style.display = show ? display : 'none';
|
|
|
|
}
|
|
|
|
|
2018-11-09 16:17:35 -05:00
|
|
|
export default PagePicker;
|