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.

88 lines
2.2 KiB
JavaScript
Raw Normal View History

import { A } from "@ember/array";
2016-06-30 13:55:44 -04:00
import Controller from "@ember/controller";
import I18n from "I18n";
import ModalFunctionality from "discourse/mixins/modal-functionality";
import { ajax } from "discourse/lib/ajax";
import { observes } from "discourse-common/utils/decorators";
2022-09-27 14:47:13 -04:00
import { inject as service } from "@ember/service";
export default Controller.extend(ModalFunctionality, {
2022-09-27 14:47:13 -04:00
dialog: service(),
@observes("model")
modelChanged() {
const model = this.model;
const copy = A();
const store = this.store;
2014-07-27 04:22:01 -04:00
if (model) {
model.forEach((o) =>
copy.pushObject(store.createRecord("badge-grouping", o))
);
2014-07-27 04:22:01 -04:00
}
this.set("workingCopy", copy);
},
2014-07-27 04:22:01 -04:00
moveItem(item, delta) {
const copy = this.workingCopy;
const index = copy.indexOf(item);
2014-07-27 04:22:01 -04:00
if (index + delta < 0 || index + delta >= copy.length) {
return;
}
copy.removeAt(index);
copy.insertAt(index + delta, item);
},
actions: {
up(item) {
2014-07-27 04:22:01 -04:00
this.moveItem(item, -1);
},
down(item) {
2014-07-27 04:22:01 -04:00
this.moveItem(item, 1);
},
delete(item) {
this.workingCopy.removeObject(item);
2014-07-27 04:22:01 -04:00
},
cancel() {
this.setProperties({ model: null, workingCopy: null });
2014-07-27 04:22:01 -04:00
this.send("closeModal");
},
edit(item) {
2014-07-27 04:22:01 -04:00
item.set("editing", true);
},
save(item) {
2014-07-27 04:22:01 -04: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 04:22:01 -04: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 11:03:24 -04:00
2016-06-30 13:55:44 -04:00
ajax("/admin/badges/badge_groupings", {
data: { ids: groupIds, names },
type: "POST",
2018-06-15 11:03:24 -04:00
}).then(
(data) => {
items = this.model;
2014-07-27 04:22:01 -04:00
items.clear();
data.badge_groupings.forEach((g) => {
items.pushObject(this.store.createRecord("badge-grouping", g));
2018-06-15 11:03:24 -04:00
});
this.setProperties({ model: null, workingCopy: null });
this.send("closeModal");
2018-06-15 11:03:24 -04:00
},
2022-09-27 14:47:13 -04:00
() => this.dialog.alert(I18n.t("generic_error"))
2018-06-15 11:03:24 -04:00
);
2014-07-27 04:22:01 -04:00
},
},
});