FIX: Title changes didn't always apply. Cleaned up some ugly JS.

This commit is contained in:
Robin Ward 2013-03-13 18:37:45 -04:00
parent 7777b3f650
commit a8c44d90a3
1 changed files with 40 additions and 43 deletions

View File

@ -25,9 +25,8 @@ Discourse.Composer = Discourse.Model.extend({
archetypesBinding: 'Discourse.site.archetypes', archetypesBinding: 'Discourse.site.archetypes',
init: function() { init: function() {
var val;
this._super(); this._super();
val = Discourse.KeyValueStore.get('composer.showPreview') || 'true'; var val = Discourse.KeyValueStore.get('composer.showPreview') || 'true';
this.set('showPreview', val === 'true'); this.set('showPreview', val === 'true');
return this.set('archetypeId', Discourse.get('site.default_archetype')); return this.set('archetypeId', Discourse.get('site.default_archetype'));
}, },
@ -68,36 +67,33 @@ Discourse.Composer = Discourse.Model.extend({
togglePreview: function() { togglePreview: function() {
this.toggleProperty('showPreview'); this.toggleProperty('showPreview');
return Discourse.KeyValueStore.set({ return Discourse.KeyValueStore.set({ key: 'showPreview', value: this.get('showPreview') });
key: 'showPreview',
value: this.get('showPreview')
});
}, },
// Import a quote from the post // Import a quote from the post
importQuote: function() { importQuote: function() {
var post, posts, var post = this.get('post');
_this = this;
post = this.get('post'); // If we don't have a post, check the topic for the first one
if (!post) { if (!post) {
posts = this.get('topic.posts'); var posts = this.get('topic.posts');
if (posts && posts.length > 0) { if (posts && posts.length > 0) {
post = posts[0]; post = posts[0];
} }
} }
if (post) { if (post) {
this.set('loading', true); this.set('loading', true);
var composer = this;
return Discourse.Post.load(post.get('id'), function(result) { return Discourse.Post.load(post.get('id'), function(result) {
var quotedText; composer.appendText(Discourse.BBCode.buildQuoteBBCode(post, result.get('raw')));
quotedText = Discourse.BBCode.buildQuoteBBCode(post, result.get('raw')); return composer.set('loading', false);
_this.appendText(quotedText);
return _this.set('loading', false);
}); });
} }
}, },
appendText: function(text) { appendText: function(text) {
return this.set('reply', (this.get('reply') || '') + text); this.set('reply', (this.get('reply') || '') + text);
}, },
// Determine the appropriate title for this action // Determine the appropriate title for this action
@ -211,22 +207,22 @@ Discourse.Composer = Discourse.Model.extend({
quote - If we're opening a reply from a quote, the quote we're making quote - If we're opening a reply from a quote, the quote we're making
*/ */
open: function(opts) { open: function(opts) {
var replyBlank, topicId, var topicId;
_this = this;
if (!opts) opts = {}; if (!opts) opts = {};
this.set('loading', false); this.set('loading', false);
if (opts.topic) { if (opts.topic) {
topicId = opts.topic.get('id'); topicId = opts.topic.get('id');
} }
replyBlank = (this.get("reply") || "") === "";
var replyBlank = (this.get("reply") || "") === "";
var composer = this;
if (!replyBlank && if (!replyBlank &&
(opts.action !== this.get('action') || ((opts.reply || opts.action === this.EDIT) && this.get('reply') !== this.get('originalText'))) && (opts.action !== this.get('action') || ((opts.reply || opts.action === this.EDIT) && this.get('reply') !== this.get('originalText'))) &&
!opts.tested) { !opts.tested) {
opts.tested = true; opts.tested = true;
this.cancel(function() { this.cancel(function() {
return _this.open(opts); return composer.open(opts);
}); });
return; return;
} }
@ -254,8 +250,8 @@ Discourse.Composer = Discourse.Model.extend({
if (opts.postId) { if (opts.postId) {
this.set('loading', true); this.set('loading', true);
Discourse.Post.load(opts.postId, function(result) { Discourse.Post.load(opts.postId, function(result) {
_this.set('post', result); composer.set('post', result);
return _this.set('loading', false); return composer.set('loading', false);
}); });
} }
@ -264,9 +260,9 @@ Discourse.Composer = Discourse.Model.extend({
this.set('title', this.get('topic.title')); this.set('title', this.get('topic.title'));
this.set('loading', true); this.set('loading', true);
Discourse.Post.load(opts.post.get('id'), function(result) { Discourse.Post.load(opts.post.get('id'), function(result) {
_this.set('reply', result.get('raw')); composer.set('reply', result.get('raw'));
_this.set('originalText', _this.get('reply')); composer.set('originalText', composer.get('reply'));
return _this.set('loading', false); composer.set('loading', false);
}); });
} }
@ -289,27 +285,30 @@ Discourse.Composer = Discourse.Model.extend({
// When you edit a post // When you edit a post
editPost: function(opts) { editPost: function(opts) {
var oldCooked, post, promise, topic, var promise = new RSVP.Promise();
_this = this; var post = this.get('post');
promise = new RSVP.Promise(); var oldCooked = post.get('cooked');
post = this.get('post'); var composer = this;
oldCooked = post.get('cooked');
// Update the title if we've changed it // Update the title if we've changed it
if (this.get('title') && post.get('post_number') === 1) { if (this.get('title') && post.get('post_number') === 1) {
topic = this.get('topic'); var topic = this.get('topic');
topic.set('title', this.get('title')); topic.set('title', this.get('title'));
topic.set('fancy_title', this.get('title'));
topic.set('categoryName', this.get('categoryName')); topic.set('categoryName', this.get('categoryName'));
topic.save(); topic.save();
} }
post.set('raw', this.get('reply')); post.set('raw', this.get('reply'));
post.set('imageSizes', opts.imageSizes); post.set('imageSizes', opts.imageSizes);
post.set('cooked', $('#wmd-preview').html()); post.set('cooked', $('#wmd-preview').html());
this.set('composeState', CLOSED); this.set('composeState', CLOSED);
post.save(function(savedPost) { post.save(function(savedPost) {
var idx, postNumber, posts; var idx, postNumber;
posts = _this.get('topic.posts'); var posts = composer.get('topic.posts');
// perhaps our post came from elsewhere eg. draft // perhaps our post came from elsewhere eg. draft
idx = -1; idx = -1;
@ -320,19 +319,17 @@ Discourse.Composer = Discourse.Model.extend({
} }
}); });
if (idx > -1) { if (idx > -1) {
savedPost.set('topic', _this.get('topic')); savedPost.set('topic', composer.get('topic'));
posts.replace(idx, 1, [savedPost]); posts.replace(idx, 1, [savedPost]);
promise.resolve({ promise.resolve({ post: post });
post: post composer.set('topic.draft_sequence', savedPost.draft_sequence);
});
_this.set('topic.draft_sequence', savedPost.draft_sequence);
} }
}, function(error) { }, function(error) {
var errors; var errors;
errors = $.parseJSON(error.responseText).errors; errors = $.parseJSON(error.responseText).errors;
promise.reject(errors[0]); promise.reject(errors[0]);
post.set('cooked', oldCooked); post.set('cooked', oldCooked);
return _this.set('composeState', OPEN); return composer.set('composeState', OPEN);
}); });
return promise; return promise;
}, },
@ -439,13 +436,11 @@ Discourse.Composer = Discourse.Model.extend({
}, },
saveDraft: function() { saveDraft: function() {
var data,
_this = this;
if (this.get('disableDrafts')) return; if (this.get('disableDrafts')) return;
if (!this.get('reply')) return; if (!this.get('reply')) return;
if (this.get('replyLength') < Discourse.SiteSettings.min_post_length) return; if (this.get('replyLength') < Discourse.SiteSettings.min_post_length) return;
data = { var data = {
reply: this.get('reply'), reply: this.get('reply'),
action: this.get('action'), action: this.get('action'),
title: this.get('title'), title: this.get('title'),
@ -457,10 +452,12 @@ Discourse.Composer = Discourse.Model.extend({
}; };
this.set('draftStatus', Em.String.i18n('composer.saving_draft_tip')); this.set('draftStatus', Em.String.i18n('composer.saving_draft_tip'));
var composer = this;
return Discourse.Draft.save(this.get('draftKey'), this.get('draftSequence'), data).then((function() { return Discourse.Draft.save(this.get('draftKey'), this.get('draftSequence'), data).then((function() {
_this.set('draftStatus', Em.String.i18n('composer.saved_draft_tip')); composer.set('draftStatus', Em.String.i18n('composer.saved_draft_tip'));
}), (function() { }), (function() {
_this.set('draftStatus', 'drafts offline'); composer.set('draftStatus', 'drafts offline');
})); }));
}, },