Skip to content

Commit

Permalink
Merge branch 't/13337b' into major
Browse files Browse the repository at this point in the history
  • Loading branch information
Reinmar committed Jun 10, 2015
2 parents ab16bc5 + b8deb7d commit 83e9980
Show file tree
Hide file tree
Showing 20 changed files with 157 additions and 16 deletions.
1 change: 1 addition & 0 deletions CHANGES.md
Expand Up @@ -7,6 +7,7 @@ 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.
* [#13337](http://dev.ckeditor.com/ticket/13337): Added the [`repository.onWidget()`](http://docs.ckeditor.com/#!/api/CKEDITOR.plugins.widget.repository-method-onWidget) method – a convenient way to listen to [widget](http://docs.ckeditor.com/#!/api/CKEDITOR.plugins.widget) events through the [repository](http://docs.ckeditor.com/#!/api/CKEDITOR.plugins.widget.repository).

Fixed Issues:

Expand Down
40 changes: 40 additions & 0 deletions plugins/widget/plugin.js
Expand Up @@ -540,6 +540,46 @@
return newInstances;
},

/**
* Allows to listen to events on specific types of widgets, even if they are not created yet.
*
* Please note that this method inherits parameters from the {@link CKEDITOR.event#method-on} method with one
* extra parameter at the beginning which is a widget name.
*
* editor.widgets.onWidget( 'image', 'action', function( evt ) {
* // Event `action` occurs on `image` widget.
* } );
*
* @since 4.5
* @param {String} widgetName
* @param {String} eventName
* @param {Function} listenerFunction
* @param {Object} [scopeObj]
* @param {Object} [listenerData]
* @param {Number} [priority=10]
*/
onWidget: function( widgetName ) {
var args = Array.prototype.slice.call( arguments );

args.shift();

for ( var i in this.instances ) {
var instance = this.instances[ i ];

if ( instance.name == widgetName ) {
instance.on.apply( instance, args );
}
}

this.on( 'instanceCreated', function( evt ) {
var widget = evt.data;

if ( widget.name == widgetName ) {
widget.on.apply( widget, args );
}
} );
},

/**
* Parses element classes string and returns an object
* whose keys contain class names. Skips all `cke_*` classes.
Expand Down
2 changes: 1 addition & 1 deletion tests/plugins/widget/acf.js
@@ -1,4 +1,4 @@
/* bender-tags: editor,unit,widgetcore */
/* bender-tags: widgetcore */
/* bender-ckeditor-plugins: widget,toolbar,clipboard */
/* bender-include: _helpers/tools.js */
/* global widgetTestsTools */
Expand Down
2 changes: 1 addition & 1 deletion tests/plugins/widget/activefilter.js
@@ -1,4 +1,4 @@
/* bender-tags: editor,unit,widgetcore */
/* bender-tags: widgetcore */
/* bender-ckeditor-plugins: widget,undo,basicstyles,clipboard,dialog,link,toolbar,stylescombo,font,colorbutton,language,indentblock */
/* bender-include: _helpers/tools.js */
/* global widgetTestsTools */
Expand Down
2 changes: 1 addition & 1 deletion tests/plugins/widget/checkdirty.js
@@ -1,4 +1,4 @@
/* bender-tags: editor,unit,widgetcore */
/* bender-tags: widgetcore */
/* bender-ckeditor-plugins: widget */
/* bender-include: _helpers/tools.js */
/* global widgetTestsTools */
Expand Down
2 changes: 1 addition & 1 deletion tests/plugins/widget/contextmenu.js
@@ -1,4 +1,4 @@
/* bender-tags: editor,unit,widgetcore */
/* bender-tags: widgetcore */
/* bender-ckeditor-plugins: widget,contextmenu */
/* bender-include: _helpers/tools.js */
/* global widgetTestsTools */
Expand Down
2 changes: 1 addition & 1 deletion tests/plugins/widget/customstylehandler.js
@@ -1,4 +1,4 @@
/* bender-tags: editor,unit,widgetcore */
/* bender-tags: widgetcore */
/* bender-ckeditor-plugins: widget */
/* bender-include: _helpers/tools.js */
/* global widgetTestsTools */
Expand Down
2 changes: 1 addition & 1 deletion tests/plugins/widget/definition.js
@@ -1,4 +1,4 @@
/* bender-tags: editor,unit,widgetcore */
/* bender-tags: widgetcore */
/* bender-ckeditor-plugins: widget,undo */
/* bender-include: _helpers/tools.js */
/* global widgetTestsTools */
Expand Down
2 changes: 1 addition & 1 deletion tests/plugins/widget/dnd.js
@@ -1,4 +1,4 @@
/* bender-tags: editor,unit,widgetcore */
/* bender-tags: widgetcore */
/* bender-ckeditor-plugins: widget,undo,clipboard */
/* bender-include: _helpers/tools.js */
/* global widgetTestsTools, lineutilsTestsTools */
Expand Down
2 changes: 1 addition & 1 deletion tests/plugins/widget/editing.js
@@ -1,4 +1,4 @@
/* bender-tags: editor,unit,widgetcore */
/* bender-tags: widgetcore */
/* bender-ckeditor-plugins: widget,dialog */
/* bender-include: _helpers/tools.js */
/* global widgetTestsTools */
Expand Down
2 changes: 1 addition & 1 deletion tests/plugins/widget/elementspath.js
@@ -1,4 +1,4 @@
/* bender-tags: editor,unit,widgetcore */
/* bender-tags: widgetcore */
/* bender-ckeditor-plugins: widget,elementspath */
/* bender-include: ../elementspath/_helpers/tools.js */
/* global elementspathTestsTools */
Expand Down
2 changes: 1 addition & 1 deletion tests/plugins/widget/nestededitables.js
@@ -1,4 +1,4 @@
/* bender-tags: editor,unit,widgetcore */
/* bender-tags: widgetcore */
/* bender-ckeditor-plugins: widget,undo,basicstyles,clipboard,dialog */
/* bender-include: _helpers/tools.js */
/* global widgetTestsTools */
Expand Down
2 changes: 1 addition & 1 deletion tests/plugins/widget/nestedwidgets.js
@@ -1,4 +1,4 @@
/* bender-tags: editor,unit,widgetcore */
/* bender-tags: widgetcore */
/* bender-ckeditor-plugins: widget,undo,clipboard */
/* bender-include: _helpers/tools.js */
/* global widgetTestsTools */
Expand Down
3 changes: 3 additions & 0 deletions tests/plugins/widget/onwidget.html
@@ -0,0 +1,3 @@
<textarea id="test_editor">
<p>Hello moto</p>
</textarea>
97 changes: 97 additions & 0 deletions tests/plugins/widget/onwidget.js
@@ -0,0 +1,97 @@
/* bender-tags: widgetcore */
/* bender-ckeditor-plugins: widget */

