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

86 lines
2.4 KiB
Plaintext
Raw Normal View History

2015-03-03 01:12:19 +08:00
import BufferedContent from 'discourse/mixins/buffered-content';
2015-05-20 00:51:03 +08:00
import ScrollTop from 'discourse/mixins/scroll-top';
2015-03-03 01:12:19 +08:00
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';
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
2015-05-20 00:51:03 +08:00
export default Ember.Component.extend(BufferedContent, ScrollTop, {
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.preview", "buffered.value")
preview(preview, value) {
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');
2015-03-03 01:12:19 +08: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-03 01:12:19 +08: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();
}
}
});