Implement Notifications for Ember

closes #2417
- Adds Notification(s)Component
- Render notifications in application.hbs
- Adds handleError in application route
This commit is contained in:
Fabian Becker 2014-03-22 12:08:15 +00:00
parent 8ff2e929ae
commit 9da6f15d33
10 changed files with 113 additions and 2 deletions

View File

@ -1,6 +1,7 @@
import Resolver from 'ember/resolver';
import initFixtures from 'ghost/fixtures/init';
import {currentUser, injectCurrentUser} from 'ghost/initializers/current-user';
import {registerNotifications, injectNotifications} from 'ghost/initializers/notifications';
import 'ghost/utils/link-view';
var App = Ember.Application.extend({
@ -20,5 +21,7 @@ initFixtures();
App.initializer(currentUser);
App.initializer(injectCurrentUser);
App.initializer(registerNotifications);
App.initializer(injectNotifications);
export default App;

View File

@ -0,0 +1,21 @@
var NotificationComponent = Ember.Component.extend({
classNames: ['js-bb-notification'],
didInsertElement: function () {
var self = this;
self.$().on('animationend webkitAnimationEnd oanimationend MSAnimationEnd', function (event) {
/* jshint unused: false */
self.notifications.removeObject(self.get('message'));
});
},
actions: {
closeNotification: function () {
var self = this;
self.notifications.removeObject(self.get('message'));
}
}
});
export default NotificationComponent;

View File

@ -0,0 +1,7 @@
var NotificationsComponent = Ember.Component.extend({
tagName: 'aside',
classNames: 'notifications',
messages: Ember.computed.alias('notifications')
});
export default NotificationsComponent;

View File

@ -0,0 +1,21 @@
import Notifications from 'ghost/utils/notifications';
var registerNotifications = {
name: 'registerNotifications',
initialize: function (container, application) {
application.register('notifications:main', Notifications);
}
};
var injectNotifications = {
name: 'injectNotifications',
initialize: function (container, application) {
application.inject('controller', 'notifications', 'notifications:main');
application.inject('component', 'notifications', 'notifications:main');
application.inject('route', 'notifications', 'notifications:main');
}
};
export {registerNotifications, injectNotifications};

View File

@ -18,6 +18,17 @@ var ApplicationRoute = Ember.Route.extend({
outlet: 'modal',
parentView: 'application'
});
},
handleErrors: function (errors) {
this.notifications.clear();
errors.forEach(function (errorObj) {
this.notifications.showError(errorObj.message || errorObj);
if (errorObj.hasOwnProperty('el')) {
errorObj.el.addClass('input-error');
}
});
}
}
});

View File

@ -23,7 +23,9 @@ var SigninRoute = Ember.Route.extend(styleBody, {
}
);
} else {
window.alert('Must enter email + passwort. Todo: Must show as notification'); // Todo Show notification
this.notifications.clear();
this.notifications.showError('Must enter email + password');
}
}
}

View File

@ -3,7 +3,8 @@
{{/unless}}
<main role="main" id="main">
{{ghost-notifications}}
{{outlet}}
</main>
{{outlet modal}}

View File

@ -0,0 +1,4 @@
<section {{bind-attr class=":js-notification message.typeClass"}}>
{{message.message}}
<a class="close" {{action "closeNotification"}}><span class="hidden">Close</span></a>
</section>

View File

@ -0,0 +1,3 @@
{{#each messages}}
{{ghost-notification message=this}}
{{/each}}

View File

@ -0,0 +1,38 @@
var Notifications = Ember.ArrayProxy.extend({
content: Ember.A(),
timeout: 3000,
pushObject: function (object) {
object.typeClass = 'notification-' + object.type;
// This should be somewhere else.
if (object.type === 'success') {
object.typeClass = object.typeClass + " notification-passive";
}
this._super(object);
},
showError: function (message) {
this.pushObject({
type: 'error',
message: message
});
},
showInfo: function (message) {
this.pushObject({
type: 'info',
message: message
});
},
showSuccess: function (message) {
this.pushObject({
type: 'success',
message: message
});
},
showWarn: function (message) {
this.pushObject({
type: 'warn',
message: message
});
}
});
export default Notifications;