From c0fc7c5bcd0c27c70a27be3eb36243f436a27681 Mon Sep 17 00:00:00 2001 From: Hannah Wolfe Date: Thu, 1 Jan 2015 19:09:55 +0000 Subject: [PATCH] Adds global feature manager closes #4409 --- core/client/controllers/settings.js | 11 ++++------- core/client/controllers/settings/labs.js | 24 ++++++++++++------------ core/client/initializers/feature.js | 17 +++++++++++++++++ core/client/routes/settings/tags.js | 2 +- core/client/templates/settings/labs.hbs | 17 +++++++++++------ core/client/utils/feature.js | 24 ++++++++++++++++++++++++ 6 files changed, 69 insertions(+), 26 deletions(-) create mode 100644 core/client/initializers/feature.js create mode 100644 core/client/utils/feature.js diff --git a/core/client/controllers/settings.js b/core/client/controllers/settings.js index 5ea159700d..df07e47fd7 100644 --- a/core/client/controllers/settings.js +++ b/core/client/controllers/settings.js @@ -5,18 +5,15 @@ var SettingsController = Ember.Controller.extend({ showUsers: Ember.computed('session.user.name', function () { return this.get('session.user.isAuthor') ? false : true; }), - showTags: Ember.computed('session.user.name', 'config.tagsUI', function () { - return this.get('session.user.isAuthor') || !this.get('config.tagsUI') ? false : true; + showTags: Ember.computed('session.user.name', 'feature.tagsUI', function () { + return this.get('session.user.isAuthor') || !this.get('feature.tagsUI') ? false : true; }), - - showCodeInjection: Ember.computed('session.user.name', 'config.codeInjectionUI', function () { - return this.get('session.user.isAuthor') || this.get('session.user.isEditor') || !this.get('config.codeInjectionUI') ? false : true; + showCodeInjection: Ember.computed('session.user.name', 'feature.codeInjectionUI', function () { + return this.get('session.user.isAuthor') || this.get('session.user.isEditor') || !this.get('feature.codeInjectionUI') ? false : true; }), - showLabs: Ember.computed('session.user.name', function () { return this.get('session.user.isAuthor') || this.get('session.user.isEditor') ? false : true; }), - showAbout: Ember.computed('session.user.name', function () { return this.get('session.user.isAuthor') ? false : true; }) diff --git a/core/client/controllers/settings/labs.js b/core/client/controllers/settings/labs.js index 07b3c8e6d1..c111d765ba 100644 --- a/core/client/controllers/settings/labs.js +++ b/core/client/controllers/settings/labs.js @@ -1,15 +1,18 @@ -var LabsController = Ember.ObjectController.extend(Ember.Evented, { +var LabsController = Ember.Controller.extend(Ember.Evented, { uploadButtonText: 'Import', importErrors: '', + labsJSON: Ember.computed('model.labs', function () { + return JSON.parse(this.get('model.labs') || {}); + }), saveLabs: function (optionName, optionValue) { - var labsConfig = this.get('labs'), - labsJSON = (this.get('labs')) ? JSON.parse(labsConfig) : {}; + var self = this, + labsJSON = this.get('labsJSON'); // Set new value in the JSON object labsJSON[optionName] = optionValue; - this.set('labs', JSON.stringify(labsJSON)); + this.set('model.labs', JSON.stringify(labsJSON)); this.get('model').save().catch(function (errors) { self.showErrors(errors); @@ -17,6 +20,9 @@ var LabsController = Ember.ObjectController.extend(Ember.Evented, { }); }, + tagsUIFlag: Ember.computed.alias('config.tagsUI'), + codeUIFlag: Ember.computed.alias('config.codeInjectionUI'), + useTagsUI: Ember.computed('tagsUI', function (key, value) { // setter if (arguments.length > 1) { @@ -24,10 +30,7 @@ var LabsController = Ember.ObjectController.extend(Ember.Evented, { } // getter - var labsConfig = (this.get('labs')) ? JSON.parse(this.get('labs')) : false; - - - return (labsConfig.tagsUI) ? labsConfig.tagsUI : false; + return this.get('feature.tagsUI') || false; }), useCodeInjectionUI: Ember.computed('codeInjectionUI', function (key, value) { @@ -37,10 +40,7 @@ var LabsController = Ember.ObjectController.extend(Ember.Evented, { } // getter - var labsConfig = (this.get('labs')) ? JSON.parse(this.get('labs')) : false; - - - return (labsConfig.codeInjectionUI) ? labsConfig.codeInjectionUI : false; + return this.get('feature.codeInjectionUI') || false; }), actions: { diff --git a/core/client/initializers/feature.js b/core/client/initializers/feature.js new file mode 100644 index 0000000000..52ff88e15e --- /dev/null +++ b/core/client/initializers/feature.js @@ -0,0 +1,17 @@ +import Feature from 'ghost/utils/feature'; + +var injectFeatureInitializer = { + name: 'injectFeature', + after: ['config', 'store'], + + initialize: function (container, application) { + application.register('feature:main', Feature); + application.inject('feature:main', 'store', 'store:main'); + application.inject('feature:main', 'config', 'ghost:config'); + + application.inject('controller', 'feature', 'feature:main'); + application.inject('route', 'feature', 'feature:main'); + } +}; + +export default injectFeatureInitializer; diff --git a/core/client/routes/settings/tags.js b/core/client/routes/settings/tags.js index 3bc3470585..e7e7469419 100644 --- a/core/client/routes/settings/tags.js +++ b/core/client/routes/settings/tags.js @@ -13,7 +13,7 @@ var TagsRoute = AuthenticatedRoute.extend(CurrentUserSettings, PaginationRouteMi titleToken: 'Tags', beforeModel: function () { - if (!this.get('config.tagsUI')) { + if (!this.get('feature.tagsUI')) { return this.transitionTo('settings.general'); } diff --git a/core/client/templates/settings/labs.hbs b/core/client/templates/settings/labs.hbs index e0cc43e11d..6af0e7f49f 100644 --- a/core/client/templates/settings/labs.hbs +++ b/core/client/templates/settings/labs.hbs @@ -49,25 +49,30 @@
+ {{#unless tagsUIFlag}}
+

A settings screen which enables you to add, edit and delete tags (work in progress)

+ {{/unless}} + {{#unless codeUIFlag}}
-
+ {{/unless}}
diff --git a/core/client/utils/feature.js b/core/client/utils/feature.js new file mode 100644 index 0000000000..185cbd9f39 --- /dev/null +++ b/core/client/utils/feature.js @@ -0,0 +1,24 @@ +var Feature; + +Feature = Ember.Object.extend({ + init: function () { + var self = this; + this.store.find('setting').then(function (settings) { + self.set('setting', settings.get('firstObject')); + }); + }, + labs: Ember.computed('setting', 'setting.labs', function () { + if (this.setting) { + return JSON.parse(this.get('setting.labs') || {}); + } + return {}; + }), + tagsUI: Ember.computed('config.tagsUI', 'labs.tagsUI', function () { + return this.config.tagsUI || this.get('labs.tagsUI'); + }), + codeInjectionUI: Ember.computed('config.codeInjectionUI', 'labs.codeInjectionUI', function () { + return this.config.codeInjectionUI || this.get('labs.codeInjectionUI'); + }) +}); + +export default Feature;