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

69 lines
1.6 KiB
Plaintext
Raw Normal View History

2018-06-15 23:03:24 +08:00
import { popupAjaxError } from "discourse/lib/ajax-error";
2015-07-31 02:52:53 +08:00
const MAX_FIELDS = 20;
export default Ember.Controller.extend({
fieldTypes: null,
2019-01-12 00:54:23 +08:00
createDisabled: Ember.computed.gte("model.length", MAX_FIELDS),
2018-06-15 23:03:24 +08:00
sortedFields: Ember.computed.sort("model", "fieldSortOrder"),
init() {
this._super(...arguments);
this.fieldSortOrder = ["position"];
},
actions: {
createField() {
2018-06-15 23:03:24 +08:00
const f = this.store.createRecord("user-field", {
field_type: "text",
position: MAX_FIELDS
});
this.model.pushObject(f);
2015-07-31 02:52:53 +08:00
},
moveUp(f) {
const idx = this.sortedFields.indexOf(f);
2015-07-31 02:52:53 +08:00
if (idx) {
const prev = this.sortedFields.objectAt(idx - 1);
2018-06-15 23:03:24 +08:00
const prevPos = prev.get("position");
2015-07-31 02:52:53 +08:00
2018-06-15 23:03:24 +08:00
prev.update({ position: f.get("position") });
2015-07-31 02:52:53 +08:00
f.update({ position: prevPos });
}
},
moveDown(f) {
const idx = this.sortedFields.indexOf(f);
2015-07-31 02:52:53 +08:00
if (idx > -1) {
const next = this.sortedFields.objectAt(idx + 1);
2018-06-15 23:03:24 +08:00
const nextPos = next.get("position");
2015-07-31 02:52:53 +08:00
2018-06-15 23:03:24 +08:00
next.update({ position: f.get("position") });
2015-07-31 02:52:53 +08:00
f.update({ position: nextPos });
}
},
destroy(f) {
const model = this.model;
// Only confirm if we already been saved
2018-06-15 23:03:24 +08:00
if (f.get("id")) {
bootbox.confirm(I18n.t("admin.user_fields.delete_confirm"), function(
result
) {
2015-07-31 02:52:53 +08:00
if (result) {
2018-06-15 23:03:24 +08:00
f.destroyRecord()
.then(function() {
model.removeObject(f);
})
.catch(popupAjaxError);
2015-07-31 02:52:53 +08:00
}
});
} else {
2015-07-31 02:52:53 +08:00
model.removeObject(f);
}
}
}
});