2015-07-10 12:28:56 -04:00
|
|
|
import { bufferedProperty } from 'discourse/mixins/buffered-content';
|
|
|
|
import UserField from 'admin/models/user-field';
|
|
|
|
import { popupAjaxError } from 'discourse/lib/ajax-error';
|
|
|
|
|
|
|
|
export default Ember.Component.extend(bufferedProperty('userField'), {
|
|
|
|
editing: Ember.computed.empty('userField.id'),
|
|
|
|
classNameBindings: [':user-field'],
|
|
|
|
|
2015-07-30 14:52:53 -04:00
|
|
|
cantMoveUp: Discourse.computed.propertyEqual('userField', 'firstField'),
|
|
|
|
cantMoveDown: Discourse.computed.propertyEqual('userField', 'lastField'),
|
|
|
|
|
2015-07-27 14:22:12 -04:00
|
|
|
userFieldsDescription: function() {
|
|
|
|
return I18n.t('admin.user_fields.description');
|
|
|
|
}.property(),
|
|
|
|
|
2015-07-28 12:29:40 -04:00
|
|
|
bufferedFieldType: function() {
|
|
|
|
return UserField.fieldTypeById(this.get('buffered.field_type'));
|
|
|
|
}.property('buffered.field_type'),
|
|
|
|
|
2015-07-10 12:28:56 -04:00
|
|
|
_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: {
|
2015-07-27 14:22:12 -04:00
|
|
|
save() {
|
2015-07-10 12:28:56 -04:00
|
|
|
const self = this;
|
2015-07-28 12:29:40 -04:00
|
|
|
const buffered = this.get('buffered');
|
|
|
|
const attrs = buffered.getProperties('name',
|
|
|
|
'description',
|
|
|
|
'field_type',
|
|
|
|
'editable',
|
|
|
|
'required',
|
|
|
|
'show_on_profile',
|
|
|
|
'options');
|
2015-07-10 12:28:56 -04:00
|
|
|
|
2015-07-30 14:52:53 -04:00
|
|
|
this.get('userField').save(attrs).then(function() {
|
2015-07-10 12:28:56 -04:00
|
|
|
self.set('editing', false);
|
|
|
|
self.commitBuffer();
|
|
|
|
}).catch(popupAjaxError);
|
|
|
|
},
|
|
|
|
|
2015-07-30 14:52:53 -04:00
|
|
|
moveUp() {
|
|
|
|
this.sendAction('moveUpAction', this.get('userField'));
|
|
|
|
},
|
|
|
|
|
|
|
|
moveDown() {
|
|
|
|
this.sendAction('moveDownAction', this.get('userField'));
|
|
|
|
},
|
|
|
|
|
2015-07-27 14:22:12 -04:00
|
|
|
edit() {
|
2015-07-10 12:28:56 -04:00
|
|
|
this.set('editing', true);
|
|
|
|
},
|
|
|
|
|
2015-07-27 14:22:12 -04:00
|
|
|
destroy() {
|
2015-07-10 12:28:56 -04:00
|
|
|
this.sendAction('destroyAction', this.get('userField'));
|
|
|
|
},
|
|
|
|
|
2015-07-27 14:22:12 -04:00
|
|
|
cancel() {
|
2015-07-10 12:28:56 -04:00
|
|
|
const id = this.get('userField.id');
|
|
|
|
if (Ember.isEmpty(id)) {
|
|
|
|
this.sendAction('destroyAction', this.get('userField'));
|
|
|
|
} else {
|
|
|
|
this.rollbackBuffer();
|
|
|
|
this.set('editing', false);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|