2020-07-16 10:28:08 +01:00
|
|
|
import layoutManager from 'layoutManager';
|
|
|
|
import globalize from 'globalize';
|
|
|
|
import require from 'require';
|
|
|
|
import events from 'events';
|
|
|
|
import browser from 'browser';
|
|
|
|
import AlphaPicker from 'alphaPicker';
|
|
|
|
import 'emby-input';
|
|
|
|
import 'flexStyles';
|
|
|
|
import 'material-icons';
|
|
|
|
import 'css!./searchfields';
|
|
|
|
|
2020-07-20 13:43:30 +01:00
|
|
|
/* eslint-disable indent */
|
2018-10-23 01:05:09 +03:00
|
|
|
|
|
|
|
function onSearchTimeout() {
|
2019-01-10 15:39:37 +03:00
|
|
|
|
2020-07-16 10:28:08 +01:00
|
|
|
const instance = this;
|
|
|
|
let value = instance.nextSearchValue;
|
2019-01-10 15:39:37 +03:00
|
|
|
|
|
|
|
value = (value || '').trim();
|
|
|
|
events.trigger(instance, 'search', [value]);
|
2018-10-23 01:05:09 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
function triggerSearch(instance, value) {
|
2019-01-10 15:39:37 +03:00
|
|
|
|
|
|
|
if (instance.searchTimeout) {
|
|
|
|
clearTimeout(instance.searchTimeout);
|
|
|
|
}
|
|
|
|
|
|
|
|
instance.nextSearchValue = value;
|
|
|
|
instance.searchTimeout = setTimeout(onSearchTimeout.bind(instance), 400);
|
2018-10-23 01:05:09 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
function onAlphaValueClicked(e) {
|
2019-01-10 15:39:37 +03:00
|
|
|
|
2020-07-16 10:28:08 +01:00
|
|
|
const value = e.detail.value;
|
|
|
|
const searchFieldsInstance = this;
|
2019-01-10 15:39:37 +03:00
|
|
|
|
2020-07-16 10:28:08 +01:00
|
|
|
const txtSearch = searchFieldsInstance.options.element.querySelector('.searchfields-txtSearch');
|
2019-01-10 15:39:37 +03:00
|
|
|
|
|
|
|
if (value === 'backspace') {
|
|
|
|
|
2020-07-16 10:28:08 +01:00
|
|
|
const val = txtSearch.value;
|
2019-01-10 15:39:37 +03:00
|
|
|
txtSearch.value = val.length ? val.substring(0, val.length - 1) : '';
|
|
|
|
|
|
|
|
} else {
|
|
|
|
txtSearch.value += value;
|
|
|
|
}
|
|
|
|
|
|
|
|
txtSearch.dispatchEvent(new CustomEvent('input', {
|
|
|
|
bubbles: true
|
|
|
|
}));
|
2018-10-23 01:05:09 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
function initAlphaPicker(alphaPickerElement, instance) {
|
2019-01-10 15:39:37 +03:00
|
|
|
|
2020-06-12 01:09:35 +03:00
|
|
|
instance.alphaPicker = new AlphaPicker.default({
|
2018-10-23 01:05:09 +03:00
|
|
|
element: alphaPickerElement,
|
2019-01-10 15:39:37 +03:00
|
|
|
mode: 'keyboard'
|
|
|
|
});
|
|
|
|
|
|
|
|
alphaPickerElement.addEventListener('alphavalueclicked', onAlphaValueClicked.bind(instance));
|
2018-10-23 01:05:09 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
function onSearchInput(e) {
|
2019-01-10 15:39:37 +03:00
|
|
|
|
2020-07-16 10:28:08 +01:00
|
|
|
const value = e.target.value;
|
|
|
|
const searchFieldsInstance = this;
|
2019-01-10 15:39:37 +03:00
|
|
|
triggerSearch(searchFieldsInstance, value);
|
2018-10-23 01:05:09 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
function embed(elem, instance, options) {
|
2019-01-10 15:39:37 +03:00
|
|
|
|
2020-07-21 13:25:50 +01:00
|
|
|
import('text!./searchfields.template.html').then(({default: template}) => {
|
2019-01-10 15:39:37 +03:00
|
|
|
|
2020-07-21 14:53:09 +01:00
|
|
|
let html = globalize.translateHtml(template, 'core');
|
2019-01-10 15:39:37 +03:00
|
|
|
|
|
|
|
if (browser.tizen || browser.orsay) {
|
|
|
|
html = html.replace('<input ', '<input readonly ');
|
|
|
|
}
|
|
|
|
|
|
|
|
elem.innerHTML = html;
|
|
|
|
|
|
|
|
elem.classList.add('searchFields');
|
|
|
|
|
2020-07-16 10:28:08 +01:00
|
|
|
const txtSearch = elem.querySelector('.searchfields-txtSearch');
|
2019-01-10 15:39:37 +03:00
|
|
|
|
2018-10-23 01:05:09 +03:00
|
|
|
if (layoutManager.tv) {
|
2020-07-16 10:28:08 +01:00
|
|
|
const alphaPickerElement = elem.querySelector('.alphaPicker');
|
2019-01-10 15:39:37 +03:00
|
|
|
|
|
|
|
elem.querySelector('.alphaPicker').classList.remove('hide');
|
|
|
|
initAlphaPicker(alphaPickerElement, instance);
|
2018-10-23 01:05:09 +03:00
|
|
|
}
|
2019-01-10 15:39:37 +03:00
|
|
|
|
|
|
|
txtSearch.addEventListener('input', onSearchInput.bind(instance));
|
|
|
|
|
|
|
|
instance.focus();
|
|
|
|
});
|
2018-10-23 01:05:09 +03:00
|
|
|
}
|
|
|
|
|
2020-07-16 10:28:08 +01:00
|
|
|
class SearchFields {
|
|
|
|
constructor(options) {
|
2019-01-10 15:39:37 +03:00
|
|
|
|
|
|
|
this.options = options;
|
|
|
|
embed(options.element, this, options);
|
2018-10-23 01:05:09 +03:00
|
|
|
}
|
2020-07-16 10:28:08 +01:00
|
|
|
focus() {
|
2019-01-10 15:39:37 +03:00
|
|
|
|
|
|
|
this.options.element.querySelector('.searchfields-txtSearch').focus();
|
2020-07-16 10:28:08 +01:00
|
|
|
}
|
|
|
|
destroy() {
|
2019-01-10 15:39:37 +03:00
|
|
|
|
2020-07-16 10:28:08 +01:00
|
|
|
const options = this.options;
|
2019-01-10 15:39:37 +03:00
|
|
|
if (options) {
|
|
|
|
options.element.classList.remove('searchFields');
|
|
|
|
}
|
|
|
|
this.options = null;
|
|
|
|
|
2020-07-16 10:28:08 +01:00
|
|
|
const alphaPicker = this.alphaPicker;
|
2019-01-10 15:39:37 +03:00
|
|
|
if (alphaPicker) {
|
|
|
|
alphaPicker.destroy();
|
|
|
|
}
|
|
|
|
this.alphaPicker = null;
|
|
|
|
|
2020-07-16 10:28:08 +01:00
|
|
|
const searchTimeout = this.searchTimeout;
|
2019-01-10 15:39:37 +03:00
|
|
|
if (searchTimeout) {
|
|
|
|
clearTimeout(searchTimeout);
|
|
|
|
}
|
|
|
|
this.searchTimeout = null;
|
|
|
|
this.nextSearchValue = null;
|
2020-07-16 10:28:08 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
export default SearchFields;
|
2019-01-10 15:39:37 +03:00
|
|
|
|
2020-07-16 10:28:08 +01:00
|
|
|
/* eslint-enable indent */
|