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

85 lines
2.3 KiB
Plaintext
Raw Normal View History

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