2020-05-14 04:23:41 +08:00
|
|
|
import I18n from "I18n";
|
2019-10-31 04:28:29 +08:00
|
|
|
import { gte, sort } from "@ember/object/computed";
|
2019-10-24 01:06:54 +08:00
|
|
|
import Controller from "@ember/controller";
|
2015-07-31 02:52:53 +08:00
|
|
|
import { popupAjaxError } from "discourse/lib/ajax-error";
|
2014-09-25 23:32:08 +08:00
|
|
|
|
2015-07-31 02:52:53 +08:00
|
|
|
const MAX_FIELDS = 20;
|
|
|
|
|
2019-10-24 01:06:54 +08:00
|
|
|
export default Controller.extend({
|
2014-09-25 23:32:08 +08:00
|
|
|
fieldTypes: null,
|
2019-10-31 04:28:29 +08:00
|
|
|
createDisabled: gte("model.length", MAX_FIELDS),
|
|
|
|
sortedFields: sort("model", "fieldSortOrder"),
|
2014-09-25 23:32:08 +08:00
|
|
|
|
2019-05-28 18:15:12 +08:00
|
|
|
init() {
|
|
|
|
this._super(...arguments);
|
|
|
|
|
|
|
|
this.fieldSortOrder = ["position"];
|
|
|
|
},
|
|
|
|
|
2014-09-25 23:32:08 +08:00
|
|
|
actions: {
|
2015-07-28 02:22:12 +08:00
|
|
|
createField() {
|
2015-07-31 02:52:53 +08:00
|
|
|
const f = this.store.createRecord("user-field", {
|
|
|
|
field_type: "text",
|
|
|
|
position: MAX_FIELDS
|
|
|
|
});
|
2019-05-27 16:15:39 +08:00
|
|
|
this.model.pushObject(f);
|
2015-07-31 02:52:53 +08:00
|
|
|
},
|
|
|
|
|
|
|
|
moveUp(f) {
|
2019-05-27 16:15:39 +08:00
|
|
|
const idx = this.sortedFields.indexOf(f);
|
2015-07-31 02:52:53 +08:00
|
|
|
if (idx) {
|
2019-05-27 16:15:39 +08:00
|
|
|
const prev = this.sortedFields.objectAt(idx - 1);
|
2015-07-31 02:52:53 +08:00
|
|
|
const prevPos = prev.get("position");
|
|
|
|
|
|
|
|
prev.update({ position: f.get("position") });
|
|
|
|
f.update({ position: prevPos });
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
|
|
|
moveDown(f) {
|
2019-05-27 16:15:39 +08:00
|
|
|
const idx = this.sortedFields.indexOf(f);
|
2015-07-31 02:52:53 +08:00
|
|
|
if (idx > -1) {
|
2019-05-27 16:15:39 +08:00
|
|
|
const next = this.sortedFields.objectAt(idx + 1);
|
2015-07-31 02:52:53 +08:00
|
|
|
const nextPos = next.get("position");
|
|
|
|
|
|
|
|
next.update({ position: f.get("position") });
|
|
|
|
f.update({ position: nextPos });
|
|
|
|
}
|
2014-09-25 23:32:08 +08:00
|
|
|
},
|
|
|
|
|
2015-07-28 02:22:12 +08:00
|
|
|
destroy(f) {
|
2019-05-27 16:15:39 +08:00
|
|
|
const model = this.model;
|
2014-09-25 23:32:08 +08: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-31 02:52:53 +08:00
|
|
|
if (result) {
|
|
|
|
f.destroyRecord()
|
|
|
|
.then(function() {
|
|
|
|
model.removeObject(f);
|
|
|
|
})
|
|
|
|
.catch(popupAjaxError);
|
|
|
|
}
|
2014-09-25 23:32:08 +08:00
|
|
|
});
|
|
|
|
} else {
|
2015-07-31 02:52:53 +08:00
|
|
|
model.removeObject(f);
|
2014-09-25 23:32:08 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|