REFACTOR: composer model (#7499)

REFACTOR: composer model (#7499)

diff --git a/app/assets/javascripts/discourse/models/composer.js.es6 b/app/assets/javascripts/discourse/models/composer.js.es6
index f68352e..5182f87 100644
--- a/app/assets/javascripts/discourse/models/composer.js.es6
+++ b/app/assets/javascripts/discourse/models/composer.js.es6
@@ -3,8 +3,13 @@ import Topic from "discourse/models/topic";
 import { throwAjaxError } from "discourse/lib/ajax-error";
 import Quote from "discourse/lib/quote";
 import Draft from "discourse/models/draft";
-import computed from "ember-addons/ember-computed-decorators";
+import {
+  default as computed,
+  observes,
+  on
+} from "ember-addons/ember-computed-decorators";
 import { escapeExpression, tinyAvatar } from "discourse/lib/utilities";
+import { propertyNotEqual } from "discourse/lib/computed";
 
 // The actions the composer can take
 export const CREATE_TOPIC = "createTopic",
@@ -77,13 +82,9 @@ const Composer = RestModel.extend({
   draftSaving: false,
   draftSaved: false,
 
-  @computed
-  archetypes() {
-    return this.site.get("archetypes");
-  },
+  archetypes: Ember.computed.reads("site.archetypes"),
 
-  @computed("action")
-  sharedDraft: action => action === CREATE_SHARED_DRAFT,
+  sharedDraft: Ember.computed.equal("action", CREATE_SHARED_DRAFT),
 
   @computed
   categoryId: {
@@ -104,6 +105,7 @@ const Composer = RestModel.extend({
       if (oldCategoryId !== categoryId) {
         this.applyTopicTemplate(oldCategoryId, categoryId);
       }
+
       return categoryId;
     }
   },
@@ -115,8 +117,8 @@ const Composer = RestModel.extend({
 
   @computed("category")
   minimumRequiredTags(category) {
-    return category && category.get("minimum_required_tags") > 0
-      ? category.get("minimum_required_tags")
+    return category && category.minimum_required_tags > 0
+      ? category.minimum_required_tags
       : null;
   },
 
@@ -128,15 +130,14 @@ const Composer = RestModel.extend({
 
   @computed("privateMessage", "archetype.hasOptions")
   showCategoryChooser(isPrivateMessage, hasOptions) {
-    const manyCategories = this.site.get("categories").length > 1;
+    const manyCategories = this.site.categories.length > 1;
     return !isPrivateMessage && (hasOptions || manyCategories);
   },
 
   @computed("creatingPrivateMessage", "topic")
   privateMessage(creatingPrivateMessage, topic) {
     return (
-      creatingPrivateMessage ||
-      (topic && topic.get("archetype") === "private_message")
+      creatingPrivateMessage || (topic && topic.archetype === "private_message")
     );
   },
 
@@ -152,52 +153,56 @@ const Composer = RestModel.extend({
   viewFullscreen: Ember.computed.equal("composeState", FULLSCREEN),
   viewOpenOrFullscreen: Ember.computed.or("viewOpen", "viewFullscreen"),
 
-  composeStateChanged: function() {
-    let oldOpen = this.get("composerOpened"),
-      elem = $("html");
+  @observes("composeState")
+  composeStateChanged() {
+    const oldOpen = this.composerOpened;
+    const elem = document.querySelector("html");
 
-    if (this.get("composeState") === FULLSCREEN) {
-      elem.addClass("fullscreen-composer");
+    if (this.composeState === FULLSCREEN) {
+      elem.classList.add("fullscreen-composer");
     } else {
-      elem.removeClass("fullscreen-composer");
+      elem.classList.remove("fullscreen-composer");
     }
 
-    if (this.get("composeState") === OPEN) {
+    if (this.composeState === OPEN) {
       this.set("composerOpened", oldOpen || new Date());
     } else {
       if (oldOpen) {
-        let oldTotal = this.get("composerTotalOpened") || 0;
+        const oldTotal = this.composerTotalOpened || 0;
         this.set("composerTotalOpened", oldTotal + (new Date() - oldOpen));
       }
       this.set("composerOpened", null);
     }
-  }.observes("composeState"),
+  },
 
-  composerTime: function() {
-    let total = this.get("composerTotalOpened") || 0,
-      oldOpen = this.get("composerOpened");
-    if (oldOpen) {
-      total += new Date() - oldOpen;
-    }
+  @computed
+  composerTime: {
+    get() {
+      let total = this.composerTotalOpened || 0;
+      const oldOpen = this.composerOpened;
 
-    return total;
-  }
-    .property()
-    .volatile(),
+      if (oldOpen) {
+        total += new Date() - oldOpen;
+      }
+
+      return total;
+    }
+  },
 
   @computed("archetypeId")
   archetype(archetypeId) {
-    return this.get("archetypes").findBy("id", archetypeId);
+    return this.archetypes.findBy("id", archetypeId);
   },
 
-  archetypeChanged: function() {
+  @observes("archetype")
+  archetypeChanged() {
     return this.set("metaData", Ember.Object.create());
-  }.observes("archetype"),
+  },
 
   // view detected user is typing
   typing: _.throttle(
     function() {
-      let typingTime = this.get("typingTime") || 0;
+      const typingTime = this.typingTime || 0;
       this.set("typingTime", typingTime + 100);
     },
     100,
@@ -229,13 +234,11 @@ const Composer = RestModel.extend({
       return false;
     }
 
-    const categoryIds = this.site.get(
-      "topic_featured_link_allowed_category_ids"
-    );
+    const categoryIds = this.site.topic_featured_link_allowed_category_ids;
     if (
       !categoryId &&
       categoryIds &&
-      (categoryIds.indexOf(this.site.get("uncategorized_category_id")) !== -1 ||
+      (categoryIds.indexOf(this.site.uncategorized_category_id) !== -1 ||
         !this.siteSettings.allow_uncategorized_topics)
     ) {
       return true;
@@ -248,15 +251,15 @@ const Composer = RestModel.extend({
   },
 
   @computed("canEditTopicFeaturedLink")
-  titlePlaceholder() {
-    return this.get("canEditTopicFeaturedLink")
+  titlePlaceholder(canEditTopicFeaturedLink) {
+    return canEditTopicFeaturedLink
       ? "composer.title_or_link_placeholder"
       : "composer.title_placeholder";
   },
 
   @computed("action", "post", "topic", "topic.title")
   replyOptions(action, post, topic, topicTitle) {
-    let options = {
+    const options = {
       userLink: null,
       topicLink: null,
       postLink: null,
@@ -266,14 +269,14 @@ const Composer = RestModel.extend({
 
     if (topic) {
       options.topicLink = {
-        href: topic.get("url"),
-        anchor: topic.get("fancy_title") || escapeExpression(topicTitle)
+        href: topic.url,
+        anchor: topic.fancy_title || escapeExpression(topicTitle)
       };
     }
 
     if (post) {
       options.label = I18n.t(`post.${action}`);
-      options.userAvatar = tinyAvatar(post.get("avatar_template"));
+      options.userAvatar = tinyAvatar(post.avatar_template);
 
       if (!this.site.mobileView) {
         const originalUserName = post.get("reply_to_user.username");
@@ -288,16 +291,16 @@ const Composer = RestModel.extend({
     }
 
     if (topic && post) {
-      const postNumber = post.get("post_number");
+      const postNumber = post.post_number;
 
       options.postLink = {
-        href: `${topic.get("url")}/${postNumber}`,
+        href: `${topic.url}/${postNumber}`,
         anchor: I18n.t("post.post_number", { number: postNumber })
       };
 
       options.userLink = {
-        href: `${topic.get("url")}/${postNumber}`,
-        anchor: post.get("username")
+        href: `${topic.url}/${postNumber}`,
+        anchor: post.username
       };
     }
 
@@ -307,7 +310,7 @@ const Composer = RestModel.extend({
   @computed
   isStaffUser() {
     const currentUser = Discourse.User.current();
-    return currentUser && currentUser.get("staff");
+    return currentUser && currentUser.staff;
   },
 
   @computed(
@@ -342,13 +345,13 @@ const Composer = RestModel.extend({
     // title is required when
     //  - creating a new topic/private message
     //  - editing the 1st post
-    if (canEditTitle && !this.get("titleLengthValid")) return true;
+    if (canEditTitle && !this.titleLengthValid) return true;
 
     // reply is always required
     if (missingReplyCharacters > 0) return true;
 
     if (
-      this.site.get("can_tag_topics") &&
+      this.site.can_tag_topics &&
       !isStaffUser &&

[... diff too long, it was truncated ...]

GitHub sha: 54c2f24e