jellyfin-web/dashboard-ui/scripts/mypreferenceshome.js

341 lines
11 KiB
JavaScript
Raw Normal View History

2016-06-25 18:33:16 -07:00
define(['listViewStyle'], function () {
2015-07-03 10:55:29 -07:00
function renderViews(page, user, result) {
var folderHtml = '';
2016-06-13 21:15:13 -07:00
folderHtml += '<div class="checkboxList">';
2015-09-15 11:09:44 -07:00
folderHtml += result.map(function (i) {
2015-07-03 10:55:29 -07:00
var currentHtml = '';
var id = 'chkGroupFolder' + i.Id;
var isChecked = (user.Configuration.ExcludeFoldersFromGrouping != null && user.Configuration.ExcludeFoldersFromGrouping.indexOf(i.Id) == -1) ||
user.Configuration.GroupedFolders.indexOf(i.Id) != -1;
var checkedHtml = isChecked ? ' checked="checked"' : '';
2016-06-13 21:15:13 -07:00
currentHtml += '<label>';
currentHtml += '<input type="checkbox" is="emby-checkbox" class="chkGroupFolder" data-folderid="' + i.Id + '" id="' + id + '"' + checkedHtml + '/>';
currentHtml += '<span>' + i.Name + '</span>';
currentHtml += '</label>';
2015-07-03 10:55:29 -07:00
return currentHtml;
}).join('');
folderHtml += '</div>';
2016-06-20 17:47:37 -07:00
page.querySelector('.folderGroupList').innerHTML = folderHtml;
2015-07-03 10:55:29 -07:00
}
function renderLatestItems(page, user, result) {
var folderHtml = '';
2016-06-13 21:15:13 -07:00
folderHtml += '<div class="checkboxList">';
2015-07-03 10:55:29 -07:00
folderHtml += result.Items.map(function (i) {
var currentHtml = '';
var id = 'chkIncludeInLatest' + i.Id;
var isChecked = user.Configuration.LatestItemsExcludes.indexOf(i.Id) == -1;
var checkedHtml = isChecked ? ' checked="checked"' : '';
2016-06-13 21:15:13 -07:00
currentHtml += '<label>';
currentHtml += '<input type="checkbox" is="emby-checkbox" class="chkIncludeInLatest" data-folderid="' + i.Id + '" id="' + id + '"' + checkedHtml + '/>';
currentHtml += '<span>' + i.Name + '</span>';
currentHtml += '</label>';
2015-07-03 10:55:29 -07:00
return currentHtml;
}).join('');
folderHtml += '</div>';
2016-06-20 17:47:37 -07:00
page.querySelector('.latestItemsList').innerHTML = folderHtml;
2015-07-03 10:55:29 -07:00
}
function renderViewOrder(page, user, result) {
var html = '';
var index = 0;
html += result.Items.map(function (view) {
var currentHtml = '';
2016-06-25 18:33:16 -07:00
currentHtml += '<div class="listItem viewItem" data-viewid="' + view.Id + '">';
2015-09-06 09:02:41 -07:00
2016-06-25 18:33:16 -07:00
currentHtml += '<button type="button" is="emby-button" class="fab mini autoSize" item-icon><i class="md-icon">folder_open</i></button>';
2015-09-06 09:02:41 -07:00
2016-06-25 18:33:16 -07:00
currentHtml += '<div class="listItemBody">';
2015-09-06 09:02:41 -07:00
currentHtml += '<div>';
currentHtml += view.Name;
currentHtml += '</div>';
2016-06-25 18:33:16 -07:00
currentHtml += '</div>';
2015-07-03 10:55:29 -07:00
if (index > 0) {
2016-06-18 22:26:52 -07:00
currentHtml += '<button type="button" is="paper-icon-button-light" class="btnViewItemUp btnViewItemMove autoSize" title="' + Globalize.translate('ButtonUp') + '"><i class="md-icon">keyboard_arrow_up</i></button>';
2015-07-03 10:55:29 -07:00
}
else if (result.Items.length > 1) {
2016-06-18 22:26:52 -07:00
currentHtml += '<button type="button" is="paper-icon-button-light" class="btnViewItemDown btnViewItemMove autoSize" title="' + Globalize.translate('ButtonDown') + '"><i class="md-icon">keyboard_arrow_down</i></button>';
2015-07-03 10:55:29 -07:00
}
2016-06-25 18:33:16 -07:00
currentHtml += '</div>';
2015-07-03 10:55:29 -07:00
index++;
return currentHtml;
}).join('');
2016-06-20 17:47:37 -07:00
page.querySelector('.viewOrderList').innerHTML = html;
2015-07-03 10:55:29 -07:00
}
function loadForm(page, user, displayPreferences) {
2015-07-28 12:42:24 -07:00
page.querySelector('.chkHidePlayedFromLatest').checked = user.Configuration.HidePlayedInLatest || false;
2016-03-29 20:10:01 -07:00
page.querySelector('.chkDisplayChannelsInline').checked = !(user.Configuration.EnableChannelView || false);
2015-07-03 10:55:29 -07:00
2016-06-20 17:47:37 -07:00
page.querySelector('#selectHomeSection1').value = displayPreferences.CustomPrefs.home0 || '';
page.querySelector('#selectHomeSection2').value = displayPreferences.CustomPrefs.home1 || '';
page.querySelector('#selectHomeSection3').value = displayPreferences.CustomPrefs.home2 || '';
page.querySelector('#selectHomeSection4').value = displayPreferences.CustomPrefs.home3 || '';
2015-07-03 10:55:29 -07:00
var promise1 = ApiClient.getItems(user.Id, {
sortBy: "SortName"
});
2015-08-14 12:14:54 -07:00
var promise2 = ApiClient.getUserViews({}, user.Id);
2016-06-03 09:24:04 -07:00
var promise3 = ApiClient.getJSON(ApiClient.getUrl("Users/" + user.Id + "/GroupingOptions"));
2015-07-03 10:55:29 -07:00
2016-06-03 09:24:04 -07:00
Promise.all([promise1, promise2, promise3]).then(function (responses) {
2015-07-03 10:55:29 -07:00
2016-06-03 09:24:04 -07:00
renderViews(page, user, responses[2]);
2015-12-14 08:43:03 -07:00
renderLatestItems(page, user, responses[0]);
renderViewOrder(page, user, responses[1]);
2015-07-03 10:55:29 -07:00
Dashboard.hideLoadingMsg();
});
}
2016-02-29 23:02:03 -07:00
function displayPreferencesKey() {
if (AppInfo.isNativeApp) {
return 'Emby Mobile';
}
return 'webclient';
}
2016-06-20 17:47:37 -07:00
function getCheckboxItems(selector, page, isChecked) {
var inputs = page.querySelectorAll(selector);
var list = [];
for (var i = 0, length = inputs.length; i < length; i++) {
if (inputs[i].checked == isChecked) {
list.push(inputs[i]);
}
}
return list;
}
2015-07-03 10:55:29 -07:00
function saveUser(page, user, displayPreferences) {
2015-07-28 12:42:24 -07:00
user.Configuration.HidePlayedInLatest = page.querySelector('.chkHidePlayedFromLatest').checked;
2015-07-03 10:55:29 -07:00
2016-03-29 20:10:01 -07:00
user.Configuration.EnableChannelView = !page.querySelector('.chkDisplayChannelsInline').checked;
2016-01-27 12:28:12 -07:00
2016-06-20 17:47:37 -07:00
user.Configuration.LatestItemsExcludes = getCheckboxItems(".chkIncludeInLatest", page, false).map(function (i) {
2015-07-03 10:55:29 -07:00
return i.getAttribute('data-folderid');
});
user.Configuration.ExcludeFoldersFromGrouping = null;
2016-06-20 17:47:37 -07:00
user.Configuration.GroupedFolders = getCheckboxItems(".chkGroupFolder", page, true).map(function (i) {
2015-07-03 10:55:29 -07:00
return i.getAttribute('data-folderid');
});
2016-06-20 17:47:37 -07:00
var viewItems = page.querySelectorAll('.viewItem');
var orderedViews = [];
for (var i = 0, length = viewItems.length; i < length; i++) {
orderedViews.push(viewItems[i].getAttribute('data-viewid'));
}
2015-07-03 10:55:29 -07:00
2016-06-20 17:47:37 -07:00
user.Configuration.OrderedViews = orderedViews;
2015-07-03 10:55:29 -07:00
2016-06-20 17:47:37 -07:00
displayPreferences.CustomPrefs.home0 = page.querySelector('#selectHomeSection1').value;
displayPreferences.CustomPrefs.home1 = page.querySelector('#selectHomeSection2').value;
displayPreferences.CustomPrefs.home2 = page.querySelector('#selectHomeSection3').value;
displayPreferences.CustomPrefs.home3 = page.querySelector('#selectHomeSection4').value;
2015-07-03 10:55:29 -07:00
2016-02-29 23:02:03 -07:00
return ApiClient.updateDisplayPreferences('home', displayPreferences, user.Id, displayPreferencesKey()).then(function () {
2015-07-30 07:34:46 -07:00
2016-01-06 13:16:16 -07:00
return ApiClient.updateUserConfiguration(user.Id, user.Configuration);
2015-07-03 10:55:29 -07:00
});
}
2016-01-06 13:16:16 -07:00
function save(page) {
2015-07-03 10:55:29 -07:00
Dashboard.showLoadingMsg();
var userId = getParameterByName('userId') || Dashboard.getCurrentUserId();
2016-01-06 13:16:16 -07:00
if (!AppInfo.enableAutoSave) {
Dashboard.showLoadingMsg();
}
2015-12-14 08:43:03 -07:00
ApiClient.getUser(userId).then(function (user) {
2015-07-03 10:55:29 -07:00
2016-02-29 23:02:03 -07:00
ApiClient.getDisplayPreferences('home', user.Id, displayPreferencesKey()).then(function (displayPreferences) {
2015-07-03 10:55:29 -07:00
2016-01-06 13:16:16 -07:00
saveUser(page, user, displayPreferences).then(function () {
2015-07-03 10:55:29 -07:00
2016-01-06 13:16:16 -07:00
Dashboard.hideLoadingMsg();
if (!AppInfo.enableAutoSave) {
2016-02-24 23:38:12 -07:00
require(['toast'], function (toast) {
toast(Globalize.translate('SettingsSaved'));
});
2016-01-06 13:16:16 -07:00
}
}, function () {
Dashboard.hideLoadingMsg();
});
2015-07-03 10:55:29 -07:00
2016-01-06 13:16:16 -07:00
});
2015-07-03 10:55:29 -07:00
});
2016-01-06 13:16:16 -07:00
}
2016-06-20 17:47:37 -07:00
function parentWithClass(elem, className) {
2016-01-06 13:16:16 -07:00
2016-06-20 17:47:37 -07:00
while (!elem.classList || !elem.classList.contains(className)) {
elem = elem.parentNode;
2016-01-06 13:16:16 -07:00
2016-06-20 17:47:37 -07:00
if (!elem) {
return null;
}
}
2015-07-03 10:55:29 -07:00
2016-06-20 17:47:37 -07:00
return elem;
2015-07-03 10:55:29 -07:00
}
2016-06-20 17:47:37 -07:00
function getSibling(elem, type, className) {
var sibling = elem[type];
2015-07-03 10:55:29 -07:00
2016-06-20 17:47:37 -07:00
while (sibling != null) {
if (sibling.classList.contains(className)) {
break;
}
}
2015-07-03 10:55:29 -07:00
2016-06-20 17:47:37 -07:00
if (sibling != null) {
if (!sibling.classList.contains(className)) {
sibling = null;
}
}
2015-07-03 10:55:29 -07:00
2016-06-20 17:47:37 -07:00
return sibling;
}
2015-07-03 10:55:29 -07:00
2016-06-20 17:47:37 -07:00
return function (view, params) {
2015-07-03 10:55:29 -07:00
2016-06-20 17:47:37 -07:00
function onSubmit(e) {
2015-07-03 10:55:29 -07:00
2016-06-20 17:47:37 -07:00
save(view);
// Disable default form submission
e.preventDefault();
return false;
}
view.querySelector('.viewOrderList').addEventListener('click', function (e) {
var target = parentWithClass(e.target, 'btnViewItemMove');
var li = parentWithClass(target, 'viewItem');
var ul = parentWithClass(li, 'paperList');
if (target.classList.contains('btnViewItemDown')) {
var next = li.nextSibling;
li.parentNode.removeChild(li);
next.parentNode.insertBefore(li, next.nextSibling);
2015-07-03 10:55:29 -07:00
} else {
2016-06-20 17:47:37 -07:00
var prev = li.previousSibling;
2015-07-03 10:55:29 -07:00
2016-06-20 17:47:37 -07:00
li.parentNode.removeChild(li);
prev.parentNode.insertBefore(li, prev);
2015-07-03 10:55:29 -07:00
}
2016-06-20 17:47:37 -07:00
var viewItems = ul.querySelectorAll('.viewItem');
for (var i = 0, length = viewItems.length; i < length; i++) {
var viewItem = viewItems[i];
var btn = viewItem.querySelector('.btnViewItemMove');
2015-07-03 10:55:29 -07:00
2016-06-20 17:47:37 -07:00
var prevViewItem = getSibling(viewItem, 'previousSibling', 'viewItem');
2015-10-25 11:41:41 -07:00
2016-06-20 17:47:37 -07:00
if (prevViewItem) {
2015-10-25 11:41:41 -07:00
btn.classList.add('btnViewItemUp');
btn.classList.remove('btnViewItemDown');
btn.icon = 'keyboard-arrow-up';
2015-07-03 10:55:29 -07:00
} else {
2015-10-25 11:41:41 -07:00
btn.classList.remove('btnViewItemUp');
btn.classList.add('btnViewItemDown');
btn.icon = 'keyboard-arrow-down';
2015-07-03 10:55:29 -07:00
}
2016-06-20 17:47:37 -07:00
}
2015-07-03 10:55:29 -07:00
});
2016-06-20 17:47:37 -07:00
view.querySelector('.homeScreenPreferencesForm').addEventListener('submit', onSubmit);
2015-07-03 10:55:29 -07:00
2016-01-06 13:16:16 -07:00
if (AppInfo.enableAutoSave) {
2016-06-20 17:47:37 -07:00
view.querySelector('.btnSave').classList.add('hide');
2016-01-06 13:16:16 -07:00
} else {
2016-06-20 17:47:37 -07:00
view.querySelector('.btnSave').classList.remove('hide');
2016-01-06 13:16:16 -07:00
}
2016-06-20 17:47:37 -07:00
view.addEventListener('viewshow', function () {
var page = this;
2015-07-03 10:55:29 -07:00
2016-06-20 17:47:37 -07:00
Dashboard.showLoadingMsg();
2015-07-03 10:55:29 -07:00
2016-06-20 17:47:37 -07:00
var userId = getParameterByName('userId') || Dashboard.getCurrentUserId();
2015-07-03 10:55:29 -07:00
2016-06-20 17:47:37 -07:00
ApiClient.getUser(userId).then(function (user) {
2015-07-03 10:55:29 -07:00
2016-06-20 17:47:37 -07:00
ApiClient.getDisplayPreferences('home', user.Id, displayPreferencesKey()).then(function (result) {
2015-07-03 10:55:29 -07:00
2016-06-20 17:47:37 -07:00
loadForm(page, user, result);
2015-07-03 10:55:29 -07:00
2016-06-20 17:47:37 -07:00
});
2015-07-03 10:55:29 -07:00
});
});
2016-06-20 17:47:37 -07:00
view.addEventListener('viewbeforehide', function () {
var page = this;
2016-01-06 13:16:16 -07:00
2016-06-20 17:47:37 -07:00
if (AppInfo.enableAutoSave) {
save(page);
}
});
};
});