2020-03-07 06:49:28 +08:00
|
|
|
import { A } from "@ember/array";
|
2016-07-01 01:55:44 +08:00
|
|
|
import Controller from "@ember/controller";
|
2020-05-14 04:23:41 +08:00
|
|
|
import I18n from "I18n";
|
2018-01-24 09:33:43 +08:00
|
|
|
import ModalFunctionality from "discourse/mixins/modal-functionality";
|
2019-11-08 05:38:28 +08:00
|
|
|
import { ajax } from "discourse/lib/ajax";
|
2020-08-27 00:57:13 +08:00
|
|
|
import bootbox from "bootbox";
|
2019-11-08 05:38:28 +08:00
|
|
|
import { observes } from "discourse-common/utils/decorators";
|
2018-01-24 09:33:43 +08:00
|
|
|
|
2019-10-24 01:06:54 +08:00
|
|
|
export default Controller.extend(ModalFunctionality, {
|
2019-02-19 16:30:24 +08:00
|
|
|
@observes("model")
|
|
|
|
modelChanged() {
|
2019-05-27 16:15:39 +08:00
|
|
|
const model = this.model;
|
2020-03-07 06:49:28 +08:00
|
|
|
const copy = A();
|
2015-12-27 06:58:54 +08:00
|
|
|
const store = this.store;
|
2014-07-27 16:22:01 +08:00
|
|
|
|
|
|
|
if (model) {
|
2019-02-19 16:30:24 +08:00
|
|
|
model.forEach((o) =>
|
|
|
|
copy.pushObject(store.createRecord("badge-grouping", o))
|
|
|
|
);
|
2014-07-27 16:22:01 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
this.set("workingCopy", copy);
|
2019-02-19 16:30:24 +08:00
|
|
|
},
|
2014-07-27 16:22:01 +08:00
|
|
|
|
2019-02-19 16:30:24 +08:00
|
|
|
moveItem(item, delta) {
|
2019-05-27 16:15:39 +08:00
|
|
|
const copy = this.workingCopy;
|
2015-12-27 06:58:54 +08:00
|
|
|
const index = copy.indexOf(item);
|
2014-07-27 16:22:01 +08:00
|
|
|
if (index + delta < 0 || index + delta >= copy.length) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
copy.removeAt(index);
|
|
|
|
copy.insertAt(index + delta, item);
|
|
|
|
},
|
|
|
|
|
|
|
|
actions: {
|
2019-02-19 16:30:24 +08:00
|
|
|
up(item) {
|
2014-07-27 16:22:01 +08:00
|
|
|
this.moveItem(item, -1);
|
|
|
|
},
|
2019-02-19 16:30:24 +08:00
|
|
|
down(item) {
|
2014-07-27 16:22:01 +08:00
|
|
|
this.moveItem(item, 1);
|
|
|
|
},
|
2019-02-19 16:30:24 +08:00
|
|
|
delete(item) {
|
2019-05-27 16:15:39 +08:00
|
|
|
this.workingCopy.removeObject(item);
|
2014-07-27 16:22:01 +08:00
|
|
|
},
|
2019-02-19 16:30:24 +08:00
|
|
|
cancel() {
|
|
|
|
this.setProperties({ model: null, workingCopy: null });
|
2014-07-27 16:22:01 +08:00
|
|
|
this.send("closeModal");
|
|
|
|
},
|
2019-02-19 16:30:24 +08:00
|
|
|
edit(item) {
|
2014-07-27 16:22:01 +08:00
|
|
|
item.set("editing", true);
|
|
|
|
},
|
2019-02-19 16:30:24 +08:00
|
|
|
save(item) {
|
2014-07-27 16:22:01 +08:00
|
|
|
item.set("editing", false);
|
|
|
|
},
|
2019-02-19 16:30:24 +08:00
|
|
|
add() {
|
2015-12-27 06:58:54 +08:00
|
|
|
const obj = this.store.createRecord("badge-grouping", {
|
|
|
|
editing: true,
|
|
|
|
name: I18n.t("admin.badges.badge_grouping"),
|
|
|
|
});
|
2019-05-27 16:15:39 +08:00
|
|
|
this.workingCopy.pushObject(obj);
|
2014-07-27 16:22:01 +08:00
|
|
|
},
|
2019-02-19 16:30:24 +08:00
|
|
|
saveAll() {
|
2019-05-27 16:15:39 +08:00
|
|
|
let items = this.workingCopy;
|
2019-02-19 16:30:24 +08:00
|
|
|
const groupIds = items.map((i) => i.get("id") || -1);
|
|
|
|
const names = items.map((i) => i.get("name"));
|
2018-06-15 23:03:24 +08:00
|
|
|
|
2016-07-01 01:55:44 +08:00
|
|
|
ajax("/admin/badges/badge_groupings", {
|
2019-02-19 16:30:24 +08:00
|
|
|
data: { ids: groupIds, names },
|
2020-03-27 04:00:10 +08:00
|
|
|
type: "POST",
|
2018-06-15 23:03:24 +08:00
|
|
|
}).then(
|
2019-02-19 16:30:24 +08:00
|
|
|
(data) => {
|
2019-05-27 16:15:39 +08:00
|
|
|
items = this.model;
|
2014-07-27 16:22:01 +08:00
|
|
|
items.clear();
|
2019-02-19 16:30:24 +08:00
|
|
|
data.badge_groupings.forEach((g) => {
|
|
|
|
items.pushObject(this.store.createRecord("badge-grouping", g));
|
2018-06-15 23:03:24 +08:00
|
|
|
});
|
2019-02-19 16:30:24 +08:00
|
|
|
this.setProperties({ model: null, workingCopy: null });
|
|
|
|
this.send("closeModal");
|
2018-06-15 23:03:24 +08:00
|
|
|
},
|
2019-02-19 16:30:24 +08:00
|
|
|
() => bootbox.alert(I18n.t("generic_error"))
|
2018-06-15 23:03:24 +08:00
|
|
|
);
|
2014-07-27 16:22:01 +08:00
|
|
|
},
|
|
|
|
},
|
|
|
|
});
|