discourse/app/assets/javascripts/admin/addon/controllers/modals/admin-edit-badge-groupings.js

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

86 lines
2.2 KiB
JavaScript
Raw Normal View History

import { A } from "@ember/array";
2016-07-01 01:55:44 +08:00
import Controller from "@ember/controller";
import I18n from "I18n";
import ModalFunctionality from "discourse/mixins/modal-functionality";
import { ajax } from "discourse/lib/ajax";
import bootbox from "bootbox";
import { observes } from "discourse-common/utils/decorators";
export default Controller.extend(ModalFunctionality, {
@observes("model")
modelChanged() {
const model = this.model;
const copy = A();
const store = this.store;
2014-07-27 16:22:01 +08:00
if (model) {
model.forEach((o) =>
copy.pushObject(store.createRecord("badge-grouping", o))
);
2014-07-27 16:22:01 +08:00
}
this.set("workingCopy", copy);
},
2014-07-27 16:22:01 +08:00
moveItem(item, delta) {
const copy = this.workingCopy;
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: {
up(item) {
2014-07-27 16:22:01 +08:00
this.moveItem(item, -1);
},
down(item) {
2014-07-27 16:22:01 +08:00
this.moveItem(item, 1);
},
delete(item) {
this.workingCopy.removeObject(item);
2014-07-27 16:22:01 +08:00
},
cancel() {
this.setProperties({ model: null, workingCopy: null });
2014-07-27 16:22:01 +08:00
this.send("closeModal");
},
edit(item) {
2014-07-27 16:22:01 +08:00
item.set("editing", true);
},
save(item) {
2014-07-27 16:22:01 +08:00
item.set("editing", false);
},
add() {
const obj = this.store.createRecord("badge-grouping", {
editing: true,
name: I18n.t("admin.badges.badge_grouping"),
});
this.workingCopy.pushObject(obj);
2014-07-27 16:22:01 +08:00
},
saveAll() {
let items = this.workingCopy;
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", {
data: { ids: groupIds, names },
type: "POST",
2018-06-15 23:03:24 +08:00
}).then(
(data) => {
items = this.model;
2014-07-27 16:22:01 +08:00
items.clear();
data.badge_groupings.forEach((g) => {
items.pushObject(this.store.createRecord("badge-grouping", g));
2018-06-15 23:03:24 +08:00
});
this.setProperties({ model: null, workingCopy: null });
this.send("closeModal");
2018-06-15 23:03: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
},
},
});