From a83a19f6cea7fa0371787d8b01d92d27022ac20b Mon Sep 17 00:00:00 2001 From: Robin Ward Date: Wed, 8 Apr 2015 14:17:21 -0400 Subject: [PATCH] ES6 module migrations --- .../admin-logs-screened-emails.js.es6 | 3 +- .../admin-logs-screened-ip-addresses.js.es6 | 3 +- .../admin-logs-screened-urls.js.es6 | 3 +- .../admin-logs-staff-action-logs.js.es6 | 3 +- .../controllers/admin-site-settings.js.es6 | 4 +- .../discourse/controllers/controller.js.es6 | 4 +- .../discourse/controllers/object.js.es6 | 4 +- .../discourse/controllers/search.js.es6 | 3 +- .../javascripts/discourse/mixins/presence.js | 36 ------------- .../discourse/mixins/presence.js.es6 | 20 +++++++ .../models/{model.js => model.js.es6} | 8 ++- .../javascripts/discourse/models/post.js.es6 | 4 +- .../javascripts/discourse/models/rest.js.es6 | 4 +- .../discourse/views/container.js.es6 | 4 +- .../discourse/views/grouped.js.es6 | 12 +++++ .../javascripts/discourse/views/view.js | 52 ------------------- .../javascripts/discourse/views/view.js.es6 | 17 ++++++ app/assets/javascripts/main_include.js | 2 + .../tilt/es6_module_transpiler_template.rb | 1 + .../controllers/admin-email-index-test.js.es6 | 5 -- .../admin-email-preview-digest-test.js.es6 | 5 -- .../controllers/discourse-test.js.es6 | 3 +- .../helpers/create-pretender.js.es6 | 2 +- test/javascripts/mixins/presence-test.js.es6 | 6 ++- test/javascripts/models/model-test.js.es6 | 13 +++-- .../views/container-view-test.js.es6 | 6 ++- test/javascripts/views/view-test.js.es6 | 6 ++- 27 files changed, 109 insertions(+), 124 deletions(-) delete mode 100644 app/assets/javascripts/discourse/mixins/presence.js create mode 100644 app/assets/javascripts/discourse/mixins/presence.js.es6 rename app/assets/javascripts/discourse/models/{model.js => model.js.es6} (63%) create mode 100644 app/assets/javascripts/discourse/views/grouped.js.es6 delete mode 100644 app/assets/javascripts/discourse/views/view.js create mode 100644 app/assets/javascripts/discourse/views/view.js.es6 delete mode 100644 test/javascripts/admin/controllers/admin-email-index-test.js.es6 delete mode 100644 test/javascripts/admin/controllers/admin-email-preview-digest-test.js.es6 diff --git a/app/assets/javascripts/admin/controllers/admin-logs-screened-emails.js.es6 b/app/assets/javascripts/admin/controllers/admin-logs-screened-emails.js.es6 index e467be5b176..c744f96c48b 100644 --- a/app/assets/javascripts/admin/controllers/admin-logs-screened-emails.js.es6 +++ b/app/assets/javascripts/admin/controllers/admin-logs-screened-emails.js.es6 @@ -1,6 +1,7 @@ +import Presence from 'discourse/mixins/presence'; import { outputExportResult } from 'discourse/lib/export-result'; -export default Ember.ArrayController.extend(Discourse.Presence, { +export default Ember.ArrayController.extend(Presence, { loading: false, actions: { diff --git a/app/assets/javascripts/admin/controllers/admin-logs-screened-ip-addresses.js.es6 b/app/assets/javascripts/admin/controllers/admin-logs-screened-ip-addresses.js.es6 index dcbd6505e1f..1bc35969730 100644 --- a/app/assets/javascripts/admin/controllers/admin-logs-screened-ip-addresses.js.es6 +++ b/app/assets/javascripts/admin/controllers/admin-logs-screened-ip-addresses.js.es6 @@ -1,6 +1,7 @@ +import Presence from 'discourse/mixins/presence'; import { outputExportResult } from 'discourse/lib/export-result'; -export default Ember.ArrayController.extend(Discourse.Presence, { +export default Ember.ArrayController.extend(Presence, { loading: false, itemController: 'admin-log-screened-ip-address', filter: null, diff --git a/app/assets/javascripts/admin/controllers/admin-logs-screened-urls.js.es6 b/app/assets/javascripts/admin/controllers/admin-logs-screened-urls.js.es6 index 003dedca089..06839b74565 100644 --- a/app/assets/javascripts/admin/controllers/admin-logs-screened-urls.js.es6 +++ b/app/assets/javascripts/admin/controllers/admin-logs-screened-urls.js.es6 @@ -1,6 +1,7 @@ +import Presence from 'discourse/mixins/presence'; import { outputExportResult } from 'discourse/lib/export-result'; -export default Ember.ArrayController.extend(Discourse.Presence, { +export default Ember.ArrayController.extend(Presence, { loading: false, show() { diff --git a/app/assets/javascripts/admin/controllers/admin-logs-staff-action-logs.js.es6 b/app/assets/javascripts/admin/controllers/admin-logs-staff-action-logs.js.es6 index 6784fa1d8f2..e3f56d57865 100644 --- a/app/assets/javascripts/admin/controllers/admin-logs-staff-action-logs.js.es6 +++ b/app/assets/javascripts/admin/controllers/admin-logs-staff-action-logs.js.es6 @@ -1,6 +1,7 @@ +import Presence from 'discourse/mixins/presence'; import { outputExportResult } from 'discourse/lib/export-result'; -export default Ember.ArrayController.extend(Discourse.Presence, { +export default Ember.ArrayController.extend(Presence, { loading: false, filters: null, diff --git a/app/assets/javascripts/admin/controllers/admin-site-settings.js.es6 b/app/assets/javascripts/admin/controllers/admin-site-settings.js.es6 index 95fdff940e9..9e67e638b58 100644 --- a/app/assets/javascripts/admin/controllers/admin-site-settings.js.es6 +++ b/app/assets/javascripts/admin/controllers/admin-site-settings.js.es6 @@ -1,4 +1,6 @@ -export default Ember.ArrayController.extend(Discourse.Presence, { +import Presence from 'discourse/mixins/presence'; + +export default Ember.ArrayController.extend(Presence, { filter: null, onlyOverridden: false, filtered: Ember.computed.notEmpty('filter'), diff --git a/app/assets/javascripts/discourse/controllers/controller.js.es6 b/app/assets/javascripts/discourse/controllers/controller.js.es6 index 94c3ba1d43e..0be72a02a4c 100644 --- a/app/assets/javascripts/discourse/controllers/controller.js.es6 +++ b/app/assets/javascripts/discourse/controllers/controller.js.es6 @@ -1 +1,3 @@ -export default Ember.Controller.extend(Discourse.Presence); +import Presence from 'discourse/mixins/presence'; + +export default Ember.Controller.extend(Presence); diff --git a/app/assets/javascripts/discourse/controllers/object.js.es6 b/app/assets/javascripts/discourse/controllers/object.js.es6 index 75a47142857..7351a0a1748 100644 --- a/app/assets/javascripts/discourse/controllers/object.js.es6 +++ b/app/assets/javascripts/discourse/controllers/object.js.es6 @@ -1 +1,3 @@ -export default Ember.ObjectController.extend(Discourse.Presence); +import Presence from 'discourse/mixins/presence'; + +export default Ember.ObjectController.extend(Presence); diff --git a/app/assets/javascripts/discourse/controllers/search.js.es6 b/app/assets/javascripts/discourse/controllers/search.js.es6 index 4efcd31dca5..bc812e3efef 100644 --- a/app/assets/javascripts/discourse/controllers/search.js.es6 +++ b/app/assets/javascripts/discourse/controllers/search.js.es6 @@ -1,8 +1,9 @@ +import Presence from 'discourse/mixins/presence'; import searchForTerm from 'discourse/lib/search-for-term'; var _dontSearch = false; -export default Em.Controller.extend(Discourse.Presence, { +export default Em.Controller.extend(Presence, { contextType: function(key, value){ if(arguments.length > 1) { diff --git a/app/assets/javascripts/discourse/mixins/presence.js b/app/assets/javascripts/discourse/mixins/presence.js deleted file mode 100644 index a09bea3d601..00000000000 --- a/app/assets/javascripts/discourse/mixins/presence.js +++ /dev/null @@ -1,36 +0,0 @@ -/** - This mixin provides `blank` and `present` to determine whether properties are - there, accounting for more cases than just null and undefined. - - @class Discourse.Presence - @extends Ember.Mixin - @namespace Discourse - @module Discourse -**/ -Discourse.Presence = Em.Mixin.create({ - - /** - Returns whether a property is blank. It considers empty arrays, string, objects, undefined and null - to be blank, otherwise true. - - @method blank - @param {String} name the name of the property we want to check - @return {Boolean} - */ - blank: function(name) { - return Ember.isEmpty(this[name] || this.get(name)); - }, - - /** - Returns whether a property is present. A present property is the opposite of a `blank` one. - - @method present - @param {String} name the name of the property we want to check - @return {Boolean} - */ - present: function(name) { - return !this.blank(name); - } -}); - - diff --git a/app/assets/javascripts/discourse/mixins/presence.js.es6 b/app/assets/javascripts/discourse/mixins/presence.js.es6 new file mode 100644 index 00000000000..742f50f402c --- /dev/null +++ b/app/assets/javascripts/discourse/mixins/presence.js.es6 @@ -0,0 +1,20 @@ +/** + This mixin provides `blank` and `present` to determine whether properties are + there, accounting for more cases than just null and undefined. +**/ +export default Ember.Mixin.create({ + + /** + Returns whether a property is blank. It considers empty arrays, string, objects, undefined and null + to be blank, otherwise true. + */ + blank(name) { + return Ember.isEmpty(this[name] || this.get(name)); + }, + + // Returns whether a property is present. A present property is the opposite of a `blank` one. + present(name) { + return !this.blank(name); + } + +}); diff --git a/app/assets/javascripts/discourse/models/model.js b/app/assets/javascripts/discourse/models/model.js.es6 similarity index 63% rename from app/assets/javascripts/discourse/models/model.js rename to app/assets/javascripts/discourse/models/model.js.es6 index 0c2619c7cba..4a5209bee59 100644 --- a/app/assets/javascripts/discourse/models/model.js +++ b/app/assets/javascripts/discourse/models/model.js.es6 @@ -1,6 +1,8 @@ -Discourse.Model = Ember.Object.extend(Discourse.Presence); +import Presence from 'discourse/mixins/presence'; -Discourse.Model.reopenClass({ +const Model = Ember.Object.extend(Presence); + +Model.reopenClass({ extractByKey: function(collection, klass) { var retval = {}; if (Ember.isEmpty(collection)) { return retval; } @@ -11,3 +13,5 @@ Discourse.Model.reopenClass({ return retval; } }); + +export default Model; diff --git a/app/assets/javascripts/discourse/models/post.js.es6 b/app/assets/javascripts/discourse/models/post.js.es6 index 1d547443321..627cc1aaf87 100644 --- a/app/assets/javascripts/discourse/models/post.js.es6 +++ b/app/assets/javascripts/discourse/models/post.js.es6 @@ -1,4 +1,6 @@ -const Post = Discourse.Model.extend({ +import RestModel from 'discourse/models/rest'; + +const Post = RestModel.extend({ init() { this.set('replyHistory', []); diff --git a/app/assets/javascripts/discourse/models/rest.js.es6 b/app/assets/javascripts/discourse/models/rest.js.es6 index d85475644ca..fc5eff01efe 100644 --- a/app/assets/javascripts/discourse/models/rest.js.es6 +++ b/app/assets/javascripts/discourse/models/rest.js.es6 @@ -1,4 +1,6 @@ -export default Ember.Object.extend({ +import Presence from 'discourse/mixins/presence'; + +export default Ember.Object.extend(Presence, { update(attrs) { const self = this, type = this.get('__type'); diff --git a/app/assets/javascripts/discourse/views/container.js.es6 b/app/assets/javascripts/discourse/views/container.js.es6 index f8cbc3bd60f..68c65335321 100644 --- a/app/assets/javascripts/discourse/views/container.js.es6 +++ b/app/assets/javascripts/discourse/views/container.js.es6 @@ -1,4 +1,6 @@ -export default Ember.ContainerView.extend(Discourse.Presence, { +import Presence from 'discourse/mixins/presence'; + +export default Ember.ContainerView.extend(Presence, { attachViewWithArgs(viewArgs, viewClass) { if (!viewClass) { viewClass = Ember.View.extend(); } diff --git a/app/assets/javascripts/discourse/views/grouped.js.es6 b/app/assets/javascripts/discourse/views/grouped.js.es6 new file mode 100644 index 00000000000..ed06f49b50c --- /dev/null +++ b/app/assets/javascripts/discourse/views/grouped.js.es6 @@ -0,0 +1,12 @@ +import Presence from 'discourse/mixins/presence'; + +export default Ember.View.extend(Presence, { + _groupInit: function() { + this.set('context', this.get('content')); + + const templateData = this.get('templateData'); + if (templateData) { + this.set('templateData.insideGroup', true); + } + }.on('init') +}); diff --git a/app/assets/javascripts/discourse/views/view.js b/app/assets/javascripts/discourse/views/view.js deleted file mode 100644 index c62af2582e9..00000000000 --- a/app/assets/javascripts/discourse/views/view.js +++ /dev/null @@ -1,52 +0,0 @@ -/** - A base view that gives us common functionality, for example `present` and `blank` - - @class View - @extends Ember.View - @uses Discourse.Presence - @namespace Discourse - @module Discourse -**/ -Discourse.View = Ember.View.extend(Discourse.Presence, {}); - -Discourse.GroupedView = Ember.View.extend(Discourse.Presence, { - init: function() { - this._super(); - this.set('context', this.get('content')); - - var templateData = this.get('templateData'); - if (templateData) { - this.set('templateData.insideGroup', true); - } - } -}); - -Discourse.View.reopenClass({ - - /** - Register a view helper for ease of use - - @method registerHelper - @param {String} helperName the name of the helper - @param {Ember.View} helperClass the view that will be inserted by the helper - **/ - registerHelper: function(helperName, helperClass) { - Ember.Handlebars.registerHelper(helperName, function(options) { - var hash = options.hash, - types = options.hashTypes; - - Discourse.Utilities.normalizeHash(hash, types); - return Ember.Handlebars.helpers.view.call(this, helperClass, options); - }); - }, - - renderIfChanged: function() { - Em.warn("`rerenderIfChanged` is deprecated. Use the `StringBuffer` mixin with `rerenderTriggers` instead."); - var args = Array.prototype.slice.call(arguments, 0); - args.unshift(function () { - Ember.run.once(this, 'rerender'); - }); - return Ember.observer.apply(this, args); - } - -}); diff --git a/app/assets/javascripts/discourse/views/view.js.es6 b/app/assets/javascripts/discourse/views/view.js.es6 new file mode 100644 index 00000000000..f939764ebb0 --- /dev/null +++ b/app/assets/javascripts/discourse/views/view.js.es6 @@ -0,0 +1,17 @@ +import Presence from 'discourse/mixins/presence'; + +const View = Ember.View.extend(Presence, {}); + +View.reopenClass({ + registerHelper(helperName, helperClass) { + Ember.Handlebars.registerHelper(helperName, function(options) { + var hash = options.hash, + types = options.hashTypes; + + Discourse.Utilities.normalizeHash(hash, types); + return Ember.Handlebars.helpers.view.call(this, helperClass, options); + }); + } +}); + +export default View; diff --git a/app/assets/javascripts/main_include.js b/app/assets/javascripts/main_include.js index 9a6e9757438..597dbbdd937 100644 --- a/app/assets/javascripts/main_include.js +++ b/app/assets/javascripts/main_include.js @@ -24,6 +24,7 @@ //= require ./discourse/lib/avatar-template //= require ./discourse/lib/safari-hacks //= require_tree ./discourse/adapters +//= require ./discourse/models/rest //= require ./discourse/models/model //= require ./discourse/models/post //= require ./discourse/models/user_action @@ -36,6 +37,7 @@ //= require ./discourse/controllers/object //= require ./discourse/controllers/navigation/default //= require ./discourse/views/view +//= require ./discourse/views/grouped //= require ./discourse/views/container //= require ./discourse/views/modal-body //= require ./discourse/views/flag diff --git a/lib/es6_module_transpiler/tilt/es6_module_transpiler_template.rb b/lib/es6_module_transpiler/tilt/es6_module_transpiler_template.rb index b5cc2a0a7f6..27776035800 100644 --- a/lib/es6_module_transpiler/tilt/es6_module_transpiler_template.rb +++ b/lib/es6_module_transpiler/tilt/es6_module_transpiler_template.rb @@ -107,6 +107,7 @@ module Tilt # HAX result = "Controller" if result == "ControllerController" result = "Route" if result == "DiscourseRoute" + result = "View" if result == "ViewView" result.gsub!(/Mixin$/, '') result.gsub!(/Model$/, '') diff --git a/test/javascripts/admin/controllers/admin-email-index-test.js.es6 b/test/javascripts/admin/controllers/admin-email-index-test.js.es6 deleted file mode 100644 index 0456c5e50e1..00000000000 --- a/test/javascripts/admin/controllers/admin-email-index-test.js.es6 +++ /dev/null @@ -1,5 +0,0 @@ -moduleFor("controller:admin-email-index"); - -test("mixes in Discourse.Presence", function() { - ok(Discourse.Presence.detect(this.subject())); -}); diff --git a/test/javascripts/admin/controllers/admin-email-preview-digest-test.js.es6 b/test/javascripts/admin/controllers/admin-email-preview-digest-test.js.es6 deleted file mode 100644 index e6aa17bfdbd..00000000000 --- a/test/javascripts/admin/controllers/admin-email-preview-digest-test.js.es6 +++ /dev/null @@ -1,5 +0,0 @@ -moduleFor("controller:admin-email-preview-digest"); - -test("mixes in Discourse.Presence", function() { - ok(Discourse.Presence.detect(this.subject())); -}); diff --git a/test/javascripts/controllers/discourse-test.js.es6 b/test/javascripts/controllers/discourse-test.js.es6 index cf5959f8ad3..ab8239f8518 100644 --- a/test/javascripts/controllers/discourse-test.js.es6 +++ b/test/javascripts/controllers/discourse-test.js.es6 @@ -1,7 +1,8 @@ import DiscourseController from 'discourse/controllers/controller'; +import Presence from 'discourse/mixins/presence'; module("DiscourseController"); test("includes mixins", function() { - ok(Discourse.Presence.detect(DiscourseController.create()), "Discourse.Presence"); + ok(Presence.detect(DiscourseController.create()), "has Presence"); }); diff --git a/test/javascripts/helpers/create-pretender.js.es6 b/test/javascripts/helpers/create-pretender.js.es6 index e6184eee2fd..71d2ab593f3 100644 --- a/test/javascripts/helpers/create-pretender.js.es6 +++ b/test/javascripts/helpers/create-pretender.js.es6 @@ -131,7 +131,7 @@ export default function() { return response(200, { basic_topic: {id: request.params.id, title: data.title, fancy_title: data.title, - slug: request.params.slug } }) + slug: request.params.slug } }); }); this.post('/posts', function(request) { diff --git a/test/javascripts/mixins/presence-test.js.es6 b/test/javascripts/mixins/presence-test.js.es6 index 268f4279084..f11a94588a4 100644 --- a/test/javascripts/mixins/presence-test.js.es6 +++ b/test/javascripts/mixins/presence-test.js.es6 @@ -1,6 +1,8 @@ -module("Discourse.Presence"); +import Presence from 'discourse/mixins/presence'; -var testObj = Em.Object.createWithMixins(Discourse.Presence, { +module("mixin:presence"); + +var testObj = Em.Object.createWithMixins(Presence, { emptyString: "", nonEmptyString: "Evil Trout", emptyArray: [], diff --git a/test/javascripts/models/model-test.js.es6 b/test/javascripts/models/model-test.js.es6 index 6404fe88a74..b8d92921ec1 100644 --- a/test/javascripts/models/model-test.js.es6 +++ b/test/javascripts/models/model-test.js.es6 @@ -1,14 +1,17 @@ +import Presence from 'discourse/mixins/presence'; +import Model from 'discourse/models/model'; + module("Discourse.Model"); -test("mixes in Discourse.Presence", function() { - ok(Discourse.Presence.detect(Discourse.Model.create())); +test("mixes in Presence", function() { + ok(Presence.detect(Model.create())); }); test("extractByKey: converts a list of hashes into a hash of instances of specified class, indexed by their ids", function() { var firstObject = {id: "id_1", foo: "foo_1"}; var secondObject = {id: "id_2", foo: "foo_2"}; - var actual = Discourse.Model.extractByKey([firstObject, secondObject], Ember.Object); + var actual = Model.extractByKey([firstObject, secondObject], Ember.Object); var expected = { id_1: Ember.Object.create(firstObject), id_2: Ember.Object.create(secondObject) @@ -18,6 +21,6 @@ test("extractByKey: converts a list of hashes into a hash of instances of specif }); test("extractByKey: returns an empty hash if there isn't anything to convert", function() { - deepEqual(Discourse.Model.extractByKey(), {}, "when called without parameters"); - deepEqual(Discourse.Model.extractByKey([]), {}, "when called with an empty array"); + deepEqual(Model.extractByKey(), {}, "when called without parameters"); + deepEqual(Model.extractByKey([]), {}, "when called with an empty array"); }); diff --git a/test/javascripts/views/container-view-test.js.es6 b/test/javascripts/views/container-view-test.js.es6 index c462559436b..e8f3b2a8a4d 100644 --- a/test/javascripts/views/container-view-test.js.es6 +++ b/test/javascripts/views/container-view-test.js.es6 @@ -1,3 +1,5 @@ +import Presence from 'discourse/mixins/presence'; + var SomeViewClass = Ember.View.extend(); function containerHasOnlyOneChild(containerView, klass) { @@ -17,9 +19,9 @@ function childHasProperty(containerView, name) { moduleFor("view:container"); -test("mixes in Discourse.Presence", function() { +test("mixes in Presence", function() { var containerView = this.subject(); - ok(Discourse.Presence.detect(containerView)); + ok(Presence.detect(containerView)); }); test("attachViewWithArgs: creates a view of a given class with given properties and appends it to the container", function() { diff --git a/test/javascripts/views/view-test.js.es6 b/test/javascripts/views/view-test.js.es6 index f9ea42dfe42..ee62c02de7d 100644 --- a/test/javascripts/views/view-test.js.es6 +++ b/test/javascripts/views/view-test.js.es6 @@ -1,3 +1,5 @@ +import Presence from 'discourse/mixins/presence'; + var oldHelpers; module("Discourse.View", { @@ -10,8 +12,8 @@ module("Discourse.View", { } }); -test("mixes in Discourse.Presence", function() { - ok(Discourse.Presence.detect(Discourse.View.create())); +test("mixes in Presence", function() { + ok(Presence.detect(Discourse.View.create())); }); test("registerHelper: enables embedding a child view in a parent view via dedicated, named helper instead of generic 'view' helper", function() {