jellyfin-web/src/scripts/libraryBrowser.js

200 lines
8.8 KiB
JavaScript
Raw Normal View History

2020-05-04 03:44:12 -07:00
define(['userSettings', 'globalize'], function (userSettings, globalize) {
'use strict';
2019-10-07 14:58:10 -07:00
2018-10-22 15:05:09 -07:00
var libraryBrowser = {
2019-10-07 14:58:10 -07:00
getSavedQueryKey: function (modifier) {
2020-05-04 03:44:12 -07:00
return window.location.href.split('#')[0] + (modifier || '');
2018-10-22 15:05:09 -07:00
},
2019-10-07 14:58:10 -07:00
loadSavedQueryValues: function (key, query) {
2018-10-22 15:05:09 -07:00
var values = userSettings.get(key);
2019-10-07 14:58:10 -07:00
if (values) {
values = JSON.parse(values);
return Object.assign(query, values);
}
return query;
2018-10-22 15:05:09 -07:00
},
2019-10-07 14:58:10 -07:00
saveQueryValues: function (key, query) {
2018-10-22 15:05:09 -07:00
var values = {};
2019-10-07 14:58:10 -07:00
if (query.SortBy) {
values.SortBy = query.SortBy;
}
if (query.SortOrder) {
values.SortOrder = query.SortOrder;
}
userSettings.set(key, JSON.stringify(values));
2018-10-22 15:05:09 -07:00
},
2019-10-07 14:58:10 -07:00
saveViewSetting: function (key, value) {
2020-05-04 03:44:12 -07:00
userSettings.set(key + '-_view', value);
2018-10-22 15:05:09 -07:00
},
2019-10-07 14:58:10 -07:00
getSavedView: function (key) {
2020-05-04 03:44:12 -07:00
return userSettings.get(key + '-_view');
2018-10-22 15:05:09 -07:00
},
2019-10-07 14:58:10 -07:00
showLayoutMenu: function (button, currentLayout, views) {
var dispatchEvent = true;
if (!views) {
dispatchEvent = false;
2020-05-04 03:44:12 -07:00
views = button.getAttribute('data-layouts');
views = views ? views.split(',') : ['List', 'Poster', 'PosterCard', 'Thumb', 'ThumbCard'];
2019-10-07 14:58:10 -07:00
}
var menuItems = views.map(function (v) {
2018-10-22 15:05:09 -07:00
return {
2020-05-04 03:44:12 -07:00
name: globalize.translate('Option' + v),
2018-10-22 15:05:09 -07:00
id: v,
selected: currentLayout == v
2019-10-07 14:58:10 -07:00
};
2018-10-22 15:05:09 -07:00
});
2019-10-07 14:58:10 -07:00
2020-05-04 03:44:12 -07:00
require(['actionsheet'], function (actionsheet) {
2018-10-22 15:05:09 -07:00
actionsheet.show({
items: menuItems,
positionTo: button,
2019-10-07 14:58:10 -07:00
callback: function (id) {
2020-05-04 03:44:12 -07:00
button.dispatchEvent(new CustomEvent('layoutchange', {
2018-10-22 15:05:09 -07:00
detail: {
viewStyle: id
},
2019-10-07 14:58:10 -07:00
bubbles: true,
cancelable: false
}));
if (!dispatchEvent) {
if (window.$) {
2020-05-04 03:44:12 -07:00
$(button).trigger('layoutchange', [id]);
2019-10-07 14:58:10 -07:00
}
}
2018-10-22 15:05:09 -07:00
}
2019-10-07 14:58:10 -07:00
});
});
2018-10-22 15:05:09 -07:00
},
2019-10-07 14:58:10 -07:00
getQueryPagingHtml: function (options) {
var startIndex = options.startIndex;
var limit = options.limit;
var totalRecordCount = options.totalRecordCount;
2020-05-04 03:44:12 -07:00
var html = '';
2019-10-07 14:58:10 -07:00
var recordsEnd = Math.min(startIndex + limit, totalRecordCount);
var showControls = limit < totalRecordCount;
2018-10-22 15:05:09 -07:00
if (html += '<div class="listPaging">', showControls) {
html += '<span style="vertical-align:middle;">';
2020-05-04 03:44:12 -07:00
html += globalize.translate('ListPaging', (totalRecordCount ? startIndex + 1 : 0), recordsEnd, totalRecordCount);
html += '</span>';
2018-10-22 15:05:09 -07:00
}
2019-10-07 14:58:10 -07:00
if (showControls || options.viewButton || options.filterButton || options.sortButton || options.addLayoutButton) {
html += '<div style="display:inline-block;">';
if (showControls) {
2020-05-04 03:44:12 -07:00
html += '<button is="paper-icon-button-light" class="btnPreviousPage autoSize" ' + (startIndex ? '' : 'disabled') + '><span class="material-icons arrow_back"></span></button>';
html += '<button is="paper-icon-button-light" class="btnNextPage autoSize" ' + (startIndex + limit >= totalRecordCount ? 'disabled' : '') + '><span class="material-icons arrow_forward"></span></button>';
2019-10-07 14:58:10 -07:00
}
if (options.addLayoutButton) {
2020-05-04 03:44:12 -07:00
html += '<button is="paper-icon-button-light" title="' + globalize.translate('ButtonSelectView') + '" class="btnChangeLayout autoSize" data-layouts="' + (options.layouts || '') + '" onclick="LibraryBrowser.showLayoutMenu(this, \'' + (options.currentLayout || '') + '\');"><span class="material-icons view_comfy"></span></button>';
2019-10-07 14:58:10 -07:00
}
if (options.sortButton) {
2020-05-04 03:44:12 -07:00
html += '<button is="paper-icon-button-light" class="btnSort autoSize" title="' + globalize.translate('ButtonSort') + '"><span class="material-icons sort_by_alpha"></span></button>';
2019-10-07 14:58:10 -07:00
}
if (options.filterButton) {
2020-05-04 03:44:12 -07:00
html += '<button is="paper-icon-button-light" class="btnFilter autoSize" title="' + globalize.translate('ButtonFilter') + '"><span class="material-icons filter_list"></span></button>';
2019-10-07 14:58:10 -07:00
}
2020-05-04 03:44:12 -07:00
html += '</div>';
2019-10-07 14:58:10 -07:00
}
2020-05-04 03:44:12 -07:00
return html += '</div>';
2018-10-22 15:05:09 -07:00
},
2019-10-07 14:58:10 -07:00
showSortMenu: function (options) {
2020-05-04 03:44:12 -07:00
require(['dialogHelper', 'emby-radio'], function (dialogHelper) {
2018-10-22 15:05:09 -07:00
function onSortByChange() {
var newValue = this.value;
2019-10-07 14:58:10 -07:00
2018-10-22 15:05:09 -07:00
if (this.checked) {
var changed = options.query.SortBy != newValue;
2020-05-04 03:44:12 -07:00
options.query.SortBy = newValue.replace('_', ',');
2019-10-07 14:58:10 -07:00
options.query.StartIndex = 0;
if (options.callback && changed) {
options.callback();
}
2018-10-22 15:05:09 -07:00
}
}
function onSortOrderChange() {
var newValue = this.value;
2019-10-07 14:58:10 -07:00
2018-10-22 15:05:09 -07:00
if (this.checked) {
var changed = options.query.SortOrder != newValue;
2019-10-07 14:58:10 -07:00
options.query.SortOrder = newValue;
options.query.StartIndex = 0;
if (options.callback && changed) {
options.callback();
}
2018-10-22 15:05:09 -07:00
}
}
2019-10-07 14:58:10 -07:00
2018-10-22 15:05:09 -07:00
var dlg = dialogHelper.createDialog({
2019-10-07 14:58:10 -07:00
removeOnClose: true,
modal: false,
2018-10-22 15:05:09 -07:00
entryAnimationDuration: 160,
exitAnimationDuration: 200
});
2020-05-04 03:44:12 -07:00
dlg.classList.add('ui-body-a');
dlg.classList.add('background-theme-a');
dlg.classList.add('formDialog');
var html = '';
2019-10-07 14:58:10 -07:00
html += '<div style="margin:0;padding:1.25em 1.5em 1.5em;">';
html += '<h2 style="margin:0 0 .5em;">';
2020-05-04 03:44:12 -07:00
html += globalize.translate('HeaderSortBy');
html += '</h2>';
var i;
var length;
2019-10-07 14:58:10 -07:00
var isChecked;
2019-10-08 09:54:02 -07:00
html += '<div>';
for (i = 0, length = options.items.length; i < length; i++) {
2019-10-07 14:58:10 -07:00
var option = options.items[i];
2020-05-04 03:44:12 -07:00
var radioValue = option.id.replace(',', '_');
isChecked = (options.query.SortBy || '').replace(',', '_') == radioValue ? ' checked' : '';
html += '<label class="radio-label-block"><input type="radio" is="emby-radio" name="SortBy" data-id="' + option.id + '" value="' + radioValue + '" class="menuSortBy" ' + isChecked + ' /><span>' + option.name + '</span></label>';
2018-10-22 15:05:09 -07:00
}
2019-10-07 14:58:10 -07:00
2020-05-04 03:44:12 -07:00
html += '</div>';
2019-10-07 14:58:10 -07:00
html += '<h2 style="margin: 1em 0 .5em;">';
2020-05-04 03:44:12 -07:00
html += globalize.translate('HeaderSortOrder');
html += '</h2>';
html += '<div>';
isChecked = 'Ascending' == options.query.SortOrder ? ' checked' : '';
html += '<label class="radio-label-block"><input type="radio" is="emby-radio" name="SortOrder" value="Ascending" class="menuSortOrder" ' + isChecked + ' /><span>' + globalize.translate('OptionAscending') + '</span></label>';
isChecked = 'Descending' == options.query.SortOrder ? ' checked' : '';
html += '<label class="radio-label-block"><input type="radio" is="emby-radio" name="SortOrder" value="Descending" class="menuSortOrder" ' + isChecked + ' /><span>' + globalize.translate('OptionDescending') + '</span></label>';
html += '</div>';
html += '</div>';
2019-10-07 14:58:10 -07:00
dlg.innerHTML = html;
dialogHelper.open(dlg);
2020-05-04 03:44:12 -07:00
var sortBys = dlg.querySelectorAll('.menuSortBy');
2019-10-07 14:58:10 -07:00
for (i = 0, length = sortBys.length; i < length; i++) {
2020-05-04 03:44:12 -07:00
sortBys[i].addEventListener('change', onSortByChange);
2019-10-07 14:58:10 -07:00
}
2020-05-04 03:44:12 -07:00
var sortOrders = dlg.querySelectorAll('.menuSortOrder');
2019-10-07 14:58:10 -07:00
for (i = 0, length = sortOrders.length; i < length; i++) {
2020-05-04 03:44:12 -07:00
sortOrders[i].addEventListener('change', onSortOrderChange);
2019-10-07 14:58:10 -07:00
}
});
2018-10-22 15:05:09 -07:00
}
};
2019-10-07 14:58:10 -07:00
window.LibraryBrowser = libraryBrowser;
return libraryBrowser;
});