Skip to content

Commit

Permalink
renaming service to simply 'notifications'
Browse files Browse the repository at this point in the history
  • Loading branch information
mansona committed Nov 11, 2019
1 parent 9d75398 commit 4b90b25
Show file tree
Hide file tree
Showing 11 changed files with 51 additions and 57 deletions.
5 changes: 4 additions & 1 deletion addon/components/notification-container.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,14 @@
import Component from '@ember/component';
import { computed } from '@ember/object';
import { htmlSafe } from '@ember/string';
import { inject as service } from '@ember/service';

import layout from '../templates/components/notification-container';

export default Component.extend({
position: 'top',
layout,
position: 'top',
notifications: service(),

classNameBindings: ['computedPosition', ':ember-cli-notifications-notification__container'],
attributeBindings: ['computedStyle:style', 'position:data-test-notification-container'],
Expand Down
9 changes: 7 additions & 2 deletions addon/components/notification-message.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,17 @@
import { htmlSafe } from '@ember/string';
import Component from '@ember/component';
import { computed } from '@ember/object';
import Ember from 'ember';

import { htmlSafe } from '@ember/string';
import { computed } from '@ember/object';
import { inject as service } from '@ember/service';

import layout from '../templates/components/notification-message';

export default Component.extend({
layout,

notifications: service(),

classNameBindings: [
'dismissClass',
'clickableClass',
Expand Down
Original file line number Diff line number Diff line change
@@ -1,15 +1,16 @@
import Service from '@ember/service';
import { assign, merge } from '@ember/polyfills';
import ArrayProxy from '@ember/array/proxy';
import { A } from '@ember/array';
import { isEmpty } from '@ember/utils';
import EmberObject, { getWithDefault, set } from '@ember/object';
import { run } from '@ember/runloop';
import config from 'ember-get-config';

const notificationAssign = assign || merge;

const globals = config['ember-cli-notifications'] || {}; // Import app config object

const NotificationMessagesService = ArrayProxy.extend({
export default Service.extend({
content: A(),

// Method for adding a notification
Expand All @@ -29,7 +30,7 @@ const NotificationMessagesService = ArrayProxy.extend({
cssClasses: options.cssClasses
});

this.pushObject(notification);
this.content.pushObject(notification);

if (notification.autoClear) {
notification.set('remaining', notification.get('clearDuration'));
Expand Down Expand Up @@ -78,7 +79,7 @@ const NotificationMessagesService = ArrayProxy.extend({

// Delay removal from DOM for dismissal animation
run.later(this, () => {
this.removeObject(notification);
this.content.removeObject(notification);
}, 500);
},

Expand Down Expand Up @@ -120,9 +121,3 @@ const NotificationMessagesService = ArrayProxy.extend({
set(globals, 'clearDuration', clearDuration);
}
});

NotificationMessagesService.reopenClass({
isServiceFactory: true
});

export default NotificationMessagesService;
4 changes: 2 additions & 2 deletions addon/templates/components/notification-container.hbs
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
{{#each notifications as |notification|}}
{{#each notifications.content as |notification|}}
{{notification-message notification=notification}}
{{/each}}
{{/each}}
21 changes: 0 additions & 21 deletions app/initializers/notifications.js

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1 +1 @@
export { default } from 'ember-cli-notifications/services/notification-messages-service';
export { default } from 'ember-cli-notifications/services/notifications';
2 changes: 1 addition & 1 deletion tests/dummy/app/controllers/application.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ export default Controller.extend({
clearAll: true,
cssClasses: 'profile-saved-success-notification',

notifications: service('notification-messages'),
notifications: service(),

disableTimeoutInput: not('autoClear'),

Expand Down
5 changes: 0 additions & 5 deletions tests/dummy/app/services/notifications.js

This file was deleted.

31 changes: 18 additions & 13 deletions tests/dummy/app/templates/application.hbs
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,12 @@
<CodeBlock @language="handlebars">&#123;&#123;notification-container position="top-right" zindex="9999"&#125;&#125;</CodeBlock>

<p>Inject the notifications service where required.</p>
<CodeBlock @language="js" @code="notifications: Ember.inject.service('notification-messages')" />
<CodeBlock @language="js">import Controller from '@ember/controller';
import { inject as service } from '@ember/service';

export default Controller.extend({
notifications: service(),
});</CodeBlock>

<p>Or inject it everywhere with an initializer.</p>
<CodeBlock @language="js">// app/initializers/inject-notifications.js
Expand All @@ -49,22 +54,22 @@ export default {
<p>There are four styles of notification available.</p>
<p class="h5"><i>Default value is info</i></p>
<CodeBlock @language="js">// Info
this.get('notifications').info('You have one unread message');
this.notifications.info('You have one unread message');

// Error
this.get('notifications').error('Something went wrong');
this.notifications.error('Something went wrong');

// Success
this.get('notifications').success('Saved successfully!');
this.notifications.success('Saved successfully!');

// Warning
this.get('notifications').warning('You have unsaved changes');
this.notifications.warning('You have unsaved changes');
</CodeBlock>

<h3>htmlContent</h3>
<p>You can enable basic HTML markup for notification messages.</p>
<p><b>Warning:</b> This introduces a potential security risk since the notification message will no longer be escaped by Ember.</p>
<CodeBlock @language="js">this.get('notifications').info('You have &lt;b&gt;one&lt;/b&gt; unread message', {
<CodeBlock @language="js">this.notifications.info('You have &lt;b&gt;one&lt;/b&gt; unread message', {
htmlContent: true
});
</CodeBlock>
Expand Down Expand Up @@ -140,23 +145,23 @@ this.get('notifications').warning('You have unsaved changes');
<p>Set a notification to automatically clear. This will take precedence over the global default value.</p>
<p>If true, inherits the default <b>clearDuration</b> value unless specified.</p>
<p class="h5"><i>Default value is false</i></p>
<CodeBlock @language="js">this.get('notifications').success('Saved successfully!', {
<CodeBlock @language="js">this.notifications.success('Saved successfully!', {
autoClear: true
});
</CodeBlock>

<h3>clearDuration</h3>
<p>Specify a duration (ms) before the notification clears. This will take precedence over the global default value.</p>
<p class="h5"><i>Default value is 3200</i></p>
<CodeBlock @language="js">this.get('notifications').success('Saved successfully!', {
<CodeBlock @language="js">this.notifications.success('Saved successfully!', {
autoClear: true,
clearDuration: 1200
});
</CodeBlock>

<h3>clearAll</h3>
<p>You can remove all present notifications before adding a new one.</p>
<CodeBlock @language="js">this.get('notifications').clearAll().success('Saved successfully!', {
<CodeBlock @language="js">this.notifications.clearAll().success('Saved successfully!', {
autoClear: true
});
</CodeBlock>
Expand Down Expand Up @@ -185,7 +190,7 @@ this.get('notifications').warning('You have unsaved changes');
<h2>Notification with custom CSS</h2>
<hr>
<p>Pass a string of CSS classes to the notification component.</p>
<CodeBlock @language="js">this.get('notifications').success('Saved successfully!', {
<CodeBlock @language="js">this.notifications.success('Saved successfully!', {
autoClear: true,
cssClasses: 'profile-saved-success-notification'
});
Expand All @@ -195,7 +200,7 @@ this.get('notifications').warning('You have unsaved changes');
<h2>Notification with callback</h2>
<hr>
<p>Add the ability to click the notification for a callback.</p>
<CodeBlock @language="js">this.get('notifications').error('Something went wrong. Click to try again', {
<CodeBlock @language="js">this.notifications.error('Something went wrong. Click to try again', {
onClick: (notification) => {
this.get('model').save();
}
Expand All @@ -220,9 +225,9 @@ var ENV = {
<div class="py2">
<h2>Extend</h2>
<hr>
<p>The <b>notification-messages-service</b> service can be imported into the consuming app and extended.</p>
<p>The <b>notifications</b> service can be imported into the consuming app and extended.</p>
<CodeBlock @language="js">// app/services/notifications.js
import NotificationsService from 'ember-cli-notifications/services/notification-messages-service';
import NotificationsService from 'ember-cli-notifications/services/notifications';

export default NotificationsService.extend({
// Extend the imported service
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ module('Integration | Component | notification container', function(hooks) {
setupRenderingTest(hooks);

test('it renders', async function(assert) {
let notificationMessages = this.owner.lookup('service:notification-messages-service');
let notificationMessages = this.owner.lookup('service:notifications');

// fix strange setup bug
notificationMessages.clearAll();
Expand Down
12 changes: 12 additions & 0 deletions tests/unit/services/notifications-test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import { module, test } from 'qunit';
import { setupTest } from 'ember-qunit';

module('Unit | Service | notifications', function(hooks) {
setupTest(hooks);

// Replace this with your real tests.
test('it exists', function(assert) {
let service = this.owner.lookup('service:notifications');
assert.ok(service);
});
});

0 comments on commit 4b90b25

Please sign in to comment.