(function () { var mod = angular.module('user-group', [ 'usefulstuff', 'angularFileUpload', 'ui.bootstrap', 'angularUtils.directives.dirPagination', 'common' ]); var conf = { modulesSharedResourcesUrl: modulesSharedResourcesUrl }; mod.value('userGroupConfiguration', conf); mod.directive('userGroup', ['$modal', '$rootScope', 'datacontext', 'fileReader', 'userGroupConfiguration', 'myUsersDataContext', 'user', 'common', function ($modal, $rootScope, datacontext, fileReader, config, myUsersDataContext, user, common) { "use strict"; return { restrict: "EA", //use this directive as an html element or an attribute templateUrl: config.modulesSharedResourcesUrl + 'Modules/UserGroup/user-group.html?version=270122', //points to the view template for this directive //create an isolated scope scope: { meId: '=', //variables with 2 way databinding group: '=', showInviteUsers: '=', showAddUsers:'=', invitedUsers: '=', users: '=', groupUser: '=', possibleUsers: '=', type: '@', addUserInvitesToGroup: '&', //pass in references to functions getUsersForGroup: '&', getPossibleUsersForGroup:'&', getUserDetailsForGroup: '&', getInvitedUsersForGroup: '&', getEmails: '&', removeUsersFromGroup: '&', addAdministratorsToGroup: '&', removeAdministratorsFromGroup: '&', addUsersToGroup: '&', getAdministratorsForGroup:'&' }, link: link //the linking function } function link($scope, element, attrs, $http) { var getLogFn = common.logger.getLogFn; var log = getLogFn("userGroup"); var logSuccess = getLogFn("userGroup", "success"); $scope.checked_users = []; $scope.checked_possibleUsers = []; $scope.file = []; $scope.emailsSent = true; $scope.isBatch = false; $scope.usersToInvite = { emails: '' }; $scope.emailsValidated = false; $scope.csvValid = true; $scope.groupUsers = []; $scope.groupUsersDetails = []; $scope.groupUsersDetailedDetails = []; $scope.emailsAlreadyInvited = []; $scope.emailsAlreadyJoined = []; $scope.pending_view = false; $scope.orderOptions = ['lastName', '-lastName', 'firstName', '-firstName']; $scope.orderInviteOptions = ['email', '-email']; $scope.myOrder = 'lastName'; $scope.myInviteOrder = 'email' $scope.orderOption = 0; $scope.orderInviteOption = 0; $scope.nextInviteOrder = function () { $scope.orderInviteOption++ if ($scope.orderInviteOption >= $scope.orderInviteOptions.length) $scope.orderInviteOption = 0 $scope.myInviteOrder = $scope.orderInviteOptions[$scope.orderInviteOption] } $scope.nextOrder = function () { $scope.orderOption++ if ($scope.orderOption >= $scope.orderOptions.length) $scope.orderOption = 0 $scope.myOrder = $scope.orderOptions[$scope.orderOption] } $scope.search = null; $scope.searchInvite = ''; $scope.search = ''; $scope.panes = [ { title: "Active members", content: '
', active: true }, { title: "Invite users", content: '' }, { title: "Add users", content: '' } ]; $scope.active = function () { return $scope.panes.filter(function (pane) { return pane.active; })[0]; }; $scope.$watch('possibleUsers', function (possibleUsers) { if (possibleUsers) trimPossibleUsers(possibleUsers); }); $scope.$watch('checked_users', function () { $scope.checkedUsersContainsAdmin = true; for (var i = 0; i < $scope.checked_users.length; i++) { for (var j = 0; j < $scope.users.length; j++) { if ($scope.checked_users[i] === $scope.users[j].id) { if ($scope.users[j].isGroupAdmin || $scope.users[j].isOrgAdmi) { $scope.checkedUsersContainsAdmin = false; } } } } for (var i = 0; i < $scope.checked_users.length; i++) { if ($scope.checked_users[i].isOrgAdmin === true || $scope.checked_users[i].isGroupAdmin === true) { $scope.checkedUsersContainsAdmin = true; } } }, true); $scope.pagination = { current: 1 }; $scope.usersPerPage = 6; $scope.startFilter = function (model) { if (model.length>=1) $scope.usersPerPage = $scope.users.length; // this should match however many results your API puts on one page else if (!model.length) $scope.usersPerPage = 6; } $scope.searchInviteFilter = function (model) { if (model.length > 1) $scope.usersPerPage = $scope.invitedUsers.length; // this should match however many results your API puts on one page else if (!model.length) $scope.usersPerPage = 6; } getUsersForGroup(); $scope.resetEmails = function () { $scope.message = null; for (var i = 0; i < $scope.invitedUsers.length; i++) { if ($scope.invitedUsers[i].email === $scope.usersToInvite.emails) { $scope.message = 'This user has already been invited!'; } } $('.label-upload').empty(); $('.label-upload').append('Choose a file to upload'); $scope.isBatch = false; $scope.usersToInvite = { emails: '' }; $scope.emailsValidated = false; $scope.emailsAlreadyInvited = []; $scope.emailsAlreadyJoined = []; } //remove users from organisation $scope.removeUsers = function (scope) { $modal.open({ templateUrl: config.modulesSharedResourcesUrl + 'Modules/OrganisationBrowser/orgusersremoval.html?version=270122', controller: manageUsersController, size: 'sm', backdrop: 'static', resolve: { checkedUsers: function () { return $scope.checked_users; }, users: function () { return $scope.users; }, theOrg: function () { return $scope.group; }, removeUsersConfirm: function () { return $scope.removeUsersConfirm; } } }); } //ensure we don't show possible users that are already in the group - probably can remove this when the MyUsers API improves function trimPossibleUsers(possUsers) { var len = possUsers.length; if (!$scope.users) return possUsers; while (len--) { for (var i = 0; i < $scope.users.length; i++) { if (possUsers[len].id === $scope.users[i].id) { possUsers.splice(len, 1); break; } } } return possUsers; } $scope.onFileSelect = function ($files) { ; if (!$files[0]) return; $scope.fileName = $files[0].name; if ($files[0].type !== 'application/vnd.ms-excel') { $scope.fileIsBad = true; } else { fileReader.readAsText($files[0], $scope).then(function (result) { $scope.fileIsBad = false; $scope.csvValid = true; $scope.validateEmails(result); clearFile(); }); } }; $scope.validateEmails = function (emails) { var emailPattern = /^(([^<>()[\]\\.,;:\s@\"]+(\.[^<>()[\]\\.,;:\s@\"]+)*)|(\".+\"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/; emails = emails.replace(/\s/, ''); emails = emails.replace(/(\r\n|\n|\r)/gm, ","); emails = emails.split(','); $scope.someEmailsAlreadyInvited = false; $scope.someEmailsAlreadyJoined = false; $scope.someUsersAlreadyExist = true; if (!$scope.emailsValidated) $scope.usersToInvite.sortedEmails = { valid: [], invalid: [] }; $scope.emailsValidated = true; var emailIsValid; $scope.usersToInvite.sortedEmails.invalid = []; emails.forEach(function (email) { email = email.trim(); var emailIsValid = emailPattern.test(email); //Check for duplicate emails if (emailIsValid) { if ($scope.usersToInvite.sortedEmails.valid.indexOf(email) == -1) { $scope.usersToInvite.sortedEmails.valid.push(email); } } else $scope.usersToInvite.sortedEmails.invalid.push(email); }); // Now lets check to see if the user has already been invited $scope.usersToInvite.sortedEmails.valid.forEach(function (email, j) { for (var i = 0; i < $scope.invitedUsers.length; i++) { if ($scope.invitedUsers[i].email === $scope.usersToInvite.sortedEmails.valid[j]) { $scope.emailsAlreadyInvited.push($scope.usersToInvite.sortedEmails.valid[j]); $scope.usersToInvite.sortedEmails.valid.splice(j, 1); $scope.someEmailsAlreadyInvited = true; } } }); // Now lets check if any of the users already exist in the org/group $scope.usersToInvite.sortedEmails.valid.forEach(function (email, j) { for (var i = 0; i < $scope.users.length; i++) { if ($scope.users[i].email === $scope.usersToInvite.sortedEmails.valid[j]) { $scope.emailsAlreadyJoined.push($scope.usersToInvite.sortedEmails.valid[j]); $scope.usersToInvite.sortedEmails.valid.splice(j, 1); $scope.someEmailsAlreadyJoined = true; } } }); $scope.usersToInvite.sortedEmails.invalidText = $scope.usersToInvite.sortedEmails.invalid.join(); } $scope.addUsers=function() { if ($scope.addUsersToGroup) { $scope.addUsersToGroup()($scope.group.id, $scope.checked_possibleUsers).then(function () { getUsersForGroup().then(function() { getPossibleUsersForGroup(); }); }); } } function clearFile() { angular.forEach( //set the file element value to null - no angular specific way to do this angular.element("input[type='file']"), function (inputElem) { angular.element(inputElem).val(null); }); } function getUsersForGroup() { if ($scope.getUsersForGroup) { return $scope.getUsersForGroup()($scope.group.id).then(function(users) { $scope.checked_users = []; $scope.users = users; if ($scope.getAdministratorsForGroup()) { $scope.getAdministratorsForGroup()($scope.group.id).then(function(groupAdmins) { for (var i = 0; i < $scope.users.length; i++) { $scope.groupUsers[i] = $scope.users[i].id; for (var j = 0; j < groupAdmins.length; j++) { if ($scope.users[i].id === groupAdmins[j].id) { $scope.users[i].isGroupAdmin = true; break; } } } getUserDetailsForGroup($scope.groupUsers); }); } }); } return null; } function getUserDetailsForGroup(userIds) { return datacontext.getUsers(userIds).then(function (data) { var il = 0; for (var i = 0, il = data.length; i < il; i++) { $scope.groupUsersDetails[i] = data[i]; } getUserDetailedDetailsForGroup($scope.groupUsers); }); } function getUserDetailedDetailsForGroup(userIds) { datacontext.getUserSummary({ userIds: userIds}).then(function (userSummarys) { $scope.userSummarys = userSummarys; var il = 0; var jl = 0; for (var i = 0, il = $scope.userSummarys.length; i < il; i++) { for (var j = 0, jl = $scope.users.length; j < jl; j++) { if ($scope.users[j].id === $scope.userSummarys[i].id) { $scope.users[j].avatarUrl = $scope.userSummarys[i].avatarUrl; } } } }); } function getPossibleUsersForGroup() { if ($scope.getPossibleUsersForGroup) { $scope.getPossibleUsersForGroup()($scope.group.id).then(function (possUsers) { $scope.possibleUsers = trimPossibleUsers(possUsers); $scope.checked_possibleUsers = []; }); } } function getInvitedUsersForGroup() { if ($scope.getInvitedUsersForGroup) { $scope.getInvitedUsersForGroup()($scope.group.id).then(function (invitedUsers) { $scope.invitedUsers = invitedUsers; }); } } $scope.getMatchingEmails = function (val) { if ($scope.getEmails) return $scope.getEmails()(val); return null; } //expects an array of email addresses $scope.invite = function (emails) { $scope.emailsSent = false; var loweredEmails = [] for (var i = 0; i < emails.length; i++) { loweredEmails.push(emails[i].toLowerCase()); } if ($scope.addUserInvitesToGroup) { $scope.addUserInvitesToGroup()($scope.group.id, loweredEmails, $scope.group.name).then(function () { getInvitedUsersForGroup(); $scope.resetEmails(); $scope.emailsSent = true; $scope.emailsValidated = false; $scope.isBatch = false; }); } } $scope.removeInvite = function (invite) { datacontext.deleteInvite(invite.inviteKey, $scope.group.id).then(function (data) { datacontext.deleteNotificationByInviteId(invite.inviteKey).then(function (data) { logSuccess("Successfully removed invite"); getInvitedUsersForGroup(); }); }); } $scope.removeUsersConfirm = function () { if ($scope.removeUsersFromGroup) { $scope.removeUsersFromGroup()($scope.group, $scope.checked_users).then(function () { getUsersForGroup().then(function() { if ($scope.showAddUsers) getPossibleUsersForGroup(); }); }); } } //close the modal $scope.cancel = function () { $modalInstance.dismiss('cancel'); }; $scope.makeUsersAdmin = function () { if ($scope.addAdministratorsToGroup) { $scope.addAdministratorsToGroup()($scope.group, $scope.checked_users).then(function () { getUsersForGroup(); }); } } $scope.removeUsersAdmin = function () { if ($scope.removeAdministratorsFromGroup) { $scope.removeAdministratorsFromGroup()($scope.group, $scope.checked_users).then(function () { getUsersForGroup(); }); } } var manageUsersController = function ($scope, $modalInstance, users, checkedUsers, datacontext, theOrg, removeUsersConfirm) { $scope.org = theOrg; $scope.users = users; $scope.checkedUsers = checkedUsers; $scope.removeUsersDetails = []; $scope.removeUsersConfirm = removeUsersConfirm; getUserDetailsForGroup(); //close the modal $scope.cancel = function () { $modalInstance.dismiss('cancel'); }; function getUserDetailsForGroup() { var il = 0; var jl = 0; for (var i = 0, il = $scope.users.length; i < il; i++) { for (var j = 0, jl = $scope.users.length; j < jl; j++) { if ($scope.users[i].id === $scope.checkedUsers[j]) { $scope.removeUsersDetails[j] = $scope.users[i]; } } } } }; } }]); })();