2019-10-24 01:06:54 +08:00
|
|
|
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";
|
2019-02-19 16:30:24 +08:00
|
|
|
import { observes } from "ember-addons/ember-computed-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;
|
2019-01-12 00:54:23 +08:00
|
|
|
const copy = Ember.A();
|
2015-12-27 06:58:54 +08:00
|
|
|
const store = this.store;
|
2014-07-27 16:22:01 +08:00
|
|
|
|
2018-06-15 23:03:24 +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
|
|
|
}
|
|
|
|
|
2018-06-15 23:03:24 +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);
|
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: {
|
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 });
|
2018-06-15 23:03:24 +08:00
|
|
|
this.send("closeModal");
|
2014-07-27 16:22:01 +08:00
|
|
|
},
|
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() {
|
2018-06-15 23:03:24 +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
|
|
|
|
|
|
|
ajax("/admin/badges/badge_groupings", {
|
2019-02-19 16:30:24 +08:00
|
|
|
data: { ids: groupIds, names },
|
2018-06-15 23:03:24 +08:00
|
|
|
method: "POST"
|
|
|
|
}).then(
|
2019-02-19 16:30:24 +08:00
|
|
|
data => {
|
2019-05-27 16:15:39 +08:00
|
|
|
items = this.model;
|
2018-06-15 23:03:24 +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
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|