Fix: Multi-Select should close when an operation succeeds. A little refactoring, too.

This commit is contained in:
Robin Ward 2013-05-08 11:46:43 -04:00
parent 2ba08d23cb
commit 0d8c962fdf
6 changed files with 40 additions and 50 deletions

View File

@ -1,7 +1,7 @@
/**
This controller supports actions related to showing modals
@class ModalController
@class ModalController
@extends Discourse.Controller
@namespace Discourse
@module Discourse

View File

@ -83,20 +83,20 @@ Discourse.TopicController = Discourse.ObjectController.extend({
if (!modalController) return;
modalController.show(Discourse.MoveSelectedView.create({
topicController: this,
topic: this.get('content'),
selectedPosts: this.get('selectedPosts')
selectedPosts: this.get('selectedPosts'),
}));
},
deleteSelected: function() {
var topicController = this;
return bootbox.confirm(Em.String.i18n("post.delete.confirm", {
count: this.get('selectedCount')
}), function(result) {
return bootbox.confirm(Em.String.i18n("post.delete.confirm", { count: this.get('selectedCount')}), function(result) {
if (result) {
var selectedPosts = topicController.get('selectedPosts');
Discourse.Post.deleteMany(selectedPosts);
topicController.get('content.posts').removeObjects(selectedPosts);
topicController.toggleMultiSelect();
}
});
},

View File

@ -451,10 +451,15 @@ Discourse.Topic.reopenClass({
// Create a topic from posts
movePosts: function(topicId, title, postIds) {
return Discourse.ajax("/t/" + topicId + "/move-posts", {
var promise = Discourse.ajax("/t/" + topicId + "/move-posts", {
type: 'POST',
data: { title: title, post_ids: postIds }
}).then(function (result) {
if (result.success) return result;
promise.reject();
});
return promise;
},
create: function(obj, topicView) {

View File

@ -10,10 +10,8 @@ Discourse.ModalBodyView = Discourse.View.extend({
// Focus on first element
didInsertElement: function() {
var _this = this;
Em.run.next(function() {
_this.$('form input:first').focus();
});
var modalBodyView = this;
Em.run.next(function() { modalBodyView.$('form input:first').focus(); });
},
// Pass the errors to our errors view
@ -25,9 +23,8 @@ Discourse.ModalBodyView = Discourse.View.extend({
// Just use jQuery to show an alert. We don't need anythign fancier for now
// like an actual ember view
flash: function(msg, flashClass) {
var $alert;
if (!flashClass) flashClass = "success";
$alert = $('#modal-alert').hide().removeClass('alert-error', 'alert-success');
var $alert = $('#modal-alert').hide().removeClass('alert-error', 'alert-success');
$alert.addClass("alert alert-" + flashClass).html(msg);
$alert.fadeIn();
}

View File

@ -16,26 +16,19 @@ Discourse.ModalView = Ember.ContainerView.extend({
templateName: 'modal/modal_header',
titleBinding: 'controller.currentView.title'
}),
modalBodyView: Ember.ContainerView.create({ currentViewBinding: 'controller.currentView' }),
modalErrorsView: Ember.View.create({ templateName: 'modal/modal_errors' }),
modalBodyView: Ember.ContainerView.create({
currentViewBinding: 'controller.currentView'
}),
modalErrorsView: Ember.View.create({
templateName: 'modal/modal_errors'
}),
viewChanged: (function() {
var view,
_this = this;
viewChanged: function() {
this.set('modalErrorsView.errors', null);
if (view = this.get('controller.currentView')) {
var view = this.get('controller.currentView');
var modalView = this;
if (view) {
$('#modal-alert').hide();
return Em.run.next(function() {
return _this.$().modal('show');
});
Em.run.next(function() { modalView.$().modal('show'); });
}
}).observes('controller.currentView')
}.observes('controller.currentView')
});

View File

@ -11,41 +11,36 @@ Discourse.MoveSelectedView = Discourse.ModalBodyView.extend({
title: Em.String.i18n('topic.move_selected.title'),
saving: false,
selectedCount: (function() {
selectedCount: function() {
if (!this.get('selectedPosts')) return 0;
return this.get('selectedPosts').length;
}).property('selectedPosts'),
}.property('selectedPosts'),
buttonDisabled: (function() {
buttonDisabled: function() {
if (this.get('saving')) return true;
return this.blank('topicName');
}).property('saving', 'topicName'),
}.property('saving', 'topicName'),
buttonTitle: (function() {
buttonTitle: function() {
if (this.get('saving')) return Em.String.i18n('saving');
return Em.String.i18n('topic.move_selected.title');
}).property('saving'),
}.property('saving'),
movePosts: function() {
var postIds,
_this = this;
this.set('saving', true);
postIds = this.get('selectedPosts').map(function(p) {
return p.get('id');
});
var postIds = this.get('selectedPosts').map(function(p) { return p.get('id'); });
var moveSelectedView = this;
Discourse.Topic.movePosts(this.get('topic.id'), this.get('topicName'), postIds).then(function(result) {
if (result.success) {
$('#discourse-modal').modal('hide');
Em.run.next(function() {
Discourse.URL.routeTo(result.url);
});
} else {
_this.flash(Em.String.i18n('topic.move_selected.error'));
return _this.set('saving', false);
}
// Posts moved
$('#discourse-modal').modal('hide');
moveSelectedView.get('topicController').toggleMultiSelect();
Em.run.next(function() { Discourse.URL.routeTo(result.url); });
}, function() {
_this.flash(Em.String.i18n('topic.move_selected.error'));
return _this.set('saving', false);
// Error moving posts
moveSelectedView.flash(Em.String.i18n('topic.move_selected.error'));
moveSelectedView.set('saving', false);
});
return false;
}