jellyfin-web/src/scripts/librarybrowser.js

199 lines
8.6 KiB
JavaScript
Raw Normal View History

2019-10-07 14:58:10 -07:00
define(["userSettings"], function (userSettings) {
2018-10-22 15:05:09 -07:00
"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) {
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) {
userSettings.set(key + "-_view", value);
2018-10-22 15:05:09 -07:00
},
2019-10-07 14:58:10 -07:00
getSavedView: function (key) {
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;
views = button.getAttribute("data-layouts");
views = views ? views.split(",") : ["List", "Poster", "PosterCard", "Thumb", "ThumbCard"];
}
var menuItems = views.map(function (v) {
2018-10-22 15:05:09 -07:00
return {
name: Globalize.translate("Option" + v),
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
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) {
2018-10-22 15:05:09 -07:00
button.dispatchEvent(new CustomEvent("layoutchange", {
detail: {
viewStyle: id
},
2019-10-07 14:58:10 -07:00
bubbles: true,
cancelable: false
}));
if (!dispatchEvent) {
if (window.$) {
$(button).trigger("layoutchange", [id]);
}
}
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;
var html = "";
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;">';
2019-10-07 14:58:10 -07:00
html += (totalRecordCount ? startIndex + 1 : 0) + "-" + recordsEnd + " of " + 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) {
html += '<button is="paper-icon-button-light" class="btnPreviousPage autoSize" ' + (startIndex ? "" : "disabled") + '><i class="md-icon">&#xE5C4;</i></button>';
html += '<button is="paper-icon-button-light" class="btnNextPage autoSize" ' + (startIndex + limit >= totalRecordCount ? "disabled" : "") + '><i class="md-icon">&#xE5C8;</i></button>';
}
if (options.addLayoutButton) {
html += '<button is="paper-icon-button-light" title="' + Globalize.translate("ButtonSelectView") + '" class="btnChangeLayout autoSize" data-layouts="' + (options.layouts || "") + '" onclick="LibraryBrowser.showLayoutMenu(this, \'' + (options.currentLayout || "") + '\');"><i class="md-icon">&#xE42A;</i></button>';
}
if (options.sortButton) {
html += '<button is="paper-icon-button-light" class="btnSort autoSize" title="' + Globalize.translate("ButtonSort") + '"><i class="md-icon">&#xE053;</i></button>';
}
if (options.filterButton) {
html += '<button is="paper-icon-button-light" class="btnFilter autoSize" title="' + Globalize.translate("ButtonFilter") + '"><i class="md-icon">&#xE152;</i></button>';
}
html += "</div>";
}
return html += "</div>";
2018-10-22 15:05:09 -07:00
},
2019-10-07 14:58:10 -07:00
showSortMenu: function (options) {
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;
2019-10-07 14:58:10 -07:00
options.query.SortBy = newValue.replace("_", ",");
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
});
2019-10-07 14:58:10 -07:00
dlg.classList.add("ui-body-a");
dlg.classList.add("background-theme-a");
dlg.classList.add("formDialog");
2018-10-22 15:05:09 -07:00
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;">';
html += Globalize.translate("HeaderSortBy");
html += "</h2>";
2019-10-08 09:54:02 -07:00
var i, 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];
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
html += "</div>";
html += '<h2 style="margin: 1em 0 .5em;">';
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>";
dlg.innerHTML = html;
dialogHelper.open(dlg);
2018-10-22 15:05:09 -07:00
var sortBys = dlg.querySelectorAll(".menuSortBy");
2019-10-07 14:58:10 -07:00
for (i = 0, length = sortBys.length; i < length; i++) {
sortBys[i].addEventListener("change", onSortByChange);
}
2018-10-22 15:05:09 -07:00
var sortOrders = dlg.querySelectorAll(".menuSortOrder");
2019-10-07 14:58:10 -07:00
for (i = 0, length = sortOrders.length; i < length; i++) {
sortOrders[i].addEventListener("change", onSortOrderChange);
}
});
2018-10-22 15:05:09 -07:00
}
};
2019-10-07 14:58:10 -07:00
window.LibraryBrowser = libraryBrowser;
return libraryBrowser;
});