diff --git a/app/assets/javascripts/discourse/controllers/edit_category_controller.js b/app/assets/javascripts/discourse/controllers/edit_category_controller.js index 557c8540f52..7dd2fbcac0d 100644 --- a/app/assets/javascripts/discourse/controllers/edit_category_controller.js +++ b/app/assets/javascripts/discourse/controllers/edit_category_controller.js @@ -96,7 +96,7 @@ Discourse.EditCategoryController = Discourse.ObjectController.extend(Discourse.M }.property(), showCategoryTopic: function() { - $('#discourse-modal').modal('hide'); + this.send('closeModal') Discourse.URL.routeTo(this.get('topic_url')); return false; }, @@ -123,7 +123,7 @@ Discourse.EditCategoryController = Discourse.ObjectController.extend(Discourse.M Discourse.SiteSetting.update('uncategorized_name', this.get('name')) ).then(function(result) { // success - $('#discourse-modal').modal('hide'); + categoryController.send('closeModal'); // We can't redirect to the uncategorized category on save because the slug // might have changed. Discourse.URL.redirectTo("/categories"); @@ -136,7 +136,7 @@ Discourse.EditCategoryController = Discourse.ObjectController.extend(Discourse.M } else { this.get('model').save().then(function(result) { // success - $('#discourse-modal').modal('hide'); + categoryController.send('closeModal'); Discourse.URL.redirectTo("/category/" + Discourse.Category.slugFor(result.category)); }, function(errors) { // errors @@ -150,11 +150,13 @@ Discourse.EditCategoryController = Discourse.ObjectController.extend(Discourse.M deleteCategory: function() { var categoryController = this; this.set('deleting', true); + $('#discourse-modal').modal('hide'); bootbox.confirm(Em.String.i18n("category.delete_confirm"), Em.String.i18n("no_value"), Em.String.i18n("yes_value"), function(result) { if (result) { categoryController.get('model').destroy().then(function(){ // success + categoryController.send('closeModal'); Discourse.URL.redirectTo("/categories"); }, function(jqXHR){ // error diff --git a/app/assets/javascripts/discourse/controllers/flag_controller.js b/app/assets/javascripts/discourse/controllers/flag_controller.js index 0d1d6c85fbd..3331287dc13 100644 --- a/app/assets/javascripts/discourse/controllers/flag_controller.js +++ b/app/assets/javascripts/discourse/controllers/flag_controller.js @@ -55,7 +55,7 @@ Discourse.FlagController = Discourse.ObjectController.extend(Discourse.ModalFunc if (opts) params = $.extend(params, opts); postAction.act(params).then(function() { - flagController.closeModal(); + flagController.send('closeModal'); }, function(errors) { flagController.displayErrors(errors); }); diff --git a/app/assets/javascripts/discourse/controllers/merge_topic_controller.js b/app/assets/javascripts/discourse/controllers/merge_topic_controller.js index 4f3e87e18e6..d7f78dd8f87 100644 --- a/app/assets/javascripts/discourse/controllers/merge_topic_controller.js +++ b/app/assets/javascripts/discourse/controllers/merge_topic_controller.js @@ -27,8 +27,6 @@ Discourse.MergeTopicController = Discourse.ObjectController.extend(Discourse.Sel movePostsToExistingTopic: function() { this.set('saving', true); - var moveSelectedView = this; - var promise = null; if (this.get('allPostsSelected')) { promise = Discourse.Topic.mergeTopic(this.get('id'), this.get('selectedTopicId')); @@ -40,15 +38,16 @@ Discourse.MergeTopicController = Discourse.ObjectController.extend(Discourse.Sel }); } + var mergeTopicController = this; promise.then(function(result) { // Posts moved - $('#discourse-modal').modal('hide'); - moveSelectedView.get('topicController').toggleMultiSelect(); + mergeTopicController.send('closeModal'); + mergeTopicController.get('topicController').toggleMultiSelect(); Em.run.next(function() { Discourse.URL.routeTo(result.url); }); }, function() { // Error moving posts - moveSelectedView.flash(Em.String.i18n('topic.merge_topic.error')); - moveSelectedView.set('saving', false); + mergeTopicController.flash(Em.String.i18n('topic.merge_topic.error')); + mergeTopicController.set('saving', false); }); return false; } diff --git a/app/assets/javascripts/discourse/controllers/modal_controller.js b/app/assets/javascripts/discourse/controllers/modal_controller.js index 9ad491122c0..d6cb21ef745 100644 --- a/app/assets/javascripts/discourse/controllers/modal_controller.js +++ b/app/assets/javascripts/discourse/controllers/modal_controller.js @@ -6,18 +6,6 @@ @namespace Discourse @module Discourse **/ -Discourse.ModalController = Discourse.Controller.extend({ - - /** - Close the modal. - - @method closeModal - **/ - closeModal: function() { - // Currently uses jQuery to hide it. - $('#discourse-modal').modal('hide'); - } - -}); +Discourse.ModalController = Discourse.Controller.extend({}); diff --git a/app/assets/javascripts/discourse/controllers/split_topic_controller.js b/app/assets/javascripts/discourse/controllers/split_topic_controller.js index d25b9150924..23224037868 100644 --- a/app/assets/javascripts/discourse/controllers/split_topic_controller.js +++ b/app/assets/javascripts/discourse/controllers/split_topic_controller.js @@ -28,20 +28,20 @@ Discourse.SplitTopicController = Discourse.ObjectController.extend(Discourse.Sel this.set('saving', true); var postIds = this.get('selectedPosts').map(function(p) { return p.get('id'); }); - var moveSelectedView = this; + var splitTopicController = this; Discourse.Topic.movePosts(this.get('id'), { title: this.get('topicName'), post_ids: postIds }).then(function(result) { // Posts moved - $('#discourse-modal').modal('hide'); - moveSelectedView.get('topicController').toggleMultiSelect(); + splitTopicController.send('closeModal'); + splitTopicController.get('topicController').toggleMultiSelect(); Em.run.next(function() { Discourse.URL.routeTo(result.url); }); }, function() { // Error moving posts - moveSelectedView.flash(Em.String.i18n('topic.split_topic.error')); - moveSelectedView.set('saving', false); + splitTopicController.flash(Em.String.i18n('topic.split_topic.error')); + splitTopicController.set('saving', false); }); return false; } diff --git a/app/assets/javascripts/discourse/mixins/modal_functionality.js b/app/assets/javascripts/discourse/mixins/modal_functionality.js index 7ddac0f78bd..acb22820496 100644 --- a/app/assets/javascripts/discourse/mixins/modal_functionality.js +++ b/app/assets/javascripts/discourse/mixins/modal_functionality.js @@ -21,16 +21,6 @@ Discourse.ModalFunctionality = Em.Mixin.create({ message: message, messageClass: messageClass })); - }, - - /** - Close the modal. - - @method closeModal - **/ - closeModal: function() { - // Currently uses jQuery to hide it. - this.get('controllers.modal').closeModal(); } }); diff --git a/app/assets/javascripts/discourse/routes/application_route.js b/app/assets/javascripts/discourse/routes/application_route.js index c47036de6bc..d09691ff985 100644 --- a/app/assets/javascripts/discourse/routes/application_route.js +++ b/app/assets/javascripts/discourse/routes/application_route.js @@ -34,6 +34,16 @@ Discourse.ApplicationRoute = Em.Route.extend({ }); }, + + /** + Close the current modal. + + @method closeModal + **/ + closeModal: function() { + this.render('hide_modal', {into: 'modal', outlet: 'modalBody'}); + }, + editCategory: function(category) { var router = this; diff --git a/app/assets/javascripts/discourse/templates/modal/merge_topic.js.handlebars b/app/assets/javascripts/discourse/templates/modal/merge_topic.js.handlebars index 137f6fa665a..2315325049c 100644 --- a/app/assets/javascripts/discourse/templates/modal/merge_topic.js.handlebars +++ b/app/assets/javascripts/discourse/templates/modal/merge_topic.js.handlebars @@ -5,6 +5,7 @@ {{/if}} + {{selectedPostsCount}}

{{{i18n topic.merge_topic.instructions count="selectedPostsCount"}}}

{{chooseTopic selectedTopicId=selectedTopicId}} diff --git a/app/assets/javascripts/discourse/views/composer_view.js b/app/assets/javascripts/discourse/views/composer_view.js index f83638c7686..18841c3061b 100644 --- a/app/assets/javascripts/discourse/views/composer_view.js +++ b/app/assets/javascripts/discourse/views/composer_view.js @@ -260,7 +260,7 @@ Discourse.ComposerView = Discourse.View.extend({ // send - this event is triggered when the upload request is about to start $uploadTarget.on('fileuploadsend', function (e, data) { // hide the "image selector" modal - $('#discourse-modal').modal('hide'); + _this.get('controller').send('closeModal'); // cf. https://github.com/blueimp/jQuery-File-Upload/wiki/API#how-to-cancel-an-upload var jqXHR = data.xhr(); // need to wait for the link to show up in the DOM diff --git a/app/assets/javascripts/discourse/views/modal/hide_modal_view.js b/app/assets/javascripts/discourse/views/modal/hide_modal_view.js new file mode 100644 index 00000000000..7844871cd33 --- /dev/null +++ b/app/assets/javascripts/discourse/views/modal/hide_modal_view.js @@ -0,0 +1,18 @@ +/** + An empty view for when we want to close a modal. + + @class HideModalView + @extends Discourse.ModalBodyView + @namespace Discourse + @module Discourse +**/ +Discourse.HideModalView = Discourse.ModalBodyView.extend({ + + // No rendering! + render: function(buffer) { }, + + didInsertElement: function() { + $('#discourse-modal').modal('hide'); + } + +}); \ No newline at end of file diff --git a/app/assets/javascripts/discourse/views/modal/image_selector_view.js b/app/assets/javascripts/discourse/views/modal/image_selector_view.js index 3607afd2609..fd190601013 100644 --- a/app/assets/javascripts/discourse/views/modal/image_selector_view.js +++ b/app/assets/javascripts/discourse/views/modal/image_selector_view.js @@ -17,7 +17,7 @@ Discourse.ImageSelectorView = Discourse.ModalBodyView.extend({ add: function() { this.get('controller.composerView').addMarkdown("![image](" + $('#fileurl-input').val() + ")"); - $('#discourse-modal').modal('hide'); + this.get('controller').send('closeModal'); } }); diff --git a/app/assets/javascripts/discourse/views/modal/modal_body_view.js b/app/assets/javascripts/discourse/views/modal/modal_body_view.js index 41a2789de36..ef66472d4a1 100644 --- a/app/assets/javascripts/discourse/views/modal/modal_body_view.js +++ b/app/assets/javascripts/discourse/views/modal/modal_body_view.js @@ -11,6 +11,12 @@ Discourse.ModalBodyView = Discourse.View.extend({ // Focus on first element didInsertElement: function() { $('#discourse-modal').modal('show'); + + var controller = this.get('controller'); + $('#discourse-modal').on('hide.discourse', function() { + controller.send('closeModal'); + }); + $('#modal-alert').hide(); var modalBodyView = this; @@ -24,6 +30,10 @@ Discourse.ModalBodyView = Discourse.View.extend({ } }, + willDestroyElement: function() { + $('#discourse-modal').off('hide.discourse'); + }, + // Pass the errors to our errors view displayErrors: function(errors, callback) { this.set('parentView.parentView.modalErrorsView.errors', errors);