Skip to content
This repository has been archived by the owner on Jun 26, 2020. It is now read-only.

Commit

Permalink
Optional notification title.
Browse files Browse the repository at this point in the history
  • Loading branch information
szymonkups committed Jun 1, 2017
1 parent 737b55f commit 10299b4
Show file tree
Hide file tree
Showing 2 changed files with 94 additions and 10 deletions.
35 changes: 31 additions & 4 deletions src/notification/notification.js
Original file line number Diff line number Diff line change
Expand Up @@ -52,16 +52,23 @@ export default class Notification extends Plugin {
* } );
*
* will fire `show:success:upload:image` event.
* Title of the notification can be provided:
*
* showSuccess( 'Image is uploaded.', {
* title: 'Image upload success'
* });
*
* @param {String} message Content of the notification.
* @param {Object} [data={}] Additional data.
* @param {String} [data.namespace] Additional event namespace.
* @param {String} [data.title] Title of the notification.
*/
showSuccess( message, data = {} ) {
this._showNotification( {
message,
type: 'success',
namespace: data.namespace
namespace: data.namespace,
title: data.title
} );
}

Expand All @@ -76,16 +83,23 @@ export default class Notification extends Plugin {
* } );
*
* will fire `show:info:editor:status` event.
* Title of the notification can be provided:
*
* showInfo( 'Editor is offline.', {
* title: 'Network information'
* });
*
* @param {String} message Content of the notification.
* @param {Object} [data={}] Additional data.
* @param {String} [data.namespace] Additional event namespace.
* @param {String} [data.title] Title of the notification.
*/
showInfo( message, data = {} ) {
this._showNotification( {
message,
type: 'info',
namespace: data.namespace
namespace: data.namespace,
title: data.title
} );
}

Expand All @@ -100,6 +114,11 @@ export default class Notification extends Plugin {
* } );
*
* will fire `show:warning:upload:image` event.
* Title of the notification can be provided:
*
* showWarning( 'Image upload error.', {
* title: 'Upload failed'
* });
*
* Note that each unhandled and not stopped `warning` notification will be displayed as system alert.
* Plugin responsible for displaying warnings should `stop()` the event to prevent of displaying it as alert:
Expand Down Expand Up @@ -127,12 +146,14 @@ export default class Notification extends Plugin {
* @param {String} message Content of the notification.
* @param {Object} [data={}] Additional data.
* @param {String} [data.namespace] Additional event namespace.
* @param {String} [data.title] Title of the notification.
*/
showWarning( message, data = {} ) {
this._showNotification( {
message,
type: 'warning',
namespace: data.namespace
namespace: data.namespace,
title: data.title
} );
}

Expand All @@ -144,13 +165,15 @@ export default class Notification extends Plugin {
* @param {String} data.message Content of the notification.
* @param {'success'|'info'|'warning'} data.type Type of message.
* @param {String} [data.namespace] Additional event namespace.
* @param {String} [data.title=''] Title of the notification.
*/
_showNotification( data ) {
const event = `show:${ data.type }` + ( data.namespace ? `:${ data.namespace }` : '' );

this.fire( event, {
message: data.message,
type: data.type
type: data.type,
title: data.title || ''
} );
}

Expand All @@ -160,6 +183,7 @@ export default class Notification extends Plugin {
* @event show
* @param {Object} data Notification data.
* @param {String} data.message Content of the notification.
* @param {String} data.title Title of the notification.
* @param {'success'|'info'|'warning'} data.type Type of notification.
*/

Expand All @@ -169,6 +193,7 @@ export default class Notification extends Plugin {
* @event show:success
* @param {Object} data Notification data.
* @param {String} data.message Content of the notification.
* @param {String} data.title Title of the notification.
* @param {'success'} data.type Type of notification.
*/

Expand All @@ -178,6 +203,7 @@ export default class Notification extends Plugin {
* @event show:info
* @param {Object} data Notification data.
* @param {String} data.message Content of the notification.
* @param {String} data.title Title of the notification.
* @param {'info'} data.type Type of notification.
*/

Expand All @@ -190,6 +216,7 @@ export default class Notification extends Plugin {
* @event show:warning
* @param {Object} data Notification data.
* @param {String} data.message Content of the notification.
* @param {String} data.title Title of the notification.
* @param {'warning'} data.type Type of notification.
*/
}
69 changes: 63 additions & 6 deletions tests/notification/notification.js
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,8 @@ describe( 'Notification', () => {
sinon.assert.calledOnce( spy );
expect( spy.firstCall.args[ 1 ] ).to.deep.equal( {
message: 'foo bar',
type: 'success'
type: 'success',
title: ''
} );
} );

Expand All @@ -58,7 +59,25 @@ describe( 'Notification', () => {
sinon.assert.calledOnce( spy );
expect( spy.firstCall.args[ 1 ] ).to.deep.equal( {
message: 'foo bar',
type: 'success'
type: 'success',
title: ''
} );
} );

it( 'should fire `show:success` event with title', () => {
const spy = testUtils.sinon.spy();

notification.on( 'show:success', spy );

notification.showSuccess( 'foo bar', {
title: 'foo bar baz'
} );

sinon.assert.calledOnce( spy );
expect( spy.firstCall.args[ 1 ] ).to.deep.equal( {
message: 'foo bar',
type: 'success',
title: 'foo bar baz'
} );
} );
} );
Expand All @@ -74,7 +93,8 @@ describe( 'Notification', () => {
sinon.assert.calledOnce( spy );
expect( spy.firstCall.args[ 1 ] ).to.deep.equal( {
message: 'foo bar',
type: 'info'
type: 'info',
title: ''
} );
} );

Expand All @@ -90,7 +110,25 @@ describe( 'Notification', () => {
sinon.assert.calledOnce( spy );
expect( spy.firstCall.args[ 1 ] ).to.deep.equal( {
message: 'foo bar',
type: 'info'
type: 'info',
title: ''
} );
} );

it( 'should fire `show:info` event with title', () => {
const spy = testUtils.sinon.spy();

notification.on( 'show:info', spy );

notification.showInfo( 'foo bar', {
title: 'foo bar baz'
} );

sinon.assert.calledOnce( spy );
expect( spy.firstCall.args[ 1 ] ).to.deep.equal( {
message: 'foo bar',
type: 'info',
title: 'foo bar baz'
} );
} );
} );
Expand All @@ -112,7 +150,8 @@ describe( 'Notification', () => {
sinon.assert.calledOnce( spy );
expect( spy.firstCall.args[ 1 ] ).to.deep.equal( {
message: 'foo bar',
type: 'warning'
type: 'warning',
title: ''
} );
} );

Expand All @@ -128,7 +167,25 @@ describe( 'Notification', () => {
sinon.assert.calledOnce( spy );
expect( spy.firstCall.args[ 1 ] ).to.deep.equal( {
message: 'foo bar',
type: 'warning'
type: 'warning',
title: ''
} );
} );

it( 'should fire `show:warning` event with title', () => {
const spy = testUtils.sinon.spy();

notification.on( 'show:warning', spy );

notification.showWarning( 'foo bar', {
title: 'foo bar baz'
} );

sinon.assert.calledOnce( spy );
expect( spy.firstCall.args[ 1 ] ).to.deep.equal( {
message: 'foo bar',
type: 'warning',
title: 'foo bar baz'
} );
} );

Expand Down

0 comments on commit 10299b4

Please sign in to comment.