discourse/app/assets/javascripts/admin/controllers/admin-badges.js.es6

176 lines
4.4 KiB
Text
Raw Normal View History

2014-03-05 07:52:20 -05:00
/**
This controller supports the interface for dealing with badges.
@class AdminBadgesController
@extends Ember.ArrayController
@namespace Discourse
@module Discourse
**/
export default Ember.ArrayController.extend({
2014-07-27 04:22:01 -04:00
needs: ['modal'],
itemController: 'admin-badge',
queryParams: ['badgeId'],
2014-05-21 03:22:42 -04:00
badgeId: Em.computed.alias('selectedId'),
/**
ID of the currently selected badge.
2014-05-21 03:22:42 -04:00
@property selectedId
@type {Integer}
**/
2014-05-21 03:22:42 -04:00
selectedId: null,
/**
Badge that is currently selected.
2014-05-23 22:33:46 -04:00
2014-05-21 03:22:42 -04:00
@property selectedItem
@type {Discourse.Badge}
**/
selectedItem: function() {
2014-05-23 22:33:46 -04:00
if (this.get('selectedId') === undefined || this.get('selectedId') === "undefined") {
// New Badge
return this.get('newBadge');
} else {
// Existing Badge
var selectedId = parseInt(this.get('selectedId'));
return this.get('model').filter(function(badge) {
return parseInt(badge.get('id')) === selectedId;
})[0];
}
}.property('selectedId', 'newBadge'),
/**
Unsaved badge, if one exists.
@property newBadge
@type {Discourse.Badge}
**/
newBadge: function() {
2014-05-21 03:22:42 -04:00
return this.get('model').filter(function(badge) {
2014-05-23 22:33:46 -04:00
return badge.get('id') === undefined;
2014-05-21 03:22:42 -04:00
})[0];
2014-05-23 22:33:46 -04:00
}.property('model.@each.id'),
/**
Whether a new unsaved badge exists.
@property newBadgeExists
@type {Discourse.Badge}
**/
newBadgeExists: Em.computed.notEmpty('newBadge'),
2014-03-05 07:52:20 -05:00
/**
We don't allow setting a description if a translation for the given badge
name exists.
2014-03-05 07:52:20 -05:00
@property canEditDescription
@type {Boolean}
**/
canEditDescription: Em.computed.none('selectedItem.translatedDescription'),
2014-03-19 01:13:38 -04:00
/**
Disable saving if the currently selected item is being saved.
@property disableSave
@type {Boolean}
**/
disableSave: Em.computed.alias('selectedItem.saving'),
2014-03-05 07:52:20 -05:00
actions: {
preview: function(badge) {
// TODO wire modal and localize
Discourse.ajax('/admin/badges/preview.json', {
method: 'post',
data: {sql: badge.query, target_posts: !!badge.target_posts}
}).then(function(json){
if(json.error){
bootbox.alert(json.error);
} else {
bootbox.alert(json.grant_count + " badges to be assigned");
}
});
},
2014-03-05 07:52:20 -05:00
/**
Create a new badge and select it.
@method newBadge
**/
2014-05-23 22:33:46 -04:00
createNewBadge: function() {
2014-03-05 07:52:20 -05:00
var badge = Discourse.Badge.create({
name: I18n.t('admin.badges.new_badge')
});
this.pushObject(badge);
this.send('selectBadge', badge);
},
/**
Select a particular badge.
@method selectBadge
@param {Discourse.Badge} badge The badge to be selected
**/
selectBadge: function(badge) {
2014-05-21 03:22:42 -04:00
this.set('selectedId', badge.get('id'));
2014-03-05 07:52:20 -05:00
},
/**
Save the selected badge.
@method save
**/
save: function() {
2014-03-19 01:13:38 -04:00
if (!this.get('disableSave')) {
var fields = ['allow_title', 'multiple_grant',
'listable', 'auto_revoke',
'enabled', 'show_posts',
'target_posts', 'name', 'description',
'icon', 'query', 'badge_grouping_id',
2014-07-25 01:48:43 -04:00
'trigger', 'badge_type_id'];
if(this.get('selectedItem.system')){
var protectedFields = this.get('protectedSystemFields');
fields = _.filter(fields, function(f){
return !_.include(protectedFields,f);
});
}
this.get('selectedItem').save(fields);
2014-03-19 01:13:38 -04:00
}
2014-03-05 07:52:20 -05:00
},
/**
Confirm before destroying the selected badge.
@method destroy
**/
destroy: function() {
// Delete immediately if the selected badge is new.
if (!this.get('selectedItem.id')) {
this.get('model').removeObject(this.get('selectedItem'));
2014-05-21 03:22:42 -04:00
this.set('selectedId', null);
return;
}
2014-03-05 07:52:20 -05:00
var self = this;
return bootbox.confirm(I18n.t("admin.badges.delete_confirm"), I18n.t("no_value"), I18n.t("yes_value"), function(result) {
if (result) {
var selected = self.get('selectedItem');
selected.destroy().then(function() {
// Success.
2014-05-21 03:22:42 -04:00
self.set('selectedId', null);
2014-03-05 07:52:20 -05:00
self.get('model').removeObject(selected);
}, function() {
// Failure.
bootbox.alert(I18n.t('generic_error'));
});
}
});
}
}
});