Skip to content
Permalink
Browse files

Merge branch 't/13215' into major

  • Loading branch information...
Reinmar committed May 26, 2015
2 parents 4da223f + c3b4d2d commit d70da21682a71e79b7baa1d9b486d0dd563bd98f
@@ -6,6 +6,7 @@ CKEditor 4 Changelog
New Features:

* [#13304](http://dev.ckeditor.com/ticket/13304): Added support for passing DOM elements to [`config.sharedSpaces`](http://docs.ckeditor.com/#!/api/CKEDITOR.config-cfg-sharedSpaces). Thanks to [Undergrounder](https://github.com/Undergrounder)!
* [#13215](http://dev.ckeditor.com/ticket/13215): Added ability to cancel fetching a resource by the Embed plugins.

Fixed Issues:

@@ -17,7 +17,8 @@ CKEDITOR.dialog.add( 'embedBase', function( editor ) {

onLoad: function() {
var that = this,
okButton = that.getButton( 'ok' );
okButton = that.getButton( 'ok' ),
loadContentRequest = null;

this.on( 'ok', function( evt ) {
// We're going to hide it manually, after remote response is fetched.
@@ -31,7 +32,7 @@ CKEDITOR.dialog.add( 'embedBase', function( editor ) {

var url = that.getValueOf( 'info', 'url' );

that.widget.loadContent( url, {
loadContentRequest = that.widget.loadContent( url, {
noNotifications: true,

callback: function() {
@@ -41,20 +42,31 @@ CKEDITOR.dialog.add( 'embedBase', function( editor ) {

editor.fire( 'saveSnapshot' );

okButton.enable();
that.hide();
unlock();
},

errorCallback: function( messageTypeOrMessage ) {
that.getContentElement( 'info', 'url' ).select();

// We need to enable the OK button so user can fix the URL.
okButton.enable();

alert( that.widget.getErrorMessage( messageTypeOrMessage, url, 'Given' ) );

unlock();
}
} );
}, null, null, 15 );

this.on( 'cancel', function( evt ) {
if ( evt.data.hide && loadContentRequest ) {
loadContentRequest.cancel();
unlock();
}
} );

function unlock() {
okButton.enable();
loadContentRequest = null;
}
},

contents: [
@@ -83,4 +95,4 @@ CKEDITOR.dialog.add( 'embedBase', function( editor ) {
}
]
};
} );
} );
@@ -178,6 +178,7 @@
* @param {Function} [opts.errorCallback] Callback called when an error occurred.
* @param {String} opts.errorCallback.messageTypeOrMessage See {@link #getErrorMessage}.
* @param {Boolean} [opts.noNotifications] Do not show notifications (useful when dialog is open).
* @returns {CKEDITOR.plugins.embedBase.request}
*/
loadContent: function( url, opts ) {
opts = opts || {};
@@ -221,6 +222,8 @@
}
}
}

return request;
},

/**
@@ -298,16 +301,22 @@
* @param {CKEDITOR.plugins.embedBase.request} request
*/
_sendRequest: function( request ) {
Jsonp.sendRequest(
this.providerUrl,
{
url: encodeURIComponent( request.url )
},
request.callback,
function() {
request.errorCallback( 'fetchingFailed' );
}
);
var that = this,
jsonpRequest = Jsonp.sendRequest(
this.providerUrl,
{
url: encodeURIComponent( request.url )
},
request.callback,
function() {
request.errorCallback( 'fetchingFailed' );
}
);

request.cancel = function() {
jsonpRequest.cancel();
that.fire( 'requestCanceled', request );
};
},

/**
@@ -530,9 +539,11 @@
* passed in `urlParams` can be used, plus `{callback}` must be defined which represent JSONP callback.
* @param {Object} urlParams Params to be passed to the `urlTemplate`.
* @param {Function} callback
* @param {Function} errorCallback
* @param {Function} [errorCallback]
* @returns {Object} The request object with a `cancel()` method.
*/
sendRequest: function( urlTemplate, urlParams, callback, errorCallback ) {
var request = {};
urlParams = urlParams || {};

var callbackKey = CKEDITOR.tools.getNextNumber(),
@@ -552,13 +563,20 @@

scriptElement = this._attachScript( urlTemplate.output( urlParams ), function() {
cleanUp();
errorCallback();
errorCallback && errorCallback();
} );

request.cancel = cleanUp;

function cleanUp() {
scriptElement.remove();
delete CKEDITOR._.jsonpCallbacks[ callbackKey ];
if ( scriptElement ) {
scriptElement.remove();
delete CKEDITOR._.jsonpCallbacks[ callbackKey ];
scriptElement = null;
}
}

return request;
}
};

@@ -602,9 +620,15 @@
* @property {Object} [response]
*/

/**
* Cancels the request.
*
* @method cancel
*/

CKEDITOR.plugins.embedBase = {
createWidgetBaseDefinition: createWidgetBaseDefinition,
_jsonp: Jsonp
};

} )();
} )();
No changes.
@@ -18,6 +18,12 @@ var embedTools = {
}, 100 );
// This method is mainly used for automated tests, but the 100ms timeout gives
// them a more realistic behaviour.

return {
cancel: function() {
throw new Error( 'Not implemented.' );
}
};
};
} );
},
@@ -28,11 +34,18 @@ var embedTools = {

CKEDITOR.plugins.embedBase._jsonp.sendRequest = function() {
var args = arguments,
that = this;
that = this,
timeout;

setTimeout( function() {
timeout = setTimeout( function() {
origSendRequest.apply( that, args );
}, Math.random() * 1000 );
}, Math.random() * 1000 + 1000 );

return {
cancel: function() {
clearTimeout( timeout );
}
};
};
} );
},
@@ -182,5 +182,34 @@ bender.test( {
dialog.setValueOf( 'info', 'url', 'bar' );
dialog.getButton( 'ok' ).click();
} );
},

'test embedding cancelled before it\'s done': function() {
var editor = this.editors.classic,
widget = {
data: {
url: 'foo'
},

isUrlValid: function() {
return true;
},

loadContent: function() {
return {
cancel: function() {
// So far so good. If we reached here, it means that cancel function was called properly.
assert.isTrue( true );
}
};
}
};

this.openDialog( editor, widget, function( dialog ) {
dialog.setValueOf( 'info', 'url', 'bar' );

dialog.getButton( 'ok' ).click();
dialog.getButton( 'cancel' ).click();
} );
}
} );
} );
@@ -74,5 +74,26 @@ bender.test( {
);

wait();
},

'test sendRequest and cancel': function() {
var successCallback = sinon.spy();

var jsonpRequest = Jsonp.sendRequest(
new CKEDITOR.template( '%TEST_DIR%_assets/void.js' ),
null,
successCallback
);

assert.isFunction( jsonpRequest.cancel );

var jsonpCallbacksNumberPre = CKEDITOR.tools.objectKeys( CKEDITOR._.jsonpCallbacks ).length;

jsonpRequest.cancel();

var jsonpCallbacksNumberPost = CKEDITOR.tools.objectKeys( CKEDITOR._.jsonpCallbacks ).length;

assert.isFalse( successCallback.called, 'success callback was not executed' );
assert.areSame( jsonpCallbacksNumberPre - 1, jsonpCallbacksNumberPost, 'callback has been removed on cancel()' );
}
} );
} );

0 comments on commit d70da21

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