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: 450,
|
|
|
|
|
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
|
|
|
|
Dashboard.validateCurrentUser(page);
|
|
|
|
|
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:
|
|
|
|
|
Dashboard.showError('File Not Found!');
|
|
|
|
|
break;
|
|
|
|
|
case evt.target.error.NOT_READABLE_ERR:
|
|
|
|
|
Dashboard.showError('File is not readable');
|
|
|
|
|
break;
|
|
|
|
|
case evt.target.error.ABORT_ERR:
|
|
|
|
|
break; // noop
|
|
|
|
|
default:
|
|
|
|
|
Dashboard.showError('An error occurred reading this file.');
|
|
|
|
|
};
|
|
|
|
|
}
|
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();
|
|
|
|
|
Dashboard.showError('File read cancelled');
|
|
|
|
|
}
|
2013-02-20 18:33:05 -07:00
|
|
|
|
|
2013-04-18 12:32:16 -07:00
|
|
|
|
function setFiles(files) {
|
2013-02-20 18:33:05 -07:00
|
|
|
|
|
|
|
|
|
var page = $.mobile.activePage;
|
|
|
|
|
|
|
|
|
|
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
|
|
|
|
|
2013-04-18 12:32:16 -07:00
|
|
|
|
setFiles(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-17 09:45:37 -07:00
|
|
|
|
|
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
|
|
|
|
|
2013-04-18 12:32:16 -07:00
|
|
|
|
Dashboard.confirm("Are you sure you wish to delete the image?", "Delete Image", 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(fileUpload.files);
|
|
|
|
|
};
|
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) {
|
|
|
|
|
$('.lnkMediaLibrary', page).show().prev().removeClass('ui-last-child');
|
|
|
|
|
} else {
|
|
|
|
|
$('.lnkMediaLibrary', page).hide().prev().addClass('ui-last-child');
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
}).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);
|