2013-04-17 15:08:21 +08:00
|
|
|
Discourse.Group = Discourse.Model.extend({
|
2013-05-09 15:37:34 +08:00
|
|
|
loaded: false,
|
|
|
|
|
2013-05-08 13:20:38 +08:00
|
|
|
userCountDisplay: function(){
|
|
|
|
var c = this.get('user_count');
|
|
|
|
// don't display zero its ugly
|
|
|
|
if(c > 0) {
|
|
|
|
return c;
|
|
|
|
}
|
|
|
|
}.property('user_count'),
|
|
|
|
|
2013-05-09 15:37:34 +08:00
|
|
|
load: function() {
|
|
|
|
var id = this.get('id');
|
|
|
|
if(id && !this.get('loaded')) {
|
|
|
|
var group = this;
|
|
|
|
Discourse.ajax('/admin/groups/' + this.get('id') + '/users').then(function(payload){
|
2013-06-22 02:06:20 +08:00
|
|
|
var users = Em.A();
|
2013-06-11 04:48:50 +08:00
|
|
|
_.each(payload,function(user){
|
2013-05-09 15:37:34 +08:00
|
|
|
users.addObject(Discourse.User.create(user));
|
|
|
|
});
|
2013-06-22 02:06:20 +08:00
|
|
|
group.set('users', users);
|
|
|
|
group.set('loaded', true);
|
2013-05-08 13:20:38 +08:00
|
|
|
});
|
2013-05-09 15:37:34 +08:00
|
|
|
}
|
2013-05-08 13:20:38 +08:00
|
|
|
},
|
|
|
|
|
|
|
|
usernames: function() {
|
|
|
|
var users = this.get('users');
|
|
|
|
var usernames = "";
|
|
|
|
if(users) {
|
2013-06-11 04:48:50 +08:00
|
|
|
usernames = _.map(users, function(user){
|
2013-05-08 13:20:38 +08:00
|
|
|
return user.get('username');
|
2013-06-22 02:06:20 +08:00
|
|
|
}).join(',');
|
2013-05-08 13:20:38 +08:00
|
|
|
}
|
|
|
|
return usernames;
|
2013-05-09 15:37:34 +08:00
|
|
|
}.property('users'),
|
|
|
|
|
|
|
|
destroy: function(){
|
2013-07-22 10:37:01 +08:00
|
|
|
if(!this.id) return;
|
|
|
|
|
2013-05-09 15:37:34 +08:00
|
|
|
var group = this;
|
|
|
|
group.set('disableSave', true);
|
|
|
|
|
2013-07-23 04:19:08 +08:00
|
|
|
return Discourse.ajax("/admin/groups/" + group.get('id'), {type: "DELETE"})
|
2013-05-09 15:37:34 +08:00
|
|
|
.then(function(){
|
2013-07-22 10:37:01 +08:00
|
|
|
return true;
|
|
|
|
}, function(jqXHR, status, error) {
|
2013-05-09 15:37:34 +08:00
|
|
|
group.set('disableSave', false);
|
2013-07-22 10:37:01 +08:00
|
|
|
bootbox.alert(I18n.t("admin.groups.delete_failed"));
|
|
|
|
return false;
|
2013-05-09 15:37:34 +08:00
|
|
|
});
|
|
|
|
},
|
|
|
|
|
|
|
|
create: function(){
|
|
|
|
var group = this;
|
|
|
|
group.set('disableSave', true);
|
|
|
|
|
|
|
|
return Discourse.ajax("/admin/groups", {type: "POST", data: {
|
|
|
|
group: {
|
|
|
|
name: this.get('name'),
|
|
|
|
usernames: this.get('usernames')
|
|
|
|
}
|
2013-07-24 12:31:15 +08:00
|
|
|
}}).then(function(resp) {
|
2013-05-09 15:37:34 +08:00
|
|
|
group.set('disableSave', false);
|
2013-07-24 12:31:15 +08:00
|
|
|
group.set('id', resp.id);
|
|
|
|
}, function (error) {
|
|
|
|
group.set('disableSave', false);
|
|
|
|
if (error && error.responseText) {
|
|
|
|
bootbox.alert($.parseJSON(error.responseText).errors);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
bootbox.alert(I18n.t('generic_error'));
|
|
|
|
}
|
2013-05-09 15:37:34 +08:00
|
|
|
});
|
2013-05-24 19:04:26 +08:00
|
|
|
},
|
|
|
|
|
|
|
|
save: function(){
|
|
|
|
var group = this;
|
|
|
|
group.set('disableSave', true);
|
|
|
|
|
2013-06-17 11:43:06 +08:00
|
|
|
return Discourse.ajax("/admin/groups/" + this.get('id'), {
|
|
|
|
type: "PUT",
|
|
|
|
data: {
|
|
|
|
group: {
|
|
|
|
name: this.get('name'),
|
|
|
|
usernames: this.get('usernames')
|
|
|
|
}
|
|
|
|
},
|
|
|
|
complete: function(){
|
|
|
|
group.set('disableSave', false);
|
2013-05-24 19:04:26 +08:00
|
|
|
}
|
2013-06-17 11:43:06 +08:00
|
|
|
}).then(null, function(e){
|
|
|
|
var message = $.parseJSON(e.responseText).errors;
|
|
|
|
bootbox.alert(message);
|
2013-05-24 19:04:26 +08:00
|
|
|
});
|
2013-05-09 15:37:34 +08:00
|
|
|
}
|
2013-04-17 15:08:21 +08:00
|
|
|
|
|
|
|
});
|
|
|
|
|
|
|
|
Discourse.Group.reopenClass({
|
|
|
|
findAll: function(){
|
|
|
|
var list = Discourse.SelectableArray.create();
|
|
|
|
|
2013-06-04 04:12:24 +08:00
|
|
|
Discourse.ajax("/admin/groups.json").then(function(groups){
|
2013-06-11 04:48:50 +08:00
|
|
|
_.each(groups,function(group){
|
2013-05-08 13:20:38 +08:00
|
|
|
list.addObject(Discourse.Group.create(group));
|
|
|
|
});
|
|
|
|
});
|
2013-04-17 15:08:21 +08:00
|
|
|
|
|
|
|
return list;
|
|
|
|
},
|
|
|
|
|
|
|
|
find: function(id) {
|
|
|
|
var promise = new Em.Deferred();
|
2013-05-08 13:20:38 +08:00
|
|
|
|
2013-04-17 15:08:21 +08:00
|
|
|
setTimeout(function(){
|
|
|
|
promise.resolve(Discourse.Group.create({id: 1, name: "all mods", members: ["A","b","c"]}));
|
|
|
|
}, 1000);
|
2013-05-08 13:20:38 +08:00
|
|
|
|
2013-04-17 15:08:21 +08:00
|
|
|
return promise;
|
|
|
|
}
|
|
|
|
});
|