88**/
99Discourse . ComposerController = Discourse . Controller . extend ( {
1010 needs : [ 'modal' , 'topic' ] ,
11- hasReply : false ,
1211
1312 togglePreview : function ( ) {
14- return this . get ( 'content' ) . togglePreview ( ) ;
13+ this . get ( 'content' ) . togglePreview ( ) ;
1514 } ,
1615
1716 // Import a quote from the post
1817 importQuote : function ( ) {
19- return this . get ( 'content' ) . importQuote ( ) ;
18+ this . get ( 'content' ) . importQuote ( ) ;
2019 } ,
2120
2221 appendText : function ( text ) {
23- var c ;
24- c = this . get ( 'content' ) ;
22+ var c = this . get ( 'content' ) ;
2523 if ( c ) {
26- return c . appendText ( text ) ;
24+ c . appendText ( text ) ;
2725 }
2826 } ,
2927
@@ -72,11 +70,10 @@ Discourse.ComposerController = Discourse.Controller.extend({
7270 }
7371
7472 bootbox . dialog ( message , buttons ) ;
75-
7673 return ;
7774 }
7875 }
79-
76+
8077 return composer . save ( {
8178 imageSizes : this . get ( 'view' ) . imageSizes ( )
8279 } ) . then ( function ( opts ) {
@@ -94,17 +91,80 @@ Discourse.ComposerController = Discourse.Controller.extend({
9491 } ) ;
9592 } ,
9693
97- checkReplyLength : function ( ) {
98- if ( this . present ( 'content.reply' ) ) {
99- this . set ( 'hasReply' , true ) ;
100- } else {
101- this . set ( 'hasReply' , false ) ;
94+ closeEducation : function ( ) {
95+ this . set ( 'educationClosed' , true ) ;
96+ } ,
97+
98+ closeSimilar : function ( ) {
99+ this . set ( 'similarClosed' , true ) ;
100+ } ,
101+
102+ similarVisible : function ( ) {
103+ if ( this . get ( 'similarClosed' ) ) return false ;
104+ if ( this . get ( 'content.composeState' ) !== Discourse . Composer . OPEN ) return false ;
105+ return ( this . get ( 'similarTopics.length' ) || 0 ) > 0 ;
106+ } . property ( 'similarTopics.length' , 'similarClosed' , 'content.composeState' ) ,
107+
108+ newUserEducationVisible : function ( ) {
109+ if ( ! this . get ( 'educationContents' ) ) return false ;
110+ if ( this . get ( 'content.composeState' ) !== Discourse . Composer . OPEN ) return false ;
111+ if ( ! this . present ( 'content.reply' ) ) return false ;
112+ if ( this . get ( 'educationClosed' ) ) return false ;
113+ return true ;
114+ } . property ( 'content.composeState' , 'content.reply' , 'educationClosed' , 'educationContents' ) ,
115+
116+ fetchNewUserEducation : function ( ) {
117+ // If creating a topic, use topic_count, otherwise post_count
118+ var count = this . get ( 'content.creatingTopic' ) ? Discourse . get ( 'currentUser.topic_count' ) : Discourse . get ( 'currentUser.reply_count' ) ;
119+ if ( count >= Discourse . SiteSettings . educate_until_posts ) {
120+ this . set ( 'educationClosed' , true ) ;
121+ this . set ( 'educationContents' , '' ) ;
122+ return ;
102123 }
124+
125+ // The user must have typed a reply
126+ if ( ! this . get ( 'typedReply' ) ) return ;
127+
128+ this . set ( 'educationClosed' , false ) ;
129+
130+ // If visible update the text
131+ var educationKey = this . get ( 'content.creatingTopic' ) ? 'new-topic' : 'new-reply' ;
132+ var composerController = this ;
133+ $ . get ( "/education/" + educationKey ) . then ( function ( result ) {
134+ composerController . set ( 'educationContents' , result ) ;
135+ } ) ;
136+ } . observes ( 'typedReply' , 'content.creatingTopic' , 'Discourse.currentUser.reply_count' ) ,
137+
138+ checkReplyLength : function ( ) {
139+ this . set ( 'typedReply' , this . present ( 'content.reply' ) ) ;
140+ } ,
141+
142+ /**
143+ Fired after a user stops typing. Considers whether to check for similar
144+ topics based on the current composer state.
145+
146+ @method findSimilarTopics
147+ **/
148+ findSimilarTopics : function ( ) {
149+
150+ // We don't care about similar topics unless creating a topic
151+ if ( ! this . get ( 'content.creatingTopic' ) ) return ;
152+
153+ var body = this . get ( 'content.reply' ) ;
154+ var title = this . get ( 'content.title' ) ;
155+
156+ // Ensure the fields are of the minimum length
157+ if ( body . length < Discourse . SiteSettings . min_body_similar_length ) return ;
158+ if ( title . length < Discourse . SiteSettings . min_title_similar_length ) return ;
159+
160+ var composerController = this ;
161+ Discourse . Topic . findSimilarTo ( title , body ) . then ( function ( topics ) {
162+ composerController . set ( 'similarTopics' , topics ) ;
163+ } ) ;
103164 } ,
104165
105166 saveDraft : function ( ) {
106- var model ;
107- model = this . get ( 'content' ) ;
167+ var model = this . get ( 'content' ) ;
108168 if ( model ) model . saveDraft ( ) ;
109169 } ,
110170
@@ -123,8 +183,11 @@ Discourse.ComposerController = Discourse.Controller.extend({
123183 _this = this ;
124184 if ( ! opts ) opts = { } ;
125185
126- opts . promise = promise = opts . promise || new RSVP . Promise ( ) ;
127- this . set ( 'hasReply' , false ) ;
186+ opts . promise = promise = opts . promise || Ember . Deferred . create ( ) ;
187+ this . set ( 'typedReply' , false ) ;
188+ this . set ( 'similarTopics' , null ) ;
189+ this . set ( 'similarClosed' , false ) ;
190+
128191 if ( ! opts . draftKey ) {
129192 alert ( "composer was opened without a draft key" ) ;
130193 throw "composer opened without a proper draft key" ;
@@ -133,9 +196,7 @@ Discourse.ComposerController = Discourse.Controller.extend({
133196 // ensure we have a view now, without it transitions are going to be messed
134197 view = this . get ( 'view' ) ;
135198 if ( ! view ) {
136- view = Discourse . ComposerView . create ( {
137- controller : this
138- } ) ;
199+ view = Discourse . ComposerView . create ( { controller : this } ) ;
139200 view . appendTo ( $ ( '#main' ) ) ;
140201 this . set ( 'view' , view ) ;
141202 // the next runloop is too soon, need to get the control rendered and then
@@ -197,8 +258,7 @@ Discourse.ComposerController = Discourse.Controller.extend({
197258 } ,
198259
199260 wouldLoseChanges : function ( ) {
200- var composer ;
201- composer = this . get ( 'content' ) ;
261+ var composer = this . get ( 'content' ) ;
202262 return composer && composer . wouldLoseChanges ( ) ;
203263 } ,
204264
@@ -210,10 +270,9 @@ Discourse.ComposerController = Discourse.Controller.extend({
210270 } ,
211271
212272 destroyDraft : function ( ) {
213- var key ;
214- key = this . get ( 'content.draftKey' ) ;
273+ var key = this . get ( 'content.draftKey' ) ;
215274 if ( key ) {
216- return Discourse . Draft . clear ( key , this . get ( 'content.draftSequence' ) ) ;
275+ Discourse . Draft . clear ( key , this . get ( 'content.draftSequence' ) ) ;
217276 }
218277 } ,
219278
@@ -243,17 +302,17 @@ Discourse.ComposerController = Discourse.Controller.extend({
243302 }
244303 } ,
245304
246- click : function ( ) {
305+ openIfDraft : function ( ) {
247306 if ( this . get ( 'content.composeState' ) === Discourse . Composer . DRAFT ) {
248- return this . set ( 'content.composeState' , Discourse . Composer . OPEN ) ;
307+ this . set ( 'content.composeState' , Discourse . Composer . OPEN ) ;
249308 }
250309 } ,
251310
252311 shrink : function ( ) {
253312 if ( this . get ( 'content.reply' ) === this . get ( 'content.originalText' ) ) {
254- return this . close ( ) ;
313+ this . close ( ) ;
255314 } else {
256- return this . collapse ( ) ;
315+ this . collapse ( ) ;
257316 }
258317 } ,
259318
0 commit comments