discourse/app/assets/javascripts/admin/components/site-setting.js.es6

97 lines
2.7 KiB
Plaintext
Raw Normal View History

2015-03-03 01:12:19 +08:00
import BufferedContent from 'discourse/mixins/buffered-content';
import SiteSetting from 'admin/models/site-setting';
2015-08-08 03:08:27 +08:00
import { propertyNotEqual } from 'discourse/lib/computed';
2015-08-12 05:34:02 +08:00
import computed from 'ember-addons/ember-computed-decorators';
import { categoryLinkHTML } from 'discourse/helpers/category-link';
2015-03-03 01:12:19 +08:00
const CustomTypes = ['bool', 'enum', 'list', 'url_list', 'host_list', 'category_list', 'value_list'];
2015-03-03 01:12:19 +08:00
2016-11-11 03:35:53 +08:00
export default Ember.Component.extend(BufferedContent, {
2015-03-03 01:12:19 +08:00
classNameBindings: [':row', ':setting', 'setting.overridden', 'typeClass'],
content: Ember.computed.alias('setting'),
2015-08-08 03:08:27 +08:00
dirty: propertyNotEqual('buffered.value', 'setting.value'),
2015-03-03 01:12:19 +08:00
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');
2015-03-03 01:12:19 +08:00
if (preview) {
return new Handlebars.SafeString("<div class='preview'>" + preview.replace(/\{\{value\}\}/g, value) + "</div>");
2015-03-03 01:12:19 +08:00
}
2015-08-12 05:34:02 +08:00
},
@computed('componentType')
typeClass(componentType) {
return componentType.replace(/\_/g, '-');
2015-08-12 05:34:02 +08:00
},
2015-03-03 01:12:19 +08:00
@computed("setting.setting")
settingName(setting) {
return setting.replace(/\_/g, ' ');
},
2015-03-03 01:12:19 +08:00
@computed("setting.type")
componentType(type) {
2015-08-12 05:34:02 +08:00
return CustomTypes.indexOf(type) !== -1 ? type : 'string';
},
2015-03-03 01:12:19 +08:00
@computed("typeClass")
componentName(typeClass) {
return "site-settings/" + typeClass;
},
2015-03-03 01:12:19 +08:00
_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"),
_save() {
const self = this,
setting = this.get('buffered');
SiteSetting.update(setting.get('setting'), setting.get('value')).then(function() {
self.set('validationMessage', null);
self.commitBuffer();
}).catch(function(e) {
if (e.jqXHR.responseJSON && e.jqXHR.responseJSON.errors) {
self.set('validationMessage', e.jqXHR.responseJSON.errors[0]);
2015-03-03 01:12:19 +08:00
} else {
self.set('validationMessage', I18n.t('generic_error'));
2015-03-03 01:12:19 +08:00
}
});
},
actions: {
save() {
this._save();
},
resetDefault() {
this.set('buffered.value', this.get('setting.default'));
this._save();
},
cancel() {
this.rollbackBuffer();
}
}
});