Skip to content
This repository was archived by the owner on Apr 3, 2021. It is now read-only.

Commit ac56b11

Browse files
committed
refactor actions, try keeping them short so indenting does not confuse us
1 parent 89c25d3 commit ac56b11

File tree

1 file changed

+101
-95
lines changed

1 file changed

+101
-95
lines changed

app/assets/javascripts/discourse/controllers/composer_controller.js

Lines changed: 101 additions & 95 deletions
Original file line numberDiff line numberDiff line change
@@ -17,42 +17,12 @@ Discourse.ComposerController = Discourse.Controller.extend({
1717
this.set('similarTopics', Em.A());
1818
},
1919

20-
updateDraftStatus: function() {
21-
this.get('model').updateDraftStatus();
22-
},
23-
24-
appendText: function(text) {
25-
var c = this.get('model');
26-
if (c) { c.appendText(text); }
27-
},
28-
29-
categories: function() {
30-
return Discourse.Category.list();
31-
}.property(),
32-
3320
actions: {
34-
3521
// Toggle the reply view
3622
toggle: function() {
37-
this.closeAutocomplete();
38-
switch (this.get('model.composeState')) {
39-
case Discourse.Composer.OPEN:
40-
if (this.blank('model.reply') && this.blank('model.title')) {
41-
this.close();
42-
} else {
43-
this.shrink();
44-
}
45-
break;
46-
case Discourse.Composer.DRAFT:
47-
this.set('model.composeState', Discourse.Composer.OPEN);
48-
break;
49-
case Discourse.Composer.SAVING:
50-
this.close();
51-
}
52-
return false;
23+
this.toggle();
5324
},
5425

55-
5626
togglePreview: function() {
5727
this.get('model').togglePreview();
5828
},
@@ -66,86 +36,122 @@ Discourse.ComposerController = Discourse.Controller.extend({
6636
this.cancelComposer();
6737
},
6838

69-
save: function(force) {
70-
var composer = this.get('model'),
71-
composerController = this;
39+
save: function() {
40+
this.save();
41+
}
42+
},
7243

73-
if( composer.get('cantSubmitPost') ) {
74-
this.set('view.showTitleTip', Date.now());
75-
this.set('view.showCategoryTip', Date.now());
76-
this.set('view.showReplyTip', Date.now());
77-
return;
78-
}
44+
updateDraftStatus: function() {
45+
this.get('model').updateDraftStatus();
46+
},
7947

80-
composer.set('disableDrafts', true);
81-
82-
// for now handle a very narrow use case
83-
// if we are replying to a topic AND not on the topic pop the window up
84-
if(!force && composer.get('replyingToTopic')) {
85-
var topic = this.get('topic');
86-
if (!topic || topic.get('id') !== composer.get('topic.id'))
87-
{
88-
var message = I18n.t("composer.posting_not_on_topic", {title: this.get('model.topic.title')});
89-
90-
var buttons = [{
91-
"label": I18n.t("composer.cancel"),
92-
"class": "cancel",
93-
"link": true
94-
}];
95-
96-
if(topic) {
97-
buttons.push({
98-
"label": I18n.t("composer.reply_here") + "<br/><div class='topic-title overflow-ellipsis'>" + topic.get('title') + "</div>",
99-
"class": "btn btn-reply-here",
100-
"callback": function(){
101-
composer.set('topic', topic);
102-
composer.set('post', null);
103-
composerController.save(true);
104-
}
105-
});
106-
}
48+
appendText: function(text) {
49+
var c = this.get('model');
50+
if (c) { c.appendText(text); }
51+
},
52+
53+
categories: function() {
54+
return Discourse.Category.list();
55+
}.property(),
56+
57+
58+
toggle: function() {
59+
this.closeAutocomplete();
60+
switch (this.get('model.composeState')) {
61+
case Discourse.Composer.OPEN:
62+
if (this.blank('model.reply') && this.blank('model.title')) {
63+
this.close();
64+
} else {
65+
this.shrink();
66+
}
67+
break;
68+
case Discourse.Composer.DRAFT:
69+
this.set('model.composeState', Discourse.Composer.OPEN);
70+
break;
71+
case Discourse.Composer.SAVING:
72+
this.close();
73+
}
74+
return false;
75+
},
10776

77+
save: function(force) {
78+
var composer = this.get('model'),
79+
composerController = this;
80+
81+
if( composer.get('cantSubmitPost') ) {
82+
this.set('view.showTitleTip', Date.now());
83+
this.set('view.showCategoryTip', Date.now());
84+
this.set('view.showReplyTip', Date.now());
85+
return;
86+
}
87+
88+
composer.set('disableDrafts', true);
89+
90+
// for now handle a very narrow use case
91+
// if we are replying to a topic AND not on the topic pop the window up
92+
if(!force && composer.get('replyingToTopic')) {
93+
var topic = this.get('topic');
94+
if (!topic || topic.get('id') !== composer.get('topic.id'))
95+
{
96+
var message = I18n.t("composer.posting_not_on_topic", {title: this.get('model.topic.title')});
97+
98+
var buttons = [{
99+
"label": I18n.t("composer.cancel"),
100+
"class": "cancel",
101+
"link": true
102+
}];
103+
104+
if(topic) {
108105
buttons.push({
109-
"label": I18n.t("composer.reply_original") + "<br/><div class='topic-title overflow-ellipsis'>" + this.get('model.topic.title') + "</div>",
110-
"class": "btn-primary btn-reply-on-original",
106+
"label": I18n.t("composer.reply_here") + "<br/><div class='topic-title overflow-ellipsis'>" + topic.get('title') + "</div>",
107+
"class": "btn btn-reply-here",
111108
"callback": function(){
112-
composerController._actions.save.apply(composerController, [true]);
109+
composer.set('topic', topic);
110+
composer.set('post', null);
111+
composerController.save(true);
113112
}
114113
});
115-
116-
bootbox.dialog(message, buttons, {"classes": "reply-where-modal"});
117-
return;
118114
}
115+
116+
buttons.push({
117+
"label": I18n.t("composer.reply_original") + "<br/><div class='topic-title overflow-ellipsis'>" + this.get('model.topic.title') + "</div>",
118+
"class": "btn-primary btn-reply-on-original",
119+
"callback": function(){
120+
composerController.save(true);
121+
}
122+
});
123+
124+
bootbox.dialog(message, buttons, {"classes": "reply-where-modal"});
125+
return;
119126
}
127+
}
120128

121-
return composer.save({
122-
imageSizes: this.get('view').imageSizes()
123-
}).then(function(opts) {
129+
return composer.save({
130+
imageSizes: this.get('view').imageSizes()
131+
}).then(function(opts) {
124132

125-
// If we replied as a new topic successfully, remove the draft.
126-
if (composerController.get('replyAsNewTopicDraft')) {
127-
composerController.destroyDraft();
128-
}
133+
// If we replied as a new topic successfully, remove the draft.
134+
if (composerController.get('replyAsNewTopicDraft')) {
135+
composerController.destroyDraft();
136+
}
129137

130-
opts = opts || {};
131-
composerController.close();
138+
opts = opts || {};
139+
composerController.close();
132140

133-
var currentUser = Discourse.User.current();
134-
if (composer.get('creatingTopic')) {
135-
currentUser.set('topic_count', currentUser.get('topic_count') + 1);
136-
} else {
137-
currentUser.set('reply_count', currentUser.get('reply_count') + 1);
138-
}
139-
Discourse.URL.routeTo(opts.post.get('url'));
141+
var currentUser = Discourse.User.current();
142+
if (composer.get('creatingTopic')) {
143+
currentUser.set('topic_count', currentUser.get('topic_count') + 1);
144+
} else {
145+
currentUser.set('reply_count', currentUser.get('reply_count') + 1);
146+
}
147+
Discourse.URL.routeTo(opts.post.get('url'));
140148

141-
}, function(error) {
142-
composer.set('disableDrafts', false);
143-
bootbox.alert(error);
144-
});
145-
}
149+
}, function(error) {
150+
composer.set('disableDrafts', false);
151+
bootbox.alert(error);
152+
});
146153
},
147154

148-
149155
/**
150156
Checks to see if a reply has been typed. This is signaled by a keyUp
151157
event in a view.

0 commit comments

Comments
 (0)