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

355 lines
9.9 KiB
JavaScript
Raw Normal View History

2016-09-25 11:39:13 -07:00
define(['jQuery', 'datetime', 'listViewStyle', 'paper-icon-button-light'], function ($, datetime) {
2016-10-22 22:11:46 -07:00
'use strict';
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>";
}
2015-09-03 10:01:51 -07:00
$('#selectMaxParentalRating', page).html(html);
}
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('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 = '';
2016-09-15 16:17:50 -07:00
html += '<h3 class="checkboxListLabel">' + Globalize.translate('HeaderBlockItemsWithNoRating') + '</h3>';
2016-09-15 16:17:50 -07:00
html += '<div class="checkboxList paperList checkboxList-paperList">';
for (var i = 0, length = items.length; i < length; i++) {
var item = items[i];
2014-12-19 23:06:27 -07:00
var checkedAttribute = user.Policy.BlockUnratedItems.indexOf(item.value) != -1 ? ' checked="checked"' : '';
2016-09-15 16:17:50 -07:00
html += '<label><input type="checkbox" is="emby-checkbox" class="chkUnratedItem" data-itemtype="' + item.value + '" type="checkbox"' + checkedAttribute + '><span>' + item.name + '</span></label>';
}
2016-02-18 22:03:00 -07:00
html += '</div>';
$('.blockUnratedItems', page).html(html).trigger('create');
}
2014-11-28 19:40:46 -07:00
function loadUser(page, user, allParentalRatings) {
LibraryMenu.setTitle(user.Name);
loadUnratedItems(page, user);
2015-02-08 23:56:45 -07:00
loadBlockedTags(page, user.Policy.BlockedTags);
2014-02-20 22:04:11 -07:00
populateRatings(allParentalRatings, page);
var ratingValue = "";
2014-12-19 23:06:27 -07:00
if (user.Policy.MaxParentalRating) {
for (var i = 0, length = allParentalRatings.length; i < length; i++) {
var rating = allParentalRatings[i];
2014-12-19 23:06:27 -07:00
if (user.Policy.MaxParentalRating >= rating.Value) {
ratingValue = rating.Value;
}
}
}
2015-09-03 10:01:51 -07:00
$('#selectMaxParentalRating', page).val(ratingValue);
2014-12-19 23:06:27 -07:00
if (user.Policy.IsAdministrator) {
$('.accessScheduleSection', page).hide();
} else {
$('.accessScheduleSection', page).show();
}
2014-12-19 23:06:27 -07:00
renderAccessSchedule(page, user.Policy.AccessSchedules || []);
Dashboard.hideLoadingMsg();
}
2015-02-08 23:56:45 -07:00
function loadBlockedTags(page, tags) {
2014-11-28 19:40:46 -07:00
2016-09-21 10:06:08 -07:00
var html = tags.map(function (h) {
2014-11-28 19:40:46 -07:00
2016-09-21 10:06:08 -07:00
var li = '<div class="listItem">';
2014-11-28 19:40:46 -07:00
2016-09-21 10:06:08 -07:00
li += '<div class="listItemBody">';
2016-09-25 11:39:13 -07:00
li += '<h3 class="listItemBodyText">';
2016-09-21 10:06:08 -07:00
li += h;
li += '</h3>';
li += '</div>';
2014-11-28 19:40:46 -07:00
2016-09-21 10:06:08 -07:00
li += '<button type="button" is="paper-icon-button-light" class="blockedTag btnDeleteTag listItemButton" data-tag="' + h + '"><i class="md-icon">delete</i></button>';
2016-09-25 11:39:13 -07:00
2016-09-21 10:06:08 -07:00
li += '</div>';
2015-02-08 23:56:45 -07:00
return li;
2016-09-21 10:06:08 -07:00
}).join('');
if (html) {
html = '<div class="paperList">' + html + '</div>';
}
2015-02-08 23:56:45 -07:00
var elem = $('.blockedTags', page).html(html).trigger('create');
$('.btnDeleteTag', elem).on('click', function () {
var tag = this.getAttribute('data-tag');
var newTags = tags.filter(function (t) {
return t != tag;
});
loadBlockedTags(page, newTags);
});
}
function deleteAccessSchedule(page, schedules, index) {
schedules.splice(index, 1);
renderAccessSchedule(page, schedules);
}
function renderAccessSchedule(page, schedules) {
2016-09-25 11:39:13 -07:00
var html = '';
var index = 0;
html += schedules.map(function (a) {
var itemHtml = '';
2016-09-25 11:39:13 -07:00
itemHtml += '<div class="liSchedule listItem" data-day="' + a.DayOfWeek + '" data-start="' + a.StartHour + '" data-end="' + a.EndHour + '">';
2016-09-25 11:39:13 -07:00
itemHtml += '<div class="listItemBody two-line">';
itemHtml += '<h3 class="listItemBodyText">';
itemHtml += Globalize.translate('Option' + a.DayOfWeek);
itemHtml += '</h3>';
itemHtml += '<div class="listItemBodyText secondary">' + getDisplayTime(a.StartHour) + ' - ' + getDisplayTime(a.EndHour) + '</div>';
itemHtml += '</div>';
2016-09-25 11:39:13 -07:00
itemHtml += '<button type="button" is="paper-icon-button-light" class="btnDelete listItemButton" data-index="' + index + '"><i class="md-icon">delete</i></button>';
2016-09-25 11:39:13 -07:00
itemHtml += '</div>';
index++;
return itemHtml;
}).join('');
2016-09-25 11:39:13 -07:00
var accessScheduleList = page.querySelector('.accessScheduleList');
accessScheduleList.innerHTML = html;
2016-09-25 11:39:13 -07:00
$('.btnDelete', accessScheduleList).on('click', function () {
deleteAccessSchedule(page, schedules, parseInt(this.getAttribute('data-index')));
});
}
function onSaveComplete(page) {
Dashboard.hideLoadingMsg();
2016-02-24 23:38:12 -07:00
require(['toast'], function (toast) {
toast(Globalize.translate('SettingsSaved'));
});
}
function saveUser(user, page) {
2014-12-19 23:06:27 -07:00
user.Policy.MaxParentalRating = $('#selectMaxParentalRating', page).val() || null;
2016-09-25 11:39:13 -07:00
user.Policy.BlockUnratedItems = $('.chkUnratedItem', page).get().filter(function (i) {
2016-02-18 22:03:00 -07:00
return i.checked;
2016-02-18 22:03:00 -07:00
}).map(function (i) {
2016-02-18 22:03:00 -07:00
return i.getAttribute('data-itemtype');
});
2014-12-19 23:06:27 -07:00
user.Policy.AccessSchedules = getSchedulesFromPage(page);
2015-02-08 23:56:45 -07:00
user.Policy.BlockedTags = getBlockedTagsFromPage(page);
2014-11-28 19:40:46 -07:00
2015-12-14 08:43:03 -07:00
ApiClient.updateUserPolicy(user.Id, user.Policy).then(function () {
onSaveComplete(page);
});
}
window.UserParentalControlPage = {
onSubmit: function () {
var page = $(this).parents('.page');
Dashboard.showLoadingMsg();
var userId = getParameterByName("userId");
2015-12-14 08:43:03 -07:00
ApiClient.getUser(userId).then(function (result) {
saveUser(result, page);
});
// Disable default form submission
return false;
}
};
2014-11-04 05:41:12 -07:00
function getDisplayTime(hours) {
var minutes = 0;
var pct = hours % 1;
if (pct) {
minutes = parseInt(pct * 60);
}
2016-09-25 11:39:13 -07:00
return datetime.getDisplayTime(new Date(2000, 1, 1, hours, minutes, 0, 0));
}
function showSchedulePopup(page, schedule, index) {
schedule = schedule || {};
2016-09-25 11:39:13 -07:00
require(['components/accessschedule/accessschedule'], function (accessschedule) {
accessschedule.show({
schedule: schedule
}).then(function (updatedSchedule) {
2016-09-25 11:39:13 -07:00
var schedules = getSchedulesFromPage(page);
2016-09-25 11:39:13 -07:00
if (index == -1) {
index = schedules.length;
}
2016-09-25 11:39:13 -07:00
schedules[index] = updatedSchedule;
2016-09-25 11:39:13 -07:00
renderAccessSchedule(page, schedules);
});
});
}
function getSchedulesFromPage(page) {
return $('.liSchedule', page).map(function () {
return {
DayOfWeek: this.getAttribute('data-day'),
StartHour: this.getAttribute('data-start'),
EndHour: this.getAttribute('data-end')
};
}).get();
}
2015-02-08 23:56:45 -07:00
function getBlockedTagsFromPage(page) {
2014-11-28 19:40:46 -07:00
2015-02-08 23:56:45 -07:00
return $('.blockedTag', page).map(function () {
2014-11-28 19:40:46 -07:00
2015-02-08 23:56:45 -07:00
return this.getAttribute('data-tag');
}).get();
}
function showBlockedTagPopup(page) {
2015-10-07 18:49:40 -07:00
require(['prompt'], function (prompt) {
prompt({
2016-02-15 07:41:07 -07:00
label: Globalize.translate('LabelTag')
2016-02-04 13:51:13 -07:00
}).then(function (value) {
var tags = getBlockedTagsFromPage(page);
if (tags.indexOf(value) == -1) {
tags.push(value);
loadBlockedTags(page, tags);
2015-10-07 18:49:40 -07:00
}
});
});
2015-02-08 23:56:45 -07:00
}
2015-09-01 07:01:59 -07:00
$(document).on('pageinit', "#userParentalControlPage", function () {
var page = this;
$('.btnAddSchedule', page).on('click', function () {
2014-11-04 05:41:12 -07:00
showSchedulePopup(page, {}, -1);
});
2015-02-08 23:56:45 -07:00
$('.btnAddBlockedTag', page).on('click', function () {
2014-11-28 19:40:46 -07:00
2015-02-08 23:56:45 -07:00
showBlockedTagPopup(page);
2014-11-28 19:40:46 -07:00
});
2015-06-07 20:16:42 -07:00
$('.userParentalControlForm').off('submit', UserParentalControlPage.onSubmit).on('submit', UserParentalControlPage.onSubmit);
2015-09-24 10:08:10 -07:00
}).on('pageshow', "#userParentalControlPage", function () {
var page = this;
Dashboard.showLoadingMsg();
var userId = getParameterByName("userId");
2015-12-14 08:43:03 -07:00
var promise1 = ApiClient.getUser(userId);
2014-11-28 19:40:46 -07:00
var promise2 = ApiClient.getParentalRatings();
2015-12-14 08:43:03 -07:00
Promise.all([promise1, promise2]).then(function (responses) {
2015-12-14 08:43:03 -07:00
loadUser(page, responses[0], responses[1]);
});
});
});