Skip to content

Commit 1f04c2c

Browse files
author
Piotr Jasiun
committed
Merge branch 't/13519' into major
2 parents 610ad24 + c5d1abb commit 1f04c2c

File tree

3 files changed

+68
-3
lines changed

3 files changed

+68
-3
lines changed

CHANGES.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ CKEditor 4 Changelog
66
New Features:
77

88
* [#12077](https://dev.ckeditor.com/ticket/12077): Add support for HTML5 "download" attribute in link (a) elements. Thanks to [sbusse](https://github.com/sbusse)!
9+
* [#13519](http://dev.ckeditor.com/ticket/13519): (http://docs.ckeditor.com/#!/api/CKEDITOR.fileTools) response is now more flexible.
910

1011
## CKEditor 4.5.5
1112

plugins/filetools/plugin.js

Lines changed: 21 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@
5858
* @since 4.5
5959
* @event fileUploadResponse
6060
* @member CKEDITOR.editor
61-
* @param data
61+
* @param data All data will be passed to the {@link CKEDITOR.fileTools.fileLoader#responseData}.
6262
* @param {CKEDITOR.fileTools.fileLoader} data.fileLoader A file loader instance.
6363
* @param {String} data.message The message from the server. Needs to be set in the listener — see the example above.
6464
* @param {String} data.fileName The file name on server. Needs to be set in the listener — see the example above.
@@ -82,8 +82,9 @@
8282
if ( !response.uploaded ) {
8383
evt.cancel();
8484
} else {
85-
data.fileName = response.fileName;
86-
data.url = response.url;
85+
for ( var i in response ) {
86+
data[ i ] = response[ i ];
87+
}
8788
}
8889
} catch ( err ) {
8990
// Response parsing error.
@@ -278,6 +279,8 @@
278279
this.uploaded = 0;
279280
this.uploadTotal = null;
280281

282+
this.responseData = null;
283+
281284
this.status = 'created';
282285

283286
this.abort = function() {
@@ -344,6 +347,16 @@
344347
* @property {Number} total
345348
*/
346349

350+
/**
351+
* All data received in the response from the server. If server return addition data they will be available
352+
* in this property.
353+
*
354+
* It contains all data set in the {@link CKEDITOR.editor#fileUploadResponse} event listener.
355+
*
356+
* @readonly
357+
* @property {Object} responseData
358+
*/
359+
347360
/**
348361
* The total size of upload data in bytes.
349362
* If the `xhr.upload` object is present, this value will indicate the total size of the request payload, not only the file
@@ -595,6 +608,11 @@
595608
}
596609
}
597610

611+
// The whole response is also hold for use by uploadwidgets (#13519).
612+
loader.responseData = data;
613+
// But without reference to the loader itself.
614+
delete loader.responseData.fileLoader;
615+
598616
if ( success === false ) {
599617
loader.changeStatus( 'error' );
600618
} else {

tests/plugins/filetools/fileloader.js

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -768,6 +768,27 @@
768768
wait();
769769
},
770770

771+
'test response with custom fields (#13519)': function() {
772+
var loader = new FileLoader( editorMock, testFile ),
773+
response = {
774+
fileName: 'name2.png',
775+
uploaded: 1,
776+
url: 'http:\/\/url\/name2.png',
777+
foo: 'bar'
778+
};
779+
780+
createXMLHttpRequestMock( [ 'progress', 'load' ],
781+
{ responseText: JSON.stringify( response ) } );
782+
783+
resumeAfter( loader, 'uploaded', function() {
784+
objectAssert.areEqual( response, loader.responseData );
785+
} );
786+
787+
loader.upload( 'http:\/\/url\/' );
788+
789+
wait();
790+
},
791+
771792
'test error 404 with message': function() {
772793
editorMock.lang = { filetools: { httpError404: 'httpError404' } };
773794

@@ -866,6 +887,31 @@
866887
wait();
867888
},
868889

890+
'test additional data in fileUploadResponse (#13519)': function() {
891+
var data,
892+
loader = new FileLoader( editorMock, testFile );
893+
894+
createXMLHttpRequestMock( [ 'progress', 'load' ],
895+
{ responseText: '{' +
896+
'"fileName":"name2.png",' +
897+
'"uploaded":1,' +
898+
'"url":"http:\/\/url\/name2.png",' +
899+
'"foo":"bar"' +
900+
'}' } );
901+
902+
attachListener( editorMock, 'fileUploadResponse', function( evt ) {
903+
data = evt.data;
904+
} );
905+
906+
resumeAfter( editorMock, 'fileUploadResponse', function() {
907+
assert.areSame( 'bar', data.foo );
908+
} );
909+
910+
loader.upload( 'http:\/\/url\/' );
911+
912+
wait();
913+
},
914+
869915
'test custom fileUploadRequest and fileUploadResponse': function() {
870916
editorMock.lang = { filetools: { responseError: 'err' } };
871917

0 commit comments

Comments
 (0)