discourse/app/assets/javascripts/admin/components/admin-user-field-item.js.es6

95 lines
2.8 KiB
Plaintext
Raw Normal View History

import UserField from 'admin/models/user-field';
2015-08-08 03:08:27 +08:00
import { bufferedProperty } from 'discourse/mixins/buffered-content';
import { popupAjaxError } from 'discourse/lib/ajax-error';
2015-08-08 03:08:27 +08:00
import { propertyEqual } from 'discourse/lib/computed';
export default Ember.Component.extend(bufferedProperty('userField'), {
editing: Ember.computed.empty('userField.id'),
classNameBindings: [':user-field'],
2015-08-08 03:08:27 +08:00
cantMoveUp: propertyEqual('userField', 'firstField'),
cantMoveDown: propertyEqual('userField', 'lastField'),
2015-07-31 02:52:53 +08:00
userFieldsDescription: function() {
return I18n.t('admin.user_fields.description');
}.property(),
bufferedFieldType: function() {
return UserField.fieldTypeById(this.get('buffered.field_type'));
}.property('buffered.field_type'),
_focusOnEdit: function() {
if (this.get('editing')) {
Ember.run.scheduleOnce('afterRender', this, '_focusName');
}
}.observes('editing').on('didInsertElement'),
_focusName: function() {
$('.user-field-name').select();
},
fieldName: function() {
return UserField.fieldTypeById(this.get('userField.field_type')).get('name');
}.property('userField.field_type'),
flags: function() {
const ret = [];
if (this.get('userField.editable')) {
ret.push(I18n.t('admin.user_fields.editable.enabled'));
}
if (this.get('userField.required')) {
ret.push(I18n.t('admin.user_fields.required.enabled'));
}
if (this.get('userField.show_on_profile')) {
ret.push(I18n.t('admin.user_fields.show_on_profile.enabled'));
}
return ret.join(', ');
}.property('userField.editable', 'userField.required', 'userField.show_on_profile'),
actions: {
save() {
const self = this;
const buffered = this.get('buffered');
const attrs = buffered.getProperties('name',
'description',
'field_type',
'editable',
'required',
'show_on_profile',
'options');
2015-07-31 02:52:53 +08:00
this.get('userField').save(attrs).then(function() {
self.set('editing', false);
self.commitBuffer();
}).catch(popupAjaxError);
},
2015-07-31 02:52:53 +08:00
moveUp() {
this.sendAction('moveUpAction', this.get('userField'));
},
moveDown() {
this.sendAction('moveDownAction', this.get('userField'));
},
edit() {
this.set('editing', true);
},
destroy() {
this.sendAction('destroyAction', this.get('userField'));
},
cancel() {
const id = this.get('userField.id');
if (Ember.isEmpty(id)) {
this.sendAction('destroyAction', this.get('userField'));
} else {
this.rollbackBuffer();
this.set('editing', false);
}
}
}
});