1
0
mirror of https://github.com/LaCasemate/fab-manager.git synced 2024-12-02 13:24:20 +01:00
fab-manager/app/assets/javascripts/controllers/admin/groups.js

101 lines
3.7 KiB
JavaScript
Raw Normal View History

/* eslint-disable
handle-callback-err,
no-return-assign,
no-undef,
*/
// TODO: This file was created by bulk-decaffeinate.
// Fix any style issues and re-enable lint.
/*
* decaffeinate suggestions:
* DS102: Remove unnecessary code created because of implicit returns
* DS207: Consider shorter variations of null checks
* Full docs: https://github.com/decaffeinate/decaffeinate/blob/master/docs/suggestions.md
*/
Application.Controllers.controller('GroupsController', ['$scope', 'groupsPromise', 'Group', 'growl', '_t', function ($scope, groupsPromise, Group, growl, _t) {
// List of users groups
2018-11-21 11:08:53 +01:00
$scope.groups = groupsPromise;
2016-03-23 18:39:41 +01:00
// Default: we show only enabled groups
2018-11-21 11:08:53 +01:00
$scope.groupFiltering = 'enabled';
2016-03-23 18:39:41 +01:00
// Available options for filtering groups by status
2017-10-05 17:12:22 +02:00
$scope.filterDisabled = [
'enabled',
'disabled',
'all'
2018-11-21 11:08:53 +01:00
];
2016-03-23 18:39:41 +01:00
/**
* Removes the newly inserted but not saved group / Cancel the current group modification
* @param rowform {Object} see http://vitalets.github.io/angular-xeditable/
* @param index {number} group index in the $scope.groups array
*/
$scope.cancelGroup = function (rowform, index) {
if ($scope.groups[index].id != null) {
2018-11-21 11:08:53 +01:00
return rowform.$cancel();
} else {
2018-11-21 11:08:53 +01:00
return $scope.groups.splice(index, 1);
}
2018-11-21 11:08:53 +01:00
};
2016-03-23 18:39:41 +01:00
/**
* Creates a new empty entry in the $scope.groups array
*/
$scope.addGroup = function () {
2016-03-23 18:39:41 +01:00
$scope.inserted =
2018-11-21 11:08:53 +01:00
{ name: '' };
return $scope.groups.push($scope.inserted);
};
/**
* Saves a new group / Update an existing group to the server (form validation callback)
* @param data {Object} group name
* @param [id] {number} group id, in case of update
*/
$scope.saveGroup = function (data, id) {
if (id != null) {
return Group.update({ id }, { group: data }, response => growl.success(_t('app.admin.members.group_form.changes_successfully_saved'))
, error => growl.error(_t('app.admin.members.group_form.an_error_occurred_while_saving_changes')));
} else {
return Group.save({ group: data }, function (resp) {
growl.success(_t('app.admin.members.group_form.new_group_successfully_saved'));
2018-11-21 11:08:53 +01:00
return $scope.groups[$scope.groups.length - 1].id = resp.id;
}
, function (error) {
growl.error(_t('app.admin.members.group_form.an_error_occurred_when_saving_the_new_group'));
2018-11-21 11:08:53 +01:00
return $scope.groups.splice($scope.groups.length - 1, 1);
});
}
2018-11-21 11:08:53 +01:00
};
/**
* Deletes the group at the specified index
* @param index {number} group index in the $scope.groups array
*/
$scope.removeGroup = index =>
Group.delete({ id: $scope.groups[index].id }, function (resp) {
growl.success(_t('app.admin.members.group_form.group_successfully_deleted'));
2018-11-21 11:08:53 +01:00
return $scope.groups.splice(index, 1);
}
, error => growl.error(_t('app.admin.members.group_form.unable_to_delete_group_because_some_users_and_or_groups_are_still_linked_to_it')));
/**
* Enable/disable the group at the specified index
* @param index {number} group index in the $scope.groups array
*/
return $scope.toggleDisableGroup = function (index) {
2018-11-21 11:08:53 +01:00
const group = $scope.groups[index];
if (!group.disabled && (group.users > 0)) {
return growl.error(_t('app.admin.members.group_form.unable_to_disable_group_with_users', { USERS: group.users }));
} else {
return Group.update({ id: group.id }, { group: { disabled: !group.disabled } }, function (response) {
2018-11-21 11:08:53 +01:00
$scope.groups[index] = response;
return growl.success(_t('app.admin.members.group_form.group_successfully_enabled_disabled', { STATUS: response.disabled }));
}
, error => growl.error(_t('app.admin.members.group_form.unable_to_enable_disable_group', { STATUS: !group.disabled })));
}
2018-11-21 11:08:53 +01:00
};
}
2018-11-21 11:08:53 +01:00
]);