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

89 lines
2.3 KiB
Plaintext
Raw Normal View History

2018-06-15 23:03:24 +08:00
import { ajax } from "discourse/lib/ajax";
import ModalFunctionality from "discourse/mixins/modal-functionality";
export default Ember.Controller.extend(ModalFunctionality, {
2018-06-15 23:03:24 +08:00
modelChanged: function() {
const model = this.get("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(function(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);
}.observes("model"),
2014-07-27 16:22:01 +08:00
2018-06-15 23:03:24 +08:00
moveItem: function(item, delta) {
const copy = this.get("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: {
2018-06-15 23:03:24 +08:00
up: function(item) {
2014-07-27 16:22:01 +08:00
this.moveItem(item, -1);
},
2018-06-15 23:03:24 +08:00
down: function(item) {
2014-07-27 16:22:01 +08:00
this.moveItem(item, 1);
},
2018-06-15 23:03:24 +08:00
delete: function(item) {
this.get("workingCopy").removeObject(item);
2014-07-27 16:22:01 +08:00
},
2018-06-15 23:03:24 +08:00
cancel: function() {
this.set("model", null);
this.set("workingCopy", null);
this.send("closeModal");
2014-07-27 16:22:01 +08:00
},
2018-06-15 23:03:24 +08:00
edit: function(item) {
2014-07-27 16:22:01 +08:00
item.set("editing", true);
},
2018-06-15 23:03:24 +08:00
save: function(item) {
2014-07-27 16:22:01 +08:00
item.set("editing", false);
},
2018-06-15 23:03:24 +08:00
add: function() {
const obj = this.store.createRecord("badge-grouping", {
editing: true,
name: I18n.t("admin.badges.badge_grouping")
});
this.get("workingCopy").pushObject(obj);
2014-07-27 16:22:01 +08:00
},
2018-06-15 23:03:24 +08:00
saveAll: function() {
const self = this;
2018-06-15 23:03:24 +08:00
var items = this.get("workingCopy");
const groupIds = items.map(function(i) {
return i.get("id") || -1;
2014-07-27 16:22:01 +08:00
});
2018-06-15 23:03:24 +08:00
const names = items.map(function(i) {
return i.get("name");
});
ajax("/admin/badges/badge_groupings", {
data: { ids: groupIds, names: names },
method: "POST"
}).then(
function(data) {
items = self.get("model");
items.clear();
data.badge_groupings.forEach(function(g) {
items.pushObject(self.store.createRecord("badge-grouping", g));
});
self.set("model", null);
self.set("workingCopy", null);
self.send("closeModal");
},
function() {
bootbox.alert(I18n.t("generic_error"));
}
);
2014-07-27 16:22:01 +08:00
}
}
});