Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Cancel notificationUpdate event prevent only show.
  • Loading branch information
Piotr Jasiun committed Feb 6, 2015
1 parent ac8a5a3 commit 7155887
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 6 deletions.
14 changes: 10 additions & 4 deletions plugins/notification/plugin.js
Expand Up @@ -50,7 +50,7 @@ CKEDITOR.plugins.add( 'notification', {

// Close the last notification on ESC.
editor.on( 'key', function( evt ) {
if ( evt.data.keyCode == 27 /* ESC */ ) {
if ( evt.data.keyCode == 27 ) { /* ESC */
var notifications = editor._.notificationArea.notifications;

if ( !notifications.length ) {
Expand Down Expand Up @@ -236,8 +236,13 @@ Notification.prototype = {
* if it was hidden and read by screen readers.
*/
update: function( options ) {
var show = true;

if ( this.editor.fire( 'notificationUpdate', { notification: this, options: options } ) === false ) {
return;
// The idea of cancelable event is to let user create his own way of displaying notification, so if
// `notificationUpdate` event will be canceled there will be no interaction with notification area, but on
// the other hand the logic should work anyway so object will be updated (including `element` property).
show = false;
}

var element = this.element,
Expand Down Expand Up @@ -276,7 +281,7 @@ Notification.prototype = {
}
}

if ( options.important ) {
if ( show && options.important ) {
element.setAttribute( 'role', 'alert' );

if ( !this.isVisible() ) {
Expand Down Expand Up @@ -873,7 +878,8 @@ CKEDITOR.plugins.notification = Notification;

/**
* This event is fired when the {@link CKEDITOR.plugins.notification#update} method is called, before the
* notification is updated. If this event will be canceled, notification will be not updated.
* notification is updated. If this event will be canceled, notification will not be shown even if update was important,
* but object will be updated anyway.
*
* Using this event allows to fully customize how a notification will be updated. It may be used to integrate
* the CKEditor notifications system with the web page's notifications.
Expand Down
18 changes: 16 additions & 2 deletions tests/plugins/notification/notification.js
Expand Up @@ -249,7 +249,7 @@ bender.test( {

assertNotifications( editor, [ { message: 'Foo', type: 'warning' } ] );

editor.fire( 'key', { keyCode: 27 /* ESC */ } );
editor.fire( 'key', { keyCode: 27 } ); /* ESC */

assertNotifications( editor, [] );

Expand Down Expand Up @@ -427,8 +427,22 @@ bender.test( {

assert.isTrue( listener.calledOnce );

assertNotifications( editor, [ { message: 'Foo', type: 'info', alert: true } ] );
assertNotifications( editor, [ { message: 'Foo', type: 'warning', alert: false } ] );
},

'test notificationUpdate event do not show if event canceled': function() {
var editor = this.editor,
notification = new CKEDITOR.plugins.notification( editor, { message: 'Foo' } );

listener = sinon.stub().returns( false );

this.editor.on( 'notificationUpdate', listener );

notification.update( { important: true } );

assert.isTrue( listener.calledOnce );

assertNotifications( editor, [] );
},

'test notificationHide event': function() {
Expand Down

0 comments on commit 7155887

Please sign in to comment.