discourse/app/assets/javascripts/admin/controllers/admin_badges_controller.js

146 lines
3.4 KiB
JavaScript
Raw Normal View History

2014-03-05 20:52:20 +08:00
/**
This controller supports the interface for dealing with badges.
@class AdminBadgesController
@extends Ember.ArrayController
@namespace Discourse
@module Discourse
**/
Discourse.AdminBadgesController = Ember.ArrayController.extend({
itemController: 'adminBadge',
queryParams: ['badgeId'],
2014-05-21 15:22:42 +08:00
badgeId: Em.computed.alias('selectedId'),
/**
ID of the currently selected badge.
2014-05-21 15:22:42 +08:00
@property selectedId
@type {Integer}
**/
2014-05-21 15:22:42 +08:00
selectedId: null,
/**
Badge that is currently selected.
2014-05-24 10:33:46 +08:00
2014-05-21 15:22:42 +08:00
@property selectedItem
@type {Discourse.Badge}
**/
selectedItem: function() {
2014-05-24 10:33:46 +08: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 15:22:42 +08:00
return this.get('model').filter(function(badge) {
2014-05-24 10:33:46 +08:00
return badge.get('id') === undefined;
2014-05-21 15:22:42 +08:00
})[0];
2014-05-24 10:33:46 +08:00
}.property('model.@each.id'),
/**
Whether a new unsaved badge exists.
@property newBadgeExists
@type {Discourse.Badge}
**/
newBadgeExists: Em.computed.notEmpty('newBadge'),
2014-03-05 20:52:20 +08:00
/**
We don't allow setting a description if a translation for the given badge
name exists.
2014-03-05 20:52:20 +08:00
@property canEditDescription
@type {Boolean}
**/
canEditDescription: Em.computed.none('selectedItem.translatedDescription'),
2014-03-19 13:13:38 +08:00
/**
Disable saving if the currently selected item is being saved.
@property disableSave
@type {Boolean}
**/
disableSave: Em.computed.alias('selectedItem.saving'),
2014-03-05 20:52:20 +08:00
actions: {
/**
Create a new badge and select it.
@method newBadge
**/
2014-05-24 10:33:46 +08:00
createNewBadge: function() {
2014-03-05 20:52:20 +08: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 15:22:42 +08:00
this.set('selectedId', badge.get('id'));
2014-03-05 20:52:20 +08:00
},
/**
Save the selected badge.
@method save
**/
save: function() {
2014-03-19 13:13:38 +08:00
if (!this.get('disableSave')) {
this.get('selectedItem').save();
}
2014-03-05 20:52:20 +08: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 15:22:42 +08:00
this.set('selectedId', null);
return;
}
2014-03-05 20:52:20 +08: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 15:22:42 +08:00
self.set('selectedId', null);
2014-03-05 20:52:20 +08:00
self.get('model').removeObject(selected);
}, function() {
// Failure.
bootbox.alert(I18n.t('generic_error'));
});
}
});
}
}
});