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

65 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,
2018-06-15 23:03:24 +08:00
createDisabled: Em.computed.gte("model.length", MAX_FIELDS),
2018-06-15 23:03:24 +08:00
fieldSortOrder: ["position"],
sortedFields: Ember.computed.sort("model", "fieldSortOrder"),
actions: {
createField() {
2018-06-15 23:03:24 +08:00
const f = this.store.createRecord("user-field", {
field_type: "text",
position: MAX_FIELDS
});
this.get("model").pushObject(f);
2015-07-31 02:52:53 +08:00
},
moveUp(f) {
2018-06-15 23:03:24 +08:00
const idx = this.get("sortedFields").indexOf(f);
2015-07-31 02:52:53 +08:00
if (idx) {
2018-06-15 23:03:24 +08:00
const prev = this.get("sortedFields").objectAt(idx - 1);
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) {
2018-06-15 23:03:24 +08:00
const idx = this.get("sortedFields").indexOf(f);
2015-07-31 02:52:53 +08:00
if (idx > -1) {
2018-06-15 23:03:24 +08:00
const next = this.get("sortedFields").objectAt(idx + 1);
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) {
2018-06-15 23:03:24 +08:00
const model = this.get("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);
}
}
}
});