-import {onChildEvent} from "../services/dom";
+import {onChildEvent} from '../services/dom';
+import {Component} from './component';
/**
* Entity Selector
- * @extends {Component}
*/
-class EntitySelector {
+export class EntitySelector extends Component {
setup() {
this.elem = this.$el;
this.searchInput = this.$refs.search;
this.loading = this.$refs.loading;
this.resultsContainer = this.$refs.results;
- this.addButton = this.$refs.add;
this.search = '';
this.lastClick = 0;
this.elem.addEventListener('click', this.onClick.bind(this));
let lastSearch = 0;
- this.searchInput.addEventListener('input', event => {
+ this.searchInput.addEventListener('input', () => {
lastSearch = Date.now();
this.showLoading();
setTimeout(() => {
if (event.keyCode === 13) event.preventDefault();
});
- if (this.addButton) {
- this.addButton.addEventListener('click', event => {
- if (this.selectedItemData) {
- this.confirmSelection(this.selectedItemData);
- this.unselectAll();
- }
- });
- }
-
// Keyboard navigation
- onChildEvent(this.$el, '[data-entity-type]', 'keydown', (e, el) => {
- if (e.ctrlKey && e.code === 'Enter') {
+ onChildEvent(this.$el, '[data-entity-type]', 'keydown', event => {
+ if (event.ctrlKey && event.code === 'Enter') {
const form = this.$el.closest('form');
if (form) {
form.submit();
- e.preventDefault();
+ event.preventDefault();
return;
}
}
- if (e.code === 'ArrowDown') {
+ if (event.code === 'ArrowDown') {
this.focusAdjacent(true);
}
- if (e.code === 'ArrowUp') {
+ if (event.code === 'ArrowUp') {
this.focusAdjacent(false);
}
});
- this.searchInput.addEventListener('keydown', e => {
- if (e.code === 'ArrowDown') {
+ this.searchInput.addEventListener('keydown', event => {
+ if (event.code === 'ArrowDown') {
this.focusAdjacent(true);
}
- })
+ });
}
focusAdjacent(forward = true) {
const items = Array.from(this.resultsContainer.querySelectorAll('[data-entity-type]'));
const selectedIndex = items.indexOf(document.activeElement);
- const newItem = items[selectedIndex+ (forward ? 1 : -1)] || items[0];
+ const newItem = items[selectedIndex + (forward ? 1 : -1)] || items[0];
if (newItem) {
newItem.focus();
}
}
+ reset() {
+ this.searchInput.value = '';
+ this.showLoading();
+ this.initialLoad();
+ }
+
+ focusSearch() {
+ this.searchInput.focus();
+ }
+
+ searchText(queryText) {
+ this.searchInput.value = queryText;
+ this.searchEntities(queryText);
+ }
+
showLoading() {
this.loading.style.display = 'block';
this.resultsContainer.style.display = 'none';
window.$http.get(this.searchUrl()).then(resp => {
this.resultsContainer.innerHTML = resp.data;
this.hideLoading();
- })
+ });
}
searchUrl() {
- return `/ajax/search/entities?types=${encodeURIComponent(this.entityTypes)}&permission=${encodeURIComponent(this.entityPermission)}`;
+ return `/search/entity-selector?types=${encodeURIComponent(this.entityTypes)}&permission=${encodeURIComponent(this.entityPermission)}`;
}
searchEntities(searchTerm) {
const link = item.getAttribute('href');
const name = item.querySelector('.entity-list-item-name').textContent;
- const data = {id: Number(id), name: name, link: link};
+ const data = {id: Number(id), name, link};
if (isSelected) {
item.classList.add('selected');
this.selectedItemData = data;
} else {
- window.$events.emit('entity-select-change', null)
+ window.$events.emit('entity-select-change', null);
}
if (!isDblClick && !isSelected) return;
this.confirmSelection(data);
}
if (isSelected) {
- window.$events.emit('entity-select-change', data)
+ window.$events.emit('entity-select-change', data);
}
}
}
}
-
-export default EntitySelector;
\ No newline at end of file