From cc1a22b7fac3d4190f41f82456af3b6978aa723e Mon Sep 17 00:00:00 2001 From: Pavel Linkesch Date: Fri, 5 Feb 2016 12:43:42 +0100 Subject: [PATCH] fix #273 - call uploadCompleted() after uploaded image replaces preview --- spec/images.spec.js | 51 +++++++++++++++++++++++++++++---------------- src/js/images.js | 15 +++++++------ 2 files changed, 42 insertions(+), 24 deletions(-) diff --git a/spec/images.spec.js b/spec/images.spec.js index 20c671e3e..ad4757898 100644 --- a/spec/images.spec.js +++ b/spec/images.spec.js @@ -160,7 +160,7 @@ describe('Images addon', function () { }); }); - it('truggers input event twice on showImage for preview', function (done) { + it('triggers input event twice on showImage for preview', function (done) { var inputTriggerCount = 0, stubbedImage = jasmine.createSpy('image'), context = this.$el.prepend('
' + @@ -184,6 +184,38 @@ describe('Images addon', function () { stubbedImage.onload(); }); + it('calls uploadCompleted when preview is enabled', function (done) { + var stubbedImage = jasmine.createSpy('image'), + context = this.$el.prepend('
' + + '
' + + '
'); + + spyOn(this.addon, 'getDOMImage').and.returnValue(stubbedImage); + + this.addon.options.uploadCompleted = function () { + expect(true).toBe(true); + done(); + }; + + this.addon.showImage('http://image.co', { + context: context + }); + stubbedImage.onload(); + }); + + it('calls uploadCompleted when preview is disabled', function (done) { + this.addon.options.preview = false; + + this.addon.options.uploadCompleted = function () { + expect(true).toBe(true); + done(); + }; + + this.addon.showImage(null, { + submit: function () {} + }); + }); + it('supports selecting image', function () { this.$el.find('p') .addClass('medium-insert-images') @@ -370,23 +402,6 @@ describe('Images addon', function () { $('.medium-insert-images-toolbar2 .medium-editor-action').first().click(); }); - it('calls uploadCompleted callback', function (done) { - this.addon.options.uploadCompleted = function () { - expect(true).toBe(true); - done(); - }; - - spyOn(this.addon, 'showImage'); - - this.addon.uploadDone(null, { - result: { - files: [ - { url: 'test.jpg' } - ] - } - }); - }); - it('validatates file type on upload', function (done) { spyOn(window, 'alert').and.callFake(function (text) { expect(text).toMatch(/^This file is not in a supported format/); diff --git a/src/js/images.js b/src/js/images.js index d356618eb..12b901485 100644 --- a/src/js/images.js +++ b/src/js/images.js @@ -342,14 +342,10 @@ */ Images.prototype.uploadDone = function (e, data) { - var $el = $.proxy(this, 'showImage', data.result.files[0].url, data)(); + $.proxy(this, 'showImage', data.result.files[0].url, data)(); this.core.clean(); this.sorting(); - - if (this.options.uploadCompleted) { - this.options.uploadCompleted($el, data); - } }; /** @@ -374,8 +370,13 @@ domImage = this.getDOMImage(); domImage.onload = function () { data.context.find('img').attr('src', domImage.src); + + if (this.options.uploadCompleted) { + this.options.uploadCompleted(data.context, data); + } + that.core.triggerInput(); - }; + }.bind(this); domImage.src = img; } else { data.context = $(this.templates['src/js/templates/images-image.hbs']({ @@ -405,6 +406,8 @@ if (this.options.preview) { data.submit(); + } else if (this.options.uploadCompleted) { + this.options.uploadCompleted(data.context, data); } }