1 import {onChildEvent} from '../services/dom';
2 import {Component} from './component';
7 export class EntitySelector extends Component {
11 this.entityTypes = this.$opts.entityTypes || 'page,book,chapter';
12 this.entityPermission = this.$opts.entityPermission || 'view';
13 this.searchEndpoint = this.$opts.searchEndpoint || '/search/entity-selector';
15 this.input = this.$refs.input;
16 this.searchInput = this.$refs.search;
17 this.loading = this.$refs.loading;
18 this.resultsContainer = this.$refs.results;
23 this.setupListeners();
29 this.elem.addEventListener('click', this.onClick.bind(this));
32 this.searchInput.addEventListener('input', () => {
33 lastSearch = Date.now();
36 if (Date.now() - lastSearch < 199) return;
37 this.searchEntities(this.searchInput.value);
41 this.searchInput.addEventListener('keydown', event => {
42 if (event.keyCode === 13) event.preventDefault();
45 // Keyboard navigation
46 onChildEvent(this.$el, '[data-entity-type]', 'keydown', event => {
47 if (event.ctrlKey && event.code === 'Enter') {
48 const form = this.$el.closest('form');
51 event.preventDefault();
56 if (event.code === 'ArrowDown') {
57 this.focusAdjacent(true);
59 if (event.code === 'ArrowUp') {
60 this.focusAdjacent(false);
64 this.searchInput.addEventListener('keydown', event => {
65 if (event.code === 'ArrowDown') {
66 this.focusAdjacent(true);
71 focusAdjacent(forward = true) {
72 const items = Array.from(this.resultsContainer.querySelectorAll('[data-entity-type]'));
73 const selectedIndex = items.indexOf(document.activeElement);
74 const newItem = items[selectedIndex + (forward ? 1 : -1)] || items[0];
81 this.searchInput.value = '';
87 this.searchInput.focus();
90 searchText(queryText) {
91 this.searchInput.value = queryText;
92 this.searchEntities(queryText);
96 this.loading.style.display = 'block';
97 this.resultsContainer.style.display = 'none';
101 this.loading.style.display = 'none';
102 this.resultsContainer.style.display = 'block';
106 window.$http.get(this.searchUrl()).then(resp => {
107 this.resultsContainer.innerHTML = resp.data;
113 return `${this.searchEndpoint}?types=${encodeURIComponent(this.entityTypes)}&permission=${encodeURIComponent(this.entityPermission)}`;
116 searchEntities(searchTerm) {
117 this.input.value = '';
118 const url = `${this.searchUrl()}&term=${encodeURIComponent(searchTerm)}`;
119 window.$http.get(url).then(resp => {
120 this.resultsContainer.innerHTML = resp.data;
126 const now = Date.now();
127 const answer = now - this.lastClick < 300;
128 this.lastClick = now;
133 const listItem = event.target.closest('[data-entity-type]');
135 event.preventDefault();
136 event.stopPropagation();
137 this.selectItem(listItem);
142 const isDblClick = this.isDoubleClick();
143 const type = item.getAttribute('data-entity-type');
144 const id = item.getAttribute('data-entity-id');
145 const isSelected = (!item.classList.contains('selected') || isDblClick);
148 this.input.value = isSelected ? `${type}:${id}` : '';
150 const link = item.getAttribute('href');
151 const name = item.querySelector('.entity-list-item-name').textContent;
152 const data = {id: Number(id), name, link};
155 item.classList.add('selected');
157 window.$events.emit('entity-select-change', null);
160 if (!isDblClick && !isSelected) return;
163 this.confirmSelection(data);
166 window.$events.emit('entity-select-change', data);
170 confirmSelection(data) {
171 window.$events.emit('entity-select-confirm', data);
175 const selected = this.elem.querySelectorAll('.selected');
176 for (const selectedElem of selected) {
177 selectedElem.classList.remove('selected', 'primary-background');