2015-07-30 14:52:53 -04:00
|
|
|
import { popupAjaxError } from 'discourse/lib/ajax-error';
|
2014-09-25 11:32:08 -04:00
|
|
|
|
2015-07-30 14:52:53 -04:00
|
|
|
const MAX_FIELDS = 20;
|
|
|
|
|
|
|
|
export default Ember.Controller.extend({
|
2014-09-25 11:32:08 -04:00
|
|
|
fieldTypes: null,
|
2015-07-30 14:52:53 -04:00
|
|
|
createDisabled: Em.computed.gte('model.length', MAX_FIELDS),
|
2014-10-02 15:56:28 -04:00
|
|
|
|
2015-07-30 14:52:53 -04:00
|
|
|
arrangedContent: function() {
|
|
|
|
return Ember.ArrayProxy.extend(Ember.SortableMixin).create({
|
|
|
|
sortProperties: ['position'],
|
|
|
|
content: this.get('model')
|
2014-09-25 11:32:08 -04:00
|
|
|
});
|
2015-07-30 14:52:53 -04:00
|
|
|
}.property('model'),
|
2014-09-25 11:32:08 -04:00
|
|
|
|
|
|
|
actions: {
|
2015-07-27 14:22:12 -04:00
|
|
|
createField() {
|
2015-07-30 14:52:53 -04:00
|
|
|
const f = this.store.createRecord('user-field', { field_type: 'text', position: MAX_FIELDS });
|
|
|
|
this.get('model').pushObject(f);
|
|
|
|
},
|
|
|
|
|
|
|
|
moveUp(f) {
|
|
|
|
const idx = this.get('arrangedContent').indexOf(f);
|
|
|
|
if (idx) {
|
|
|
|
const prev = this.get('arrangedContent').objectAt(idx-1);
|
|
|
|
const prevPos = prev.get('position');
|
|
|
|
|
|
|
|
prev.update({ position: f.get('position') });
|
|
|
|
f.update({ position: prevPos });
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
|
|
|
moveDown(f) {
|
|
|
|
const idx = this.get('arrangedContent').indexOf(f);
|
|
|
|
if (idx > -1) {
|
|
|
|
const next = this.get('arrangedContent').objectAt(idx+1);
|
|
|
|
const nextPos = next.get('position');
|
|
|
|
|
|
|
|
next.update({ position: f.get('position') });
|
|
|
|
f.update({ position: nextPos });
|
|
|
|
}
|
2014-09-25 11:32:08 -04:00
|
|
|
},
|
|
|
|
|
2015-07-27 14:22:12 -04:00
|
|
|
destroy(f) {
|
2015-07-30 14:52:53 -04:00
|
|
|
const model = this.get('model');
|
2014-09-25 11:32:08 -04:00
|
|
|
|
|
|
|
// Only confirm if we already been saved
|
|
|
|
if (f.get('id')) {
|
|
|
|
bootbox.confirm(I18n.t("admin.user_fields.delete_confirm"), function(result) {
|
2015-07-30 14:52:53 -04:00
|
|
|
if (result) {
|
|
|
|
f.destroyRecord().then(function() {
|
|
|
|
model.removeObject(f);
|
|
|
|
}).catch(popupAjaxError);
|
|
|
|
}
|
2014-09-25 11:32:08 -04:00
|
|
|
});
|
|
|
|
} else {
|
2015-07-30 14:52:53 -04:00
|
|
|
model.removeObject(f);
|
2014-09-25 11:32:08 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|