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

238 lines
7.3 KiB
JavaScript
Raw Normal View History

define(['jQuery'], function ($) {
2014-10-13 21:59:34 -07:00
function loadMediaFolders(page, user, mediaFolders) {
var html = '';
2016-04-15 22:27:10 -07:00
html += '<div class="paperListLabel">' + Globalize.translate('HeaderLibraries') + '</div>';
2014-10-13 21:59:34 -07:00
2016-04-14 21:44:51 -07:00
html += '<div class="paperCheckboxList paperList">';
2014-10-13 21:59:34 -07:00
for (var i = 0, length = mediaFolders.length; i < length; i++) {
var folder = mediaFolders[i];
2015-03-31 21:23:34 -07:00
var isChecked = user.Policy.EnableAllFolders || user.Policy.EnabledFolders.indexOf(folder.Id) != -1;
2015-01-22 23:15:15 -07:00
var checkedAttribute = isChecked ? ' checked="checked"' : '';
2014-10-13 21:59:34 -07:00
2016-04-14 21:44:51 -07:00
html += '<paper-checkbox class="chkFolder" data-id="' + folder.Id + '" type="checkbox"' + checkedAttribute + '>' + folder.Name + '</paper-checkbox>';
2014-10-13 21:59:34 -07:00
}
2016-04-14 21:44:51 -07:00
html += '</div>';
2014-10-13 21:59:34 -07:00
2015-01-19 22:19:13 -07:00
$('.folderAccess', page).html(html).trigger('create');
2016-04-14 21:44:51 -07:00
$('#chkEnableAllFolders', page).checked(user.Policy.EnableAllFolders).trigger('change');
2014-10-13 21:59:34 -07:00
}
function loadChannels(page, user, channels) {
var html = '';
2016-04-15 22:27:10 -07:00
html += '<div class="paperListLabel">' + Globalize.translate('HeaderChannels') + '</div>';
2014-10-13 21:59:34 -07:00
2016-04-14 21:44:51 -07:00
html += '<div class="paperCheckboxList paperList">';
2014-10-13 21:59:34 -07:00
for (var i = 0, length = channels.length; i < length; i++) {
var folder = channels[i];
2015-03-31 21:23:34 -07:00
var isChecked = user.Policy.EnableAllChannels || user.Policy.EnabledChannels.indexOf(folder.Id) != -1;
2015-01-22 23:15:15 -07:00
var checkedAttribute = isChecked ? ' checked="checked"' : '';
2014-10-13 21:59:34 -07:00
2016-04-14 21:44:51 -07:00
html += '<paper-checkbox class="chkChannel" data-id="' + folder.Id + '" type="checkbox"' + checkedAttribute + '>' + folder.Name + '</paper-checkbox>';
2014-10-13 21:59:34 -07:00
}
2016-04-14 21:44:51 -07:00
html += '</div>';
2014-10-13 21:59:34 -07:00
$('.channelAccess', page).show().html(html).trigger('create');
if (channels.length) {
$('.channelAccessContainer', page).show();
} else {
$('.channelAccessContainer', page).hide();
}
2016-04-14 21:44:51 -07:00
$('#chkEnableAllChannels', page).checked(user.Policy.EnableAllChannels).trigger('change');
2014-10-13 21:59:34 -07:00
}
2014-12-29 13:18:48 -07:00
function loadDevices(page, user, devices) {
var html = '';
2016-04-15 22:27:10 -07:00
html += '<div class="paperListLabel">' + Globalize.translate('HeaderDevices') + '</div>';
2014-12-29 13:18:48 -07:00
2016-04-14 21:44:51 -07:00
html += '<div class="paperCheckboxList paperList">';
2014-12-29 13:18:48 -07:00
for (var i = 0, length = devices.length; i < length; i++) {
var device = devices[i];
var checkedAttribute = user.Policy.EnableAllDevices || user.Policy.EnabledDevices.indexOf(device.Id) != -1 ? ' checked="checked"' : '';
2016-04-14 21:44:51 -07:00
html += '<paper-checkbox class="chkChannel" data-id="' + device.Id + '" type="checkbox"' + checkedAttribute + '>' + device.Name + ' - ' + device.AppName + '</paper-checkbox>';
2014-12-29 13:18:48 -07:00
}
2016-04-14 21:44:51 -07:00
html += '</div>';
2014-12-29 13:18:48 -07:00
$('.deviceAccess', page).show().html(html).trigger('create');
2016-04-14 21:44:51 -07:00
$('#chkEnableAllDevices', page).checked(user.Policy.EnableAllDevices).trigger('change');
2015-10-25 22:29:32 -07:00
if (user.Policy.IsAdministrator) {
page.querySelector('.deviceAccessContainer').classList.add('hide');
} else {
page.querySelector('.deviceAccessContainer').classList.remove('hide');
}
2014-12-29 13:18:48 -07:00
}
function loadUser(page, user, loggedInUser, mediaFolders, channels, devices) {
2014-10-13 21:59:34 -07:00
2014-10-15 20:26:39 -07:00
$(page).trigger('userloaded', [user]);
2014-10-13 21:59:34 -07:00
Dashboard.setPageTitle(user.Name);
loadChannels(page, user, channels);
loadMediaFolders(page, user, mediaFolders);
2014-12-29 13:18:48 -07:00
loadDevices(page, user, devices);
2014-10-13 21:59:34 -07:00
Dashboard.hideLoadingMsg();
}
function onSaveComplete(page) {
Dashboard.hideLoadingMsg();
2016-02-24 23:38:12 -07:00
require(['toast'], function (toast) {
toast(Globalize.translate('SettingsSaved'));
});
2014-10-13 21:59:34 -07:00
}
function saveUser(user, page) {
2015-01-19 22:19:13 -07:00
user.Policy.EnableAllFolders = $('#chkEnableAllFolders', page).checked();
user.Policy.EnabledFolders = user.Policy.EnableAllFolders ?
[] :
2016-04-14 21:44:51 -07:00
$('.chkFolder', page).get().filter(function (c) {
return c.checked;
}).map(function (c) {
return c.getAttribute('data-id');
});
2014-10-13 21:59:34 -07:00
user.Policy.EnableAllChannels = $('#chkEnableAllChannels', page).checked();
user.Policy.EnabledChannels = user.Policy.EnableAllChannels ?
[] :
2016-04-14 21:44:51 -07:00
$('.chkChannel', page).get().filter(function (c) {
return c.checked;
}).map(function (c) {
return c.getAttribute('data-id');
});
2014-10-13 21:59:34 -07:00
2014-12-29 13:18:48 -07:00
user.Policy.EnableAllDevices = $('#chkEnableAllDevices', page).checked();
user.Policy.EnabledDevices = user.Policy.EnableAllDevices ?
[] :
2016-04-14 21:44:51 -07:00
$('.chkDevice', page).get().filter(function (c) {
return c.checked;
}).map(function (c) {
return c.getAttribute('data-id');
});
2014-12-29 13:18:48 -07:00
2015-05-16 20:17:23 -07:00
// Legacy
user.Policy.BlockedChannels = null;
user.Policy.BlockedMediaFolders = null;
2015-12-14 08:43:03 -07:00
ApiClient.updateUserPolicy(user.Id, user.Policy).then(function () {
2014-10-13 21:59:34 -07:00
onSaveComplete(page);
});
}
2015-06-07 20:16:42 -07:00
function onSubmit() {
var page = $(this).parents('.page');
2014-10-13 21:59:34 -07:00
2015-06-07 20:16:42 -07:00
Dashboard.showLoadingMsg();
2014-10-13 21:59:34 -07:00
2015-06-07 20:16:42 -07:00
var userId = getParameterByName("userId");
2014-10-13 21:59:34 -07:00
2015-12-14 08:43:03 -07:00
ApiClient.getUser(userId).then(function (result) {
2015-06-07 20:16:42 -07:00
saveUser(result, page);
});
2014-10-13 21:59:34 -07:00
2015-06-07 20:16:42 -07:00
// Disable default form submission
return false;
}
2014-10-13 21:59:34 -07:00
2015-09-01 07:01:59 -07:00
$(document).on('pageinit', "#userLibraryAccessPage", function () {
2014-12-29 13:18:48 -07:00
var page = this;
$('#chkEnableAllDevices', page).on('change', function () {
if (this.checked) {
$('.deviceAccessListContainer', page).hide();
} else {
$('.deviceAccessListContainer', page).show();
}
});
$('#chkEnableAllChannels', page).on('change', function () {
if (this.checked) {
$('.channelAccessListContainer', page).hide();
} else {
$('.channelAccessListContainer', page).show();
}
});
2015-01-19 22:19:13 -07:00
$('#chkEnableAllFolders', page).on('change', function () {
if (this.checked) {
$('.folderAccessListContainer', page).hide();
} else {
$('.folderAccessListContainer', page).show();
}
});
2015-06-07 20:16:42 -07:00
$('.userLibraryAccessForm').off('submit', onSubmit).on('submit', onSubmit);
2015-09-24 10:08:10 -07:00
}).on('pageshow', "#userLibraryAccessPage", function () {
2014-10-13 21:59:34 -07:00
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 promise4 = ApiClient.getJSON(ApiClient.getUrl("Library/MediaFolders", { IsHidden: false }));
var promise5 = ApiClient.getJSON(ApiClient.getUrl("Channels"));
2014-12-29 13:18:48 -07:00
var promise6 = ApiClient.getJSON(ApiClient.getUrl('Devices', {
2015-01-19 22:19:13 -07:00
SupportsPersistentIdentifier: true
2014-12-29 13:18:48 -07:00
}));
2014-10-13 21:59:34 -07:00
2015-12-14 08:43:03 -07:00
Promise.all([promise1, promise2, promise4, promise5, promise6]).then(function (responses) {
2014-10-13 21:59:34 -07:00
2015-12-14 08:43:03 -07:00
loadUser(page, responses[0], responses[1], responses[2].Items, responses[3].Items, responses[4].Items);
2014-10-13 21:59:34 -07:00
});
});
});