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

463 lines
13 KiB
JavaScript
Raw Normal View History

define(['jQuery'], function ($) {
2013-02-20 18:33:05 -07:00
2013-04-18 12:32:16 -07:00
var currentFile;
2013-02-20 18:33:05 -07:00
2013-04-18 12:32:16 -07:00
function reloadUser(page) {
2013-02-20 18:33:05 -07:00
var userId = getParameterByName("userId");
Dashboard.showLoadingMsg();
2015-12-14 08:43:03 -07:00
ApiClient.getUser(userId).then(function (user) {
2013-02-20 18:33:05 -07:00
2014-10-15 20:26:39 -07:00
$('.username', page).html(user.Name);
2015-12-23 10:46:01 -07:00
$('#uploadUserImage', page).val('').trigger('change');
2013-02-20 18:33:05 -07:00
Dashboard.setPageTitle(user.Name);
2014-10-29 15:01:02 -07:00
var imageUrl;
2013-02-20 18:33:05 -07:00
if (user.PrimaryImageTag) {
2014-10-29 15:01:02 -07:00
imageUrl = ApiClient.getUserImageUrl(user.Id, {
height: 200,
2013-02-20 18:33:05 -07:00
tag: user.PrimaryImageTag,
type: "Primary"
});
2014-10-29 15:01:02 -07:00
} else {
imageUrl = "css/images/logindefault.png";
2014-10-13 21:22:17 -07:00
}
2014-10-29 15:01:02 -07:00
$('#fldImage', page).show().html('').html("<img width='140px' src='" + imageUrl + "' />");
2015-12-14 08:43:03 -07:00
var showImageEditing = false;
2014-10-29 15:01:02 -07:00
2014-10-13 21:22:17 -07:00
if (user.ConnectLinkType == 'Guest') {
2014-10-16 21:52:41 -07:00
$('.connectMessage', page).show();
2014-10-13 21:22:17 -07:00
}
else if (user.PrimaryImageTag) {
2013-02-20 18:33:05 -07:00
$('#headerUploadNewImage', page).show();
2015-12-14 08:43:03 -07:00
showImageEditing = true;
2014-10-16 21:52:41 -07:00
$('.connectMessage', page).hide();
2014-10-13 21:22:17 -07:00
2013-02-20 18:33:05 -07:00
} else {
2015-12-14 08:43:03 -07:00
showImageEditing = true;
2014-10-29 15:01:02 -07:00
$('#headerUploadNewImage', page).show();
2014-10-16 21:52:41 -07:00
$('.connectMessage', page).hide();
2013-02-20 18:33:05 -07:00
}
2015-12-14 08:43:03 -07:00
Dashboard.getCurrentUser().then(function (loggedInUser) {
if (showImageEditing && AppInfo.supportsFileInput && (loggedInUser.Policy.IsAdministrator || user.Policy.EnableUserPreferenceAccess)) {
$('.newImageForm', page).show();
$('#btnDeleteImage', page).removeClass('hide');
} else {
$('.newImageForm', page).hide();
$('#btnDeleteImage', page).addClass('hide');
}
});
2013-02-20 18:33:05 -07:00
Dashboard.hideLoadingMsg();
});
2013-04-18 12:32:16 -07:00
}
2013-02-20 18:33:05 -07:00
2013-04-18 12:32:16 -07:00
function processImageChangeResult() {
2013-02-20 18:33:05 -07:00
2013-04-18 12:32:16 -07:00
Dashboard.hideLoadingMsg();
2013-02-20 18:33:05 -07:00
2015-07-28 12:42:24 -07:00
var page = $($.mobile.activePage)[0];
2013-02-20 18:33:05 -07:00
2013-04-18 12:32:16 -07:00
reloadUser(page);
}
2013-02-20 18:33:05 -07:00
2013-04-18 12:32:16 -07:00
function onFileReaderError(evt) {
2013-02-20 18:33:05 -07:00
Dashboard.hideLoadingMsg();
2013-04-18 12:32:16 -07:00
switch (evt.target.error.code) {
case evt.target.error.NOT_FOUND_ERR:
2016-02-24 23:38:12 -07:00
require(['toast'], function (toast) {
toast(Globalize.translate('FileNotFound'));
});
2013-04-18 12:32:16 -07:00
break;
case evt.target.error.NOT_READABLE_ERR:
2016-02-24 23:38:12 -07:00
require(['toast'], function (toast) {
toast(Globalize.translate('FileReadError'));
});
2013-04-18 12:32:16 -07:00
break;
case evt.target.error.ABORT_ERR:
break; // noop
default:
2016-03-20 10:52:23 -07:00
{
require(['toast'], function (toast) {
toast(Globalize.translate('FileReadError'));
});
break;
}
2013-04-18 12:32:16 -07:00
};
}
2013-02-20 18:33:05 -07:00
2013-04-18 12:32:16 -07:00
function onFileReaderOnloadStart(evt) {
2013-02-20 18:33:05 -07:00
2013-04-18 12:32:16 -07:00
$('#fldUpload', $.mobile.activePage).hide();
}
function onFileReaderAbort(evt) {
Dashboard.hideLoadingMsg();
2016-02-24 23:38:12 -07:00
require(['toast'], function (toast) {
toast(Globalize.translate('FileReadCancelled'));
});
2013-04-18 12:32:16 -07:00
}
2013-02-20 18:33:05 -07:00
function setFiles(page, files) {
2013-02-20 18:33:05 -07:00
var file = files[0];
if (!file || !file.type.match('image.*')) {
$('#userImageOutput', page).html('');
$('#fldUpload', page).hide();
2013-04-18 12:32:16 -07:00
currentFile = null;
2013-02-20 18:33:05 -07:00
return;
}
2013-04-18 12:32:16 -07:00
currentFile = file;
2013-02-20 18:33:05 -07:00
var reader = new FileReader();
2013-04-18 12:32:16 -07:00
reader.onerror = onFileReaderError;
reader.onloadstart = onFileReaderOnloadStart;
reader.onabort = onFileReaderAbort;
2013-02-20 18:33:05 -07:00
// Closure to capture the file information.
2015-12-14 08:43:03 -07:00
reader.onload = function (e) {
2013-02-20 18:33:05 -07:00
2015-12-14 08:43:03 -07:00
// Render thumbnail.
var html = ['<img style="max-width:500px;max-height:200px;" src="', e.target.result, '" title="', escape(file.name), '"/>'].join('');
2013-02-20 18:33:05 -07:00
2015-12-14 08:43:03 -07:00
$('#userImageOutput', page).html(html);
$('#fldUpload', page).show();
};
2013-02-20 18:33:05 -07:00
// Read in the image file as a data URL.
reader.readAsDataURL(file);
2013-04-18 12:32:16 -07:00
}
2013-02-20 18:33:05 -07:00
2013-04-18 12:32:16 -07:00
function onImageDrop(e) {
2013-02-20 18:33:05 -07:00
2013-04-18 12:32:16 -07:00
e.preventDefault();
2013-02-20 18:33:05 -07:00
setFiles($.mobile.activePage, e.originalEvent.dataTransfer.files);
2013-02-20 18:33:05 -07:00
2013-04-18 12:32:16 -07:00
return false;
}
2013-02-20 18:33:05 -07:00
2013-04-18 12:32:16 -07:00
function onImageDragOver(e) {
2013-02-20 18:33:05 -07:00
2013-04-18 12:32:16 -07:00
e.preventDefault();
2013-02-20 18:33:05 -07:00
2013-04-18 12:32:16 -07:00
e.originalEvent.dataTransfer.dropEffect = 'Copy';
2013-02-20 18:33:05 -07:00
2013-04-18 12:32:16 -07:00
return false;
}
2013-02-20 18:33:05 -07:00
2014-10-15 20:26:39 -07:00
function myProfilePage() {
2013-02-20 18:33:05 -07:00
2013-04-18 12:32:16 -07:00
var self = this;
2014-10-15 20:26:39 -07:00
self.onImageSubmit = function () {
2013-02-20 18:33:05 -07:00
2013-04-18 12:32:16 -07:00
var file = currentFile;
2013-02-20 18:33:05 -07:00
2013-04-18 12:32:16 -07:00
if (!file) {
return false;
}
2013-02-20 18:33:05 -07:00
2013-04-18 12:32:16 -07:00
if (file.type != "image/png" && file.type != "image/jpeg" && file.type != "image/jpeg") {
return false;
}
2013-02-20 18:33:05 -07:00
2013-04-18 12:32:16 -07:00
Dashboard.showLoadingMsg();
2013-02-20 18:33:05 -07:00
2013-04-18 12:32:16 -07:00
var userId = getParameterByName("userId");
2013-02-20 18:33:05 -07:00
2015-12-14 08:43:03 -07:00
ApiClient.uploadUserImage(userId, 'Primary', file).then(processImageChangeResult);
2013-02-20 18:33:05 -07:00
2013-04-18 12:32:16 -07:00
return false;
};
2013-02-20 18:33:05 -07:00
}
2014-10-15 20:26:39 -07:00
window.MyProfilePage = new myProfilePage();
2013-04-18 12:32:16 -07:00
2015-09-01 07:01:59 -07:00
$(document).on('pageinit', "#userImagePage", function () {
2013-04-18 12:32:16 -07:00
var page = this;
reloadUser(page);
$("#userImageDropZone", page).on('dragover', onImageDragOver).on('drop', onImageDrop);
2014-10-15 20:26:39 -07:00
$('#btnDeleteImage', page).on('click', function () {
2013-04-18 12:32:16 -07:00
2016-02-22 12:31:28 -07:00
require(['confirm'], function (confirm) {
2014-10-15 20:26:39 -07:00
2016-02-22 12:31:28 -07:00
confirm(Globalize.translate('DeleteImageConfirmation'), Globalize.translate('DeleteImage')).then(function () {
2014-10-15 20:26:39 -07:00
Dashboard.showLoadingMsg();
2013-04-18 12:32:16 -07:00
2014-10-15 20:26:39 -07:00
var userId = getParameterByName("userId");
2015-12-14 08:43:03 -07:00
ApiClient.deleteUserImage(userId, "primary").then(processImageChangeResult);
2016-02-22 12:31:28 -07:00
});
2014-10-15 20:26:39 -07:00
});
});
2015-05-27 22:51:48 -07:00
$('.newImageForm').off('submit', MyProfilePage.onImageSubmit).on('submit', MyProfilePage.onImageSubmit);
2016-03-20 10:52:23 -07:00
page.querySelector('#uploadUserImage').addEventListener('change', function (e) {
2015-12-14 08:43:03 -07:00
setFiles(page, e.target.files);
});
2013-04-18 12:32:16 -07:00
});
2014-10-22 21:26:01 -07:00
function loadUser(page) {
var userid = getParameterByName("userId");
2015-12-14 08:43:03 -07:00
ApiClient.getUser(userid).then(function (user) {
2014-10-22 21:26:01 -07:00
2016-03-20 10:52:23 -07:00
Dashboard.getCurrentUser().then(function (loggedInUser) {
2015-12-14 08:43:03 -07:00
Dashboard.setPageTitle(user.Name);
2014-10-22 21:26:01 -07:00
2015-12-14 08:43:03 -07:00
var showPasswordSection = true;
var showLocalAccessSection = false;
if (user.ConnectLinkType == 'Guest') {
$('.localAccessSection', page).hide();
showPasswordSection = false;
}
else if (user.HasConfiguredPassword) {
$('#btnResetPassword', page).show();
$('#fldCurrentPassword', page).show();
showLocalAccessSection = true;
} else {
$('#btnResetPassword', page).hide();
$('#fldCurrentPassword', page).hide();
}
2014-10-22 21:26:01 -07:00
2015-12-14 08:43:03 -07:00
if (showPasswordSection && (loggedInUser.Policy.IsAdministrator || user.Policy.EnableUserPreferenceAccess)) {
$('.passwordSection', page).show();
} else {
$('.passwordSection', page).hide();
}
if (showLocalAccessSection && (loggedInUser.Policy.IsAdministrator || user.Policy.EnableUserPreferenceAccess)) {
$('.localAccessSection', page).show();
} else {
$('.localAccessSection', page).hide();
}
2015-01-28 23:06:24 -07:00
2015-12-14 08:43:03 -07:00
if (user.HasConfiguredEasyPassword) {
$('#txtEasyPassword', page).val('').attr('placeholder', '******');
$('#btnResetEasyPassword', page).removeClass('hide');
} else {
$('#txtEasyPassword', page).val('').attr('placeholder', '');
$('#btnResetEasyPassword', page).addClass('hide');
}
page.querySelector('.chkEnableLocalEasyPassword').checked = user.Configuration.EnableLocalPassword;
});
2014-10-22 21:26:01 -07:00
});
$('#txtCurrentPassword', page).val('');
$('#txtNewPassword', page).val('');
$('#txtNewPasswordConfirm', page).val('');
}
2015-01-28 23:06:24 -07:00
function saveEasyPassword(page) {
2014-10-22 21:26:01 -07:00
var userId = getParameterByName("userId");
2015-01-28 23:06:24 -07:00
var easyPassword = $('#txtEasyPassword', page).val();
if (easyPassword) {
2015-12-14 08:43:03 -07:00
ApiClient.updateEasyPassword(userId, easyPassword).then(function () {
2015-01-28 23:06:24 -07:00
onEasyPasswordSaved(page, userId);
});
} else {
onEasyPasswordSaved(page, userId);
}
}
function onEasyPasswordSaved(page, userId) {
2015-12-14 08:43:03 -07:00
ApiClient.getUser(userId).then(function (user) {
2014-10-22 21:26:01 -07:00
2015-08-26 18:31:54 -07:00
user.Configuration.EnableLocalPassword = page.querySelector('.chkEnableLocalEasyPassword').checked;
2014-10-22 21:26:01 -07:00
2015-12-14 08:43:03 -07:00
ApiClient.updateUserConfiguration(user.Id, user.Configuration).then(function () {
2014-10-22 21:26:01 -07:00
Dashboard.hideLoadingMsg();
2016-02-24 23:38:12 -07:00
require(['toast'], function (toast) {
toast(Globalize.translate('MessageSettingsSaved'));
});
2014-10-22 21:26:01 -07:00
loadUser(page);
});
});
}
function savePassword(page) {
var userId = getParameterByName("userId");
var currentPassword = $('#txtCurrentPassword', page).val();
var newPassword = $('#txtNewPassword', page).val();
2015-12-14 08:43:03 -07:00
ApiClient.updateUserPassword(userId, currentPassword, newPassword).then(function () {
2014-10-22 21:26:01 -07:00
Dashboard.hideLoadingMsg();
2016-02-24 23:38:12 -07:00
require(['toast'], function (toast) {
toast(Globalize.translate('PasswordSaved'));
});
2014-10-22 21:26:01 -07:00
loadUser(page);
2016-03-20 10:52:23 -07:00
}, function () {
2016-02-12 11:12:59 -07:00
Dashboard.hideLoadingMsg();
Dashboard.alert({
title: Globalize.translate('HeaderLoginFailure'),
message: Globalize.translate('MessageInvalidUser')
});
2014-10-22 21:26:01 -07:00
});
}
function updatePasswordPage() {
var self = this;
self.onSubmit = function () {
2015-07-28 12:42:24 -07:00
var page = $($.mobile.activePage)[0];
2014-10-22 21:26:01 -07:00
if ($('#txtNewPassword', page).val() != $('#txtNewPasswordConfirm', page).val()) {
2016-02-24 23:38:12 -07:00
require(['toast'], function (toast) {
toast(Globalize.translate('PasswordMatchError'));
});
2014-11-05 12:28:41 -07:00
} else {
2015-05-27 22:51:48 -07:00
2014-11-05 12:28:41 -07:00
Dashboard.showLoadingMsg();
savePassword(page);
2014-10-22 21:26:01 -07:00
}
// Disable default form submission
return false;
};
self.onLocalAccessSubmit = function () {
2015-07-28 12:42:24 -07:00
var page = $($.mobile.activePage)[0];
2014-10-22 21:26:01 -07:00
Dashboard.showLoadingMsg();
2015-01-28 23:06:24 -07:00
saveEasyPassword(page);
2014-10-22 21:26:01 -07:00
// Disable default form submission
return false;
};
self.resetPassword = function () {
var msg = Globalize.translate('PasswordResetConfirmation');
2015-07-28 12:42:24 -07:00
var page = $($.mobile.activePage)[0];
2014-10-22 21:26:01 -07:00
2016-02-22 12:31:28 -07:00
require(['confirm'], function (confirm) {
confirm(msg, Globalize.translate('PasswordResetHeader')).then(function () {
2014-10-22 21:26:01 -07:00
var userId = getParameterByName("userId");
Dashboard.showLoadingMsg();
2015-12-14 08:43:03 -07:00
ApiClient.resetUserPassword(userId).then(function () {
2014-10-22 21:26:01 -07:00
Dashboard.hideLoadingMsg();
Dashboard.alert({
message: Globalize.translate('PasswordResetComplete'),
title: Globalize.translate('PasswordResetHeader')
});
loadUser(page);
});
2016-02-22 12:31:28 -07:00
});
2014-10-22 21:26:01 -07:00
});
};
2015-01-28 23:06:24 -07:00
self.resetEasyPassword = function () {
var msg = Globalize.translate('PinCodeResetConfirmation');
2015-07-28 12:42:24 -07:00
var page = $($.mobile.activePage)[0];
2015-01-28 23:06:24 -07:00
2016-02-22 12:31:28 -07:00
require(['confirm'], function (confirm) {
confirm(msg, Globalize.translate('HeaderPinCodeReset')).then(function () {
2015-01-28 23:06:24 -07:00
var userId = getParameterByName("userId");
Dashboard.showLoadingMsg();
2015-12-14 08:43:03 -07:00
ApiClient.resetEasyPassword(userId).then(function () {
2015-01-28 23:06:24 -07:00
Dashboard.hideLoadingMsg();
Dashboard.alert({
message: Globalize.translate('PinCodeResetComplete'),
title: Globalize.translate('HeaderPinCodeReset')
});
loadUser(page);
});
2016-02-22 12:31:28 -07:00
});
2015-01-28 23:06:24 -07:00
});
};
2014-10-22 21:26:01 -07:00
}
window.UpdatePasswordPage = new updatePasswordPage();
2015-09-01 07:01:59 -07:00
$(document).on('pageinit', ".userPasswordPage", function () {
2015-05-27 22:51:48 -07:00
var page = this;
$('.updatePasswordForm').off('submit', UpdatePasswordPage.onSubmit).on('submit', UpdatePasswordPage.onSubmit);
$('.localAccessForm').off('submit', UpdatePasswordPage.onLocalAccessSubmit).on('submit', UpdatePasswordPage.onLocalAccessSubmit);
2015-09-24 10:08:10 -07:00
}).on('pageshow', ".userPasswordPage", function () {
2014-10-22 21:26:01 -07:00
var page = this;
loadUser(page);
});
});