From f42a5c1ba3e2d5d421666d091065969490a318e4 Mon Sep 17 00:00:00 2001 From: Robin Ward Date: Mon, 15 Dec 2014 14:41:08 -0500 Subject: [PATCH] Convert a lot of Globals to ES6 modules --- ..._input_component.js => color-input.js.es6} | 16 +++------ ...tting_component.js => list-setting.js.es6} | 36 ++++++++----------- .../controllers/admin-users-list-show.js.es6 | 3 +- .../admin/controllers/admin.js.es6 | 2 +- .../admin/routes/admin-dashboard.js.es6 | 12 ++----- .../components/posts-count-column.js.es6 | 7 ++-- .../discourse/initializers/csrf-token.js.es6 | 14 ++++---- .../initializers/inject-objects.js.es6 | 10 ++++++ .../javascripts/discourse/models/session.js | 16 --------- .../discourse/models/session.js.es6 | 11 ++++++ .../discourse/routes/discovery-route.js.es6 | 2 +- .../javascripts/discourse/routes/topic.js.es6 | 2 +- .../discourse/views/discovery-topics.js.es6 | 4 +-- .../discourse/views/topic-list-item.js.es6 | 6 ++-- test/javascripts/models/session-test.js.es6 | 4 ++- 15 files changed, 65 insertions(+), 80 deletions(-) rename app/assets/javascripts/admin/components/{color_input_component.js => color-input.js.es6} (73%) rename app/assets/javascripts/admin/components/{list_setting_component.js => list-setting.js.es6} (65%) delete mode 100644 app/assets/javascripts/discourse/models/session.js create mode 100644 app/assets/javascripts/discourse/models/session.js.es6 diff --git a/app/assets/javascripts/admin/components/color_input_component.js b/app/assets/javascripts/admin/components/color-input.js.es6 similarity index 73% rename from app/assets/javascripts/admin/components/color_input_component.js rename to app/assets/javascripts/admin/components/color-input.js.es6 index 099d4ab1e..98d5f6e6b 100644 --- a/app/assets/javascripts/admin/components/color_input_component.js +++ b/app/assets/javascripts/admin/components/color-input.js.es6 @@ -4,15 +4,8 @@ @param hexValue is a reference to the color's hex value. @param brightnessValue is a number from 0 to 255 representing the brightness of the color. See ColorSchemeColor. @params valid is a boolean indicating if the input field is a valid color. - - @class Discourse.ColorInputComponent - @extends Ember.Component - @namespace Discourse - @module Discourse - **/ -Discourse.ColorInputComponent = Ember.Component.extend({ - layoutName: 'components/color-input', - +**/ +export default Ember.Component.extend({ hexValueChanged: function() { var hex = this.get('hexValue'); if (this.get('valid')) { @@ -22,11 +15,10 @@ Discourse.ColorInputComponent = Ember.Component.extend({ } }.observes('hexValue', 'brightnessValue', 'valid'), - didInsertElement: function() { + _triggerHexChanged: function() { var self = this; - this._super(); Em.run.schedule('afterRender', function() { self.hexValueChanged(); }); - } + }.on('didInsertElement') }); diff --git a/app/assets/javascripts/admin/components/list_setting_component.js b/app/assets/javascripts/admin/components/list-setting.js.es6 similarity index 65% rename from app/assets/javascripts/admin/components/list_setting_component.js rename to app/assets/javascripts/admin/components/list-setting.js.es6 index ecbe97c95..55ff799fb 100644 --- a/app/assets/javascripts/admin/components/list_setting_component.js +++ b/app/assets/javascripts/admin/components/list-setting.js.es6 @@ -3,16 +3,8 @@ @param settingValue is a reference to SiteSetting.value. @param choices is a reference to SiteSetting.choices - - @class Discourse.ListSettingComponent - @extends Ember.Component - @namespace Discourse - @module Discourse - **/ - -Discourse.ListSettingComponent = Ember.Component.extend({ - tagName: 'div', - +**/ +export default Ember.Component.extend({ _select2FormatSelection: function(selectedObject, jqueryWrapper, htmlEscaper) { var text = selectedObject.text; @@ -22,9 +14,8 @@ Discourse.ListSettingComponent = Ember.Component.extend({ return htmlEscaper(text); }, - didInsertElement: function(){ - - var select2_options = { + _initializeSelect2: function(){ + var options = { multiple: false, separator: "|", tokenSeparators: ["|"], @@ -35,24 +26,27 @@ Discourse.ListSettingComponent = Ember.Component.extend({ var settingName = this.get('settingName'); if (typeof settingName === 'string' && settingName.indexOf('colors') > -1) { - select2_options.formatSelection = this._select2FormatSelection; + options.formatSelection = this._select2FormatSelection; } - this.$("input").select2(select2_options).on("change", function(obj) { - this.set("settingValue", obj.val.join("|")); - this.refreshSortables(); - }.bind(this)); + + var self = this; + this.$("input").select2(options).on("change", function(obj) { + self.set("settingValue", obj.val.join("|")); + self.refreshSortables(); + }); this.refreshSortables(); - }, + }.on('didInsertElement'), refreshOnReset: function() { this.$("input").select2("val", this.get("settingValue").split("|")); }.observes("settingValue"), refreshSortables: function() { + var self = this; this.$("ul.select2-choices").sortable().on('sortupdate', function() { - this.$("input").select2("onSortEnd"); - }.bind(this)); + self.$("input").select2("onSortEnd"); + }); } }); diff --git a/app/assets/javascripts/admin/controllers/admin-users-list-show.js.es6 b/app/assets/javascripts/admin/controllers/admin-users-list-show.js.es6 index 20a48ea5d..708207f84 100644 --- a/app/assets/javascripts/admin/controllers/admin-users-list-show.js.es6 +++ b/app/assets/javascripts/admin/controllers/admin-users-list-show.js.es6 @@ -53,12 +53,13 @@ export default Ember.ArrayController.extend({ }, rejectUsers: function() { + var maxPostAge = this.siteSettings.delete_user_max_post_age; var controller = this; Discourse.AdminUser.bulkReject(this.get('model').filterProperty('selected')).then(function(result){ var message = I18n.t("admin.users.reject_successful", {count: result.success}); if (result.failed > 0) { message += ' ' + I18n.t("admin.users.reject_failures", {count: result.failed}); - message += ' ' + I18n.t("admin.user.delete_forbidden", {count: Discourse.SiteSettings.delete_user_max_post_age}); + message += ' ' + I18n.t("admin.user.delete_forbidden", {count: maxPostAge}); } bootbox.alert(message); controller._refreshUsers(); diff --git a/app/assets/javascripts/admin/controllers/admin.js.es6 b/app/assets/javascripts/admin/controllers/admin.js.es6 index b255781ff..ae2b81f08 100644 --- a/app/assets/javascripts/admin/controllers/admin.js.es6 +++ b/app/assets/javascripts/admin/controllers/admin.js.es6 @@ -2,6 +2,6 @@ import DiscourseController from 'discourse/controllers/controller'; export default DiscourseController.extend({ showBadges: function() { - return this.get('currentUser.admin') && Discourse.SiteSettings.enable_badges; + return this.get('currentUser.admin') && this.siteSettings.enable_badges; }.property() }); diff --git a/app/assets/javascripts/admin/routes/admin-dashboard.js.es6 b/app/assets/javascripts/admin/routes/admin-dashboard.js.es6 index acf3b504b..a5da039ed 100644 --- a/app/assets/javascripts/admin/routes/admin-dashboard.js.es6 +++ b/app/assets/javascripts/admin/routes/admin-dashboard.js.es6 @@ -1,12 +1,3 @@ -/** - Handles the default admin route - - @class AdminDashboardRoute - @extends Discourse.Route - @namespace Discourse - @module Discourse -**/ - export default Discourse.Route.extend({ setupController: function(c) { @@ -16,8 +7,9 @@ export default Discourse.Route.extend({ fetchDashboardData: function(c) { if( !c.get('dashboardFetchedAt') || moment().subtract(30, 'minutes').toDate() > c.get('dashboardFetchedAt') ) { c.set('dashboardFetchedAt', new Date()); + var versionChecks = this.siteSettings.version_checks; Discourse.AdminDashboard.find().then(function(d) { - if( Discourse.SiteSettings.version_checks ){ + if (versionChecks) { c.set('versionCheck', Discourse.VersionCheck.create(d.version_check)); } _.each(d.reports,function(report){ diff --git a/app/assets/javascripts/discourse/components/posts-count-column.js.es6 b/app/assets/javascripts/discourse/components/posts-count-column.js.es6 index f5b74917a..66e939c71 100644 --- a/app/assets/javascripts/discourse/components/posts-count-column.js.es6 +++ b/app/assets/javascripts/discourse/components/posts-count-column.js.es6 @@ -22,9 +22,10 @@ export default Ember.Component.extend({ ratioText: function() { var ratio = this.get('ratio'); - if (ratio > Discourse.SiteSettings.topic_post_like_heat_high) { return 'high'; } - if (ratio > Discourse.SiteSettings.topic_post_like_heat_medium) { return 'med'; } - if (ratio > Discourse.SiteSettings.topic_post_like_heat_low) { return 'low'; } + var settings = this.siteSettings; + if (ratio > settings.topic_post_like_heat_high) { return 'high'; } + if (ratio > settings.topic_post_like_heat_medium) { return 'med'; } + if (ratio > settings.topic_post_like_heat_low) { return 'low'; } return ''; }.property('ratio'), diff --git a/app/assets/javascripts/discourse/initializers/csrf-token.js.es6 b/app/assets/javascripts/discourse/initializers/csrf-token.js.es6 index 946fa5b2d..f83ea3caa 100644 --- a/app/assets/javascripts/discourse/initializers/csrf-token.js.es6 +++ b/app/assets/javascripts/discourse/initializers/csrf-token.js.es6 @@ -1,17 +1,17 @@ -/** - Append our CSRF token to AJAX requests when necessary. -**/ +// Append our CSRF token to AJAX requests when necessary. export default { name: "csrf-token", - initialize: function() { - var session = Discourse.Session; + after: 'inject-objects', + initialize: function(container) { + + var session = container.lookup('session:main'); // Add a CSRF token to all AJAX requests - session.currentProp('csrfToken', $('meta[name=csrf-token]').attr('content')); + session.set('csrfToken', $('meta[name=csrf-token]').attr('content')); $.ajaxPrefilter(function(options, originalOptions, xhr) { if (!options.crossDomain) { - xhr.setRequestHeader('X-CSRF-Token', session.currentProp('csrfToken')); + xhr.setRequestHeader('X-CSRF-Token', session.get('csrfToken')); } }); } diff --git a/app/assets/javascripts/discourse/initializers/inject-objects.js.es6 b/app/assets/javascripts/discourse/initializers/inject-objects.js.es6 index aa412adc7..ab5578971 100644 --- a/app/assets/javascripts/discourse/initializers/inject-objects.js.es6 +++ b/app/assets/javascripts/discourse/initializers/inject-objects.js.es6 @@ -1,3 +1,5 @@ +import Session from 'discourse/models/session'; + export default { name: "inject-objects", initialize: function(container, application) { @@ -29,5 +31,13 @@ export default { application.inject('route', 'siteSettings', 'site-settings:main'); application.inject('view', 'siteSettings', 'site-settings:main'); application.inject('model', 'siteSettings', 'site-settings:main'); + + // Inject Session for transient data + application.register('session:main', Session.current(), { instantiate: false }); + application.inject('controller', 'session', 'session:main'); + application.inject('component', 'session', 'session:main'); + application.inject('route', 'session', 'session:main'); + application.inject('view', 'session', 'session:main'); + application.inject('model', 'session', 'session:main'); } }; diff --git a/app/assets/javascripts/discourse/models/session.js b/app/assets/javascripts/discourse/models/session.js deleted file mode 100644 index 52a44444d..000000000 --- a/app/assets/javascripts/discourse/models/session.js +++ /dev/null @@ -1,16 +0,0 @@ -/** - A data model representing current session data. You can put transient - data here you might want later. It is not stored or serialized anywhere. - - @class Session - @extends Discourse.Model - @namespace Discourse - @module Discourse -**/ -Discourse.Session = Discourse.Model.extend({ - init: function() { - this.set('highestSeenByTopic', {}); - } -}); - -Discourse.Session.reopenClass(Discourse.Singleton); diff --git a/app/assets/javascripts/discourse/models/session.js.es6 b/app/assets/javascripts/discourse/models/session.js.es6 new file mode 100644 index 000000000..894dc6b2f --- /dev/null +++ b/app/assets/javascripts/discourse/models/session.js.es6 @@ -0,0 +1,11 @@ +// A data model representing current session data. You can put transient +// data here you might want later. It is not stored or serialized anywhere. +var Session = Discourse.Model.extend({ + init: function() { + this.set('highestSeenByTopic', {}); + } +}); + +Session.reopenClass(Discourse.Singleton); + +export default Session; diff --git a/app/assets/javascripts/discourse/routes/discovery-route.js.es6 b/app/assets/javascripts/discourse/routes/discovery-route.js.es6 index f4e3a0a68..3d6522fe8 100644 --- a/app/assets/javascripts/discourse/routes/discovery-route.js.es6 +++ b/app/assets/javascripts/discourse/routes/discovery-route.js.es6 @@ -24,7 +24,7 @@ Discourse.DiscoveryRoute = Discourse.Route.extend(Discourse.ScrollTop, Discourse loadingComplete: function() { this.controllerFor('discovery').set('loading', false); - if (!Discourse.Session.currentProp('topicListScrollPosition')) { + if (!this.session.get('topicListScrollPosition')) { this._scrollTop(); } }, diff --git a/app/assets/javascripts/discourse/routes/topic.js.es6 b/app/assets/javascripts/discourse/routes/topic.js.es6 index 0efd06837..7d47ae63c 100644 --- a/app/assets/javascripts/discourse/routes/topic.js.es6 +++ b/app/assets/javascripts/discourse/routes/topic.js.es6 @@ -172,7 +172,7 @@ var TopicRoute = Discourse.Route.extend(ShowFooter, { isTransitioning = false; var topic = this.modelFor('topic'); - Discourse.Session.currentProp('lastTopicIdViewed', parseInt(topic.get('id'), 10)); + this.session.set('lastTopicIdViewed', parseInt(topic.get('id'), 10)); this.controllerFor('search').set('searchContext', topic.get('searchContext')); }, diff --git a/app/assets/javascripts/discourse/views/discovery-topics.js.es6 b/app/assets/javascripts/discourse/views/discovery-topics.js.es6 index 02d374c4a..cdafd0e5c 100644 --- a/app/assets/javascripts/discourse/views/discovery-topics.js.es6 +++ b/app/assets/javascripts/discourse/views/discovery-topics.js.es6 @@ -20,7 +20,7 @@ export default Discourse.View.extend(LoadMore, UrlRefresh, { }, _readjustScrollPosition: function() { - var scrollTo = Discourse.Session.currentProp('topicListScrollPosition'); + var scrollTo = this.session.get('topicListScrollPosition'); if (typeof scrollTo !== "undefined") { Em.run.schedule('afterRender', function() { @@ -35,7 +35,7 @@ export default Discourse.View.extend(LoadMore, UrlRefresh, { // Remember where we were scrolled to saveScrollPosition: function() { - Discourse.Session.current().set('topicListScrollPosition', $(window).scrollTop()); + this.session.set('topicListScrollPosition', $(window).scrollTop()); }, // When the topic list is scrolled diff --git a/app/assets/javascripts/discourse/views/topic-list-item.js.es6 b/app/assets/javascripts/discourse/views/topic-list-item.js.es6 index e886aa99e..7b353e9d0 100644 --- a/app/assets/javascripts/discourse/views/topic-list-item.js.es6 +++ b/app/assets/javascripts/discourse/views/topic-list-item.js.es6 @@ -17,11 +17,9 @@ export default Discourse.GroupedView.extend({ }, _highlightIfNeeded: function() { - var session = Discourse.Session.current(); - // highlight the last topic viewed - if (session.get('lastTopicIdViewed') === this.get('content.id')) { - session.set('lastTopicIdViewed', null); + if (this.session.get('lastTopicIdViewed') === this.get('content.id')) { + this.session.set('lastTopicIdViewed', null); this.highlight(); } else if (this.get('content.highlight')) { // highlight new topics that have been loaded from the server or the one we just created diff --git a/test/javascripts/models/session-test.js.es6 b/test/javascripts/models/session-test.js.es6 index 84589a9c7..d4d3ff5a5 100644 --- a/test/javascripts/models/session-test.js.es6 +++ b/test/javascripts/models/session-test.js.es6 @@ -1,6 +1,8 @@ +import Session from "discourse/models/session"; + module("Discourse.Session"); test('highestSeenByTopic', function() { - var session = Discourse.Session.current(); + var session = Session.current(); deepEqual(session.get('highestSeenByTopic'), {}, "by default it returns an empty object"); });