some ES6 refactoring

This commit is contained in:
Régis Hanol 2015-02-26 11:07:21 +01:00
parent 8772643e63
commit fc6e7f5667
4 changed files with 97 additions and 119 deletions

View File

@ -5,50 +5,31 @@ export default Ember.ArrayController.extend({
// Whether we've checked our messages
checkedMessages: false,
/**
Initialize the controller
**/
init: function() {
init() {
this._super();
this.reset();
},
actions: {
/**
Closes and hides a message.
@method closeMessage
@params {Object} message The message to dismiss
**/
closeMessage: function(message) {
closeMessage(message) {
this.removeObject(message);
},
hideMessage: function(message) {
var messagesByTemplate = this.get('messagesByTemplate'),
templateName = message.get('templateName');
// kind of hacky but the visibility depends on this
messagesByTemplate[templateName] = undefined;
hideMessage(message) {
this.removeObject(message);
}
},
// kind of hacky but the visibility depends on this
this.get('messagesByTemplate')[message.get('templateName')] = undefined;
},
/**
Displays a new message
popup(message) {
let messagesByTemplate = this.get('messagesByTemplate');
const templateName = message.get('templateName');
@method popup
@params {Object} msg The message to display
**/
popup: function(msg) {
var messagesByTemplate = this.get('messagesByTemplate'),
templateName = msg.get('templateName'),
existing = messagesByTemplate[templateName];
if (!existing) {
this.pushObject(msg);
messagesByTemplate[templateName] = msg;
}
if (!messagesByTemplate[templateName]) {
this.pushObject(message);
messagesByTemplate[templateName] = message;
}
},
},
/**
@ -56,11 +37,13 @@ export default Ember.ArrayController.extend({
@method reset
**/
reset: function() {
reset() {
this.clear();
this.set('messagesByTemplate', {});
this.set('queuedForTyping', []);
this.set('checkedMessages', false);
this.setProperties({
messagesByTemplate: {},
queuedForTyping: [],
checkedMessages: false
});
},
/**
@ -69,11 +52,8 @@ export default Ember.ArrayController.extend({
@method typedReply
**/
typedReply: function() {
var self = this;
this.get('queuedForTyping').forEach(function (msg) {
self.popup(msg);
});
typedReply() {
this.get('queuedForTyping').forEach(msg => this.popup(msg));
},
/**
@ -82,11 +62,11 @@ export default Ember.ArrayController.extend({
@method queryFor
@params {Discourse.Composer} composer The composer model
**/
queryFor: function(composer) {
queryFor(composer) {
if (this.get('checkedMessages')) { return; }
var self = this,
queuedForTyping = self.get('queuedForTyping');
const self = this;
let queuedForTyping = self.get('queuedForTyping');
Discourse.ComposerMessage.find(composer).then(function (messages) {
self.set('checkedMessages', true);

View File

@ -29,18 +29,18 @@ export default DiscourseController.extend({
actions: {
// Toggle the reply view
toggle: function() {
toggle() {
this.toggle();
},
togglePreview: function() {
togglePreview() {
this.get('model').togglePreview();
},
// Import a quote from the post
importQuote: function() {
var postStream = this.get('topic.postStream'),
postId = this.get('model.post.id');
importQuote() {
const postStream = this.get('topic.postStream');
let postId = this.get('model.post.id');
// If there is no current post, use the first post id from the stream
if (!postId && postStream) {
@ -49,9 +49,9 @@ export default DiscourseController.extend({
// If we're editing a post, fetch the reply when importing a quote
if (this.get('model.editingPost')) {
var replyToPostNumber = this.get('model.post.reply_to_post_number');
const replyToPostNumber = this.get('model.post.reply_to_post_number');
if (replyToPostNumber) {
var replyPost = postStream.get('posts').findBy('post_number', replyToPostNumber);
const replyPost = postStream.get('posts').findBy('post_number', replyToPostNumber);
if (replyPost) {
postId = replyPost.get('id');
}
@ -60,34 +60,34 @@ export default DiscourseController.extend({
if (postId) {
this.set('model.loading', true);
var composer = this;
const composer = this;
return Discourse.Post.load(postId).then(function(post) {
var quote = Discourse.Quote.build(post, post.get("raw"));
const quote = Discourse.Quote.build(post, post.get("raw"));
composer.appendBlockAtCursor(quote);
composer.set('model.loading', false);
});
}
},
cancel: function() {
cancel() {
this.cancelComposer();
},
save: function() {
save() {
this.save();
},
displayEditReason: function() {
displayEditReason() {
this.set("showEditReason", true);
},
hitEsc: function() {
hitEsc() {
if (this.get('model.viewOpen')) {
this.shrink();
}
},
openIfDraft: function() {
openIfDraft() {
if (this.get('model.viewDraft')) {
this.set('model.composeState', Discourse.Composer.OPEN);
}
@ -95,35 +95,33 @@ export default DiscourseController.extend({
},
updateDraftStatus: function() {
var c = this.get('model');
updateDraftStatus() {
const c = this.get('model');
if (c) { c.updateDraftStatus(); }
},
appendText: function(text, opts) {
var c = this.get('model');
appendText(text, opts) {
const c = this.get('model');
if (c) {
opts = opts || {};
var wmd = $('#wmd-input');
var val = wmd.val() || '';
var position = opts.position === "cursor" ? wmd.caret() : val.length;
const wmd = $('#wmd-input'),
val = wmd.val() || '',
position = opts.position === "cursor" ? wmd.caret() : val.length,
caret = c.appendText(text, position, opts);
var caret = c.appendText(text, position, opts);
if(wmd[0]){
Em.run.next(function(){
Discourse.Utilities.setCaretPosition(wmd[0], caret);
});
if (wmd[0]) {
Em.run.next(() => Discourse.Utilities.setCaretPosition(wmd[0], caret));
}
}
},
appendTextAtCursor: function(text, opts) {
appendTextAtCursor(text, opts) {
opts = opts || {};
opts.position = "cursor";
this.appendText(text, opts);
},
appendBlockAtCursor: function(text, opts) {
appendBlockAtCursor(text, opts) {
opts = opts || {};
opts.position = "cursor";
opts.block = true;
@ -135,7 +133,7 @@ export default DiscourseController.extend({
}.property(),
toggle: function() {
toggle() {
this.closeAutocomplete();
switch (this.get('model.composeState')) {
case Discourse.Composer.OPEN:
@ -158,9 +156,9 @@ export default DiscourseController.extend({
return this.get('model.loading');
}.property('model.loading'),
save: function(force) {
var composer = this.get('model'),
self = this;
save(force) {
const composer = this.get('model'),
self = this;
// Clear the warning state if we're not showing the checkbox anymore
if (!this.get('showWarning')) {
@ -168,7 +166,7 @@ export default DiscourseController.extend({
}
if(composer.get('cantSubmitPost')) {
var now = Date.now();
const now = Date.now();
this.setProperties({
'view.showTitleTip': now,
'view.showCategoryTip': now,
@ -182,12 +180,12 @@ export default DiscourseController.extend({
// for now handle a very narrow use case
// if we are replying to a topic AND not on the topic pop the window up
if (!force && composer.get('replyingToTopic')) {
var topic = this.get('topic');
const topic = this.get('topic');
if (!topic || topic.get('id') !== composer.get('topic.id'))
{
var message = I18n.t("composer.posting_not_on_topic");
const message = I18n.t("composer.posting_not_on_topic");
var buttons = [{
let buttons = [{
"label": I18n.t("composer.cancel"),
"class": "cancel",
"link": true
@ -231,7 +229,7 @@ export default DiscourseController.extend({
opts = opts || {};
self.close();
var currentUser = Discourse.User.current();
const currentUser = Discourse.User.current();
if (composer.get('creatingTopic')) {
currentUser.set('topic_count', currentUser.get('topic_count') + 1);
} else {
@ -255,7 +253,7 @@ export default DiscourseController.extend({
@method checkReplyLength
**/
checkReplyLength: function() {
checkReplyLength() {
if (this.present('model.reply')) {
// Notify the composer messages controller that a reply has been typed. Some
// messages only appear after typing.
@ -269,29 +267,29 @@ export default DiscourseController.extend({
@method findSimilarTopics
**/
findSimilarTopics: function() {
findSimilarTopics() {
// We don't care about similar topics unless creating a topic
if (!this.get('model.creatingTopic')) return;
if (!this.get('model.creatingTopic')) { return; }
var body = this.get('model.reply'),
title = this.get('model.title'),
self = this,
let body = this.get('model.reply'),
message;
const title = this.get('model.title'),
self = this;
// Ensure the fields are of the minimum length
if (body.length < Discourse.SiteSettings.min_body_similar_length ||
title.length < Discourse.SiteSettings.min_title_similar_length) { return; }
if (body.length < Discourse.SiteSettings.min_body_similar_length) { return; }
if (title.length < Discourse.SiteSettings.min_title_similar_length) { return; }
// TODO pass the 200 in from somewhere
body = body.substr(0, 200);
// Done search over and over
if((title + body) === this.get('lastSimilaritySearch')) { return; }
if ((title + body) === this.get('lastSimilaritySearch')) { return; }
this.set('lastSimilaritySearch', title + body);
var messageController = this.get('controllers.composer-messages'),
similarTopics = this.get('similarTopics');
const messageController = this.get('controllers.composer-messages'),
similarTopics = this.get('similarTopics');
Discourse.Topic.findSimilarTo(title, body).then(function (newTopics) {
similarTopics.clear();
@ -300,24 +298,24 @@ export default DiscourseController.extend({
if (similarTopics.get('length') > 0) {
message = Discourse.ComposerMessage.create({
templateName: 'composer/similar_topics',
similarTopics: similarTopics,
similarTopics,
extraClass: 'similar-topics'
});
self.set('similarTopicsMessage', message);
messageController.popup(message);
messageController.send("popup", message);
} else {
message = self.get('similarTopicsMessage');
if (message) {
messageController.send('hideMessage', message);
messageController.send("hideMessage", message);
}
}
});
},
saveDraft: function() {
var model = this.get('model');
saveDraft() {
const model = this.get('model');
if (model) { model.saveDraft(); }
},
@ -331,7 +329,7 @@ export default DiscourseController.extend({
@param {Discourse.Topic} [opts.topic] The topic we're replying to
@param {String} [opts.quote] If we're opening a reply from a quote, the quote we're making
**/
open: function(opts) {
open(opts) {
opts = opts || {};
if (!opts.draftKey) {
@ -345,15 +343,17 @@ export default DiscourseController.extend({
this.set('scopedCategoryId', opts.categoryId);
}
var composerMessages = this.get('controllers.composer-messages'),
self = this,
composerModel = this.get('model');
const composerMessages = this.get('controllers.composer-messages'),
self = this;
let composerModel = this.get('model');
this.setProperties({ showEditReason: false, editReason: null });
composerMessages.reset();
// If we want a different draft than the current composer, close it and clear our model.
if (composerModel && opts.draftKey !== composerModel.draftKey &&
if (composerModel &&
opts.draftKey !== composerModel.draftKey &&
composerModel.composeState === Discourse.Composer.DRAFT) {
this.close();
composerModel = null;
@ -396,7 +396,7 @@ export default DiscourseController.extend({
},
// Given a potential instance and options, set the model for this composer.
_setModel: function(composerModel, opts) {
_setModel(composerModel, opts) {
if (opts.draft) {
composerModel = Discourse.Composer.loadDraft(opts.draftKey, opts.draftSequence, opts.draft);
if (composerModel) {
@ -411,26 +411,25 @@ export default DiscourseController.extend({
composerModel.set('composeState', Discourse.Composer.OPEN);
composerModel.set('isWarning', false);
var composerMessages = this.get('controllers.composer-messages');
composerMessages.queryFor(composerModel);
this.get('controllers.composer-messages').queryFor(composerModel);
},
// View a new reply we've made
viewNewReply: function() {
viewNewReply() {
Discourse.URL.routeTo(this.get('createdPost.url'));
this.close();
return false;
},
destroyDraft: function() {
var key = this.get('model.draftKey');
destroyDraft() {
const key = this.get('model.draftKey');
if (key) {
Discourse.Draft.clear(key, this.get('model.draftSequence'));
}
},
cancelComposer: function() {
var self = this;
cancelComposer() {
const self = this;
return new Ember.RSVP.Promise(function (resolve) {
if (self.get('model.hasMetaData') || self.get('model.replyDirty')) {
@ -454,7 +453,7 @@ export default DiscourseController.extend({
},
shrink: function() {
shrink() {
if (this.get('model.replyDirty')) {
this.collapse();
} else {
@ -462,12 +461,12 @@ export default DiscourseController.extend({
}
},
collapse: function() {
collapse() {
this.saveDraft();
this.set('model.composeState', Discourse.Composer.DRAFT);
},
close: function() {
close() {
this.setProperties({
model: null,
'view.showTitleTip': false,
@ -476,11 +475,11 @@ export default DiscourseController.extend({
});
},
closeAutocomplete: function() {
closeAutocomplete() {
$('#wmd-input').autocomplete({ cancel: true });
},
showOptions: function() {
showOptions() {
var _ref;
return (_ref = this.get('controllers.modal')) ? _ref.show(Discourse.ArchetypeOptionsModalView.create({
archetype: this.get('model.archetype'),

View File

@ -411,7 +411,6 @@ Topic.reopenClass({
});
return Discourse.ajax(topic.get('url'), { type: 'PUT', data: props }).then(function(result) {
// The title can be cleaned up server side
props.title = result.basic_topic.title;
props.fancy_title = result.basic_topic.fancy_title;

View File

@ -1,4 +1,4 @@
<a href='#' {{action "closeMessage" this}} class='close'><i class='fa fa-times-circle'></i></a>
<a href='#' {{action "closeMessage" this}} class='close'>{{fa-icon "times-circle"}}</a>
<h3>{{i18n 'composer.similar_topics'}}</h3>
<ul class='topics'>