discourse/app/assets/javascripts/admin/controllers/admin-user-fields.js.es6

70 lines
1.7 KiB
Plaintext
Raw Normal View History

import Controller from "@ember/controller";
2018-06-15 11:03:24 -04:00
import { popupAjaxError } from "discourse/lib/ajax-error";
2015-07-30 14:52:53 -04:00
const MAX_FIELDS = 20;
export default Controller.extend({
fieldTypes: null,
2019-01-11 11:54:23 -05:00
createDisabled: Ember.computed.gte("model.length", MAX_FIELDS),
2018-06-15 11:03:24 -04:00
sortedFields: Ember.computed.sort("model", "fieldSortOrder"),
init() {
this._super(...arguments);
this.fieldSortOrder = ["position"];
},
actions: {
createField() {
2018-06-15 11:03:24 -04:00
const f = this.store.createRecord("user-field", {
field_type: "text",
position: MAX_FIELDS
});
this.model.pushObject(f);
2015-07-30 14:52:53 -04:00
},
moveUp(f) {
const idx = this.sortedFields.indexOf(f);
2015-07-30 14:52:53 -04:00
if (idx) {
const prev = this.sortedFields.objectAt(idx - 1);
2018-06-15 11:03:24 -04:00
const prevPos = prev.get("position");
2015-07-30 14:52:53 -04:00
2018-06-15 11:03:24 -04:00
prev.update({ position: f.get("position") });
2015-07-30 14:52:53 -04:00
f.update({ position: prevPos });
}
},
moveDown(f) {
const idx = this.sortedFields.indexOf(f);
2015-07-30 14:52:53 -04:00
if (idx > -1) {
const next = this.sortedFields.objectAt(idx + 1);
2018-06-15 11:03:24 -04:00
const nextPos = next.get("position");
2015-07-30 14:52:53 -04:00
2018-06-15 11:03:24 -04:00
next.update({ position: f.get("position") });
2015-07-30 14:52:53 -04:00
f.update({ position: nextPos });
}
},
destroy(f) {
const model = this.model;
// Only confirm if we already been saved
2018-06-15 11:03:24 -04:00
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) {
2018-06-15 11:03:24 -04:00
f.destroyRecord()
.then(function() {
model.removeObject(f);
})
.catch(popupAjaxError);
2015-07-30 14:52:53 -04:00
}
});
} else {
2015-07-30 14:52:53 -04:00
model.removeObject(f);
}
}
}
});