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

217 lines
6.4 KiB
JavaScript
Raw Normal View History

2016-06-21 21:39:47 -07:00
define(['scripts/userpasswordpage'], function (Userpasswordpage) {
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
2016-06-21 21:39:47 -07:00
page.querySelector('.username').innerHTML = user.Name;
var uploadUserImage = page.querySelector('#uploadUserImage');
uploadUserImage.value = '';
uploadUserImage.dispatchEvent(new CustomEvent('change', {}));
2013-02-20 18:33:05 -07:00
LibraryMenu.setTitle(user.Name);
2013-02-20 18:33:05 -07:00
2014-10-29 15:01:02 -07:00
var imageUrl;
2016-09-11 00:33:53 -07:00
var fldImage = page.querySelector('#fldImage');
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
}
2016-06-21 21:39:47 -07:00
fldImage.classList.remove('hide');
fldImage.innerHTML = "<img width='140px' src='" + imageUrl + "' />";
2014-10-29 15:01:02 -07:00
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') {
2016-06-21 21:39:47 -07:00
page.querySelector('.connectMessage').classList.remove('hide');
2014-10-13 21:22:17 -07:00
}
else if (user.PrimaryImageTag) {
2013-02-20 18:33:05 -07:00
2015-12-14 08:43:03 -07:00
showImageEditing = true;
2016-06-21 21:39:47 -07:00
page.querySelector('.connectMessage').classList.add('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;
2016-06-21 21:39:47 -07:00
page.querySelector('.connectMessage').classList.add('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)) {
2016-06-21 21:39:47 -07:00
page.querySelector('.newImageForm').classList.remove('hide');
page.querySelector('#btnDeleteImage').classList.remove('hide');
2015-12-14 08:43:03 -07:00
} else {
2016-06-21 21:39:47 -07:00
page.querySelector('.newImageForm').classList.add('hide');
page.querySelector('#btnDeleteImage').classList.add('hide');
2015-12-14 08:43:03 -07:00
}
});
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 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 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.*')) {
2016-06-21 21:39:47 -07:00
page.querySelector('#userImageOutput').innerHTML = '';
page.querySelector('#fldUpload').classList.add('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;
2016-06-20 18:59:39 -07:00
reader.onloadstart = function () {
2016-06-21 21:39:47 -07:00
page.querySelector('#fldUpload').classList.add('hide');
2016-06-11 10:10:06 -07:00
};
2013-04-18 12:32:16 -07:00
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
2016-06-21 21:39:47 -07:00
page.querySelector('#userImageOutput').innerHTML = html;
page.querySelector('#fldUpload').classList.remove('hide');
2015-12-14 08:43:03 -07:00
};
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
2016-06-11 10:10:06 -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
2016-06-11 10:10:06 -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
2016-06-21 21:39:47 -07:00
return function (view, params) {
2013-02-20 18:33:05 -07:00
2016-06-21 21:39:47 -07:00
reloadUser(view);
2013-02-20 18:33:05 -07:00
2016-06-21 21:39:47 -07:00
var userpasswordpage = new Userpasswordpage(view, params);
2013-02-20 18:33:05 -07:00
2016-06-21 21:39:47 -07:00
var userImageDropZone = view.querySelector('#userImageDropZone');
userImageDropZone.addEventListener('dragOver', onImageDragOver);
userImageDropZone.addEventListener('drop', function (e) {
2016-06-20 18:59:39 -07:00
2016-06-11 10:10:06 -07:00
e.preventDefault();
2013-02-20 18:33:05 -07:00
2016-06-21 21:39:47 -07:00
setFiles(view, e.originalEvent.dataTransfer.files);
2013-02-20 18:33:05 -07:00
2016-06-11 10:10:06 -07:00
return false;
});
2016-06-21 21:39:47 -07:00
view.querySelector('#btnDeleteImage').addEventListener('click', function () {
2013-02-20 18:33:05 -07:00
2016-06-11 10:10:06 -07:00
require(['confirm'], function (confirm) {
2013-02-20 18:33:05 -07:00
2016-06-11 10:10:06 -07:00
confirm(Globalize.translate('DeleteImageConfirmation'), Globalize.translate('DeleteImage')).then(function () {
2013-02-20 18:33:05 -07:00
2016-06-11 10:10:06 -07:00
Dashboard.showLoadingMsg();
2013-02-20 18:33:05 -07:00
2016-06-11 10:10:06 -07:00
var userId = getParameterByName("userId");
2013-02-20 18:33:05 -07:00
2016-06-11 10:10:06 -07:00
ApiClient.deleteUserImage(userId, "primary").then(function () {
2013-02-20 18:33:05 -07:00
2016-06-11 10:10:06 -07:00
Dashboard.hideLoadingMsg();
2013-02-20 18:33:05 -07:00
2016-06-21 21:39:47 -07:00
reloadUser(view);
2016-06-11 10:10:06 -07:00
});
});
});
});
2013-02-20 18:33:05 -07:00
2016-06-21 21:39:47 -07:00
view.querySelector('.newImageForm').addEventListener('submit', function (e) {
2013-04-18 12:32:16 -07:00
2016-06-21 21:39:47 -07:00
var file = currentFile;
2015-05-27 22:51:48 -07:00
2016-06-21 21:39:47 -07:00
if (!file) {
2016-06-11 10:10:06 -07:00
return false;
2014-10-22 21:26:01 -07:00
}
2016-06-21 21:39:47 -07:00
if (file.type != "image/png" && file.type != "image/jpeg" && file.type != "image/jpeg") {
return false;
}
2014-10-22 21:26:01 -07:00
Dashboard.showLoadingMsg();
2016-06-21 21:39:47 -07:00
var userId = getParameterByName("userId");
2014-10-22 21:26:01 -07:00
2016-06-21 21:39:47 -07:00
ApiClient.uploadUserImage(userId, 'Primary', file).then(function () {
2014-10-22 21:26:01 -07:00
2016-06-21 21:39:47 -07:00
Dashboard.hideLoadingMsg();
2015-01-28 23:06:24 -07:00
2016-06-21 21:39:47 -07:00
reloadUser(view);
2015-01-28 23:06:24 -07:00
});
2015-05-27 22:51:48 -07:00
2016-06-21 21:39:47 -07:00
e.preventDefault();
return false;
});
2014-10-22 21:26:01 -07:00
2016-06-21 21:39:47 -07:00
view.querySelector('#uploadUserImage').addEventListener('change', function (e) {
setFiles(view, e.target.files);
});
};
});