1
0
Fork 0
mirror of https://github.com/jellyfin/jellyfin-web synced 2025-03-30 19:56:21 +00:00
jellyfin-web/src/controllers/dashboard/users/usernew.js

127 lines
4.6 KiB
JavaScript
Raw Normal View History

define(['jQuery', 'loading', 'globalize', 'emby-checkbox'], function ($, loading, globalize) {
2020-05-04 12:44:12 +02:00
'use strict';
2018-10-23 01:05:09 +03:00
function loadMediaFolders(page, mediaFolders) {
2020-05-04 12:44:12 +02:00
var html = '';
html += '<h3 class="checkboxListLabel">' + globalize.translate('HeaderLibraries') + '</h3>';
2019-06-29 23:45:07 -07:00
html += '<div class="checkboxList paperList" style="padding:.5em 1em;">';
2019-06-29 23:45:07 -07:00
for (var i = 0; i < mediaFolders.length; i++) {
2018-10-23 01:05:09 +03:00
var folder = mediaFolders[i];
2020-05-14 13:26:52 +09:00
html += '<label><input type="checkbox" is="emby-checkbox" class="chkFolder" data-id="' + folder.Id + '"/><span>' + folder.Name + '</span></label>';
2018-10-23 01:05:09 +03:00
}
2020-05-04 12:44:12 +02:00
html += '</div>';
$('.folderAccess', page).html(html).trigger('create');
$('#chkEnableAllFolders', page).checked = false;
2018-10-23 01:05:09 +03:00
}
function loadChannels(page, channels) {
2020-05-04 12:44:12 +02:00
var html = '';
html += '<h3 class="checkboxListLabel">' + globalize.translate('HeaderChannels') + '</h3>';
2019-06-29 23:45:07 -07:00
html += '<div class="checkboxList paperList" style="padding:.5em 1em;">';
2019-06-29 23:45:07 -07:00
for (var i = 0; i < channels.length; i++) {
2018-10-23 01:05:09 +03:00
var folder = channels[i];
2020-05-14 13:26:52 +09:00
html += '<label><input type="checkbox" is="emby-checkbox" class="chkChannel" data-id="' + folder.Id + '"/><span>' + folder.Name + '</span></label>';
2018-10-23 01:05:09 +03:00
}
2020-05-04 12:44:12 +02:00
html += '</div>';
$('.channelAccess', page).show().html(html).trigger('create');
2019-07-01 12:42:15 -07:00
if (channels.length) {
2020-05-04 12:44:12 +02:00
$('.channelAccessContainer', page).show();
2019-07-01 12:42:15 -07:00
} else {
2020-05-04 12:44:12 +02:00
$('.channelAccessContainer', page).hide();
2019-07-01 12:42:15 -07:00
}
$('#chkEnableAllChannels', page).checked = false;
2018-10-23 01:05:09 +03:00
}
function loadUser(page) {
2020-05-04 12:44:12 +02:00
$('#txtUsername', page).val('');
$('#txtPassword', page).val('');
2019-06-29 23:45:07 -07:00
loading.show();
2020-05-04 12:44:12 +02:00
var promiseFolders = ApiClient.getJSON(ApiClient.getUrl('Library/MediaFolders', {
IsHidden: false
2019-06-29 23:45:07 -07:00
}));
2020-05-04 12:44:12 +02:00
var promiseChannels = ApiClient.getJSON(ApiClient.getUrl('Channels'));
Promise.all([promiseFolders, promiseChannels]).then(function (responses) {
2019-06-29 23:45:07 -07:00
loadMediaFolders(page, responses[0].Items);
loadChannels(page, responses[1].Items);
loading.hide();
});
2018-10-23 01:05:09 +03:00
}
function saveUser(page) {
var user = {};
2020-05-04 12:44:12 +02:00
user.Name = $('#txtUsername', page).val();
user.Password = $('#txtPassword', page).val();
ApiClient.createUser(user).then(function (user) {
user.Policy.EnableAllFolders = $('#chkEnableAllFolders', page).checked;
2019-07-01 12:42:15 -07:00
user.Policy.EnabledFolders = [];
2019-07-01 12:42:15 -07:00
if (!user.Policy.EnableAllFolders) {
2020-05-04 12:44:12 +02:00
user.Policy.EnabledFolders = $('.chkFolder', page).get().filter(function (i) {
return i.checked;
}).map(function (i) {
2020-05-04 12:44:12 +02:00
return i.getAttribute('data-id');
2019-07-01 12:42:15 -07:00
});
}
user.Policy.EnableAllChannels = $('#chkEnableAllChannels', page).checked;
2019-07-01 12:42:15 -07:00
user.Policy.EnabledChannels = [];
2019-07-01 12:42:15 -07:00
if (!user.Policy.EnableAllChannels) {
2020-05-04 12:44:12 +02:00
user.Policy.EnabledChannels = $('.chkChannel', page).get().filter(function (i) {
return i.checked;
}).map(function (i) {
2020-05-04 12:44:12 +02:00
return i.getAttribute('data-id');
2019-07-01 12:42:15 -07:00
});
}
ApiClient.updateUserPolicy(user.Id, user.Policy).then(function () {
2020-05-04 12:44:12 +02:00
Dashboard.navigate('useredit.html?userId=' + user.Id);
2019-06-29 23:45:07 -07:00
});
}, function (response) {
2020-05-04 12:44:12 +02:00
require(['toast'], function (toast) {
toast(globalize.translate('DefaultErrorMessage'));
});
2019-06-29 23:45:07 -07:00
loading.hide();
});
2018-10-23 01:05:09 +03:00
}
function onSubmit() {
2020-05-04 12:44:12 +02:00
var page = $(this).parents('.page')[0];
2019-06-29 23:45:07 -07:00
loading.show();
saveUser(page);
return false;
2018-10-23 01:05:09 +03:00
}
function loadData(page) {
2019-06-29 23:45:07 -07:00
loadUser(page);
2018-10-23 01:05:09 +03:00
}
2019-06-29 23:45:07 -07:00
2020-05-04 12:44:12 +02:00
$(document).on('pageinit', '#newUserPage', function () {
2018-10-23 01:05:09 +03:00
var page = this;
2020-05-04 12:44:12 +02:00
$('#chkEnableAllChannels', page).on('change', function () {
2019-07-01 12:42:15 -07:00
if (this.checked) {
2020-05-04 12:44:12 +02:00
$('.channelAccessListContainer', page).hide();
2019-07-01 12:42:15 -07:00
} else {
2020-05-04 12:44:12 +02:00
$('.channelAccessListContainer', page).show();
2019-07-01 12:42:15 -07:00
}
2019-06-29 23:45:07 -07:00
});
2020-05-04 12:44:12 +02:00
$('#chkEnableAllFolders', page).on('change', function () {
2019-07-01 12:42:15 -07:00
if (this.checked) {
2020-05-04 12:44:12 +02:00
$('.folderAccessListContainer', page).hide();
2019-07-01 12:42:15 -07:00
} else {
2020-05-04 12:44:12 +02:00
$('.folderAccessListContainer', page).show();
2019-07-01 12:42:15 -07:00
}
2019-06-29 23:45:07 -07:00
});
2020-05-04 12:44:12 +02:00
$('.newUserProfileForm').off('submit', onSubmit).on('submit', onSubmit);
}).on('pageshow', '#newUserPage', function () {
2019-06-29 23:45:07 -07:00
loadData(this);
});
});