jellyfin-web/dashboard-ui/components/imageuploader/imageuploader.js

197 lines
5.3 KiB
JavaScript
Raw Normal View History

2016-01-30 12:31:22 -07:00
define(['paperdialoghelper', 'paper-dialog', 'paper-fab'], function (paperDialogHelper) {
2015-09-17 09:04:04 -07:00
var currentItemId;
var currentFile;
var currentDeferred;
var hasChanges = false;
function onFileReaderError(evt) {
Dashboard.hideLoadingMsg();
switch (evt.target.error.code) {
case evt.target.error.NOT_FOUND_ERR:
2015-12-14 08:43:03 -07:00
Dashboard.alert(Globalize.translate('MessageFileNotFound'));
2015-09-17 09:04:04 -07:00
break;
case evt.target.error.ABORT_ERR:
break; // noop
default:
2015-12-14 08:43:03 -07:00
Dashboard.alert(Globalize.translate('MessageFileReadError'));
2015-09-17 09:04:04 -07:00
break;
};
}
function setFiles(page, files) {
var file = files[0];
if (!file || !file.type.match('image.*')) {
$('#imageOutput', page).html('');
$('#fldUpload', page).hide();
currentFile = null;
return;
}
currentFile = file;
var reader = new FileReader();
reader.onerror = onFileReaderError;
reader.onloadstart = function () {
$('#fldUpload', page).hide();
};
reader.onabort = function () {
Dashboard.hideLoadingMsg();
2015-12-23 10:46:01 -07:00
console.log('File read cancelled');
2015-09-17 09:04:04 -07:00
};
// Closure to capture the file information.
reader.onload = (function (theFile) {
return function (e) {
// Render thumbnail.
var html = ['<img style="max-width:300px;max-height:100px;" src="', e.target.result, '" title="', escape(theFile.name), '"/>'].join('');
$('#imageOutput', page).html(html);
$('#fldUpload', page).show();
};
})(file);
// Read in the image file as a data URL.
reader.readAsDataURL(file);
}
function processImageChangeResult(page) {
hasChanges = true;
history.back();
}
function onSubmit() {
var file = currentFile;
if (!file) {
return false;
}
if (file.type != "image/png" && file.type != "image/jpeg" && file.type != "image/jpeg") {
return false;
}
Dashboard.showLoadingMsg();
var page = $(this).parents('paper-dialog');
var imageType = $('#selectImageType', page).val();
2015-12-14 08:43:03 -07:00
ApiClient.uploadItemImage(currentItemId, imageType, file).then(function () {
2015-09-17 09:04:04 -07:00
$('#uploadImage', page).val('').trigger('change');
Dashboard.hideLoadingMsg();
processImageChangeResult(page);
});
return false;
}
function initEditor(page) {
$('form', page).off('submit', onSubmit).on('submit', onSubmit);
$('#uploadImage', page).on("change", function () {
setFiles(page, this.files);
});
$("#imageDropZone", page).on('dragover', function (e) {
e.preventDefault();
e.originalEvent.dataTransfer.dropEffect = 'Copy';
return false;
}).on('drop', function (e) {
e.preventDefault();
setFiles(page, e.originalEvent.dataTransfer.files);
return false;
});
}
2015-10-15 22:36:16 -07:00
function showEditor(itemId, options) {
options = options || {};
2015-09-17 09:04:04 -07:00
2015-12-14 08:43:03 -07:00
var xhr = new XMLHttpRequest();
xhr.open('GET', 'components/imageuploader/imageuploader.template.html', true);
2015-09-17 09:04:04 -07:00
2015-12-14 08:43:03 -07:00
xhr.onload = function (e) {
2015-09-17 09:04:04 -07:00
2015-12-14 08:43:03 -07:00
var template = this.response;
2015-09-17 09:04:04 -07:00
currentItemId = itemId;
2015-12-14 08:43:03 -07:00
var dlg = paperDialogHelper.createDialog({
2016-01-30 13:59:09 -07:00
size: 'fullscreen-border'
2015-10-15 22:36:16 -07:00
});
2015-09-17 09:04:04 -07:00
2016-01-30 13:59:09 -07:00
var theme = options.theme || 'b';
dlg.classList.add('ui-body-' + theme);
dlg.classList.add('background-theme-' + theme);
dlg.classList.add('popupEditor');
2015-09-17 09:04:04 -07:00
var html = '';
html += '<h2 class="dialogHeader">';
2016-01-30 13:59:09 -07:00
html += '<paper-fab icon="arrow-back" mini class="btnCloseDialog" tabindex="-1"></paper-fab>';
2015-09-17 09:04:04 -07:00
html += '<div style="display:inline-block;margin-left:.6em;vertical-align:middle;">' + Globalize.translate('HeaderUploadImage') + '</div>';
html += '</h2>';
html += '<div class="editorContent">';
html += Globalize.translateDocument(template);
html += '</div>';
dlg.innerHTML = html;
document.body.appendChild(dlg);
// Has to be assigned a z-index after the call to .open()
$(dlg).on('iron-overlay-closed', onDialogClosed);
2015-12-14 08:43:03 -07:00
paperDialogHelper.open(dlg);
2015-09-17 09:04:04 -07:00
var editorContent = dlg.querySelector('.editorContent');
initEditor(editorContent);
2016-02-06 13:32:14 -07:00
$('#selectImageType', dlg).val(options.imageType || 'Primary');
2015-09-29 09:29:06 -07:00
$('.btnCloseDialog', dlg).on('click', function () {
2015-09-17 09:04:04 -07:00
2015-12-14 08:43:03 -07:00
paperDialogHelper.close(dlg);
2015-09-29 09:29:06 -07:00
});
2015-12-14 08:43:03 -07:00
}
xhr.send();
2015-09-17 09:04:04 -07:00
}
function onDialogClosed() {
$(this).remove();
Dashboard.hideLoadingMsg();
currentDeferred.resolveWith(null, [hasChanges]);
}
2015-12-14 08:43:03 -07:00
return {
2015-10-15 22:36:16 -07:00
show: function (itemId, options) {
2015-09-17 09:04:04 -07:00
var deferred = DeferredBuilder.Deferred();
currentDeferred = deferred;
hasChanges = false;
2015-12-14 08:43:03 -07:00
showEditor(itemId, options);
2015-09-17 09:04:04 -07:00
return deferred.promise();
}
};
2015-12-14 08:43:03 -07:00
});