/*global jQuery, Ghost, document, Image, window */ (function ($) { "use strict"; var UploadUi, $progress = $('
', { "class" : "js-upload-progress progress progress-success active", "style": "opacity:0", "role": "progressbar", "aria-valuemin": "0", "aria-valuemax": "100" }).append($("", { "class": "js-upload-progress-bar bar", "style": "width:0%" })); UploadUi = function ($dropzone, settings) { var source, $link = $('' + ''), $back = $('' + ''); $.extend(this, { bindFileUpload: function () { var self = this; $dropzone.find('.js-fileupload').fileupload().fileupload("option", { url: '/ghost/upload', add: function (e, data) { $progress.find('.js-upload-progress-bar').removeClass('fail'); $dropzone.trigger('uploadstart'); $dropzone.find('a.js-return-image').remove(); $dropzone.find('span.media, div.description, a.image-url, a.image-webcam, div.js-fail, button.js-fail') .animate({opacity: 0}, 250, function () { if (settings.progressbar) { $dropzone.find('div.js-fail').after($progress); $progress.animate({opacity: 100}, 250); } data.submit(); }); }, dropZone: $dropzone, progressall: function (e, data) { var progress = parseInt(data.loaded / data.total * 100, 10); if (!settings.editor) {$progress.find('div.js-progress').css({"position": "absolute", "top": "40px"}); } if (settings.progressbar) { $dropzone.trigger("uploadprogress", [progress, data]); $progress.find('.js-upload-progress-bar').css('width', progress + '%'); } }, done: function (e, data) { function showImage(width, height) { $dropzone.find('img.js-upload-target').attr({"width": width, "height": height}).css({"display": "block"}); $dropzone.find('.fileupload-loading').removeClass('fileupload-loading'); $dropzone.css({"height": "auto"}); if (!$dropzone.find('a.js-edit-image')[0]) { $link.css({"opacity": 100}); $dropzone.find('.js-upload-target').after($link); } $dropzone.delay(250).animate({opacity: 100}, 1000, function () { self.init(); }); } function animateDropzone($img) { $dropzone.animate({opacity: 0}, 250, function () { $dropzone.removeClass('image-uploader').addClass('pre-image-uploader'); $dropzone.css({minHeight: 0}); self.removeExtras(); $dropzone.animate({height: $img.height()}, 250, function () { showImage($img.width(), $img.height()); }); }); } function failedImageUpload() { $progress.find('.js-upload-progress-bar').addClass('fail'); $dropzone.find('div.js-fail').animate({"opacity": 100}, 1500); $dropzone.find('button.js-fail').animate({"opacity": 100}, 1500).on('click', function () { $dropzone.css({minHeight: 0}); if (source !== undefined && !$dropzone.find('a.js-return-image')[0]) { console.log("source:", source); $back.css({"opacity": 100}); $dropzone.find('.js-upload-target').after($back); } self.removeExtras(); self.init(); }); } function preLoadImage() { var $img = $dropzone.find('img.js-upload-target') .attr({'src': '', "width": 'auto', "height": 'auto'}); if (data.result === "Invalid filetype") { $dropzone.trigger("uploadfailed", [data.result]); failedImageUpload(); } else { $progress.animate({opacity: 0}, 250, function () { $dropzone.find('span.media').after(''); if (!settings.editor) {$progress.find('.fileupload-loading').css({"top": "56px"}); } }); $dropzone.trigger("uploadsuccess", [data.result]); $img.one('load', function () { animateDropzone($img); }) .attr('src', data.result); } } preLoadImage(); } }); }, buildExtras: function () { if (!$dropzone.find('span.media')[0]) { $dropzone.prepend('Image Upload'); } if (!$dropzone.find('div.description')[0]) { $dropzone.append('