2015-05-21 16:05:48 -04:00
|
|
|
import { popupAjaxError } from 'discourse/lib/ajax-error';
|
2014-10-17 14:27:40 -04:00
|
|
|
import BufferedContent from 'discourse/mixins/buffered-content';
|
2015-08-07 15:08:27 -04:00
|
|
|
import { propertyNotEqual } from 'discourse/lib/computed';
|
2014-10-17 14:27:40 -04:00
|
|
|
|
2015-08-11 12:27:07 -04:00
|
|
|
export default Ember.Controller.extend(BufferedContent, {
|
2014-10-17 14:27:40 -04:00
|
|
|
needs: ['admin-badges'],
|
|
|
|
saving: false,
|
|
|
|
savingStatus: '',
|
|
|
|
|
|
|
|
badgeTypes: Em.computed.alias('controllers.admin-badges.badgeTypes'),
|
|
|
|
badgeGroupings: Em.computed.alias('controllers.admin-badges.badgeGroupings'),
|
|
|
|
badgeTriggers: Em.computed.alias('controllers.admin-badges.badgeTriggers'),
|
|
|
|
protectedSystemFields: Em.computed.alias('controllers.admin-badges.protectedSystemFields'),
|
|
|
|
|
|
|
|
readOnly: Ember.computed.alias('buffered.system'),
|
2015-08-07 15:08:27 -04:00
|
|
|
showDisplayName: propertyNotEqual('name', 'displayName'),
|
2014-10-17 14:27:40 -04:00
|
|
|
canEditDescription: Em.computed.none('buffered.translatedDescription'),
|
|
|
|
|
2015-08-25 16:30:09 -04:00
|
|
|
hasQuery: function() {
|
|
|
|
const bQuery = this.get('buffered.query');
|
|
|
|
if (bQuery) {
|
|
|
|
return bQuery.trim().length > 0;
|
|
|
|
}
|
|
|
|
const mQuery = this.get('model.query');
|
|
|
|
return mQuery && mQuery.trim().length > 0;
|
|
|
|
}.property('model.query', 'buffered.query'),
|
|
|
|
|
2014-10-17 14:27:40 -04:00
|
|
|
_resetSaving: function() {
|
|
|
|
this.set('saving', false);
|
|
|
|
this.set('savingStatus', '');
|
|
|
|
}.observes('model.id'),
|
|
|
|
|
|
|
|
actions: {
|
|
|
|
save: function() {
|
|
|
|
if (!this.get('saving')) {
|
|
|
|
var fields = ['allow_title', 'multiple_grant',
|
|
|
|
'listable', 'auto_revoke',
|
|
|
|
'enabled', 'show_posts',
|
|
|
|
'target_posts', 'name', 'description',
|
2014-10-20 13:15:58 -04:00
|
|
|
'icon', 'image', 'query', 'badge_grouping_id',
|
2014-10-17 14:27:40 -04:00
|
|
|
'trigger', 'badge_type_id'],
|
|
|
|
self = this;
|
|
|
|
|
|
|
|
if (this.get('buffered.system')){
|
|
|
|
var protectedFields = this.get('protectedSystemFields');
|
|
|
|
fields = _.filter(fields, function(f){
|
|
|
|
return !_.include(protectedFields,f);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
this.set('saving', true);
|
|
|
|
this.set('savingStatus', I18n.t('saving'));
|
|
|
|
|
|
|
|
var boolFields = ['allow_title', 'multiple_grant',
|
|
|
|
'listable', 'auto_revoke',
|
|
|
|
'enabled', 'show_posts',
|
|
|
|
'target_posts' ];
|
|
|
|
|
|
|
|
var data = {},
|
|
|
|
buffered = this.get('buffered');
|
|
|
|
fields.forEach(function(field){
|
|
|
|
var d = buffered.get(field);
|
|
|
|
if (_.include(boolFields, field)) { d = !!d; }
|
|
|
|
data[field] = d;
|
|
|
|
});
|
|
|
|
|
|
|
|
var newBadge = !this.get('id'),
|
|
|
|
model = this.get('model');
|
|
|
|
this.get('model').save(data).then(function() {
|
|
|
|
if (newBadge) {
|
2015-11-02 00:13:17 -05:00
|
|
|
var adminBadgesController = self.get('controllers.admin-badges');
|
|
|
|
if (!adminBadgesController.contains(model)) adminBadgesController.pushObject(model);
|
2014-10-17 14:27:40 -04:00
|
|
|
self.transitionToRoute('adminBadges.show', model.get('id'));
|
|
|
|
} else {
|
|
|
|
self.commitBuffer();
|
|
|
|
self.set('savingStatus', I18n.t('saved'));
|
|
|
|
}
|
|
|
|
|
2015-05-21 16:05:48 -04:00
|
|
|
}).catch(popupAjaxError).finally(function() {
|
2014-10-17 14:27:40 -04:00
|
|
|
self.set('saving', false);
|
2015-05-21 16:05:48 -04:00
|
|
|
self.set('savingStatus', '');
|
2014-10-17 14:27:40 -04:00
|
|
|
});
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
|
|
|
destroy: function() {
|
|
|
|
var self = this,
|
|
|
|
adminBadgesController = this.get('controllers.admin-badges'),
|
|
|
|
model = this.get('model');
|
|
|
|
|
|
|
|
if (!model.get('id')) {
|
|
|
|
self.transitionToRoute('adminBadges.index');
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
return bootbox.confirm(I18n.t("admin.badges.delete_confirm"), I18n.t("no_value"), I18n.t("yes_value"), function(result) {
|
|
|
|
if (result) {
|
|
|
|
model.destroy().then(function() {
|
|
|
|
adminBadgesController.removeObject(model);
|
|
|
|
self.transitionToRoute('adminBadges.index');
|
|
|
|
}).catch(function() {
|
|
|
|
bootbox.alert(I18n.t('generic_error'));
|
|
|
|
});
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|