import * as userSettings from './settings/userSettings'; import globalize from './globalize'; export function getSavedQueryKey(modifier) { return window.location.href.split('#')[0] + (modifier || ''); } export function loadSavedQueryValues(key, query) { let values = userSettings.get(key); if (values) { values = JSON.parse(values); return Object.assign(query, values); } return query; } export function saveQueryValues(key, query) { const values = {}; if (query.SortBy) { values.SortBy = query.SortBy; } if (query.SortOrder) { values.SortOrder = query.SortOrder; } userSettings.set(key, JSON.stringify(values)); } export function saveViewSetting (key, value) { userSettings.set(key + '-_view', value); } export function getSavedView (key) { return userSettings.get(key + '-_view'); } export function showLayoutMenu (button, currentLayout, views) { let dispatchEvent = true; if (!views) { dispatchEvent = false; views = button.getAttribute('data-layouts'); views = views ? views.split(',') : ['List', 'Poster', 'PosterCard', 'Thumb', 'ThumbCard']; } const menuItems = views.map(function (v) { return { name: globalize.translate(v), id: v, selected: currentLayout == v }; }); import('../components/actionSheet/actionSheet').then(({default: actionsheet}) => { actionsheet.show({ items: menuItems, positionTo: button, callback: function (id) { button.dispatchEvent(new CustomEvent('layoutchange', { detail: { viewStyle: id }, bubbles: true, cancelable: false })); if (!dispatchEvent && window.$) { $(button).trigger('layoutchange', [id]); } } }); }); } export function getQueryPagingHtml (options) { const startIndex = options.startIndex; const limit = options.limit; const totalRecordCount = options.totalRecordCount; let html = ''; const recordsEnd = Math.min(startIndex + limit, totalRecordCount); const showControls = limit < totalRecordCount; html += '
'; if (showControls) { html += ''; html += globalize.translate('ListPaging', totalRecordCount ? startIndex + 1 : 0, recordsEnd, totalRecordCount); html += ''; } if (showControls || options.viewButton || options.filterButton || options.sortButton || options.addLayoutButton) { html += '
'; if (showControls) { html += ''; html += ''; } if (options.addLayoutButton) { html += ''; } if (options.sortButton) { html += ''; } if (options.filterButton) { html += ''; } html += '
'; } html += '
'; return html; } export function showSortMenu (options) { Promise.all([ import('../components/dialogHelper/dialogHelper'), import('../elements/emby-radio/emby-radio') ]).then(([{default: dialogHelper}]) => { function onSortByChange() { const newValue = this.value; if (this.checked) { const changed = options.query.SortBy != newValue; options.query.SortBy = newValue.replace('_', ','); options.query.StartIndex = 0; if (options.callback && changed) { options.callback(); } } } function onSortOrderChange() { const newValue = this.value; if (this.checked) { const changed = options.query.SortOrder != newValue; options.query.SortOrder = newValue; options.query.StartIndex = 0; if (options.callback && changed) { options.callback(); } } } const dlg = dialogHelper.createDialog({ removeOnClose: true, modal: false, entryAnimationDuration: 160, exitAnimationDuration: 200 }); dlg.classList.add('ui-body-a'); dlg.classList.add('background-theme-a'); dlg.classList.add('formDialog'); let html = ''; html += '
'; html += '

'; html += globalize.translate('HeaderSortBy'); html += '

'; let i; let length; let isChecked; html += '
'; for (i = 0, length = options.items.length; i < length; i++) { const option = options.items[i]; const radioValue = option.id.replace(',', '_'); isChecked = (options.query.SortBy || '').replace(',', '_') == radioValue ? ' checked' : ''; html += ''; } html += '
'; html += '

'; html += globalize.translate('HeaderSortOrder'); html += '

'; html += '
'; isChecked = options.query.SortOrder == 'Ascending' ? ' checked' : ''; html += ''; isChecked = options.query.SortOrder == 'Descending' ? ' checked' : ''; html += ''; html += '
'; html += '
'; dlg.innerHTML = html; dialogHelper.open(dlg); const sortBys = dlg.querySelectorAll('.menuSortBy'); for (i = 0, length = sortBys.length; i < length; i++) { sortBys[i].addEventListener('change', onSortByChange); } const sortOrders = dlg.querySelectorAll('.menuSortOrder'); for (i = 0, length = sortOrders.length; i < length; i++) { sortOrders[i].addEventListener('change', onSortOrderChange); } }); } const libraryBrowser = { getSavedQueryKey, loadSavedQueryValues, saveQueryValues, saveViewSetting, getSavedView, showLayoutMenu, getQueryPagingHtml, showSortMenu }; window.LibraryBrowser = libraryBrowser; export default libraryBrowser;