Skip to content
This repository has been archived by the owner on Sep 11, 2020. It is now read-only.

Commit

Permalink
Merge pull request #274 from orthes/fix-uploadCompleted
Browse files Browse the repository at this point in the history
Fix #273 - call uploadCompleted() after uploaded image replaces preview
  • Loading branch information
j0k3r committed Feb 5, 2016
2 parents 72dfb52 + cc1a22b commit d964f57
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 24 deletions.
51 changes: 33 additions & 18 deletions spec/images.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -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('<div class="medium-insert-images medium-insert-active">' +
Expand All @@ -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('<div class="medium-insert-images medium-insert-active">' +
'<figure><img src="data:" alt=""></figure>' +
'</div>');

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')
Expand Down Expand Up @@ -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/);
Expand Down
15 changes: 9 additions & 6 deletions src/js/images.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
};

/**
Expand All @@ -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']({
Expand Down Expand Up @@ -405,6 +406,8 @@

if (this.options.preview) {
data.submit();
} else if (this.options.uploadCompleted) {
this.options.uploadCompleted(data.context, data);
}
}

Expand Down

0 comments on commit d964f57

Please sign in to comment.