2014-09-25 23:32:08 +08:00
|
|
|
import UserField from 'admin/models/user-field';
|
|
|
|
import BufferedContent from 'discourse/mixins/buffered-content';
|
|
|
|
|
|
|
|
export default Ember.ObjectController.extend(BufferedContent, {
|
|
|
|
needs: ['admin-user-fields'],
|
|
|
|
editing: Ember.computed.empty('id'),
|
|
|
|
|
|
|
|
fieldName: function() {
|
|
|
|
return UserField.fieldTypeById(this.get('field_type')).get('name');
|
|
|
|
}.property('field_type'),
|
|
|
|
|
2014-10-09 02:38:18 +08:00
|
|
|
flags: function() {
|
|
|
|
var ret = [];
|
|
|
|
if (this.get('editable')) {
|
|
|
|
ret.push(I18n.t('admin.user_fields.editable.enabled'));
|
|
|
|
}
|
|
|
|
if (this.get('required')) {
|
|
|
|
ret.push(I18n.t('admin.user_fields.required.enabled'));
|
|
|
|
}
|
2015-01-30 06:38:39 +08:00
|
|
|
if (this.get('show_on_profile')) {
|
|
|
|
ret.push(I18n.t('admin.user_fields.show_on_profile.enabled'));
|
|
|
|
}
|
2014-10-09 02:38:18 +08:00
|
|
|
|
|
|
|
return ret.join(', ');
|
2015-01-30 06:38:39 +08:00
|
|
|
}.property('editable', 'required', 'show_on_profile'),
|
2014-10-09 02:38:18 +08:00
|
|
|
|
2014-09-25 23:32:08 +08:00
|
|
|
actions: {
|
|
|
|
save: function() {
|
|
|
|
var self = this;
|
|
|
|
|
2015-01-30 06:38:39 +08:00
|
|
|
var attrs = this.get('buffered').getProperties('name', 'description', 'field_type', 'editable', 'required', 'show_on_profile');
|
2014-09-27 02:48:34 +08:00
|
|
|
|
|
|
|
this.get('model').save(attrs).then(function(res) {
|
2014-09-25 23:32:08 +08:00
|
|
|
self.set('model.id', res.user_field.id);
|
|
|
|
self.set('editing', false);
|
2014-09-27 02:48:34 +08:00
|
|
|
self.commitBuffer();
|
2014-10-03 03:56:28 +08:00
|
|
|
}).catch(function(e) {
|
|
|
|
var msg = I18n.t("generic_error");
|
|
|
|
if (e.responseJSON && e.responseJSON.errors) {
|
|
|
|
msg = I18n.t("generic_error_with_reason", {error: e.responseJSON.errors.join('. ')});
|
|
|
|
}
|
|
|
|
bootbox.alert(msg);
|
2014-09-25 23:32:08 +08:00
|
|
|
});
|
|
|
|
},
|
|
|
|
|
|
|
|
edit: function() {
|
|
|
|
this.set('editing', true);
|
|
|
|
},
|
|
|
|
|
|
|
|
destroy: function() {
|
|
|
|
this.get('controllers.admin-user-fields').send('destroy', this.get('model'));
|
|
|
|
},
|
|
|
|
|
|
|
|
cancel: function() {
|
|
|
|
var id = this.get('id');
|
2014-12-11 02:15:30 +08:00
|
|
|
if (Ember.isEmpty(id)) {
|
2014-09-25 23:32:08 +08:00
|
|
|
this.get('controllers.admin-user-fields').send('destroy', this.get('model'));
|
|
|
|
} else {
|
|
|
|
this.rollbackBuffer();
|
|
|
|
this.set('editing', false);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|