2019-10-23 13:06:54 -04:00
|
|
|
import Controller from "@ember/controller";
|
2016-06-30 13:55:44 -04:00
|
|
|
import { ajax } from "discourse/lib/ajax";
|
2018-01-23 20:33:43 -05:00
|
|
|
import ModalFunctionality from "discourse/mixins/modal-functionality";
|
2019-11-07 16:38:28 -05:00
|
|
|
import { observes } from "discourse-common/utils/decorators";
|
2018-01-23 20:33:43 -05:00
|
|
|
|
2019-10-23 13:06:54 -04:00
|
|
|
export default Controller.extend(ModalFunctionality, {
|
2019-02-19 03:30:24 -05:00
|
|
|
@observes("model")
|
|
|
|
modelChanged() {
|
2019-05-27 04:15:39 -04:00
|
|
|
const model = this.model;
|
2019-01-11 11:54:23 -05:00
|
|
|
const copy = Ember.A();
|
2015-12-26 17:58:54 -05:00
|
|
|
const store = this.store;
|
2014-07-27 04:22:01 -04:00
|
|
|
|
|
|
|
if (model) {
|
2019-02-19 03:30:24 -05:00
|
|
|
model.forEach(o =>
|
|
|
|
copy.pushObject(store.createRecord("badge-grouping", o))
|
|
|
|
);
|
2014-07-27 04:22:01 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
this.set("workingCopy", copy);
|
2019-02-19 03:30:24 -05:00
|
|
|
},
|
2014-07-27 04:22:01 -04:00
|
|
|
|
2019-02-19 03:30:24 -05:00
|
|
|
moveItem(item, delta) {
|
2019-05-27 04:15:39 -04:00
|
|
|
const copy = this.workingCopy;
|
2015-12-26 17:58:54 -05:00
|
|
|
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: {
|
2019-02-19 03:30:24 -05:00
|
|
|
up(item) {
|
2014-07-27 04:22:01 -04:00
|
|
|
this.moveItem(item, -1);
|
|
|
|
},
|
2019-02-19 03:30:24 -05:00
|
|
|
down(item) {
|
2014-07-27 04:22:01 -04:00
|
|
|
this.moveItem(item, 1);
|
|
|
|
},
|
2019-02-19 03:30:24 -05:00
|
|
|
delete(item) {
|
2019-05-27 04:15:39 -04:00
|
|
|
this.workingCopy.removeObject(item);
|
2014-07-27 04:22:01 -04:00
|
|
|
},
|
2019-02-19 03:30:24 -05:00
|
|
|
cancel() {
|
|
|
|
this.setProperties({ model: null, workingCopy: null });
|
2014-07-27 04:22:01 -04:00
|
|
|
this.send("closeModal");
|
|
|
|
},
|
2019-02-19 03:30:24 -05:00
|
|
|
edit(item) {
|
2014-07-27 04:22:01 -04:00
|
|
|
item.set("editing", true);
|
|
|
|
},
|
2019-02-19 03:30:24 -05:00
|
|
|
save(item) {
|
2014-07-27 04:22:01 -04:00
|
|
|
item.set("editing", false);
|
|
|
|
},
|
2019-02-19 03:30:24 -05:00
|
|
|
add() {
|
2015-12-26 17:58:54 -05:00
|
|
|
const obj = this.store.createRecord("badge-grouping", {
|
|
|
|
editing: true,
|
|
|
|
name: I18n.t("admin.badges.badge_grouping")
|
|
|
|
});
|
2019-05-27 04:15:39 -04:00
|
|
|
this.workingCopy.pushObject(obj);
|
2014-07-27 04:22:01 -04:00
|
|
|
},
|
2019-02-19 03:30:24 -05:00
|
|
|
saveAll() {
|
2019-05-27 04:15:39 -04:00
|
|
|
let items = this.workingCopy;
|
2019-02-19 03:30:24 -05:00
|
|
|
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", {
|
2019-02-19 03:30:24 -05:00
|
|
|
data: { ids: groupIds, names },
|
2014-07-27 04:22:01 -04:00
|
|
|
method: "POST"
|
2018-06-15 11:03:24 -04:00
|
|
|
}).then(
|
2019-02-19 03:30:24 -05:00
|
|
|
data => {
|
2019-05-27 04:15:39 -04:00
|
|
|
items = this.model;
|
2014-07-27 04:22:01 -04:00
|
|
|
items.clear();
|
2019-02-19 03:30:24 -05:00
|
|
|
data.badge_groupings.forEach(g => {
|
|
|
|
items.pushObject(this.store.createRecord("badge-grouping", g));
|
2018-06-15 11:03:24 -04:00
|
|
|
});
|
2019-02-19 03:30:24 -05:00
|
|
|
this.setProperties({ model: null, workingCopy: null });
|
|
|
|
this.send("closeModal");
|
2018-06-15 11:03:24 -04:00
|
|
|
},
|
2019-02-19 03:30:24 -05:00
|
|
|
() => bootbox.alert(I18n.t("generic_error"))
|
2018-06-15 11:03:24 -04:00
|
|
|
);
|
2014-07-27 04:22:01 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|