Skip to content
Permalink
Browse files

Merge branch 't/12965' into major

  • Loading branch information...
Reinmar committed Apr 6, 2015
2 parents 8b2157e + f6ebaab commit 6fce1f89281ed4149b10ad2a01a52d5be643d34d
@@ -204,15 +204,13 @@
function UploadsRepository( editor ) {
this.editor = editor;

this._ = {
loaders: []
};
this.loaders = [];
}

UploadsRepository.prototype = {
/**
* Creates a {@link CKEDITOR.fileTools.fileLoader file loader} instance with a unique id.
* The instance can be later retrieved from the repository using the {@link #get} method.
* The instance can be later retrieved from the repository using the {@link #loaders} array.
*
* Fires {@link CKEDITOR.fileTools.uploadsRepository#instanceCreated instanceCreated} event.
*
@@ -221,27 +219,40 @@
* @returns {CKEDITOR.fileTools.fileLoader} The created file loader instance.
*/
create: function( fileOrData, fileName ) {
var id = this._.loaders.length,
var id = this.loaders.length,
loader = new FileLoader( this.editor, fileOrData, fileName );

loader.id = id;
this._.loaders[ id ] = loader;
this.loaders[ id ] = loader;

this.fire( 'instanceCreated', loader );

return loader;
},

/**
* Gets a {@link CKEDITOR.fileTools.fileLoader file loader} instance with a given id.
* Returns `true` if all loaders finished their job.
*
* @param {Number} id File loader id.
* @returns {CKEDITOR.fileTools.fileLoader} File loader instance with a given id.
* @returns {Boolean} `true` if all loaders finished their job, `false` otherwise.
*/
get: function( id ) {
return this._.loaders[ id ];
isFinished: function() {
for ( var id = 0; id < this.loaders.length; ++id ) {
if ( !this.loaders[ id ].isFinished() ) {
return false;
}
}

return true;
}

/**
* Array of loaders created by the {@link #create} method. Loaders' {@link CKEDITOR.fileTools.fileLoader#id ids}
* are indexes.
*
* @readonly
* @property {CKEDITOR.fileTools.fileLoader[]} loaders
*/

/**
* Event fired when {@link CKEDITOR.fileTools.fileLoader FileLoader} is created.
*
@@ -661,6 +672,16 @@
*/
update: function() {
this.fire( 'update' );
},

/**
* Returns `true` if the loading and uploading finished (successfully or not), so the {@link #status} is
* `loaded`, `uploaded`, `error` or `abort`.
*
* @returns {Boolean} `true` if the loading and uploading finished.
*/
isFinished: function() {
return !!this.status.match( /^(?:loaded|uploaded|error|abort)$/ );
}

/**
@@ -222,7 +222,7 @@
init: function() {
var widget = this,
id = this.wrapper.findOne( '[data-cke-upload-id]' ).data( 'cke-upload-id' ),
loader = uploads.get( id ),
loader = uploads.loaders[ id ],
capitalize = CKEDITOR.tools.capitalize,
oldStyle, newStyle;

@@ -935,6 +935,55 @@
loader.loadAndUpload( 'http:\/\/url\/' );

wait();
},

'test isFinished created': function() {
var loader = new FileLoader( editorMock, testFile );
loader.status = 'created';

assert.isFalse( loader.isFinished() );
},

'test isFinished loading': function() {
var loader = new FileLoader( editorMock, testFile );
loader.status = 'loading';

assert.isFalse( loader.isFinished() );
},

'test isFinished loaded': function() {
var loader = new FileLoader( editorMock, testFile );
loader.status = 'loaded';

assert.isTrue( loader.isFinished() );
},

'test isFinished uploading': function() {
var loader = new FileLoader( editorMock, testFile );
loader.status = 'uploading';

assert.isFalse( loader.isFinished() );
},

'test isFinished uploaded': function() {
var loader = new FileLoader( editorMock, testFile );
loader.status = 'uploaded';

assert.isTrue( loader.isFinished() );
},

'test isFinished error': function() {
var loader = new FileLoader( editorMock, testFile );
loader.status = 'error';

assert.isTrue( loader.isFinished() );
},

'test isFinished abort': function() {
var loader = new FileLoader( editorMock, testFile );
loader.status = 'abort';

assert.isTrue( loader.isFinished() );
}
} );
} )();
@@ -21,6 +21,9 @@
getUploadUrl = CKEDITOR.fileTools.getUploadUrl;
isTypeSupported = CKEDITOR.fileTools.isTypeSupported;
getExtention = CKEDITOR.fileTools.getExtention;

// Reset uploadsRepository.
this.editor.uploadsRepository.loaders = [];
},

'test getUploadUrl 1': function() {
@@ -110,27 +113,27 @@
'test UploadsRepository': function() {
var repository = this.editor.uploadsRepository;

assert.areSame( 0, repository._.loaders.length );
assert.isUndefined( repository.get( 0 ) );
assert.areSame( 0, repository.loaders.length );
assert.isUndefined( repository.loaders[ 0 ] );

var loader1 = repository.create( { name: 'name1' } );

assert.areSame( 0, loader1.id );
assert.areSame( 'name1', loader1.fileName );

assert.areSame( 1, repository._.loaders.length );
assert.areSame( 'name1', repository.get( 0 ).fileName );
assert.isUndefined( repository.get( 1 ) );
assert.areSame( 1, repository.loaders.length );
assert.areSame( 'name1', repository.loaders[ 0 ].fileName );
assert.isUndefined( repository.loaders[ 1 ] );

var loader2 = repository.create( { name: 'name2' } );

assert.areSame( 1, loader2.id );
assert.areSame( 'name2', loader2.fileName );

assert.areSame( 2, repository._.loaders.length );
assert.areSame( 'name1', repository.get( 0 ).fileName );
assert.areSame( 'name2', repository.get( 1 ).fileName );
assert.isUndefined( repository.get( 2 ) );
assert.areSame( 2, repository.loaders.length );
assert.areSame( 'name1', repository.loaders[ 0 ].fileName );
assert.areSame( 'name2', repository.loaders[ 1 ].fileName );
assert.isUndefined( repository.loaders[ 2 ] );
},


@@ -144,6 +147,29 @@

assert.isTrue( listener.calledOnce, 'Should be called once.' );
assert.areSame( loader, listener.firstCall.args[ 0 ].data, 'Should be called with loader.' );
},

'test UploadsRepository isFinished': function() {
var repository = this.editor.uploadsRepository;


repository.create( { name: 'foo1' } );
repository.create( { name: 'foo2' } );
repository.create( { name: 'foo3' } );

assert.isFalse( repository.isFinished(), '0/3' );

sinon.stub( repository.loaders[ 0 ], 'isFinished' ).returns( true );

assert.isFalse( repository.isFinished(), '1/3' );

sinon.stub( repository.loaders[ 2 ], 'isFinished' ).returns( true );

assert.isFalse( repository.isFinished(), '2/3' );

sinon.stub( repository.loaders[ 1 ], 'isFinished' ).returns( true );

assert.isTrue( repository.isFinished(), '3/3' );
}
} );
} )();
@@ -77,7 +77,7 @@

for ( editorName in this.editors ) {
// Clear uploads repository.
this.editors[ editorName ].uploadsRepository._.loaders = [];
this.editors[ editorName ].uploadsRepository.loaders = [];
}

if ( CKEDITOR.fileTools.bindNotifications.reset ) {
@@ -93,7 +93,7 @@
assertUploadingWidgets( editor, LOADING_IMG );
assert.areSame( '', editor.getData(), 'getData on loading.' );

var loader = editor.uploadsRepository.get( 0 );
var loader = editor.uploadsRepository.loaders[ 0 ];

loader.data = bender.tools.pngBase64;
loader.changeStatus( 'uploading' );
@@ -123,7 +123,7 @@
assertUploadingWidgets( editor, LOADING_IMG );
assert.areSame( '', editor.getData(), 'getData on loading.' );

var loader = editor.uploadsRepository.get( 0 );
var loader = editor.uploadsRepository.loaders[ 0 ];

loader.data = bender.tools.pngBase64;
loader.changeStatus( 'uploading' );
@@ -155,7 +155,7 @@
assertUploadingWidgets( editor, LOADED_IMG );
assert.areSame( '<p>xx</p>', editor.getData(), 'getData on loading.' );

var loader = editor.uploadsRepository.get( 0 );
var loader = editor.uploadsRepository.loaders[ 0 ];

loader.data = bender.tools.pngBase64;
loader.changeStatus( 'uploading' );
@@ -121,7 +121,7 @@
editor = bot.editor;

// Clear uploads repository.
editor.uploadsRepository._.loaders = [];
editor.uploadsRepository.loaders = [];

editor.resetUndo();
},
@@ -141,7 +141,7 @@
assert.areSame( 1, loadAndUploadCount, 'loadAndUpload should be called once.' );
assert.areSame( 'uploadUrl', lastUploadUrl );

var loader = editor.uploadsRepository.get( 0 );
var loader = editor.uploadsRepository.loaders[ 0 ];

loader.changeStatus( 'uploaded' );

0 comments on commit 6fce1f8

Please sign in to comment.
You can’t perform that action at this time.