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

83 lines
2.1 KiB
Plaintext
Raw Normal View History

import Controller from "@ember/controller";
2018-06-15 23:03:24 +08:00
import { ajax } from "discourse/lib/ajax";
import ModalFunctionality from "discourse/mixins/modal-functionality";
import { observes } from "ember-addons/ember-computed-decorators";
export default Controller.extend(ModalFunctionality, {
@observes("model")
modelChanged() {
const model = this.model;
2019-01-12 00:54:23 +08:00
const copy = Ember.A();
const store = this.store;
2014-07-27 16:22:01 +08:00
2018-06-15 23:03:24 +08:00
if (model) {
model.forEach(o =>
copy.pushObject(store.createRecord("badge-grouping", o))
);
2014-07-27 16:22:01 +08:00
}
2018-06-15 23:03:24 +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);
2018-06-15 23:03:24 +08:00
if (index + delta < 0 || index + delta >= copy.length) {
2014-07-27 16:22:01 +08:00
return;
}
copy.removeAt(index);
2018-06-15 23:03:24 +08:00
copy.insertAt(index + delta, item);
2014-07-27 16:22:01 +08:00
},
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 });
2018-06-15 23:03:24 +08:00
this.send("closeModal");
2014-07-27 16:22:01 +08:00
},
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() {
2018-06-15 23:03:24 +08:00
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
ajax("/admin/badges/badge_groupings", {
data: { ids: groupIds, names },
2018-06-15 23:03:24 +08:00
method: "POST"
}).then(
data => {
items = this.model;
2018-06-15 23:03:24 +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
}
}
});