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

78 lines
2.1 KiB
Plaintext
Raw Normal View History

2016-07-01 01:55:44 +08:00
import { ajax } from 'discourse/lib/ajax';
2014-07-27 16:22:01 +08:00
export default Ember.Controller.extend({
modelChanged: function(){
const model = this.get('model');
const copy = Em.A();
const store = this.store;
2014-07-27 16:22:01 +08:00
if(model){
model.forEach(function(o){
copy.pushObject(store.createRecord('badge-grouping', o));
2014-07-27 16:22:01 +08:00
});
}
this.set('workingCopy', copy);
}.observes('model'),
moveItem: function(item, delta){
const copy = this.get('workingCopy');
const index = copy.indexOf(item);
2014-07-27 16:22:01 +08:00
if (index + delta < 0 || index + delta >= copy.length){
return;
}
copy.removeAt(index);
copy.insertAt(index+delta, item);
},
actions: {
up: function(item){
this.moveItem(item, -1);
},
down: function(item){
this.moveItem(item, 1);
},
"delete": function(item){
this.get('workingCopy').removeObject(item);
},
cancel: function(){
this.set('model', null);
this.set('workingCopy', null);
this.send('closeModal');
},
edit: function(item){
item.set("editing", true);
},
save: function(item){
item.set("editing", false);
},
add: function(){
const obj = this.store.createRecord('badge-grouping', {editing: true, name: I18n.t('admin.badges.badge_grouping')});
2014-07-27 16:22:01 +08:00
this.get('workingCopy').pushObject(obj);
},
saveAll: function(){
const self = this;
2014-07-27 16:22:01 +08:00
var items = this.get('workingCopy');
const groupIds = items.map(function(i){return i.get("id") || -1;});
const names = items.map(function(i){return i.get("name");});
2014-07-27 16:22:01 +08:00
2016-07-01 01:55:44 +08:00
ajax('/admin/badges/badge_groupings',{
2014-07-27 16:22:01 +08:00
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));
2014-07-27 16:22:01 +08:00
});
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
});
}
}
});