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

209 lines
5.4 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) {
$('#uploadUserImage', page).val('').trigger('change');
Dashboard.setPageTitle(user.Name);
if (user.PrimaryImageTag) {
var imageUrl = ApiClient.getUserImageUrl(user.Id, {
height: 200,
2013-02-20 18:33:05 -07:00
tag: user.PrimaryImageTag,
type: "Primary"
});
$('#fldImage', page).show().html('').html("<img height='200px' src='" + imageUrl + "' />");
$('#fldDeleteImage', page).show();
$('#headerUploadNewImage', page).show();
} else {
$('#fldImage', page).hide().html('');
$('#fldDeleteImage', page).hide();
$('#headerUploadNewImage', page).hide();
}
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
2013-04-18 12:32:16 -07:00
var page = $.mobile.activePage;
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
2013-04-18 12:32:16 -07:00
function userImagePage() {
2013-02-20 18:33:05 -07:00
2013-04-18 12:32:16 -07:00
var self = this;
2013-04-18 12:32:16 -07:00
self.onSubmit = 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.deleteImage = function () {
2013-02-20 18:33:05 -07:00
2014-05-30 12:23:56 -07:00
Dashboard.confirm(Globalize.translate('DeleteImageConfirmation'), Globalize.translate('DeleteImage'), function (result) {
2013-02-20 18:33:05 -07:00
2013-04-18 12:32:16 -07:00
if (result) {
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");
ApiClient.deleteUserImage(userId, "primary").done(processImageChangeResult);
}
});
};
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
}
2013-04-18 12:32:16 -07:00
window.UserImagePage = new userImagePage();
$(document).on('pagebeforeshow', "#userImagePage", function () {
var page = this;
Dashboard.getCurrentUser().done(function (loggedInUser) {
if (loggedInUser.Configuration.IsAdministrator) {
$('#lnkParentalControl', page).show();
2013-04-18 12:32:16 -07:00
} else {
$('#lnkParentalControl', page).hide();
2013-04-18 12:32:16 -07:00
}
});
}).on('pageshow', "#userImagePage", function () {
var page = this;
reloadUser(page);
$("#userImageDropZone", page).on('dragover', onImageDragOver).on('drop', onImageDrop);
}).on('pagehide', "#userImagePage", function () {
var page = this;
$("#userImageDropZone", page).off('dragover', onImageDragOver).off('drop', onImageDrop);
});
})(jQuery, window, document, window.FileReader);