mirror of
https://github.com/flarum/core.git
synced 2025-07-24 18:21:33 +02:00
Add alert messages
This commit is contained in:
@@ -27,9 +27,20 @@ export default Ember.Controller.extend({
|
||||
searchQuery: '',
|
||||
searchActive: false,
|
||||
|
||||
alerts: [],
|
||||
|
||||
actions: {
|
||||
search: function(query) {
|
||||
this.transitionToRoute('index', {queryParams: {searchQuery: query, sort: query ? 'relevance' : 'recent'}});
|
||||
},
|
||||
alert: function(message) {
|
||||
this.get('alerts').pushObject(message);
|
||||
},
|
||||
dismissAlert: function(message) {
|
||||
this.get('alerts').removeObject(message);
|
||||
},
|
||||
clearAlerts: function() {
|
||||
this.get('alerts').clear();
|
||||
}
|
||||
}
|
||||
});
|
||||
|
@@ -3,6 +3,7 @@ import Ember from 'ember';
|
||||
import PostStream from '../models/post-stream';
|
||||
import ComposerReply from '../components/discussions/composer-reply';
|
||||
import ActionButton from '../components/ui/controls/action-button';
|
||||
import AlertMessage from '../components/alert-message';
|
||||
|
||||
export default Ember.ObjectController.extend(Ember.Evented, {
|
||||
|
||||
@@ -40,35 +41,62 @@ export default Ember.ObjectController.extend(Ember.Evented, {
|
||||
});
|
||||
},
|
||||
|
||||
// Save a reply. This may be called by a composer-reply component that was
|
||||
// set up on a different discussion, so we require a discussion model to
|
||||
// be explicitly passed rather than using the controller's implicit one.
|
||||
saveReply: function(discussion, content) {
|
||||
var controller = this;
|
||||
var composer = this.get('controllers.composer');
|
||||
var stream = this.get('stream');
|
||||
|
||||
composer.set('content.loading', true);
|
||||
controller.get('controllers.application').send('clearAlerts');
|
||||
|
||||
var post = this.store.createRecord('post', {
|
||||
content: content,
|
||||
discussion: discussion
|
||||
});
|
||||
|
||||
var promise = post.save().then(function(post) {
|
||||
if (discussion == controller.get('model')) {
|
||||
discussion.set('posts', discussion.get('posts')+','+post.get('id'));
|
||||
stream.set('ids', controller.get('model.postIds'));
|
||||
stream.addPostToEnd(post);
|
||||
}
|
||||
return post.save().then(function(post) {
|
||||
composer.send('hide');
|
||||
}, function(reason) {
|
||||
var error = reason.errors[0].detail;
|
||||
alert(error);
|
||||
});
|
||||
|
||||
promise.finally(function() {
|
||||
// If we're currently viewing the discussion which this reply was
|
||||
// made in, then we can add the post to the end of the post
|
||||
// stream.
|
||||
discussion.set('posts', discussion.get('posts')+','+post.get('id'));
|
||||
if (discussion == controller.get('model')) {
|
||||
stream.set('ids', discussion.get('postIds'));
|
||||
stream.addPostToEnd(post);
|
||||
} else {
|
||||
// Otherwise, we'll create an alert message to inform the user
|
||||
// that their reply has been posted, containing a button which
|
||||
// will transition to their new post when clicked.
|
||||
var message = AlertMessage.create({
|
||||
type: 'success',
|
||||
message: 'Your reply was posted.'
|
||||
});
|
||||
message.on('populateControls', function(controls) {
|
||||
controls.pushObjectWithTag(ActionButton.extend({
|
||||
label: 'View',
|
||||
action: function() {
|
||||
controller.transitionToRoute('discussion', post.get('discussion'), {queryParams: {start: post.get('number')}});
|
||||
message.send('dismiss');
|
||||
}
|
||||
}), 'view');
|
||||
});
|
||||
controller.get('controllers.application').send('alert', message);
|
||||
}
|
||||
},
|
||||
function(reason) {
|
||||
var message = AlertMessage.create({
|
||||
type: 'warning',
|
||||
message: reason.errors[0].detail
|
||||
});
|
||||
controller.get('controllers.application').send('alert', message);
|
||||
})
|
||||
.finally(function() {
|
||||
composer.set('content.loading', false);
|
||||
});
|
||||
|
||||
return promise;
|
||||
},
|
||||
|
||||
actions: {
|
||||
@@ -76,15 +104,19 @@ export default Ember.ObjectController.extend(Ember.Evented, {
|
||||
var controller = this;
|
||||
var discussion = this.get('model');
|
||||
var composer = this.get('controllers.composer');
|
||||
|
||||
// If the composer is already set up for this discussion, then we
|
||||
// don't need to change its content - we can just show it.
|
||||
if (composer.get('content.discussion') != discussion) {
|
||||
composer.switchContent(ComposerReply.create({
|
||||
user: controller.get('session.user'),
|
||||
discussion: discussion,
|
||||
submit: function(value) {
|
||||
controller.saveReply(this.get('discussion'), value);
|
||||
controller.saveReply(discussion, value);
|
||||
}
|
||||
}));
|
||||
}
|
||||
|
||||
composer.send('show');
|
||||
},
|
||||
|
||||
|
Reference in New Issue
Block a user