( function() {
'use strict';

var Repository,
Widget,
onSpy,
repo;

function widgetElementFactory() {
var span = new CKEDITOR.dom.element( 'span' );
new CKEDITOR.dom.element( 'p' ).append( span );

return span;
}

bender.editors = {
editor: {
creator: 'inline',
name: 'test_editor'
}
};

bender.test( {
'setUp': function() {
Repository = CKEDITOR.plugins.widget.repository;
Widget = CKEDITOR.plugins.widget;
repo = new Repository( bender.editors.editor );

onSpy = sinon.spy( Widget.prototype, 'on' );
},

'tearDown': function() {
Widget.prototype.on.restore();
},

'test has onWidget function': function() {
assert.isFunction( repo.onWidget );
},

'test event listener added on existing widget': function() {
var cbMock = sinon.mock();

var widget = new Widget( repo, 1, widgetElementFactory(), { name: 'image' }, {} );
repo.instances[ widget.id ] = widget;

repo.onWidget( 'image', 'action', cbMock );

assert.isTrue( onSpy.calledOn( widget ) );
assert.isTrue( onSpy.calledWith( 'action', cbMock ) );
},

'test event listener added for element added to repo after calling "onWidget" method': function() {
var cbMock = sinon.mock();

repo.onWidget( 'image', 'action', cbMock );

// The constructor fires repo#instanceCreated itself.
var widget = new Widget( repo, 1, widgetElementFactory(), { name: 'image' }, {} );
repo.instances[ widget.id ] = widget;

assert.isTrue( onSpy.firstCall.calledOn( widget ) );
assert.isTrue( onSpy.firstCall.calledWithExactly( 'action', cbMock ) );
},

'test event listener not added for different widget type': function() {
repo.onWidget( 'image', 'action', sinon.mock() );

var widget = new Widget( repo, 1, widgetElementFactory(), { name: 'notimage' }, {} );
repo.instances[ widget.id ] = widget;

assert.isTrue( onSpy.neverCalledWith( 'action' ) );
},

'test event listener not added for different widget type 2': function() {
repo.onWidget( 'notimage', 'action', sinon.mock() );

var widget = new Widget( repo, 1, widgetElementFactory(), { name: 'image' }, {} );
repo.instances[ widget.id ] = widget;

assert.isTrue( onSpy.neverCalledWith( 'action' ) );
},

'test event listener called with proper arguments': function() {
var cbMock = sinon.mock();

repo.onWidget( 'image', 'action', cbMock, null, null, 5 );

var widget = new Widget( repo, 1, widgetElementFactory(), { name: 'image' }, {} );
repo.instances[ widget.id ] = widget;

assert.isTrue( onSpy.firstCall.calledWithExactly( 'action', cbMock, null, null, 5 ) );
}
} );
}() );
2 changes: 1 addition & 1 deletion tests/plugins/widget/undo.js
@@ -1,4 +1,4 @@
/* bender-tags: editor,unit,widgetcore */
/* bender-tags: widgetcore */
/* bender-ckeditor-plugins: widget,undo,dialog,basicstyles,clipboard */
/* bender-include: _helpers/tools.js */
/* global widgetTestsTools */
Expand Down
2 changes: 1 addition & 1 deletion tests/plugins/widget/widgetapi.js
@@ -1,4 +1,4 @@
/* bender-tags: editor,unit,widgetcore */
/* bender-tags: widgetcore */
/* bender-ckeditor-plugins: widget,undo */
/* bender-include: _helpers/tools.js */
/* global widgetTestsTools */
Expand Down
2 changes: 1 addition & 1 deletion tests/plugins/widget/widgetselection.js
@@ -1,4 +1,4 @@
/* bender-tags: editor,unit,widgetcore */
/* bender-tags: widgetcore */
/* bender-ckeditor-plugins: widget,undo */
/* bender-include: _helpers/tools.js */
/* global widgetTestsTools */
Expand Down
2 changes: 1 addition & 1 deletion tests/plugins/widget/widgetsintegration.js
@@ -1,4 +1,4 @@
/* bender-tags: editor,unit,widgetcore */
/* bender-tags: widgetcore */
/* bender-include: _helpers/tools.js */
/* global widgetTestsTools */

Expand Down
2 changes: 1 addition & 1 deletion tests/plugins/widget/widgetsrepoapi.js
@@ -1,4 +1,4 @@
/* bender-tags: editor,unit,widgetcore,widgetcore */
/* bender-tags: widgetcore */
/* bender-ckeditor-plugins: widget */
/* bender-include: _helpers/tools.js */
/* global widgetTestsTools */
Expand Down

0 comments on commit 83e9980

Please sign in to comment.