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

98 lines
2.8 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'];
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,
preview: function() {
const preview = this.get('setting.preview');
if (preview) {
return new Handlebars.SafeString("<div class='preview'>" +
preview.replace(/\{\{value\}\}/g, this.get('buffered.value')) +
2015-03-03 01:12:19 +08:00
"</div>");
}
}.property('buffered.value'),
2015-08-12 05:34:02 +08:00
@computed('partialType')
typeClass() {
2015-03-03 01:12:19 +08:00
return this.get('partialType').replace("_", "-");
2015-08-12 05:34:02 +08:00
},
@computed('buffered.value')
enabled: {
get(bufferedValue) {
2015-08-12 05:34:02 +08:00
if (Ember.isEmpty(bufferedValue)) { return false; }
return bufferedValue === 'true';
},
set(value) {
2015-03-03 01:12:19 +08:00
this.set('buffered.value', value ? 'true' : 'false');
return value;
2015-03-03 01:12:19 +08:00
}
2015-08-12 05:34:02 +08:00
},
2015-03-03 01:12:19 +08:00
settingName: function() {
return this.get('setting.setting').replace(/\_/g, ' ');
}.property('setting.setting'),
partialType: function() {
let type = this.get('setting.type');
2015-08-12 05:34:02 +08:00
return CustomTypes.indexOf(type) !== -1 ? type : 'string';
2015-03-03 01:12:19 +08:00
}.property('setting.type'),
partialName: function() {
return 'admin/templates/site-settings/' + this.get('partialType');
}.property('partialType'),
_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 setting = this.get('buffered');
const self = this;
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();
}
}
});