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

323 lines
9.9 KiB
JavaScript
Raw Normal View History

2015-07-03 10:55:29 -07:00
(function ($, window, document) {
function renderViews(page, user, result) {
var folderHtml = '';
2015-08-18 10:54:29 -07:00
folderHtml += '<div class="paperCheckboxList">';
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"' : '';
2015-08-18 10:54:29 -07:00
currentHtml += '<paper-checkbox class="chkGroupFolder" data-folderid="' + i.Id + '" id="' + id + '"' + checkedHtml + '>' + i.Name + '</paper-checkbox>';
2015-07-03 10:55:29 -07:00
return currentHtml;
}).join('');
folderHtml += '</div>';
2015-10-25 11:41:41 -07:00
$('.folderGroupList', page).html(folderHtml);
2015-07-03 10:55:29 -07:00
}
function renderViewStyles(page, user, result) {
var folderHtml = '';
2015-08-18 10:54:29 -07:00
folderHtml += '<div class="paperCheckboxList">';
2015-07-03 10:55:29 -07:00
folderHtml += result.map(function (i) {
var currentHtml = '';
var id = 'chkPlainFolder' + i.Id;
var isChecked = user.Configuration.PlainFolderViews.indexOf(i.Id) == -1;
var checkedHtml = isChecked ? ' checked="checked"' : '';
2015-08-18 10:54:29 -07:00
currentHtml += '<paper-checkbox class="chkPlainFolder" data-folderid="' + i.Id + '" id="' + id + '"' + checkedHtml + '>' + i.Name + '</paper-checkbox>';
2015-07-03 10:55:29 -07:00
return currentHtml;
}).join('');
folderHtml += '</div>';
2015-10-25 11:41:41 -07:00
$('.viewStylesList', page).html(folderHtml);
2015-07-03 10:55:29 -07:00
if (result.length) {
$('.viewStylesSection', page).show();
} else {
$('.viewStylesSection', page).hide();
}
}
function renderLatestItems(page, user, result) {
var folderHtml = '';
2015-08-18 10:54:29 -07:00
folderHtml += '<div class="paperCheckboxList">';
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"' : '';
2015-08-18 10:54:29 -07:00
currentHtml += '<paper-checkbox class="chkIncludeInLatest" data-folderid="' + i.Id + '" id="' + id + '"' + checkedHtml + '>' + i.Name + '</paper-checkbox>';
2015-07-03 10:55:29 -07:00
return currentHtml;
}).join('');
folderHtml += '</div>';
2015-10-25 11:41:41 -07:00
$('.latestItemsList', page).html(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 = '';
2015-09-06 09:02:41 -07:00
currentHtml += '<paper-icon-item class="viewItem" data-viewid="' + view.Id + '">';
2015-10-26 11:55:46 -07:00
currentHtml += '<paper-fab mini style="background-color:#444;" icon="folder-open" item-icon></paper-fab>';
2015-09-06 09:02:41 -07:00
currentHtml += '<paper-item-body>';
currentHtml += '<div>';
currentHtml += view.Name;
currentHtml += '</div>';
currentHtml += '</paper-item-body>';
2015-07-03 10:55:29 -07:00
if (index > 0) {
2015-09-06 09:02:41 -07:00
currentHtml += '<paper-icon-button icon="keyboard-arrow-up" class="btnViewItemUp btnViewItemMove" title="' + Globalize.translate('ButtonUp') + '"></paper-icon-button>';
2015-07-03 10:55:29 -07:00
}
else if (result.Items.length > 1) {
2015-09-06 09:02:41 -07:00
currentHtml += '<paper-icon-button icon="keyboard-arrow-down" class="btnViewItemDown btnViewItemMove" title="' + Globalize.translate('ButtonDown') + '"></paper-icon-button>';
2015-07-03 10:55:29 -07:00
}
2015-09-06 09:02:41 -07:00
currentHtml += '</paper-icon-item>';
2015-07-03 10:55:29 -07:00
index++;
return currentHtml;
}).join('');
2015-10-25 11:41:41 -07:00
$('.viewOrderList', page).html(html);
2015-07-03 10:55:29 -07:00
}
function loadForm(page, user, displayPreferences) {
2015-07-28 12:42:24 -07:00
page.querySelector('.chkDisplayCollectionView').checked = user.Configuration.DisplayCollectionsView || false;
page.querySelector('.chkHidePlayedFromLatest').checked = user.Configuration.HidePlayedInLatest || false;
2015-07-03 10:55:29 -07:00
2015-09-03 10:01:51 -07:00
$('#selectHomeSection1', page).val(displayPreferences.CustomPrefs.home0 || '');
$('#selectHomeSection2', page).val(displayPreferences.CustomPrefs.home1 || '');
$('#selectHomeSection3', page).val(displayPreferences.CustomPrefs.home2 || '');
$('#selectHomeSection4', page).val(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);
var promise3 = ApiClient.getJSON(ApiClient.getUrl("Users/" + user.Id + "/SpecialViewOptions"));
2015-09-15 11:09:44 -07:00
var promise4 = ApiClient.getJSON(ApiClient.getUrl("Users/" + user.Id + "/GroupingOptions"));
2015-07-03 10:55:29 -07:00
2015-12-14 08:43:03 -07:00
Promise.all([promise1, promise2, promise3, promise4]).then(function (responses) {
2015-07-03 10:55:29 -07:00
2015-12-14 08:43:03 -07:00
renderViews(page, user, responses[3]);
renderLatestItems(page, user, responses[0]);
renderViewOrder(page, user, responses[1]);
renderViewStyles(page, user, responses[2]);
2015-07-03 10:55:29 -07:00
Dashboard.hideLoadingMsg();
});
}
function saveUser(page, user, displayPreferences) {
2015-07-28 12:42:24 -07:00
user.Configuration.DisplayCollectionsView = page.querySelector('.chkDisplayCollectionView').checked;
user.Configuration.HidePlayedInLatest = page.querySelector('.chkHidePlayedFromLatest').checked;
2015-07-03 10:55:29 -07:00
2015-08-19 04:56:03 -07:00
user.Configuration.LatestItemsExcludes = $(".chkIncludeInLatest", page).get().filter(function (i) {
return !i.checked;
}).map(function (i) {
2015-07-03 10:55:29 -07:00
return i.getAttribute('data-folderid');
});
user.Configuration.ExcludeFoldersFromGrouping = null;
2015-09-24 22:15:29 -07:00
user.Configuration.GroupedFolders = $(".chkGroupFolder", page).get().filter(function (i) {
2015-08-19 04:56:03 -07:00
return i.checked;
}).map(function (i) {
2015-07-03 10:55:29 -07:00
return i.getAttribute('data-folderid');
});
2015-08-19 04:56:03 -07:00
user.Configuration.PlainFolderViews = $(".chkPlainFolder", page).get().filter(function (i) {
return !i.checked;
}).map(function (i) {
2015-07-03 10:55:29 -07:00
return i.getAttribute('data-folderid');
});
user.Configuration.OrderedViews = $(".viewItem", page).get().map(function (i) {
return i.getAttribute('data-viewid');
});
displayPreferences.CustomPrefs.home0 = $('#selectHomeSection1', page).val();
displayPreferences.CustomPrefs.home1 = $('#selectHomeSection2', page).val();
displayPreferences.CustomPrefs.home2 = $('#selectHomeSection3', page).val();
displayPreferences.CustomPrefs.home3 = $('#selectHomeSection4', page).val();
2016-01-06 13:16:16 -07:00
return ApiClient.updateDisplayPreferences('home', displayPreferences, user.Id, AppSettings.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
2015-12-14 08:43:03 -07:00
ApiClient.getDisplayPreferences('home', user.Id, AppSettings.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) {
Dashboard.alert(Globalize.translate('SettingsSaved'));
}
}, 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
}
function onSubmit() {
var page = $(this).parents('.page')[0];
save(page);
2015-07-03 10:55:29 -07:00
// Disable default form submission
return false;
}
2016-01-06 13:16:16 -07:00
pageIdOn('pageinit', "homeScreenPreferencesPage", function () {
2015-07-03 10:55:29 -07:00
var page = this;
$('.viewOrderList', page).on('click', '.btnViewItemMove', function () {
var li = $(this).parents('.viewItem');
2015-10-25 11:41:41 -07:00
var ul = li.parents('.paperList');
2015-07-03 10:55:29 -07:00
if ($(this).hasClass('btnViewItemDown')) {
var next = li.next();
li.remove().insertAfter(next);
} else {
var prev = li.prev();
li.remove().insertBefore(prev);
}
$('.viewItem', ul).each(function () {
2015-10-25 11:41:41 -07:00
var btn = $('.btnViewItemMove', this)[0];
2015-07-03 10:55:29 -07:00
if ($(this).prev('.viewItem').length) {
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
}
});
});
$('.homeScreenPreferencesForm').off('submit', onSubmit).on('submit', onSubmit);
2016-01-06 13:16:16 -07:00
if (AppInfo.enableAutoSave) {
page.querySelector('.btnSave').classList.add('hide');
} else {
page.querySelector('.btnSave').classList.remove('hide');
}
});
pageIdOn('pageshow', "homeScreenPreferencesPage", function () {
2015-07-03 10:55:29 -07:00
var page = this;
Dashboard.showLoadingMsg();
var userId = getParameterByName('userId') || Dashboard.getCurrentUserId();
2015-12-14 08:43:03 -07:00
ApiClient.getUser(userId).then(function (user) {
2015-07-03 10:55:29 -07:00
2015-12-14 08:43:03 -07:00
ApiClient.getDisplayPreferences('home', user.Id, AppSettings.displayPreferencesKey()).then(function (result) {
2015-07-03 10:55:29 -07:00
loadForm(page, user, result);
});
});
});
2016-01-06 13:16:16 -07:00
pageIdOn('pagebeforehide', "homeScreenPreferencesPage", function () {
var page = this;
if (AppInfo.enableAutoSave) {
save(page);
}
});
2015-07-03 10:55:29 -07:00
})(jQuery, window, document);