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

82 lines
2.1 KiB
Plaintext
Raw Normal View History

2018-06-15 11:03:24 -04:00
import { ajax } from "discourse/lib/ajax";
import ModalFunctionality from "discourse/mixins/modal-functionality";
import { observes } from "ember-addons/ember-computed-decorators";
export default Ember.Controller.extend(ModalFunctionality, {
@observes("model")
modelChanged() {
2018-06-15 11:03:24 -04:00
const model = this.get("model");
2019-01-11 11:54:23 -05:00
const copy = Ember.A();
const store = this.store;
2014-07-27 04:22:01 -04:00
2018-06-15 11:03:24 -04:00
if (model) {
model.forEach(o =>
copy.pushObject(store.createRecord("badge-grouping", o))
);
2014-07-27 04:22:01 -04:00
}
2018-06-15 11:03:24 -04:00
this.set("workingCopy", copy);
},
2014-07-27 04:22:01 -04:00
moveItem(item, delta) {
2018-06-15 11:03:24 -04:00
const copy = this.get("workingCopy");
const index = copy.indexOf(item);
2018-06-15 11:03:24 -04:00
if (index + delta < 0 || index + delta >= copy.length) {
2014-07-27 04:22:01 -04:00
return;
}
copy.removeAt(index);
2018-06-15 11:03:24 -04:00
copy.insertAt(index + delta, item);
2014-07-27 04:22:01 -04:00
},
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) {
2018-06-15 11:03:24 -04:00
this.get("workingCopy").removeObject(item);
2014-07-27 04:22:01 -04:00
},
cancel() {
this.setProperties({ model: null, workingCopy: null });
2018-06-15 11:03:24 -04:00
this.send("closeModal");
2014-07-27 04:22:01 -04:00
},
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() {
2018-06-15 11:03:24 -04:00
const obj = this.store.createRecord("badge-grouping", {
editing: true,
name: I18n.t("admin.badges.badge_grouping")
});
this.get("workingCopy").pushObject(obj);
2014-07-27 04:22:01 -04:00
},
saveAll() {
let items = this.get("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
ajax("/admin/badges/badge_groupings", {
data: { ids: groupIds, names },
2018-06-15 11:03:24 -04:00
method: "POST"
}).then(
data => {
items = this.get("model");
2018-06-15 11:03:24 -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
},
() => bootbox.alert(I18n.t("generic_error"))
2018-06-15 11:03:24 -04:00
);
2014-07-27 04:22:01 -04:00
}
}
});