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

172 lines
4.8 KiB
JavaScript
Raw Normal View History

define(['jQuery'], function ($) {
2014-10-29 15:01:02 -07:00
2014-12-22 20:58:14 -07:00
function loadMediaFolders(page, mediaFolders) {
2014-10-29 19:06:05 -07:00
var html = '';
2016-04-15 22:27:10 -07:00
html += '<div class="paperListLabel">' + Globalize.translate('HeaderLibraries') + '</div>';
2014-10-29 19:06:05 -07:00
2016-04-09 12:04:14 -07:00
html += '<div class="paperCheckboxList paperList" style="padding:.5em 1em;">';
2014-10-29 19:06:05 -07:00
for (var i = 0, length = mediaFolders.length; i < length; i++) {
var folder = mediaFolders[i];
2014-12-22 20:58:14 -07:00
var checkedAttribute = ' checked="checked"';
2014-10-29 19:06:05 -07:00
2016-04-09 12:04:14 -07:00
html += '<paper-checkbox class="chkFolder" data-id="' + folder.Id + '"' + checkedAttribute + '>' + folder.Name + '</paper-checkbox>';
2014-10-29 19:06:05 -07:00
}
2016-04-09 12:04:14 -07:00
html += '</div>';
2014-10-29 19:06:05 -07:00
2015-01-19 22:19:13 -07:00
$('.folderAccess', page).html(html).trigger('create');
2016-04-09 12:04:14 -07:00
$('#chkEnableAllFolders', page).checked(true).trigger('change');
2014-10-29 19:06:05 -07:00
}
2014-12-22 20:58:14 -07:00
function loadChannels(page, channels) {
2014-10-29 19:06:05 -07:00
var html = '';
2016-04-15 22:27:10 -07:00
html += '<div class="paperListLabel">' + Globalize.translate('HeaderChannels') + '</div>';
2014-10-29 19:06:05 -07:00
2016-04-09 12:04:14 -07:00
html += '<div class="paperCheckboxList paperList" style="padding:.5em 1em;">';
2014-10-29 19:06:05 -07:00
for (var i = 0, length = channels.length; i < length; i++) {
var folder = channels[i];
2014-12-22 20:58:14 -07:00
var checkedAttribute = ' checked="checked"';
2014-10-29 19:06:05 -07:00
2016-04-09 12:04:14 -07:00
html += '<paper-checkbox class="chkChannel" data-id="' + folder.Id + '"' + checkedAttribute + '>' + folder.Name + '</paper-checkbox>';
2014-10-29 19:06:05 -07:00
}
2016-04-09 12:04:14 -07:00
html += '</div>';
2014-10-29 19:06:05 -07:00
$('.channelAccess', page).show().html(html).trigger('create');
if (channels.length) {
$('.channelAccessContainer', page).show();
} else {
$('.channelAccessContainer', page).hide();
}
2015-01-19 13:53:02 -07:00
2016-04-09 12:04:14 -07:00
$('#chkEnableAllChannels', page).checked(true).trigger('change');
2014-10-29 19:06:05 -07:00
}
2014-12-22 20:58:14 -07:00
function loadUser(page) {
2014-10-29 15:01:02 -07:00
2014-12-22 20:58:14 -07:00
$('#txtUserName', page).val('');
2014-10-29 19:06:05 -07:00
Dashboard.showLoadingMsg();
var promise4 = ApiClient.getJSON(ApiClient.getUrl("Library/MediaFolders", { IsHidden: false }));
var promise5 = ApiClient.getJSON(ApiClient.getUrl("Channels"));
2015-12-14 08:43:03 -07:00
Promise.all([promise4, promise5]).then(function (responses) {
2014-10-29 19:06:05 -07:00
2015-12-14 08:43:03 -07:00
loadMediaFolders(page, responses[0].Items);
loadChannels(page, responses[1].Items);
2014-10-29 19:06:05 -07:00
Dashboard.hideLoadingMsg();
});
2014-10-29 15:01:02 -07:00
}
2014-10-29 19:06:05 -07:00
function saveUser(page) {
var name = $('#txtUserName', page).val();
2015-12-14 08:43:03 -07:00
ApiClient.createUser(name).then(function (user) {
2014-10-29 19:06:05 -07:00
2015-01-19 22:19:13 -07:00
user.Policy.EnableAllFolders = $('#chkEnableAllFolders', page).checked();
user.Policy.EnabledFolders = user.Policy.EnableAllFolders ?
[] :
2016-04-09 12:04:14 -07:00
$('.chkFolder', page).get().filter(function (i) {
return i.checked;
}).map(function (i) {
return i.getAttribute('data-id');
});
2014-10-29 19:06:05 -07:00
2015-01-19 13:53:02 -07:00
user.Policy.EnableAllChannels = $('#chkEnableAllChannels', page).checked();
user.Policy.EnabledChannels = user.Policy.EnableAllChannels ?
[] :
2016-04-09 12:04:14 -07:00
$('.chkChannel', page).get().filter(function (i) {
return i.checked;
}).map(function (i) {
return i.getAttribute('data-id');
});
2015-01-13 21:20:30 -07:00
2015-12-14 08:43:03 -07:00
ApiClient.updateUserPolicy(user.Id, user.Policy).then(function () {
2016-04-09 12:04:14 -07:00
Dashboard.navigate("userprofiles.html");
2014-10-29 19:06:05 -07:00
});
2015-10-13 12:22:45 -07:00
2016-01-27 23:42:43 -07:00
}, function (response) {
if (response.status == 400) {
Dashboard.alert({
message: page.querySelector('.labelNewUserNameHelp').innerHTML
});
} else {
2016-02-24 23:38:12 -07:00
require(['toast'], function (toast) {
toast(Globalize.translate('DefaultErrorMessage'));
});
2016-01-27 23:42:43 -07:00
}
2015-10-13 12:22:45 -07:00
Dashboard.hideLoadingMsg();
2014-10-29 15:01:02 -07:00
});
}
2015-05-17 18:27:48 -07:00
function onSubmit() {
2016-01-27 23:42:43 -07:00
var page = $(this).parents('.page')[0];
2014-10-29 15:01:02 -07:00
2015-05-17 18:27:48 -07:00
Dashboard.showLoadingMsg();
2014-10-29 15:01:02 -07:00
2015-05-17 18:27:48 -07:00
saveUser(page);
2014-10-29 15:01:02 -07:00
2015-05-17 18:27:48 -07:00
// Disable default form submission
return false;
2014-10-29 15:01:02 -07:00
}
function loadData(page) {
2014-12-22 20:58:14 -07:00
loadUser(page);
2014-10-29 15:01:02 -07:00
}
2015-09-01 07:01:59 -07:00
$(document).on('pageinit', "#newUserPage", function () {
2015-01-19 13:53:02 -07:00
var page = this;
$('#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-05-17 18:27:48 -07:00
$('.newUserProfileForm').off('submit', onSubmit).on('submit', onSubmit);
2015-09-24 10:08:10 -07:00
}).on('pageshow', "#newUserPage", function () {
2014-10-29 15:01:02 -07:00
var page = this;
loadData(page);
});
});