2014-07-27 04:22:01 -04:00
|
|
|
export default Ember.Controller.extend({
|
|
|
|
needs: ['modal'],
|
|
|
|
|
|
|
|
modelChanged: function(){
|
2015-12-26 17:58:54 -05:00
|
|
|
const model = this.get('model');
|
|
|
|
const copy = Em.A();
|
|
|
|
const store = this.store;
|
2014-07-27 04:22:01 -04:00
|
|
|
|
|
|
|
if(model){
|
|
|
|
model.forEach(function(o){
|
2015-12-26 17:58:54 -05:00
|
|
|
copy.pushObject(store.createRecord('badge-grouping', o));
|
2014-07-27 04:22:01 -04:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
this.set('workingCopy', copy);
|
|
|
|
}.observes('model'),
|
|
|
|
|
|
|
|
moveItem: function(item, delta){
|
2015-12-26 17:58:54 -05:00
|
|
|
const copy = this.get('workingCopy');
|
|
|
|
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: {
|
|
|
|
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(){
|
2015-12-26 17:58:54 -05:00
|
|
|
const obj = this.store.createRecord('badge-grouping', {editing: true, name: I18n.t('admin.badges.badge_grouping')});
|
2014-07-27 04:22:01 -04:00
|
|
|
this.get('workingCopy').pushObject(obj);
|
|
|
|
},
|
|
|
|
saveAll: function(){
|
2015-12-26 17:58:54 -05:00
|
|
|
const self = this;
|
2014-07-27 04:22:01 -04:00
|
|
|
var items = this.get('workingCopy');
|
2015-12-26 17:58:54 -05:00
|
|
|
const groupIds = items.map(function(i){return i.get("id") || -1;});
|
|
|
|
const names = items.map(function(i){return i.get("name");});
|
2014-07-27 04:22:01 -04:00
|
|
|
|
|
|
|
Discourse.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){
|
2015-12-26 17:58:54 -05:00
|
|
|
items.pushObject(self.store.createRecord('badge-grouping', g));
|
2014-07-27 04:22:01 -04:00
|
|
|
});
|
|
|
|
self.set('model', null);
|
|
|
|
self.set('workingCopy', null);
|
|
|
|
self.send('closeModal');
|
|
|
|
},function(){
|
2015-12-26 17:58:54 -05:00
|
|
|
bootbox.alert(I18n.t('generic_error'));
|
2014-07-27 04:22:01 -04:00
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|