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';
|
2017-08-05 01:56:27 +08:00
|
|
|
import { categoryLinkHTML } from 'discourse/helpers/category-link';
|
2015-03-03 01:12:19 +08:00
|
|
|
|
2015-09-01 23:54:16 +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,
|
|
|
|
|
2017-08-05 01:56:27 +08:00
|
|
|
@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) {
|
2015-08-22 02:39:21 +08:00
|
|
|
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
|
|
|
},
|
|
|
|
|
2015-08-22 02:39:21 +08:00
|
|
|
@computed('componentType')
|
|
|
|
typeClass(componentType) {
|
2015-08-28 07:13:11 +08:00
|
|
|
return componentType.replace(/\_/g, '-');
|
2015-08-12 05:34:02 +08:00
|
|
|
},
|
2015-03-03 01:12:19 +08:00
|
|
|
|
2015-08-22 02:39:21 +08:00
|
|
|
@computed("setting.setting")
|
|
|
|
settingName(setting) {
|
|
|
|
return setting.replace(/\_/g, ' ');
|
|
|
|
},
|
2015-03-03 01:12:19 +08:00
|
|
|
|
2015-08-22 02:39:21 +08:00
|
|
|
@computed("setting.type")
|
|
|
|
componentType(type) {
|
2015-08-12 05:34:02 +08:00
|
|
|
return CustomTypes.indexOf(type) !== -1 ? type : 'string';
|
2015-08-22 02:39:21 +08:00
|
|
|
},
|
2015-03-03 01:12:19 +08:00
|
|
|
|
2015-08-22 02:39:21 +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() {
|
2017-08-03 00:24:19 +08:00
|
|
|
const setting = this.get('buffered'),
|
|
|
|
action = SiteSetting.update(setting.get('setting'), setting.get('value'));
|
|
|
|
action.then(() => {
|
|
|
|
this.set('validationMessage', null);
|
|
|
|
this.commitBuffer();
|
|
|
|
}).catch((e) => {
|
2015-05-17 16:45:27 +08:00
|
|
|
if (e.jqXHR.responseJSON && e.jqXHR.responseJSON.errors) {
|
2017-08-03 00:24:19 +08:00
|
|
|
this.set('validationMessage', e.jqXHR.responseJSON.errors[0]);
|
2015-03-03 01:12:19 +08:00
|
|
|
} else {
|
2017-08-03 00:24:19 +08:00
|
|
|
this.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();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
});
|