1
0
Fork 0
mirror of https://github.com/jellyfin/jellyfin-web synced 2025-03-30 19:56:21 +00:00
jellyfin-web/src/components/search/searchfields.js

123 lines
3.6 KiB
JavaScript
Raw Normal View History

2020-08-14 08:46:34 +02:00
import layoutManager from '../layoutManager';
import globalize from '../../scripts/globalize';
2020-09-08 02:05:02 -04:00
import { Events } from 'jellyfin-apiclient';
2020-08-14 08:46:34 +02:00
import browser from '../../scripts/browser';
import AlphaPicker from '../alphaPicker/alphaPicker';
import '../../elements/emby-input/emby-input';
import '../../assets/css/flexstyles.scss';
2020-08-14 08:46:34 +02:00
import 'material-design-icons-iconfont';
import './searchfields.css';
2020-07-20 13:43:30 +01:00
/* eslint-disable indent */
2018-10-23 01:05:09 +03:00
function onSearchTimeout() {
const instance = this;
let value = instance.nextSearchValue;
value = (value || '').trim();
2020-09-08 02:05:02 -04:00
Events.trigger(instance, 'search', [value]);
2018-10-23 01:05:09 +03:00
}
function triggerSearch(instance, value) {
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) {
const value = e.detail.value;
const searchFieldsInstance = this;
const txtSearch = searchFieldsInstance.options.element.querySelector('.searchfields-txtSearch');
if (value === 'backspace') {
const val = txtSearch.value;
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) {
2020-07-24 17:18:28 +02:00
instance.alphaPicker = new AlphaPicker({
2018-10-23 01:05:09 +03:00
element: alphaPickerElement,
mode: 'keyboard'
});
alphaPickerElement.addEventListener('alphavalueclicked', onAlphaValueClicked.bind(instance));
2018-10-23 01:05:09 +03:00
}
function onSearchInput(e) {
const value = e.target.value;
const searchFieldsInstance = this;
triggerSearch(searchFieldsInstance, value);
2018-10-23 01:05:09 +03:00
}
function embed(elem, instance, options) {
2020-08-14 08:46:34 +02:00
import('./searchfields.template.html').then(({default: template}) => {
let html = globalize.translateHtml(template, 'core');
if (browser.tizen || browser.orsay) {
html = html.replace('<input ', '<input readonly ');
}
elem.innerHTML = html;
elem.classList.add('searchFields');
const txtSearch = elem.querySelector('.searchfields-txtSearch');
2018-10-23 01:05:09 +03:00
if (layoutManager.tv) {
const alphaPickerElement = elem.querySelector('.alphaPicker');
elem.querySelector('.alphaPicker').classList.remove('hide');
initAlphaPicker(alphaPickerElement, instance);
2018-10-23 01:05:09 +03:00
}
txtSearch.addEventListener('input', onSearchInput.bind(instance));
instance.focus();
});
2018-10-23 01:05:09 +03:00
}
class SearchFields {
constructor(options) {
this.options = options;
embed(options.element, this, options);
2018-10-23 01:05:09 +03:00
}
focus() {
this.options.element.querySelector('.searchfields-txtSearch').focus();
}
destroy() {
const options = this.options;
if (options) {
options.element.classList.remove('searchFields');
}
this.options = null;
const alphaPicker = this.alphaPicker;
if (alphaPicker) {
alphaPicker.destroy();
}
this.alphaPicker = null;
const searchTimeout = this.searchTimeout;
if (searchTimeout) {
clearTimeout(searchTimeout);
}
this.searchTimeout = null;
this.nextSearchValue = null;
}
}
export default SearchFields;
/* eslint-enable indent */