FEATURE: Theme settings (2) (#5611)

Allows theme authors to specify custom theme settings for the theme. 

Centralizes the theme/site settings into a single construct
This commit is contained in:
OsamaSayegh
2018-03-05 03:04:23 +03:00
committed by Sam
parent 322618fc34
commit 282f53f0cd
42 changed files with 1202 additions and 217 deletions

View File

@@ -1,31 +1,7 @@
import { ajax } from 'discourse/lib/ajax';
const SiteSetting = Discourse.Model.extend({
overridden: function() {
let val = this.get('value'),
defaultVal = this.get('default');
import Setting from 'admin/mixins/setting-object';
if (val === null) val = '';
if (defaultVal === null) defaultVal = '';
return val.toString() !== defaultVal.toString();
}.property('value', 'default'),
validValues: function() {
const vals = [],
translateNames = this.get('translate_names');
this.get('valid_values').forEach(function(v) {
if (v.name && v.name.length > 0) {
vals.addObject(translateNames ? {name: I18n.t(v.name), value: v.value} : v);
}
});
return vals;
}.property('valid_values'),
allowsNone: function() {
if ( _.indexOf(this.get('valid_values'), '') >= 0 ) return 'admin.site_settings.none';
}.property('valid_values')
});
const SiteSetting = Discourse.Model.extend(Setting, {});
SiteSetting.reopenClass({
findAll() {

View File

@@ -0,0 +1,3 @@
import Setting from 'admin/mixins/setting-object';
export default Discourse.Model.extend(Setting, {});

View File

@@ -2,6 +2,7 @@ import RestModel from 'discourse/models/rest';
import { default as computed } from 'ember-addons/ember-computed-decorators';
const THEME_UPLOAD_VAR = 2;
const FIELDS_IDS = [0, 1, 5];
const Theme = RestModel.extend({
@@ -14,13 +15,11 @@ const Theme = RestModel.extend({
}
let hash = {};
if (fields) {
fields.forEach(field=>{
if (!field.type_id || field.type_id < THEME_UPLOAD_VAR) {
hash[this.getKey(field)] = field;
}
});
}
fields.forEach(field => {
if (!field.type_id || FIELDS_IDS.includes(field.type_id)) {
hash[this.getKey(field)] = field;
}
});
return hash;
},
@@ -29,11 +28,11 @@ const Theme = RestModel.extend({
if (!fields) {
return [];
}
return fields.filter((f)=> f.target === 'common' && f.type_id === THEME_UPLOAD_VAR);
return fields.filter(f => f.target === 'common' && f.type_id === THEME_UPLOAD_VAR);
},
getKey(field){
return field.target + " " + field.name;
return `${field.target} ${field.name}`;
},
hasEdited(target, name){
@@ -151,6 +150,11 @@ const Theme = RestModel.extend({
.then(() => this.set("changed", false));
},
saveSettings(name, value) {
const settings = {};
settings[name] = value;
return this.save({ settings });
}
});
export default Theme;