2015-05-22 02:57:45 +08:00
|
|
|
import { popupAjaxError } from 'discourse/lib/ajax-error';
|
2015-08-08 03:08:27 +08:00
|
|
|
import { propertyEqual } from 'discourse/lib/computed';
|
2015-05-22 02:57:45 +08:00
|
|
|
|
2015-08-12 00:27:07 +08:00
|
|
|
export default Ember.Controller.extend({
|
2015-01-22 03:52:48 +08:00
|
|
|
needs: ['adminGroupsType'],
|
2014-04-24 01:25:02 +08:00
|
|
|
disableSave: false,
|
2015-01-06 01:51:45 +08:00
|
|
|
|
|
|
|
currentPage: function() {
|
2015-08-13 22:01:29 +08:00
|
|
|
if (this.get("model.user_count") === 0) { return 0; }
|
|
|
|
return Math.floor(this.get("model.offset") / this.get("model.limit")) + 1;
|
|
|
|
}.property("model.limit", "model.offset", "model.user_count"),
|
2015-01-06 01:51:45 +08:00
|
|
|
|
|
|
|
totalPages: function() {
|
2015-08-13 22:01:29 +08:00
|
|
|
if (this.get("model.user_count") === 0) { return 0; }
|
|
|
|
return Math.floor(this.get("model.user_count") / this.get("model.limit")) + 1;
|
|
|
|
}.property("model.limit", "model.user_count"),
|
2015-01-06 01:51:45 +08:00
|
|
|
|
|
|
|
showingFirst: Em.computed.lte("currentPage", 1),
|
2015-08-08 03:08:27 +08:00
|
|
|
showingLast: propertyEqual("currentPage", "totalPages"),
|
2014-04-24 01:25:02 +08:00
|
|
|
|
|
|
|
aliasLevelOptions: function() {
|
|
|
|
return [
|
2015-01-06 01:51:45 +08:00
|
|
|
{ name: I18n.t("groups.alias_levels.nobody"), value: 0 },
|
|
|
|
{ name: I18n.t("groups.alias_levels.mods_and_admins"), value: 2 },
|
|
|
|
{ name: I18n.t("groups.alias_levels.members_mods_and_admins"), value: 3 },
|
|
|
|
{ name: I18n.t("groups.alias_levels.everyone"), value: 99 }
|
2014-04-24 01:25:02 +08:00
|
|
|
];
|
|
|
|
}.property(),
|
|
|
|
|
2015-09-02 04:52:05 +08:00
|
|
|
trustLevelOptions: function() {
|
|
|
|
return [
|
|
|
|
{ name: I18n.t("groups.trust_levels.none"), value: 0 },
|
|
|
|
{ name: 1, value: 1 }, { name: 2, value: 2 }, { name: 3, value: 3 }, { name: 4, value: 4 }
|
|
|
|
];
|
|
|
|
}.property(),
|
|
|
|
|
2014-04-24 01:25:02 +08:00
|
|
|
actions: {
|
2015-04-03 22:50:51 +08:00
|
|
|
next() {
|
2015-01-06 01:51:45 +08:00
|
|
|
if (this.get("showingLast")) { return; }
|
|
|
|
|
2015-04-03 22:50:51 +08:00
|
|
|
const group = this.get("model"),
|
2015-09-15 03:54:34 +08:00
|
|
|
offset = Math.min(group.get("offset") + group.get("limit"), group.get("user_count"));
|
2015-01-06 01:51:45 +08:00
|
|
|
|
|
|
|
group.set("offset", offset);
|
|
|
|
|
|
|
|
return group.findMembers();
|
|
|
|
},
|
|
|
|
|
2015-04-03 22:50:51 +08:00
|
|
|
previous() {
|
2015-01-06 01:51:45 +08:00
|
|
|
if (this.get("showingFirst")) { return; }
|
|
|
|
|
2015-04-03 22:50:51 +08:00
|
|
|
const group = this.get("model"),
|
2015-09-15 03:54:34 +08:00
|
|
|
offset = Math.max(group.get("offset") - group.get("limit"), 0);
|
2015-01-06 01:51:45 +08:00
|
|
|
|
|
|
|
group.set("offset", offset);
|
|
|
|
|
|
|
|
return group.findMembers();
|
|
|
|
},
|
|
|
|
|
2015-04-03 22:50:51 +08:00
|
|
|
removeMember(member) {
|
2015-04-03 23:09:51 +08:00
|
|
|
const self = this,
|
2015-08-13 22:01:29 +08:00
|
|
|
message = I18n.t("admin.groups.delete_member_confirm", { username: member.get("username"), group: this.get("model.name") });
|
2015-01-06 01:51:45 +08:00
|
|
|
return bootbox.confirm(message, I18n.t("no_value"), I18n.t("yes_value"), function(confirm) {
|
|
|
|
if (confirm) {
|
|
|
|
self.get("model").removeMember(member);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
},
|
|
|
|
|
2015-11-09 21:52:04 +08:00
|
|
|
removeOwner(member) {
|
|
|
|
const self = this,
|
|
|
|
message = I18n.t("admin.groups.delete_owner_confirm", { username: member.get("username"), group: this.get("model.name") });
|
|
|
|
return bootbox.confirm(message, I18n.t("no_value"), I18n.t("yes_value"), function(confirm) {
|
|
|
|
if (confirm) {
|
|
|
|
self.get("model").removeOwner(member);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
},
|
|
|
|
|
|
|
|
addOwners() {
|
|
|
|
if (Em.isEmpty(this.get("model.ownerUsernames"))) { return; }
|
|
|
|
this.get("model").addOwners(this.get("model.ownerUsernames")).catch(popupAjaxError);
|
|
|
|
this.set("model.ownerUsernames", null);
|
|
|
|
},
|
|
|
|
|
2015-04-03 22:50:51 +08:00
|
|
|
addMembers() {
|
2015-08-13 22:01:29 +08:00
|
|
|
if (Em.isEmpty(this.get("model.usernames"))) { return; }
|
|
|
|
this.get("model").addMembers(this.get("model.usernames")).catch(popupAjaxError);
|
|
|
|
this.set("model.usernames", null);
|
2015-01-06 01:51:45 +08:00
|
|
|
},
|
|
|
|
|
2015-04-03 22:50:51 +08:00
|
|
|
save() {
|
|
|
|
const group = this.get('model'),
|
2015-09-17 22:20:21 +08:00
|
|
|
groupsController = this.get("controllers.adminGroupsType"),
|
|
|
|
groupType = groupsController.get("type");
|
2014-04-24 01:25:02 +08:00
|
|
|
|
2015-01-22 03:52:48 +08:00
|
|
|
this.set('disableSave', true);
|
2014-04-24 01:25:02 +08:00
|
|
|
|
2015-04-03 22:50:51 +08:00
|
|
|
let promise = group.get("id") ? group.save() : group.create().then(() => groupsController.addObject(group));
|
|
|
|
|
2015-09-17 22:20:21 +08:00
|
|
|
promise.then(() => this.transitionToRoute("adminGroup", groupType, group.get('name')))
|
2015-05-22 02:57:45 +08:00
|
|
|
.catch(popupAjaxError)
|
2015-04-03 22:50:51 +08:00
|
|
|
.finally(() => this.set('disableSave', false));
|
2014-04-24 01:25:02 +08:00
|
|
|
},
|
|
|
|
|
2015-04-03 22:50:51 +08:00
|
|
|
destroy() {
|
|
|
|
const group = this.get('model'),
|
|
|
|
groupsController = this.get('controllers.adminGroupsType'),
|
|
|
|
self = this;
|
2014-04-24 01:25:02 +08:00
|
|
|
|
2015-09-17 22:20:21 +08:00
|
|
|
if (!group.get('id')) {
|
|
|
|
self.transitionToRoute('adminGroupsType.index', 'custom');
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2015-01-22 03:52:48 +08:00
|
|
|
this.set('disableSave', true);
|
|
|
|
|
2015-04-03 22:50:51 +08:00
|
|
|
bootbox.confirm(
|
|
|
|
I18n.t("admin.groups.delete_confirm"),
|
|
|
|
I18n.t("no_value"),
|
|
|
|
I18n.t("yes_value"),
|
|
|
|
function(confirmed) {
|
|
|
|
if (confirmed) {
|
|
|
|
group.destroy().then(() => {
|
|
|
|
groupsController.get('model').removeObject(group);
|
|
|
|
self.transitionToRoute('adminGroups.index');
|
|
|
|
}).catch(() => bootbox.alert(I18n.t("admin.groups.delete_failed")))
|
|
|
|
.finally(() => self.set('disableSave', false));
|
|
|
|
} else {
|
2014-04-24 01:25:02 +08:00
|
|
|
self.set('disableSave', false);
|
2015-04-03 22:50:51 +08:00
|
|
|
}
|
2014-04-24 01:25:02 +08:00
|
|
|
}
|
2015-04-03 22:50:51 +08:00
|
|
|
);
|
2014-04-24 01:25:02 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|