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

182 lines
5.1 KiB
JavaScript
Raw Normal View History

(function ($, window, document) {
function populateRatings(allParentalRatings, page) {
var html = "";
html += "<option value=''></option>";
var ratings = [];
var i, length, rating;
for (i = 0, length = allParentalRatings.length; i < length; i++) {
rating = allParentalRatings[i];
if (ratings.length) {
var lastRating = ratings[ratings.length - 1];
if (lastRating.Value === rating.Value) {
lastRating.Name += "/" + rating.Name;
continue;
}
}
ratings.push({ Name: rating.Name, Value: rating.Value });
}
for (i = 0, length = ratings.length; i < length; i++) {
rating = ratings[i];
html += "<option value='" + rating.Value + "'>" + rating.Name + "</option>";
}
$('#selectMaxParentalRating', page).html(html).selectmenu("refresh");
}
function loadUnratedItems(page, user) {
var items = [
2014-07-09 20:48:08 -07:00
{ name: Globalize.translate('OptionBlockBooks'), value: 'Book' },
{ name: Globalize.translate('OptionBlockGames'), value: 'Game' },
{ name: Globalize.translate('OptionBlockChannelContent'), value: 'ChannelContent' },
{ name: Globalize.translate('OptionBlockLiveTvChannels'), value: 'LiveTvChannel' },
{ name: Globalize.translate('OptionBlockLiveTvPrograms'), value: 'LiveTvProgram' },
{ name: Globalize.translate('OptionBlockMovies'), value: 'Movie' },
{ name: Globalize.translate('OptionBlockMusic'), value: 'Music' },
{ name: Globalize.translate('OptionBlockTrailers'), value: 'Trailer' },
{ name: Globalize.translate('OptionBlockTvShows'), value: 'Series' },
{ name: Globalize.translate('OptionBlockOthers'), value: 'Other' }
];
var html = '';
html += '<fieldset data-role="controlgroup">';
2014-07-09 20:48:08 -07:00
html += '<legend>' + Globalize.translate('HeaderBlockItemsWithNoRating') + '</legend>';
for (var i = 0, length = items.length; i < length; i++) {
var item = items[i];
var id = 'unratedItem' + i;
var checkedAttribute = user.Configuration.BlockUnratedItems.indexOf(item.value) != -1 ? ' checked="checked"' : '';
2014-08-18 19:49:16 -07:00
html += '<input class="chkUnratedItem" data-itemtype="' + item.value + '" type="checkbox" id="' + id + '"' + checkedAttribute + ' />';
html += '<label for="' + id + '">' + item.name + '</label>';
}
html += '</fieldset>';
$('.blockUnratedItems', page).html(html).trigger('create');
}
2014-10-13 21:59:34 -07:00
function loadUser(page, user, loggedInUser, allParentalRatings) {
Dashboard.setPageTitle(user.Name);
loadUnratedItems(page, user);
2014-02-20 22:04:11 -07:00
populateRatings(allParentalRatings, page);
var ratingValue = "";
if (user.Configuration.MaxParentalRating) {
for (var i = 0, length = allParentalRatings.length; i < length; i++) {
var rating = allParentalRatings[i];
if (user.Configuration.MaxParentalRating >= rating.Value) {
ratingValue = rating.Value;
}
}
}
$('#selectMaxParentalRating', page).val(ratingValue).selectmenu("refresh");
Dashboard.hideLoadingMsg();
}
function onSaveComplete(page) {
Dashboard.hideLoadingMsg();
2014-05-30 12:23:56 -07:00
Dashboard.alert(Globalize.translate('SettingsSaved'));
}
function saveUser(user, page) {
user.Configuration.MaxParentalRating = $('#selectMaxParentalRating', page).val() || null;
user.Configuration.BlockUnratedItems = $('.chkUnratedItem:checked', page).map(function () {
return this.getAttribute('data-itemtype');
}).get();
ApiClient.updateUser(user).done(function () {
onSaveComplete(page);
});
}
window.UserParentalControlPage = {
onSubmit: function () {
var page = $(this).parents('.page');
Dashboard.showLoadingMsg();
var userId = getParameterByName("userId");
ApiClient.getUser(userId).done(function (result) {
saveUser(result, page);
});
// Disable default form submission
return false;
}
};
$(document).on('pageshow', "#userParentalControlPage", function () {
var page = this;
Dashboard.showLoadingMsg();
var userId = getParameterByName("userId");
var promise1;
if (!userId) {
var deferred = $.Deferred();
deferred.resolveWith(null, [{
Configuration: {}
}]);
promise1 = deferred.promise();
} else {
promise1 = ApiClient.getUser(userId);
}
var promise2 = Dashboard.getCurrentUser();
var promise3 = ApiClient.getParentalRatings();
2014-10-13 21:59:34 -07:00
$.when(promise1, promise2, promise3).done(function (response1, response2, response3) {
2014-10-13 21:59:34 -07:00
loadUser(page, response1[0] || response1, response2[0], response3[0]);
});
});
})(jQuery, window, document);