forked from discourse/discourse
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
FEATURE: Theme settings (2) (discourse#5611)
Allows theme authors to specify custom theme settings for the theme. Centralizes the theme/site settings into a single construct
- Loading branch information
1 parent
322618f
commit 282f53f
Showing
42 changed files
with
1,202 additions
and
217 deletions.
There are no files selected for viewing
94 changes: 4 additions & 90 deletions
94
app/assets/javascripts/admin/components/site-setting.js.es6
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,96 +1,10 @@ | ||
import BufferedContent from 'discourse/mixins/buffered-content'; | ||
import SiteSetting from 'admin/models/site-setting'; | ||
import { propertyNotEqual } from 'discourse/lib/computed'; | ||
import computed from 'ember-addons/ember-computed-decorators'; | ||
import { categoryLinkHTML } from 'discourse/helpers/category-link'; | ||
|
||
const CustomTypes = ['bool', 'enum', 'list', 'url_list', 'host_list', 'category_list', 'value_list']; | ||
|
||
export default Ember.Component.extend(BufferedContent, { | ||
classNameBindings: [':row', ':setting', 'setting.overridden', 'typeClass'], | ||
content: Ember.computed.alias('setting'), | ||
dirty: propertyNotEqual('buffered.value', 'setting.value'), | ||
validationMessage: null, | ||
|
||
@computed("setting", "buffered.value") | ||
preview(setting, value) { | ||
// A bit hacky, but allows us to use helpers | ||
if (setting.get('setting') === 'category_style') { | ||
let category = this.site.get('categories.firstObject'); | ||
if (category) { | ||
return categoryLinkHTML(category, { | ||
categoryStyle: value | ||
}); | ||
} | ||
} | ||
|
||
let preview = setting.get('preview'); | ||
if (preview) { | ||
return new Handlebars.SafeString("<div class='preview'>" + preview.replace(/\{\{value\}\}/g, value) + "</div>"); | ||
} | ||
}, | ||
|
||
@computed('componentType') | ||
typeClass(componentType) { | ||
return componentType.replace(/\_/g, '-'); | ||
}, | ||
|
||
@computed("setting.setting") | ||
settingName(setting) { | ||
return setting.replace(/\_/g, ' '); | ||
}, | ||
|
||
@computed("setting.type") | ||
componentType(type) { | ||
return CustomTypes.indexOf(type) !== -1 ? type : 'string'; | ||
}, | ||
|
||
@computed("typeClass") | ||
componentName(typeClass) { | ||
return "site-settings/" + typeClass; | ||
}, | ||
|
||
_watchEnterKey: function() { | ||
const self = this; | ||
this.$().on("keydown.site-setting-enter", ".input-setting-string", function (e) { | ||
if (e.keyCode === 13) { // enter key | ||
self._save(); | ||
} | ||
}); | ||
}.on('didInsertElement'), | ||
|
||
_removeBindings: function() { | ||
this.$().off("keydown.site-setting-enter"); | ||
}.on("willDestroyElement"), | ||
import SettingComponent from 'admin/mixins/setting-component'; | ||
|
||
export default Ember.Component.extend(BufferedContent, SettingComponent, { | ||
_save() { | ||
const setting = this.get('buffered'), | ||
action = SiteSetting.update(setting.get('setting'), setting.get('value')); | ||
action.then(() => { | ||
this.set('validationMessage', null); | ||
this.commitBuffer(); | ||
}).catch((e) => { | ||
if (e.jqXHR.responseJSON && e.jqXHR.responseJSON.errors) { | ||
this.set('validationMessage', e.jqXHR.responseJSON.errors[0]); | ||
} else { | ||
this.set('validationMessage', I18n.t('generic_error')); | ||
} | ||
}); | ||
}, | ||
|
||
actions: { | ||
save() { | ||
this._save(); | ||
}, | ||
|
||
resetDefault() { | ||
this.set('buffered.value', this.get('setting.default')); | ||
this._save(); | ||
}, | ||
|
||
cancel() { | ||
this.rollbackBuffer(); | ||
} | ||
const setting = this.get('buffered'); | ||
return SiteSetting.update(setting.get('setting'), setting.get('value')); | ||
} | ||
|
||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
import BufferedContent from 'discourse/mixins/buffered-content'; | ||
import SettingComponent from 'admin/mixins/setting-component'; | ||
|
||
export default Ember.Component.extend(BufferedContent, SettingComponent, { | ||
layoutName: 'admin/templates/components/site-setting', | ||
_save() { | ||
return this.get('model').saveSettings(this.get('setting.setting'), this.get('buffered.value')); | ||
} | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
98 changes: 98 additions & 0 deletions
98
app/assets/javascripts/admin/mixins/setting-component.js.es6
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,98 @@ | ||
import computed from 'ember-addons/ember-computed-decorators'; | ||
import { categoryLinkHTML } from 'discourse/helpers/category-link'; | ||
|
||
const CustomTypes = ['bool', 'enum', 'list', 'url_list', 'host_list', 'category_list', 'value_list']; | ||
|
||
export default Ember.Mixin.create({ | ||
classNameBindings: [':row', ':setting', 'setting.overridden', 'typeClass'], | ||
content: Ember.computed.alias('setting'), | ||
validationMessage: null, | ||
|
||
@computed("buffered.value", "setting.value") | ||
dirty(bufferVal, settingVal) { | ||
if (bufferVal === null || bufferVal === undefined) bufferVal = ''; | ||
if (settingVal === null || settingVal === undefined) settingVal = ''; | ||
|
||
return bufferVal.toString() !== settingVal.toString(); | ||
}, | ||
|
||
@computed("setting", "buffered.value") | ||
preview(setting, value) { | ||
// A bit hacky, but allows us to use helpers | ||
if (setting.get('setting') === 'category_style') { | ||
let category = this.site.get('categories.firstObject'); | ||
if (category) { | ||
return categoryLinkHTML(category, { | ||
categoryStyle: value | ||
}); | ||
} | ||
} | ||
|
||
let preview = setting.get('preview'); | ||
if (preview) { | ||
return new Handlebars.SafeString("<div class='preview'>" + preview.replace(/\{\{value\}\}/g, value) + "</div>"); | ||
} | ||
}, | ||
|
||
@computed('componentType') | ||
typeClass(componentType) { | ||
return componentType.replace(/\_/g, '-'); | ||
}, | ||
|
||
@computed("setting.setting") | ||
settingName(setting) { | ||
return setting.replace(/\_/g, ' '); | ||
}, | ||
|
||
@computed("setting.type") | ||
componentType(type) { | ||
return CustomTypes.indexOf(type) !== -1 ? type : 'string'; | ||
}, | ||
|
||
@computed("typeClass") | ||
componentName(typeClass) { | ||
return "site-settings/" + typeClass; | ||
}, | ||
|
||
_watchEnterKey: function() { | ||
const self = this; | ||
this.$().on("keydown.setting-enter", ".input-setting-string", function (e) { | ||
if (e.keyCode === 13) { // enter key | ||
self._save(); | ||
} | ||
}); | ||
}.on('didInsertElement'), | ||
|
||
_removeBindings: function() { | ||
this.$().off("keydown.setting-enter"); | ||
}.on("willDestroyElement"), | ||
|
||
_save() { | ||
Em.warn("You should define a `_save` method", { id: "admin.mixins.setting-component" }); | ||
return Ember.RSVP.resolve(); | ||
}, | ||
|
||
actions: { | ||
save() { | ||
this._save().then(() => { | ||
this.set('validationMessage', null); | ||
this.commitBuffer(); | ||
}).catch(e => { | ||
if (e.jqXHR.responseJSON && e.jqXHR.responseJSON.errors) { | ||
this.set('validationMessage', e.jqXHR.responseJSON.errors[0]); | ||
} else { | ||
this.set('validationMessage', I18n.t('generic_error')); | ||
} | ||
}); | ||
}, | ||
|
||
resetDefault() { | ||
this.set('buffered.value', this.get('setting.default')); | ||
this._save(); | ||
}, | ||
|
||
cancel() { | ||
this.rollbackBuffer(); | ||
} | ||
} | ||
}); |
Oops, something went wrong.