1 import {onChildEvent} from '../services/dom.ts';
2 import {Component} from './component';
5 * @typedef EntitySelectorSearchOptions
6 * @property entityTypes string
7 * @property entityPermission string
8 * @property searchEndpoint string
9 * @property initialValue string
15 export class EntitySelector extends Component {
20 this.input = this.$refs.input;
21 this.searchInput = this.$refs.search;
22 this.loading = this.$refs.loading;
23 this.resultsContainer = this.$refs.results;
25 this.searchOptions = {
26 entityTypes: this.$opts.entityTypes || 'page,book,chapter',
27 entityPermission: this.$opts.entityPermission || 'view',
28 searchEndpoint: this.$opts.searchEndpoint || '',
29 initialValue: this.searchInput.value || '',
35 this.setupListeners();
38 if (this.searchOptions.searchEndpoint) {
44 * @param {EntitySelectorSearchOptions} options
46 configureSearchOptions(options) {
47 Object.assign(this.searchOptions, options);
49 this.searchInput.value = this.searchOptions.initialValue;
53 this.elem.addEventListener('click', this.onClick.bind(this));
56 this.searchInput.addEventListener('input', () => {
57 lastSearch = Date.now();
60 if (Date.now() - lastSearch < 199) return;
61 this.searchEntities(this.searchInput.value);
65 this.searchInput.addEventListener('keydown', event => {
66 if (event.keyCode === 13) event.preventDefault();
69 // Keyboard navigation
70 onChildEvent(this.$el, '[data-entity-type]', 'keydown', event => {
71 if (event.ctrlKey && event.code === 'Enter') {
72 const form = this.$el.closest('form');
75 event.preventDefault();
80 if (event.code === 'ArrowDown') {
81 this.focusAdjacent(true);
83 if (event.code === 'ArrowUp') {
84 this.focusAdjacent(false);
88 this.searchInput.addEventListener('keydown', event => {
89 if (event.code === 'ArrowDown') {
90 this.focusAdjacent(true);
95 focusAdjacent(forward = true) {
96 const items = Array.from(this.resultsContainer.querySelectorAll('[data-entity-type]'));
97 const selectedIndex = items.indexOf(document.activeElement);
98 const newItem = items[selectedIndex + (forward ? 1 : -1)] || items[0];
105 this.searchInput.value = '';
111 this.searchInput.focus();
115 this.loading.style.display = 'block';
116 this.resultsContainer.style.display = 'none';
120 this.loading.style.display = 'none';
121 this.resultsContainer.style.display = 'block';
125 if (!this.searchOptions.searchEndpoint) {
126 throw new Error('Search endpoint not set for entity-selector load');
129 if (this.searchOptions.initialValue) {
130 this.searchEntities(this.searchOptions.initialValue);
134 window.$http.get(this.searchUrl()).then(resp => {
135 this.resultsContainer.innerHTML = resp.data;
141 const query = `types=${encodeURIComponent(this.searchOptions.entityTypes)}&permission=${encodeURIComponent(this.searchOptions.entityPermission)}`;
142 return `${this.searchOptions.searchEndpoint}?${query}`;
145 searchEntities(searchTerm) {
146 if (!this.searchOptions.searchEndpoint) {
147 throw new Error('Search endpoint not set for entity-selector load');
150 this.input.value = '';
151 const url = `${this.searchUrl()}&term=${encodeURIComponent(searchTerm)}`;
152 window.$http.get(url).then(resp => {
153 this.resultsContainer.innerHTML = resp.data;
159 const now = Date.now();
160 const answer = now - this.lastClick < 300;
161 this.lastClick = now;
166 const listItem = event.target.closest('[data-entity-type]');
168 event.preventDefault();
169 event.stopPropagation();
170 this.selectItem(listItem);
175 const isDblClick = this.isDoubleClick();
176 const type = item.getAttribute('data-entity-type');
177 const id = item.getAttribute('data-entity-id');
178 const isSelected = (!item.classList.contains('selected') || isDblClick);
181 this.input.value = isSelected ? `${type}:${id}` : '';
183 const link = item.getAttribute('href');
184 const name = item.querySelector('.entity-list-item-name').textContent;
185 const data = {id: Number(id), name, link};
188 item.classList.add('selected');
190 window.$events.emit('entity-select-change', null);
193 if (!isDblClick && !isSelected) return;
196 this.confirmSelection(data);
199 window.$events.emit('entity-select-change', data);
203 confirmSelection(data) {
204 window.$events.emit('entity-select-confirm', data);
208 const selected = this.elem.querySelectorAll('.selected');
209 for (const selectedElem of selected) {
210 selectedElem.classList.remove('selected', 'primary-background');