2018-07-27 10:18:29 -04:00
|
|
|
/*
|
|
|
|
* Copyright 2018 Comcast Cable Communications Management, LLC
|
|
|
|
*
|
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
* you may not use this file except in compliance with the License.
|
|
|
|
* You may obtain a copy of the License at
|
|
|
|
*
|
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
*
|
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
* See the License for the specific language governing permissions and
|
|
|
|
* limitations under the License.
|
|
|
|
*/
|
|
|
|
|
|
|
|
angular.module('controller.groups', []).controller('GroupsController', function ($scope, $log, $location, groupsService, profileService, utilityService) {
|
2022-05-19 14:41:11 -04:00
|
|
|
//registering bootstrap modal close event to refresh data after create group action
|
2018-07-27 10:18:29 -04:00
|
|
|
angular.element('#modal_new_group').one('hide.bs.modal', function () {
|
|
|
|
$scope.closeModal();
|
|
|
|
});
|
|
|
|
|
2022-05-19 14:41:11 -04:00
|
|
|
$scope.groups = {items: []};
|
2018-07-27 10:18:29 -04:00
|
|
|
$scope.groupsLoaded = false;
|
|
|
|
$scope.alerts = [];
|
2019-08-21 14:26:51 -04:00
|
|
|
$scope.ignoreAccess = false;
|
2021-10-04 13:24:47 +05:30
|
|
|
$scope.hasGroups = false; // Re-assigned each time groups are fetched without a query
|
|
|
|
$scope.query = "";
|
2018-07-27 10:18:29 -04:00
|
|
|
|
|
|
|
function handleError(error, type) {
|
|
|
|
var alert = utilityService.failure(error, type);
|
|
|
|
$scope.alerts.push(alert);
|
|
|
|
$scope.processing = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
//views
|
|
|
|
//shared modal
|
|
|
|
var modalDialog;
|
|
|
|
|
2022-05-19 14:41:11 -04:00
|
|
|
$scope.openModal = function (evt) {
|
2018-07-27 10:18:29 -04:00
|
|
|
$scope.currentGroup = {};
|
2022-05-19 14:41:11 -04:00
|
|
|
void (evt && evt.preventDefault());
|
|
|
|
if (!modalDialog) {
|
2018-07-27 10:18:29 -04:00
|
|
|
modalDialog = angular.element('#modal_new_group').modal();
|
|
|
|
}
|
|
|
|
modalDialog.modal('show');
|
|
|
|
};
|
|
|
|
|
2022-05-19 14:41:11 -04:00
|
|
|
$scope.closeModal = function (evt) {
|
|
|
|
void (evt && evt.preventDefault());
|
|
|
|
if (!modalDialog) {
|
2018-07-27 10:18:29 -04:00
|
|
|
modalDialog = angular.element('#modal_new_group').modal();
|
|
|
|
}
|
|
|
|
modalDialog.modal('hide');
|
|
|
|
return true;
|
|
|
|
};
|
|
|
|
|
2022-05-19 14:41:11 -04:00
|
|
|
$scope.closeEditModal = function (evt) {
|
|
|
|
void (evt && evt.preventDefault());
|
2018-10-05 10:04:06 -04:00
|
|
|
editModalDialog = angular.element('#modal_edit_group').modal();
|
|
|
|
editModalDialog.modal('hide');
|
|
|
|
$scope.reset();
|
|
|
|
$scope.refresh();
|
|
|
|
return true;
|
|
|
|
};
|
|
|
|
|
2022-06-30 17:58:16 +05:30
|
|
|
// Autocomplete for group search
|
|
|
|
$("#group-search-text").autocomplete({
|
|
|
|
source: function( request, response ) {
|
|
|
|
$.ajax({
|
|
|
|
url: "/api/groups?maxItems=1500&abridged=true",
|
|
|
|
dataType: "json",
|
|
|
|
data: {groupNameFilter: request.term, ignoreAccess: $scope.ignoreAccess},
|
|
|
|
success: function(data) {
|
|
|
|
const search = JSON.parse(JSON.stringify(data));
|
|
|
|
response($.map(search.groups, function(group) {
|
2022-07-04 18:52:27 +05:30
|
|
|
return {value: group.name, label: group.name}
|
|
|
|
}))
|
2022-07-04 17:39:27 +05:30
|
|
|
}
|
2022-06-30 17:58:16 +05:30
|
|
|
});
|
|
|
|
},
|
2022-07-04 12:52:48 +05:30
|
|
|
minLength: 1,
|
2022-06-30 17:58:16 +05:30
|
|
|
select: function (event, ui) {
|
|
|
|
$("#group-search-text").val(ui.item.value);
|
|
|
|
return false;
|
|
|
|
},
|
|
|
|
open: function() {
|
2022-07-04 17:39:27 +05:30
|
|
|
$(this).removeClass("ui-corner-all").addClass("ui-corner-top");
|
2022-06-30 17:58:16 +05:30
|
|
|
},
|
|
|
|
close: function() {
|
|
|
|
$(this).removeClass("ui-corner-top").addClass("ui-corner-all");
|
|
|
|
}
|
2022-07-04 18:52:27 +05:30
|
|
|
});
|
|
|
|
|
2022-07-05 10:45:14 +05:30
|
|
|
// Autocomplete text-highlight
|
2022-07-04 18:52:27 +05:30
|
|
|
$.ui.autocomplete.prototype._renderItem = function(ul, item) {
|
2022-06-30 17:58:16 +05:30
|
|
|
let txt = String(item.label).replace(new RegExp(this.term, "gi"),"<b>$&</b>");
|
|
|
|
return $("<li></li>")
|
|
|
|
.data("ui-autocomplete-item", item.value)
|
2022-07-04 18:52:27 +05:30
|
|
|
.append("<div>" + txt + "</div>")
|
2022-07-04 17:39:27 +05:30
|
|
|
.appendTo(ul);
|
|
|
|
};
|
2022-06-30 17:58:16 +05:30
|
|
|
|
2018-07-27 10:18:29 -04:00
|
|
|
$scope.createGroup = function (name, email, description) {
|
|
|
|
//prevent user executing service call multiple times
|
|
|
|
//if true prevent, if false allow for execution of rest of code
|
|
|
|
//ng-href='/groups'
|
2022-05-19 14:41:11 -04:00
|
|
|
$log.log('createGroup::called', $scope.data);
|
2018-07-27 10:18:29 -04:00
|
|
|
|
|
|
|
if ($scope.processing) {
|
|
|
|
$log.log('createGroup::processing is true; exiting');
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
//flag to prevent multiple clicks until previous promise has resolved.
|
|
|
|
$scope.processing = true;
|
|
|
|
|
|
|
|
//data from user form values
|
|
|
|
var payload =
|
|
|
|
{
|
|
|
|
'name': name,
|
|
|
|
'email': email,
|
|
|
|
'description': description,
|
2022-05-19 14:41:11 -04:00
|
|
|
'members': [{id: $scope.profile.id}],
|
|
|
|
'admins': [{id: $scope.profile.id}]
|
2018-07-27 10:18:29 -04:00
|
|
|
};
|
|
|
|
|
|
|
|
//create group success callback
|
|
|
|
function success(response) {
|
2022-05-19 14:41:11 -04:00
|
|
|
var alert = utilityService.success('Successfully Created Group: ' + name, response, 'createGroup::createGroup successful');
|
|
|
|
$scope.alerts.push(alert);
|
2018-07-27 10:18:29 -04:00
|
|
|
$scope.closeModal();
|
|
|
|
$scope.reset();
|
|
|
|
$scope.refresh();
|
|
|
|
return response.data;
|
|
|
|
}
|
|
|
|
|
|
|
|
return groupsService.createGroup(payload)
|
|
|
|
.then(success)
|
2022-05-19 14:41:11 -04:00
|
|
|
.catch(function (error) {
|
2018-07-27 10:18:29 -04:00
|
|
|
handleError(error, 'groupsService::createGroup-failure');
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
2022-05-19 14:41:11 -04:00
|
|
|
$scope.allGroups = function () {
|
2019-08-21 14:26:51 -04:00
|
|
|
$scope.ignoreAccess = true;
|
|
|
|
$scope.refresh();
|
|
|
|
}
|
|
|
|
|
2022-05-19 14:41:11 -04:00
|
|
|
$scope.myGroups = function () {
|
2019-08-21 14:26:51 -04:00
|
|
|
$scope.ignoreAccess = false;
|
|
|
|
$scope.refresh();
|
|
|
|
}
|
|
|
|
|
2018-07-27 10:18:29 -04:00
|
|
|
$scope.refresh = function () {
|
|
|
|
function success(result) {
|
2019-08-21 14:26:51 -04:00
|
|
|
$log.log('getGroups:refresh-success', result);
|
2018-07-27 10:18:29 -04:00
|
|
|
//update groups
|
|
|
|
$scope.groups.items = result.groups;
|
|
|
|
$scope.groupsLoaded = true;
|
2021-10-04 13:24:47 +05:30
|
|
|
if (!$scope.query.length) {
|
2022-05-19 14:41:11 -04:00
|
|
|
$scope.hasGroups = $scope.groups.items.length > 0;
|
2021-10-04 13:24:47 +05:30
|
|
|
}
|
2018-07-27 10:18:29 -04:00
|
|
|
return result;
|
|
|
|
}
|
2022-05-19 14:41:11 -04:00
|
|
|
|
|
|
|
getGroupsAbridged($scope.ignoreAccess)
|
2018-07-27 10:18:29 -04:00
|
|
|
.then(success)
|
|
|
|
.catch(function (error) {
|
2019-08-21 14:26:51 -04:00
|
|
|
handleError(error, 'getGroups::refresh-failure');
|
2018-07-27 10:18:29 -04:00
|
|
|
});
|
|
|
|
};
|
|
|
|
|
|
|
|
$scope.reset = function () {
|
|
|
|
//reset processing flag
|
|
|
|
$scope.processing = false;
|
|
|
|
//fields with ng-patterns need to be set to null first then cleared
|
|
|
|
$scope.createGroupForm.$commitViewValue();
|
|
|
|
//this resets $scope.currentGroup object to empty object;
|
|
|
|
angular.copy({}, $scope.currentGroup);
|
|
|
|
//reset all validations & error messages to pre-form submission state
|
|
|
|
$scope.createGroupForm.$setUntouched();
|
|
|
|
$scope.createGroupForm.$setPristine();
|
|
|
|
|
|
|
|
return true;
|
|
|
|
};
|
|
|
|
|
2019-08-21 14:26:51 -04:00
|
|
|
function getGroups() {
|
2018-07-27 10:18:29 -04:00
|
|
|
function success(response) {
|
2019-08-21 14:26:51 -04:00
|
|
|
$log.log('groupsService::getGroups-success');
|
2018-07-27 10:18:29 -04:00
|
|
|
return response.data;
|
|
|
|
}
|
2022-05-19 14:41:11 -04:00
|
|
|
|
2018-07-27 10:18:29 -04:00
|
|
|
return groupsService
|
2021-10-04 13:24:47 +05:30
|
|
|
.getGroups($scope.ignoreAccess, $scope.query)
|
2018-07-27 10:18:29 -04:00
|
|
|
.then(success)
|
2022-05-19 14:41:11 -04:00
|
|
|
.catch(function (error) {
|
2019-08-21 14:26:51 -04:00
|
|
|
handleError(error, 'groupsService::getGroups-failure');
|
2022-05-19 14:41:11 -04:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
function getGroupsAbridged() {
|
|
|
|
function success(response) {
|
|
|
|
$log.log('groupsService::getGroups-success');
|
|
|
|
return response.data;
|
|
|
|
}
|
|
|
|
|
|
|
|
return groupsService
|
|
|
|
.getGroupsAbridged($scope.ignoreAccess, $scope.query)
|
|
|
|
.then(success)
|
|
|
|
.catch(function (error) {
|
|
|
|
handleError(error, 'groupsService::getGroups-failure');
|
|
|
|
});
|
2018-07-27 10:18:29 -04:00
|
|
|
}
|
|
|
|
|
2021-10-04 13:24:47 +05:30
|
|
|
// Return true if there are no groups created by the user
|
2022-05-19 14:41:11 -04:00
|
|
|
$scope.haveNoGroups = function (groupLength) {
|
|
|
|
if (!$scope.hasGroups && !groupLength && $scope.groupsLoaded && $scope.query.length == "") {
|
2021-10-04 13:24:47 +05:30
|
|
|
return true
|
2022-05-19 14:41:11 -04:00
|
|
|
} else {
|
2021-10-04 13:24:47 +05:30
|
|
|
return false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Return true if no groups are found related to the search query
|
2022-05-19 14:41:11 -04:00
|
|
|
$scope.searchCriteria = function (groupLength) {
|
|
|
|
if ($scope.groupsLoaded && !groupLength && $scope.query.length != "") {
|
2021-10-04 13:24:47 +05:30
|
|
|
return true
|
2022-05-19 14:41:11 -04:00
|
|
|
} else {
|
2021-10-04 13:24:47 +05:30
|
|
|
return false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-10-05 10:04:06 -04:00
|
|
|
$scope.editGroup = function (groupInfo) {
|
|
|
|
$scope.currentGroup = groupInfo;
|
|
|
|
$("#modal_edit_group").modal("show");
|
|
|
|
};
|
|
|
|
|
|
|
|
$scope.submitEditGroup = function (name, email, description) {
|
|
|
|
//prevent user executing service call multiple times
|
|
|
|
//if true prevent, if false allow for execution of rest of code
|
|
|
|
//ng-href='/groups'
|
2022-05-19 14:41:11 -04:00
|
|
|
$log.log('updateGroup::called', $scope.data);
|
2018-10-05 10:04:06 -04:00
|
|
|
|
|
|
|
if ($scope.processing) {
|
|
|
|
$log.log('updateGroup::processing is true; exiting');
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
//flag to prevent multiple clicks until previous promise has resolved.
|
|
|
|
$scope.processing = true;
|
|
|
|
|
|
|
|
//data from user form values
|
|
|
|
var payload =
|
|
|
|
{
|
|
|
|
'id': $scope.currentGroup.id,
|
|
|
|
'name': name,
|
|
|
|
'email': email,
|
|
|
|
'members': $scope.currentGroup.members,
|
|
|
|
'admins': $scope.currentGroup.admins
|
|
|
|
};
|
|
|
|
|
|
|
|
if (description) {
|
|
|
|
payload['description'] = description;
|
|
|
|
}
|
|
|
|
|
|
|
|
//update group success callback
|
|
|
|
function success(response) {
|
2022-05-19 14:41:11 -04:00
|
|
|
var alert = utilityService.success('Successfully Updated Group: ' + name, response, 'updateGroup::updateGroup successful');
|
|
|
|
$scope.alerts.push(alert);
|
|
|
|
$scope.closeEditModal();
|
|
|
|
$scope.reset();
|
|
|
|
$scope.refresh();
|
|
|
|
return response.data;
|
2018-10-05 10:04:06 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
return groupsService.updateGroup($scope.currentGroup.id, payload)
|
|
|
|
.then(success)
|
2022-05-19 14:41:11 -04:00
|
|
|
.catch(function (error) {
|
2018-10-05 10:04:06 -04:00
|
|
|
handleError(error, 'groupsService::updateGroup-failure');
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
2018-07-27 10:18:29 -04:00
|
|
|
$scope.confirmDeleteGroup = function (groupInfo) {
|
|
|
|
$scope.currentGroup = groupInfo;
|
|
|
|
$("#delete_group_modal").modal("show");
|
|
|
|
};
|
|
|
|
|
|
|
|
$scope.submitDeleteGroup = function () {
|
2022-05-19 14:41:11 -04:00
|
|
|
function success(response) {
|
2018-07-27 10:18:29 -04:00
|
|
|
$("#delete_group_modal").modal("hide");
|
|
|
|
$scope.refresh();
|
|
|
|
var alert = utilityService.success('Removed Group: ' + $scope.currentGroup.name, response, 'groupsService::deleteGroup successful');
|
|
|
|
$scope.alerts.push(alert);
|
|
|
|
}
|
2022-05-19 14:41:11 -04:00
|
|
|
|
2018-07-27 10:18:29 -04:00
|
|
|
groupsService.deleteGroups($scope.currentGroup.id)
|
2022-05-19 14:41:11 -04:00
|
|
|
.then(success)
|
|
|
|
.catch(function (error) {
|
|
|
|
handleError(error, 'groupsService::deleteGroup-failure');
|
|
|
|
});
|
2018-07-27 10:18:29 -04:00
|
|
|
};
|
|
|
|
|
|
|
|
function profileSuccess(results) {
|
|
|
|
//if data is provided
|
|
|
|
if (results.data) {
|
|
|
|
//update user profile data
|
|
|
|
//make user profile available to page
|
|
|
|
$scope.profile = results.data;
|
|
|
|
$log.log($scope.profile);
|
|
|
|
//load data in grid
|
|
|
|
$scope.refresh();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
function profileFailure(results) {
|
|
|
|
$scope.profile = $scope.profile || {};
|
|
|
|
}
|
|
|
|
|
2022-05-19 14:41:11 -04:00
|
|
|
$scope.groupAdmin = function (group) {
|
2018-10-05 10:04:06 -04:00
|
|
|
var isAdmin = group.admins.find(function (x) {
|
2022-05-19 14:41:11 -04:00
|
|
|
return x.id === $scope.profile.id;
|
2018-10-05 10:04:06 -04:00
|
|
|
});
|
|
|
|
var isSuper = $scope.profile.isSuper;
|
2019-08-21 14:26:51 -04:00
|
|
|
return isAdmin || isSuper;
|
|
|
|
}
|
|
|
|
|
2022-05-19 14:41:11 -04:00
|
|
|
$scope.canSeeGroup = function (group) {
|
2019-08-21 14:26:51 -04:00
|
|
|
var isMember = group.members.some(x => x.id === $scope.profile.id);
|
|
|
|
var isSupport = $scope.profile.isSupport;
|
|
|
|
var isSuper = $scope.profile.isSuper;
|
|
|
|
return isMember || isSupport || isSuper;
|
2018-10-05 10:04:06 -04:00
|
|
|
}
|
|
|
|
|
2018-07-27 10:18:29 -04:00
|
|
|
//get user data on groups view load
|
|
|
|
profileService.getAuthenticatedUserData()
|
|
|
|
.then(profileSuccess, profileFailure)
|
|
|
|
.catch(profileFailure);
|
|
|
|
|
2022-05-19 14:41:11 -04:00
|
|
|
});
|