define(['userSettings', 'globalize'], function (userSettings, globalize) { 'use strict'; var libraryBrowser = { getSavedQueryKey: function (modifier) { return window.location.href.split('#')[0] + (modifier || ''); }, loadSavedQueryValues: function (key, query) { var values = userSettings.get(key); if (values) { values = JSON.parse(values); return Object.assign(query, values); } return query; }, saveQueryValues: function (key, query) { var values = {}; if (query.SortBy) { values.SortBy = query.SortBy; } if (query.SortOrder) { values.SortOrder = query.SortOrder; } userSettings.set(key, JSON.stringify(values)); }, saveViewSetting: function (key, value) { userSettings.set(key + '-_view', value); }, getSavedView: function (key) { return userSettings.get(key + '-_view'); }, showLayoutMenu: function (button, currentLayout, views) { var dispatchEvent = true; if (!views) { dispatchEvent = false; views = button.getAttribute('data-layouts'); views = views ? views.split(',') : ['List', 'Poster', 'PosterCard', 'Thumb', 'ThumbCard']; } var menuItems = views.map(function (v) { return { name: globalize.translate('Option' + v), id: v, selected: currentLayout == v }; }); require(['actionsheet'], function (actionsheet) { actionsheet.show({ items: menuItems, positionTo: button, callback: function (id) { button.dispatchEvent(new CustomEvent('layoutchange', { detail: { viewStyle: id }, bubbles: true, cancelable: false })); if (!dispatchEvent) { if (window.$) { $(button).trigger('layoutchange', [id]); } } } }); }); }, getQueryPagingHtml: function (options) { var startIndex = options.startIndex; var limit = options.limit; var totalRecordCount = options.totalRecordCount; var html = ''; var recordsEnd = Math.min(startIndex + limit, totalRecordCount); var showControls = limit < totalRecordCount; if (html += '
', 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 += '
'; } return html += '
'; }, showSortMenu: function (options) { require(['dialogHelper', 'emby-radio'], function (dialogHelper) { function onSortByChange() { var newValue = this.value; if (this.checked) { var changed = options.query.SortBy != newValue; options.query.SortBy = newValue.replace('_', ','); options.query.StartIndex = 0; if (options.callback && changed) { options.callback(); } } } function onSortOrderChange() { var newValue = this.value; if (this.checked) { var changed = options.query.SortOrder != newValue; options.query.SortOrder = newValue; options.query.StartIndex = 0; if (options.callback && changed) { options.callback(); } } } var 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'); var html = ''; html += '
'; html += '

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

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

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

'; html += '
'; isChecked = 'Ascending' == options.query.SortOrder ? ' checked' : ''; html += ''; isChecked = 'Descending' == options.query.SortOrder ? ' checked' : ''; html += ''; html += '
'; html += '
'; dlg.innerHTML = html; dialogHelper.open(dlg); var sortBys = dlg.querySelectorAll('.menuSortBy'); for (i = 0, length = sortBys.length; i < length; i++) { sortBys[i].addEventListener('change', onSortByChange); } var sortOrders = dlg.querySelectorAll('.menuSortOrder'); for (i = 0, length = sortOrders.length; i < length; i++) { sortOrders[i].addEventListener('change', onSortOrderChange); } }); } }; window.LibraryBrowser = libraryBrowser; return libraryBrowser; });