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

424 lines
12 KiB
JavaScript
Raw Normal View History

2013-04-18 12:32:16 -07:00
(function ($, window, document, FileReader) {
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();
ApiClient.getUser(userId).done(function (user) {
2014-10-15 20:26:39 -07:00
$('.username', page).html(user.Name);
2015-06-29 18:56:25 -07:00
Events.trigger($('#uploadUserImage', page).val('')[0], '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 + "' />");
2014-10-13 21:22:17 -07:00
if (user.ConnectLinkType == 'Guest') {
2014-10-15 20:26:39 -07:00
$('.newImageForm', page).hide();
$('#btnDeleteImage', page).hide();
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
2014-10-15 20:26:39 -07:00
$('#btnDeleteImage', page).show();
2013-02-20 18:33:05 -07:00
$('#headerUploadNewImage', page).show();
2014-10-15 20:26:39 -07:00
$('.newImageForm', page).show();
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 {
2014-10-29 15:01:02 -07:00
$('.newImageForm', page).show();
2014-10-15 20:26:39 -07:00
$('#btnDeleteImage', page).hide();
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
}
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:
2014-05-30 12:23:56 -07:00
Dashboard.showError(Globalize.translate('FileNotFound'));
2013-04-18 12:32:16 -07:00
break;
case evt.target.error.NOT_READABLE_ERR:
2014-05-30 12:23:56 -07:00
Dashboard.showError(Globalize.translate('FileReadError'));
2013-04-18 12:32:16 -07:00
break;
case evt.target.error.ABORT_ERR:
break; // noop
default:
2014-05-30 12:23:56 -07:00
Dashboard.showError(Globalize.translate('FileReadError'));
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();
2014-05-30 12:23:56 -07:00
Dashboard.showError(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.
reader.onload = (function (theFile) {
return function (e) {
// Render thumbnail.
var html = ['<img style="max-width:500px;max-height:200px;" src="', e.target.result, '" title="', escape(theFile.name), '"/>'].join('');
$('#userImageOutput', page).html(html);
$('#fldUpload', page).show();
};
})(file);
// 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
2013-04-18 12:32:16 -07:00
ApiClient.uploadUserImage(userId, 'Primary', file).done(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
2013-04-18 12:32:16 -07:00
self.onFileUploadChange = function (fileUpload) {
setFiles($.mobile.activePage, fileUpload.files);
2013-04-18 12:32:16 -07:00
};
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-05-27 22:51:48 -07:00
$(document).on('pageinitdepends', "#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
2014-10-15 20:26:39 -07:00
Dashboard.confirm(Globalize.translate('DeleteImageConfirmation'), Globalize.translate('DeleteImage'), function (result) {
if (result) {
Dashboard.showLoadingMsg();
2013-04-18 12:32:16 -07:00
2014-10-15 20:26:39 -07:00
var userId = getParameterByName("userId");
ApiClient.deleteUserImage(userId, "primary").done(processImageChangeResult);
}
});
});
2015-05-27 22:51:48 -07:00
$('.newImageForm').off('submit', MyProfilePage.onImageSubmit).on('submit', MyProfilePage.onImageSubmit);
2013-04-18 12:32:16 -07:00
});
2014-10-22 21:26:01 -07:00
})(jQuery, window, document, window.FileReader);
(function ($, document, window) {
function loadUser(page) {
var userid = getParameterByName("userId");
ApiClient.getUser(userid).done(function (user) {
Dashboard.setPageTitle(user.Name);
if (user.ConnectLinkType == 'Guest') {
$('.localAccessSection', page).hide();
$('.passwordSection', page).hide();
}
else if (user.HasConfiguredPassword) {
$('#btnResetPassword', page).show();
$('#fldCurrentPassword', page).show();
$('.localAccessSection', page).show();
$('.passwordSection', page).show();
} else {
$('#btnResetPassword', page).hide();
$('#fldCurrentPassword', page).hide();
$('.localAccessSection', page).hide();
$('.passwordSection', page).show();
}
2015-01-28 23:06:24 -07:00
if (user.HasConfiguredEasyPassword) {
$('#txtEasyPassword', page).val('').attr('placeholder', '******');
$('#btnResetEasyPassword', page).show();
} else {
$('#txtEasyPassword', page).val('').attr('placeholder', '');
$('#btnResetEasyPassword', page).hide();
}
2015-07-28 12:42:24 -07:00
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) {
ApiClient.updateEasyPassword(userId, easyPassword).done(function () {
onEasyPasswordSaved(page, userId);
});
} else {
onEasyPasswordSaved(page, userId);
}
}
function onEasyPasswordSaved(page, userId) {
2014-10-22 21:26:01 -07:00
ApiClient.getUser(userId).done(function (user) {
2015-08-26 18:31:54 -07:00
user.Configuration.EnableLocalPassword = page.querySelector('.chkEnableLocalEasyPassword').checked;
2014-10-22 21:26:01 -07:00
2014-12-18 21:20:07 -07:00
ApiClient.updateUserConfiguration(user.Id, user.Configuration).done(function () {
2014-10-22 21:26:01 -07:00
Dashboard.hideLoadingMsg();
Dashboard.alert(Globalize.translate('MessageSettingsSaved'));
loadUser(page);
});
});
}
function savePassword(page) {
var userId = getParameterByName("userId");
var currentPassword = $('#txtCurrentPassword', page).val();
var newPassword = $('#txtNewPassword', page).val();
ApiClient.updateUserPassword(userId, currentPassword, newPassword).done(function () {
Dashboard.hideLoadingMsg();
Dashboard.alert(Globalize.translate('PasswordSaved'));
loadUser(page);
});
}
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()) {
Dashboard.showError(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
Dashboard.confirm(msg, Globalize.translate('PasswordResetHeader'), function (result) {
if (result) {
var userId = getParameterByName("userId");
Dashboard.showLoadingMsg();
ApiClient.resetUserPassword(userId).done(function () {
Dashboard.hideLoadingMsg();
Dashboard.alert({
message: Globalize.translate('PasswordResetComplete'),
title: Globalize.translate('PasswordResetHeader')
});
loadUser(page);
});
}
});
};
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
Dashboard.confirm(msg, Globalize.translate('HeaderPinCodeReset'), function (result) {
if (result) {
var userId = getParameterByName("userId");
Dashboard.showLoadingMsg();
ApiClient.resetEasyPassword(userId).done(function () {
Dashboard.hideLoadingMsg();
Dashboard.alert({
message: Globalize.translate('PinCodeResetComplete'),
title: Globalize.translate('HeaderPinCodeReset')
});
loadUser(page);
});
}
});
};
2014-10-22 21:26:01 -07:00
}
window.UpdatePasswordPage = new updatePasswordPage();
2015-05-27 22:51:48 -07:00
$(document).on('pageinitdepends', ".userPasswordPage", function () {
var page = this;
$('.updatePasswordForm').off('submit', UpdatePasswordPage.onSubmit).on('submit', UpdatePasswordPage.onSubmit);
$('.localAccessForm').off('submit', UpdatePasswordPage.onLocalAccessSubmit).on('submit', UpdatePasswordPage.onLocalAccessSubmit);
}).on('pageshowready', ".userPasswordPage", function () {
2014-10-22 21:26:01 -07:00
var page = this;
loadUser(page);
});
})(jQuery, document, window);