(function () { var mod = angular.module('organisations', [ //inject the other modules that this module needs 'common', 'ui.tree', 'ui.bootstrap', 'ui.bootstrap.modal', 'usefulstuff', 'user-group', 'myusers', 'frameworksModule', 'mk.editablespan', 'angularUtils.directives.dirPagination', ]); mod.directive('orgJoinRules', ['configuration', function (config) { "use strict"; return { restrict: "EA", //directive can be used as an html element or attribute templateUrl: config.modulesSharedResourcesUrl + 'Modules/OrganisationBrowser/orgjoinrules.html?version=270122', //the angular template (the view) for this directive scope: { //the isolated scope for this directive organisation: '=' }, link: link } function link($scope, element, attrs) { $scope.organisation.validEmailDomainsValid = []; $scope.organisation.validEmailDomains.forEach(function (emailDomain) { if ($scope.organisation.validEmailDomainsValid.indexOf(emailDomain.emailDomain) == -1) { $scope.organisation.validEmailDomainsValid.push(emailDomain.emailDomain); } }); $scope.organisation.newRule = ""; $scope.addEmailSubstringRule = function () { $scope.organisation.validEmailDomains.push({ emailDomain: $scope.organisation.newRule }); $scope.organisation.validEmailDomainsValid.push($scope.organisation.newRule); $scope.organisation.newRule = null; } $scope.removeEmailSubstringRule = function (emailAddressJoinRule) { var index = $scope.organisation.validEmailDomains.indexOf(emailAddressJoinRule); var indexValid = $scope.organisation.validEmailDomainsValid.indexOf(emailAddressJoinRule); $scope.organisation.validEmailDomainsValid.splice(indexValid, 1); $scope.organisation.validEmailDomainsValidForSave = []; $scope.organisation.validEmailDomain - [] for (var i = 0; i < $scope.organisation.validEmailDomainsValid.length; i++) { $scope.organisation.validEmailDomainsValidForSave.push({ emailDomain: $scope.organisation.validEmailDomainsValid[i] }) } $scope.organisation.validEmailDomains = $scope.organisation.validEmailDomainsValidForSave; } } } ]); // directive to browse and manage an organisation mod.directive('orgBrowser', ['common', 'myUsersDataContext', '$modal', '$rootScope', 'configuration', 'frameworks', '$upload', 'user', function (common, myUsersDataContext, $modal, $rootScope, config, frameworks, $upload, user) { "use strict"; var noOrgId = config.noOrgId; return { restrict: "EA", //directive can be used as an html element or attribute templateUrl: config.modulesSharedResourcesUrl + 'Modules/OrganisationBrowser/orgbrowser.html?version=270122', //the angular template (the view) for this directive scope: { //the isolated scope for this directive meId: '=', addNewOrg: '&', onEditOrg: '&', onDeleteOrg: '&', onAddUsersToOrg: '&', onRemoveUsersFromOrg: '&', onMakeUsersAdmin: '&', onRemoveUsersAdmin: '&', noOrgs: '=', azureUpload: '&', cloudinaryUpload: '&', hideTree: '@', multipleIssuers: '=', multipleAdmins: '=', manageApiKey: '=' }, link: link } function link($scope, element, attrs) { var controllerId = 'organisations'; var getLogFn = common.logger.getLogFn; var logSuccess = getLogFn(controllerId, "success"); var logError = getLogFn(controllerId, "error"); $scope.nameRegex = '/[a-zA-Z]{4}[0-9]{6,6}[a-zA-Z0-9]{3}/'; //[a-zA-Z0-9][a-zA-Z0-9'.\s- ']{0,1000}$ $scope.features = $rootScope.features; $scope.options = { //function called when an org is dropped (after dragging) dropped: function (event) { var idOfMoved = event.source.nodeScope.$modelValue.id; //the id of the orgnaisation being dropped var indexDroppedPosition = event.dest.index; //where in the list is it being dropped var indexSourcePosition = event.source.index; //what was the index in the last before it was dropped if (event.dest.nodesScope.$nodeScope) { var idOfNewParent = event.dest.nodesScope.$nodeScope.$modelValue.id; //the id of the org that is the parent after dropping var idOfOldParent = event.source.nodeScope.$parentNodeScope.$modelValue.id; //the id of the org that is the parent before dropping //make sure either the organistion has moved position within the same parent or moved to a new parent if (idOfNewParent !== idOfOldParent || indexDroppedPosition !== indexSourcePosition) { //call the service that does the move - telling it where it was moved to myUsersDataContext.moveOrg(idOfMoved, { parentId: idOfNewParent, order: indexDroppedPosition }).then(function () { //redisplay the org tree $rootScope.$broadcast('UpdateOrganisationDetails'); }); } } } }; setOrganisations(); function dynamicSort(property) { var sortOrder = 1; if (property[0] === "-") { sortOrder = -1; property = property.substr(1); } return function (a, b) { var result = (a[property] < b[property]) ? -1 : (a[property] > b[property]) ? 1 : 0; return result * sortOrder; } } function setOrganisations(org) { myUsersDataContext.getOrganisationsFull(config.organisationTypeId).then(function (data) { $scope.organisations = data; $scope.noOrgs = true; if ($scope.organisations.length > 0) { $scope.noOrgs = false; if (org) $scope.currentOrg = org; else $scope.currentOrg = $scope.organisations[0]; setOrganisationDetails($scope.currentOrg.id); } }); } $scope.clearModal = true; $scope.activated = false; //show / hide child orgs $scope.toggleOrg = function (scope) { scope.toggle(); } //this works but not sure that we should be allowed to make a parent of the 'MKM' org $scope.createTopLevelOrg = function (org) { // openAddEditModal(org, false,true); } //get the data for the tree function setOrganisationDetails(orgId) { myUsersDataContext.getOrganisationDetails(orgId).then(function (data) { //remove the'no org' org from the tree - it shouldn't be shown var index = -1; for (var i = 0; i < data.children.length; i++) { if (data.children[i].id === noOrgId) { index = i; break; } } if (index > -1) data.children.splice(index, 1); $scope.organisationDetails = [data]; $scope.organisationDetails[0].children.sort(dynamicSort("name")); $scope.loadedyet = true; }); } //remove an organisation $scope.removeOrg = function (scope) { var org = scope.$modelValue; $modal.open({ templateUrl: config.modulesSharedResourcesUrl + 'Modules/OrganisationBrowser/orgremoval.html?version=270122', controller: removeOrgController, size: 'sm', backdrop: 'static', resolve: { theOrg: function () { return org; }, fullOrgDetails: function () { return $scope.organisationDetails; }, onDeleteOrg: function () { return $scope.onDeleteOrg; } } }); }; //chnage to a new top level org $scope.changeOrganisation = function () { setOrganisationDetails($scope.currentOrg.id); } //edit an existing org $scope.editOrganisation = function (scope) { var org = scope.$modelValue; openAddEditModal(org, true, false); } //manage the users for an organisation $scope.manageUsers = function (scope) { var theOrg = scope.$modelValue; $modal.open({ templateUrl: config.modulesSharedResourcesUrl + 'Modules/OrganisationBrowser/orgusers.html?version=270122', controller: manageUsersController, size: 'lg', backdrop: 'static', resolve: { theOrg: function () { return theOrg; }, meId: function () { return $scope.meId; }, onAddUsersToOrg: function () { return $scope.onAddUsersToOrg(); }, onRemoveUsersFromOrg: function () { return $scope.onRemoveUsersFromOrg(); }, onMakeUsersAdmin: function () { return $scope.onMakeUsersAdmin(); }, onRemoveUsersAdmin: function () { return $scope.onRemoveUsersAdmin; } } }); } $scope.manageFrameworks = function (scope) { var theOrg = scope.$modelValue; $modal.open({ templateUrl: config.modulesSharedResourcesUrl + 'Modules/OrganisationBrowser/orgframeworks.html?version=270122', controller: manageFrameworksController, size: 'lg', backdrop: 'static', resolve: { theOrg: function () { return theOrg; }, meId: function () { return $scope.meId; } } }); } $scope.managingJoinRules = function (scope) { var theOrg = scope.$modelValue; $modal.open({ templateUrl: config.modulesSharedResourcesUrl + 'Modules/OrganisationBrowser/orgjoinrulesmodal.html?version=270122', controller: manageJoinRulesController, size: 'lg', backdrop: 'static', resolve: { theOrg: function () { return theOrg; }, meId: function () { return $scope.meId; } } }); } //manage the users for an organisation $scope.manageIssuer = function (scope) { var theOrg = scope.$modelValue; $scope.issuer = null; myUsersDataContext.getOrganisationIssuer(theOrg.id).then(function (data) { $scope.issuer = data; openIssueModal(); }).catch(function (error) { openIssueModal(); }); function openIssueModal() { $modal.open({ templateUrl: config.modulesSharedResourcesUrl + 'Modules/OrganisationBrowser/orgissuer.html?version=270122', controller: manageIssuerController, size: 'sm', backdrop: 'static', resolve: { theOrg: function () { return theOrg; }, meId: function () { return $scope.meId; }, Issuer: function () { return $scope.issuer; }, createOrgIssuer: function () { return $scope.createOrgIssuer; }, deleteOrgIssuer: function () { return $scope.deleteOrgIssuer; }, updateOrgIssuer: function () { return $scope.updateOrgIssuer; }, azureUpload: function () { return $scope.azureUpload; }, cloudinaryUpload: function () { return $scope.cloudinaryUpload; } } }); } } $scope.createOrgIssuer = function (orgId) { myUsersDataContext.createOrganisationIssuer(orgId).then(function (data) { $rootScope.containsValidIssuer = true; logSuccess('Badge issuer successfully updated'); return data; }); } $scope.deleteOrgIssuer = function (orgId) { myUsersDataContext.deleteOrganisationIssuer(orgId).then(function (data) { logSuccess('Badge issuer successfully deleted'); return data; }); } $scope.updateOrgIssuer = function (issuer) { if (issuer.id == null) myUsersDataContext.createOrganisationIssuer(issuer).then(function (data) { $rootScope.containsValidIssuer = true; logSuccess('Badge issuer successfully updated'); return data; }); else { myUsersDataContext.updateOrganisationIssuer(issuer).then(function (data) { $rootScope.containsValidIssuer = true; logSuccess('Badge issuer successfully updated'); return data; }); } } $scope.clearModal = true; $scope.$on('UpdateOrganisationDetails', function (event, args) { setOrganisationDetails($scope.currentOrg.id); }); $scope.$on('UpdateOrganisations', function (event, args) { setOrganisations(args); }); function openAddEditModal(parentOrg, isEdit, isTopLevel) { $modal.open({ templateUrl: config.modulesSharedResourcesUrl + 'Modules/OrganisationBrowser/addeditorg.html?version=270122', controller: addEditOrgController, size: 'sm', backdrop: 'static', resolve: { parentOrg: function () { return parentOrg; }, isEdit: function () { return isEdit; }, isTopLevel: function () { return isTopLevel; }, addNewOrg: function () { return $scope.addNewOrg(); }, onEditOrg: function () { return $scope.onEditOrg(); }, manageApiKey: function () { return $scope.manageApiKey; } } }); } $scope.newSubOrg = function (scope) { var parentOrg = scope.$modelValue; openAddEditModal(parentOrg, false, false); }; var addEditOrgController = function (common, $scope, $modalInstance, parentOrg, isEdit, isTopLevel, addNewOrg, onEditOrg, manageApiKey) { var controllerId = 'organisations'; var getLogFn = common.logger.getLogFn; var logSuccess = getLogFn(controllerId, "success"); var logError = getLogFn(controllerId, "error"); $scope.parentOrg = parentOrg; $scope.isTopLevel = isTopLevel; $scope.isEdit = isEdit; $scope.addNewOrg = addNewOrg; $scope.onEditOrg = onEditOrg; $scope.manageApiKey = manageApiKey; if (isEdit) { $scope.organisation = { id: parentOrg.id, name: parentOrg.name, description: parentOrg.description, children: parentOrg.children, order: parentOrg.order, selfRegistration: parentOrg.selfRegistration, apiKey: parentOrg.apiKey, apiUrl: parentOrg.apiUrl, externalId: "" }; myUsersDataContext.getOrganisationExternalId($scope.organisation.id).then(function (externalId) { if (externalId) { $scope.organisation.externalId = externalId } }); myUsersDataContext.getOrganisationApiDetails($scope.organisation.id).then(function (externalId) { if (externalId) { angular.forEach(apiDetails, function (apiDetail) { if (apiDetail) { if (apiDetail.connectionType != 17) { return; } if (apiDetail.properties) { $scope.organisation.apiKey = apiDetail.properties.apiKey; $scope.organisation.apiUrl = apiDetail.properties.systemUrl; } } }); } }); if (manageApiKey) { myUsersDataContext.getOrganisationApiDetails($scope.organisation.id).then(function (apiDetails) { if (apiDetails) { angular.forEach(apiDetails, function (apiDetail) { if (apiDetail) { if (apiDetail.connectionType != 17) { return; } if (apiDetail.properties) { $scope.organisation.apiKey = apiDetail.properties.apiKey; $scope.organisation.apiUrl = apiDetail.properties.systemUrl; } } }); } }); } } else $scope.organisation = {}; //close the modal $scope.cancel = function () { $modalInstance.dismiss('cancel'); }; $("#description").keypress(function (e) { if (e.keyCode != 13) return; var msg = $("#description").val().replace(/\n/g, ""); if (!util.isBlank(msg)) { send(msg); $("#description").val(""); } return false; }); $scope.ok = function () { $scope.organisation.selfRegistration = true; //TODO - maybe take this out if ($scope.isTopLevel) { myUsersDataContext.createNewParentOrg($scope.parentOrg.id, $scope.organisation).then(function (newOrg) { updateApiDetails(); completeUpdate(newOrg); $scope.organisation.id = newOrg; updateExternalId(); if ($scope.addNewOrg) $scope.addNewOrg($scope.organisation); }); } else { if ($scope.isEdit) { myUsersDataContext.editOrganisation($scope.organisation.id, $scope.organisation).then(function () { updateApiDetails(); updateExternalId(); completeUpdate(); if ($scope.onEditOrg) $scope.onEditOrg($scope.organisation); }); } else { myUsersDataContext.addChildOrganisation($scope.parentOrg.id, $scope.organisation).then(function (newOrg) { updateApiDetails(); completeUpdate(); $scope.organisation.id = newOrg; updateExternalId(); if ($scope.addNewOrg) $scope.addNewOrg($scope.organisation); }); } } logSuccess("Organisation created"); $rootScope.$broadcast('UpdateOrganisations'); }; function updateApiDetails() { if (manageApiKey) myUsersDataContext.editOrganisationApiDetails({ organisationId: $scope.organisation.id, systemUrl: $scope.apiUrl, connectionName: $scope.apiUrl, connectionType: 17, properties: { "ApiKey": $scope.apiKey, "SystemUrl": $scope.apiUrl } }).then(function (apiDetailsUpdated) { logSuccess("Api details saved"); }); } function updateExternalId() { if ($scope.organisation.externalId) { var externalModel = { externalId: $scope.organisation.externalId } myUsersDataContext.saveOrganisationExternalId($scope.organisation.id, externalModel).then(function (apiDetailsUpdated) { logSuccess("External Id details saved"); }); } else { var externalModel = { externalId: "" } myUsersDataContext.saveOrganisationExternalId($scope.organisation.id, externalModel).then(function (apiDetailsUpdated) { logSuccess("External Id details saved"); }); } } function completeUpdate(newOrg) { if (isTopLevel) $rootScope.$broadcast('UpdateOrganisations', newOrg); else $rootScope.$broadcast('UpdateOrganisationDetails'); $modalInstance.close(); } }; //var controllerId = 'manageUsersController'; //angular.module('app').controller(controllerId, ['common', 'config', '$sce', '$location', '$window', 'datacontext', '$rootScope', 'validation', manageUsersController]); var manageUsersController = function ($scope, $modalInstance, theOrg, meId, onAddUsersToOrg, onRemoveUsersFromOrg, onMakeUsersAdmin, onRemoveUsersAdmin) { $scope.theOrg = theOrg; $scope.meId = meId; $scope.onAddUsersToOrg = onAddUsersToOrg; $scope.onRemoveUsersFromOrg = onRemoveUsersFromOrg; $scope.onMakeUsersAdmin = onMakeUsersAdmin; $scope.onRemoveUsersAdmin = onRemoveUsersAdmin; //close the modal $scope.cancel = function () { $modalInstance.dismiss('cancel'); }; $scope.getUsersForOrganisation = function (orgId) { return myUsersDataContext.getUsers(orgId).then(function (data) { $scope.users = data; return data; }); } $scope.getPossibleUsersForOrganisation = function () { return myUsersDataContext.getUsers(noOrgId).then(function (data) { return $scope.possibleUsers = data; }); } $scope.getAdministratorsForOrganisation = function (orgId) { return myUsersDataContext.getAdministratorsForOrganisation(orgId).then(function (data) { return data; }); } $scope.getInvitedUsersForOrganisation = function (orgId) { return myUsersDataContext.getInvitedUsers(orgId).then(function (invitedUsers) { for (var i = 0; i < invitedUsers.length; i++) { invitedUsers[i].email = invitedUsers[i].emailAddress; } $scope.invitedUsers = invitedUsers; }); } $scope.invite = function (orgId, emails, orgName) { return myUsersDataContext.addInvitedUsers(orgId, emails).then(function (data) { if (data) return myUsersDataContext.sendInviteEmails({ IndividualOrganisationInvites: data, organisation: { id: orgId, name: orgName }, isOrg: true }); }); } $scope.removeUsers = function (org, users) { return myUsersDataContext.removeUsers(org.id, users).then(function () { if ($scope.onRemoveUsersFromOrg) $scope.onRemoveUsersFromOrg(org, users); }); } $scope.makeUsersAdmin = function (org, users) { return myUsersDataContext.addAdministrators(org.id, users, true, false).then(function () { if ($scope.onMakeUsersAdmin) $scope.onMakeUsersAdmin(org, users); }); } $scope.removeUsersAdmin = function (org, users) { return myUsersDataContext.removeAdministrators(org.id, users).then(function () { if ($scope.onRemoveUsersAdmin) $scope.onRemoveUsersAdmin(org, users); }); } $scope.addUsersToOrganisation = function (orgId, addedUsers) { return myUsersDataContext.addUsers(orgId, addedUsers).then(function () { if ($scope.onAddUsersToOrg) { var fullAddedUsers = []; for (var i = 0; i < $scope.possibleUsers.length; i++) { if (addedUsers.length === fullAddedUsers.length) break; for (var j = 0; j < addedUsers.length; j++) { if ($scope.possibleUsers[i].id === addedUsers[j]) { fullAddedUsers.push($scope.possibleUsers[i]); break; } } } $scope.onAddUsersToOrg(orgId, fullAddedUsers); } }); } $scope.getUsersForOrganisation($scope.theOrg.id); $scope.getInvitedUsersForOrganisation($scope.theOrg.id); $scope.getPossibleUsersForOrganisation($scope.theOrg.id); }; //Controller for managing issuer var manageIssuerController = function ($scope, $modalInstance, theOrg, meId, Issuer, createOrgIssuer, deleteOrgIssuer, updateOrgIssuer, azureUpload, cloudinaryUpload) { $scope.theOrg = theOrg; $scope.createOrgIssuer = createOrgIssuer; $scope.deleteOrgIssuer = deleteOrgIssuer; $scope.updateOrgIssuer = updateOrgIssuer; $scope.isEdit = false; $scope.issuer = Issuer; $scope.fileName = 'Choose file'; // get the current issuer if it exists else we create the issuer object if ($scope.issuer) { $scope.issuer = Issuer; $scope.isEdit = true; } else { $scope.issuer = { organisationId: theOrg.id, createdBy: meId, name: theOrg.name, description: theOrg.description, jsonUrl: '', logoUrl: null, }; } // Called when image file has been selected $scope.onOrgLogoFileSelect = function (files) { $scope.file = files[0]; // we're not interested in multiple file uploads here // check if the user has actually selected an image file else throw an error if ($scope.file.type !== 'image/jpeg' && $scope.file.type !== 'image/png' && $scope.file.type !== 'image/gif' || $scope.file.type === 'image/tiff') { $scope.errorMessage = "That doesn't look like an image file!"; return; } // check if the user has selected an image below our maximum file size of 20MB if ($scope.file.size < 20000000) { $scope.imageAdded = true; var reader = new FileReader(); reader.onload = function (e) { $scope.$apply(function () { // get loaded data and render thumbnail. $scope.logoPreview = e.target.result; }); }; // Generate base 64 image for preview reader.readAsDataURL($scope.file); } else { $scope.errorMessage = 'Image is too large!'; return; } $scope.fileName = $scope.file.name; files.originalImageFileName = $scope.file.name; files.imageFileSize = $scope.file.size; files.imageFileType = $scope.file.type; $scope.files = files; } //upload org logo function uploadImage(obj, file, success, progress, error, uploadPath) { var data = { upload_preset: cloudinaryUploadPreset, tags: 'mybadges,' + theOrg.id }; //images are uploaded to Cloudinary $upload.upload({ url: cloudinaryApiBaseUrl + cloudinaryName + uploadPath, data: data, file: file }).progress(function (e) { var percent = Math.round((e.loaded * 100.0) / e.total); return progress(percent); }).success(function (data, status, headers, config) { return success(obj, data); }).error(function (data) { return error(); }); } //called when a logo is uploaded function UploadSuccess(files, data) { //ensure we always use https data.url = data.url.replace(/^http:\/\//i, 'https://'); data.url = data.url.replace(/image\/upload\//g, 'image/upload/w_200,h_200/'); $scope.issuer.logoUrl = data.url $scope.progress = null; // check if we are editing or creating a new issuer if ($scope.isEdit === false) $scope.createOrgIssuer($scope.issuer); else $scope.updateOrgIssuer($scope.issuer); $modalInstance.dismiss('cancel'); //work out if its and bade or image and update accordingly return; } // Generate the image upload process percentage function UploadProgress(percentComplete) { $scope.progress = percentComplete; $scope.uploadStatus = "Uploading... " + percentComplete + "%"; } function UploadError() { console.log("Problem uploading json"); } // save the organisation issuer $scope.saveOrganisationIssuer = function () { // Upload the image to cloudinary if ($scope.logoPreview) { uploadImage($scope.files, $scope.file, UploadSuccess, UploadProgress, UploadError, cloudinaryUploadPath); } else { $scope.updateOrgIssuer($scope.issuer); $modalInstance.dismiss('cancel'); } } $scope.resetImage = function () { $scope.issuer.logoUrl = null; $scope.logoPreview = null; $scope.orgLogo = null; $scope.fileName = 'Choose file'; } // close the modal $scope.cancel = function () { $modalInstance.dismiss('cancel'); }; }; var manageFrameworksController = function ($scope, $modalInstance, theOrg, meId) { $scope.theOrg = theOrg; $scope.meId = meId; myUsersDataContext.getFrameworksForOrg(theOrg.id).then(function (theFrameworks) { if (theFrameworks.length > 0) { frameworks.getFrameworkList(theFrameworks).then(function (data) { for (var i = 0; i < data.length; i++) { data[i].deactivated = false; } $scope.chosenFrameworks = data; }); } else $scope.chosenFrameworks = []; }); $scope.ok = function () { var ids = []; for (var i = 0; i < $scope.chosenFrameworks.length; i++) { ids.push($scope.chosenFrameworks[i].id); } myUsersDataContext.editFrameworksForOrg(theOrg.id, ids).then(function () { $modalInstance.close(); }); } //close the modal $scope.cancel = function () { $modalInstance.dismiss('cancel'); }; }; var manageJoinRulesController = function ($scope, $modalInstance, $window, theOrg, meId) { $scope.clearModal = true; $scope.saveIsDisabled = false; $scope.organisation = theOrg; $scope.organisationTemp = angular.copy(theOrg); $scope.meId = meId; //close the modal $scope.cancel = function () { $scope.clearModal = false; $modalInstance.dismiss('cancel'); }; $scope.ok = function () { $scope.organisation = theOrg; $scope.organisation = $scope.organisationTemp; $scope.saveIsDisabled = true; myUsersDataContext.editOrganisation($scope.organisation.id, $scope.organisation).then(function () { $rootScope.$broadcast('UpdateOrganisations'); $modalInstance.dismiss('cancel'); logSuccess('Organisation sucessfully updated'); }); } }; var removeOrgController = function ($scope, $modalInstance, theOrg, fullOrgDetails, onDeleteOrg) { $scope.theOrg = theOrg; $scope.fullOrgDetails = fullOrgDetails; $scope.rootOrgId = fullOrgDetails[0].id; $scope.onDeleteOrg = onDeleteOrg; //close the modal $scope.cancel = function () { $modalInstance.dismiss('cancel'); }; $scope.getUsersForOrganisation = function (orgId) { return myUsersDataContext.getUsers(orgId).then(function (data) { $scope.users = data; }); } $scope.confirmDelete = function () { deleteOrg($scope.theOrg.id); } function deleteOrg(orgId) { myUsersDataContext.deleteOrganisation(orgId).then(function () { $scope.onDeleteOrg()(orgId); $rootScope.$broadcast('UpdateOrganisationDetails'); $modalInstance.dismiss(); }); } function getOrganisationDetails(orgId) { myUsersDataContext.getOrganisationDetails(orgId).then(function (data) { //remove the'no org' org $scope.hasChildOrgs = data.children.length > 0; var index = -1; for (var i = 0; i < data.children.length; i++) { if (data.children[i].id === noOrgId) { index = i; break; } } if (index > -1) data.children.splice(index, 1); $scope.organisationDetails = [data]; }); } function getUserIds(users) { var userIds = []; for (var i = 0; i < users.length; i++) { userIds.push(users[i].id); } return userIds; } $scope.removeUsers = function () { return myUsersDataContext.removeUsers($scope.theOrg.id, getUserIds($scope.users)).then(function () { deleteOrg($scope.theOrg.id); }); } $scope.reassignUsersToRoot = function () { var userIds = getUserIds($scope.users); return myUsersDataContext.removeUsers($scope.theOrg.id, userIds).then(function () { myUsersDataContext.addUsers($scope.rootOrgId, userIds).then(function () { deleteOrg($scope.theOrg.id); }); }); } $scope.addUsersToOrganisation = function (orgId, users) { return myUsersDataContext.addUsers(orgId, users); } $scope.getUsersForOrganisation($scope.theOrg.id); getOrganisationDetails($scope.theOrg.id); }; } }]); mod.directive('orgAndGroupChooser', ['myUsersDataContext', 'configuration', function (myUsersDataContext, config) { return { restrict: "EA", //directive can be used as an html element or attribute templateUrl: config.modulesSharedResourcesUrl + 'Modules/OrganisationBrowser/organdgroupchooser.html?version=270122', //the angular template (the view) for this directive scope: { //the isolated scope for this directive chosenOrganisations: '=', chosenUsers: '=', allocateUsers: '=', showInteractionRules: '=', activityInteractionRules: '=', disableOrgChoice: '@', hideOrgs: '=', availableOrgs: '=', hideGroups: '=', removedOrganisations: '=', includeGroupType: '=', ownerOrganisations: '=' }, link: link } function link(scope, element, attrs) { scope.organisations = []; scope.groups = []; scope.jobRoles = []; scope.teams = []; scope.employerGroups = []; scope.mentorGroups = []; scope.employerOrganisations = []; setOrganisations(); scope.topLevelOrg = null; function setOrganisations() { myUsersDataContext.getOrganisationsFullFlatSimple().then(function (data) { for (var i = 0; i < data.length; i++) { if (data[i].type === 0) { scope.organisations.push(data[i]) } if (data[i].type === 2) { scope.groups.push(data[i]) } if (data[i].type === 5) { scope.jobRoles.push(data[i]) } if (data[i].type === 6) { scope.teams.push(data[i]) } if (data[i].type === 8) { scope.employerGroups.push(data[i]) } if (data[i].type === 7) { scope.employerOrganisations.push(data[i]) } if (data[i].type === 9) { scope.mentorGroups.push(data[i]) } //Top level parent is the org without a parentid if (data[i].parentId == null && data[i].type == 0) { scope.topLevelOrg = data[i]; } } //If topLevelOrg null, just set the first org/group the user has since it's a hierarchical list if (scope.topLevelOrg == null) { scope.topLevelOrg = data[0]; } scope.refreshIsSelected(); }); } scope.refreshIsSelected = function() { for (var i = 0; i < scope.organisations.length; i++) { if (scope.chosenOrganisations) { if (scope.organisations[i] != null) { var groupIndex = scope.chosenOrganisations.indexOf(scope.organisations[i].id); if (groupIndex > -1) { scope.organisations[i].isSelected = true; //if (scope.topLevelOrg != null && scope.organisations[i].id == scope.topLevelOrg.id) { // scope.organisations[i].disableSelect = true; //} } //We need to disable deselecting orgs that must always be able to manage this badge template, i.e. the 'owners' if (scope.ownerOrganisations) { var ownerIdsIndex = scope.ownerOrganisations.indexOf(scope.organisations[i].id); if (ownerIdsIndex > -1) { scope.organisations[i].disableSelect = true; } } } } } for (var i = 0; i < scope.groups.length; i++) { if (scope.chosenOrganisations) { if (scope.groups[i] != null) { var groupIndex = scope.chosenOrganisations.indexOf(scope.groups[i].id); if (groupIndex > -1) { scope.groups[i].isSelected = true; if (scope.topLevelOrg != null && scope.groups[i].id == scope.topLevelOrg.id) { scope.groups[i].disableSelect = true; } } //We need to disable deselecting orgs that must always be able to manage this badge template, i.e. the 'owners' if (scope.ownerOrganisations) { var ownerIdsIndex = scope.ownerOrganisations.indexOf(scope.groups[i].id); if (ownerIdsIndex > -1) { scope.groups[i].disableSelect = true; } } } } } for (var i = 0; i < scope.jobRoles.length; i++) { if (scope.chosenOrganisations) { if (scope.jobRoles[i] != null) { var groupIndex = scope.chosenOrganisations.indexOf(scope.jobRoles[i].id); if (groupIndex > -1) { scope.jobRoles[i].isSelected = true; } } } } for (var i = 0; i < scope.teams.length; i++) { if (scope.chosenOrganisations) { if (scope.teams[i] != null) { var groupIndex = scope.chosenOrganisations.indexOf(scope.teams[i].id); if (groupIndex > -1) { scope.teams[i].isSelected = true; } } } } for (var i = 0; i < scope.employerGroups.length; i++) { if (scope.chosenOrganisations) { if (scope.employerGroups[i] != null) { var groupIndex = scope.chosenOrganisations.indexOf(scope.employerGroups[i].id); if (groupIndex > -1) { scope.employerGroups[i].isSelected = true; } } } } for (var i = 0; i < scope.mentorGroups.length; i++) { if (scope.chosenOrganisations) { if (scope.mentorGroups[i] != null) { var groupIndex = scope.chosenOrganisations.indexOf(scope.mentorGroups[i].id); if (groupIndex > -1) { scope.mentorGroups[i].isSelected = true; } } } } for (var i = 0; i < scope.employerOrganisations.length; i++) { if (scope.chosenOrganisations) { if (scope.employerOrganisations[i] != null) { var groupIndex = scope.chosenOrganisations.indexOf(scope.employerOrganisations[i].id); if (groupIndex > -1) { scope.employerOrganisations[i].isSelected = true; } } } } } scope.orgSelected = function (org, onlyAvailable) { if (!angular.isArray(scope.chosenOrganisations)) { return null; } if (org.isSelected) { scope.chosenOrganisations.push(org.id); } else { var index = scope.chosenOrganisations.indexOf(org.id); scope.chosenOrganisations.splice(index, 1); if (!scope.removedOrganisations) { scope.removedOrganisations = []; } scope.removedOrganisations.push(org.id); } scope.refreshIsSelected(); } } }]); mod.directive('userChooser', ['usersAdminDataContext', 'config', 'usersAdminService', 'common', '$location', '$modal', '$filter', '$timeout', '$q', 'user', 'rolesAdminDataContext', function (usersAdminDataContext, config, usersAdminService, common, $location, $modal, $filter, $timeout, $q, user, rolesAdminDataContext) { return { restrict: 'E', templateUrl: config.modulesSharedResourcesUrl + 'Modules/OrganisationBrowser/userchooser.html', scope: { singleCheckOnly: '@', checkedUsers: '=', pageSize: '=', removedUsers: '=' }, link: link }; function link(scope, elem, attrs) { scope.users = []; var userIds = []; scope.groups = []; scope.ready = false; scope.roles = []; scope.search = ''; scope.orderOptions = ['-lastName', 'lastName', '-firstName', 'firstName']; scope.myOrder = '-lastName'; scope.orderOption = 0; scope.pending_view = false; scope.checkAll = false; scope.pagination = { current: 1 }; if (scope.pageSize == null) scope.pageSize = 6; scope.selectedOrg; scope.startFilter = function (model) { if (model.length >= 1) { scope.pageSize = scope.users.length; // this should match however many results your API puts on one page scope.pagination.current = 1; } else scope.pageSize = 6; } // Make sure we clear any previously selected user usersAdminService.setManagingUser(null); function getUsersAndOrgs() { return usersAdminService.getAllUsers().then(function (data) { var users = data.users; scope.orgs = data.organisations; for (first in scope.orgs) { scope.selectedOrg = first; break; } scope.users = users; scope.users.map(function (theUser) { userIds.push(theUser.id) theUser.userId = theUser.id; theUser.organisations = theUser.organisations.map(function (orgId) { return scope.orgs[orgId] }); return theUser; }); scope.userIds = userIds; scope.allUsers = users; scope.userAllocations = {}; scope.ready = true; }); } scope.userChecked = function (user) { if (scope.singleCheckOnly) { scope.checkedUsers = [user.id]; } } scope.checkAllUsers = function (users, checkAll) { if (checkAll) { for (i in users) { scope.checkedUsers.push(users[i].id); } } else { for (i in users) { const index = scope.checkedUsers.indexOf(users[i].id); if (index > -1) { scope.checkedUsers.splice(index, 1); } } } } scope.nextOrder = function () { scope.orderOption++ if (scope.orderOption >= scope.orderOptions.length) scope.orderOption = 0 scope.myOrder = scope.orderOptions[scope.orderOption] } getUsersAndOrgs(); } }]); mod.directive('selectedUsersViewer', ['usersAdminDataContext', 'config', 'usersAdminService', 'common', '$location', '$modal', '$filter', '$timeout', '$q', 'user', 'rolesAdminDataContext', function (usersAdminDataContext, config, usersAdminService, common, $location, $modal, $filter, $timeout, $q, user, rolesAdminDataContext) { return { restrict: 'E', templateUrl: config.modulesSharedResourcesUrl + 'Modules/OrganisationBrowser/selectedusersviewer.html', scope: { selectedUsers: '=', removedUsers: '=', pageSize: '=', descriptionText: '@' }, link: link }; function link($scope, elem, attrs) { $scope.users = []; var userIds = []; var removing = false; $scope.groups = []; $scope.ready = false; $scope.orderOptions = ['lastName', '-lastName', 'firstName', '-firstName']; $scope.myOrder = 'lastName'; $scope.orderOption = 0; $scope.pending_view = false; $scope.pagination = { current: 1 }; if ($scope.pageSize == null) $scope.pageSize = 6; $scope.startFilter = function (model) { if (model.length >= 1) { $scope.pageSize = $scope.users.length; // this should match however many results your API puts on one page $scope.pagination.current = 1; } else $scope.pageSize = 6; } $scope.$watch('selectedUsers', function (newValue, oldValue) { if (!removing) { if (!$scope.ready && $scope.selectedUsers.length > 0) { for (i in newValue) { getUserDetails(newValue[i]) } $scope.ready = true; } else { var newUserId = oldValue.filter(function (obj) { return newValue.indexOf(obj) == -1; }) if (newUserId.length == 0) { newUserId = newValue.filter(function (obj) { return oldValue.indexOf(obj) == -1; }); } if ($scope.users.length > 0) { for (i in $scope.users) { if ($scope.users[i].userId == newUserId) { $scope.users.splice(i, 1); CheckValues(newValue, oldValue); return; } } } if (newUserId.length > 0) { for (i in newUserId) { getUserDetails(newUserId[i]) } $scope.ready = true; } } console.log($scope.users); } CheckValues(newValue, oldValue); }, true); function CheckValues(newValue, oldValue) { if (newValue.length < oldValue.length) { //user has been removed. for (var i = 0; i < oldValue.length; i++) { if (newValue.indexOf(oldValue[i]) == -1) { if (!$scope.removedUsers) { $scope.removedUsers = []; } $scope.removedUsers.push(oldValue[i]); } } } } function getUserDetails(userId) { user.getUserProfile(userId).then(function (user) { $scope.users.push(user); console.log(user); }); } $scope.removeUser = function (userId) { removing = true; var index = $scope.selectedUsers.indexOf(userId); $scope.selectedUsers.splice(index, 1); if (!$scope.removedUsers) { $scope.removedUsers = []; } $scope.removedUsers.push(userId); // $scope.users = $filter('filter')($scope.users, function(user){ // return user.id != userId // }) // var index = $scope.users.indexOf(user.id); // $scope.users.splice(user, 1); removing = false; } $scope.nextOrder = function () { $scope.orderOption++ if ($scope.orderOption >= $scope.orderOptions.length) $scope.orderOption = 0 $scope.myOrder = $scope.orderOptions[$scope.orderOption] } } }]); var conf = { organisationTypeId: organisationTypeId, noOrgId: noOrgId, groupTypeId: groupTypeId, modulesSharedResourcesUrl: modulesSharedResourcesUrl, cloudinaryUploadPath: cloudinaryUploadPath, cloudinaryUploadPreset: cloudinaryUploadPreset, cloudinaryApiBaseUrl: cloudinaryApiBaseUrl, cloudinaryName: cloudinaryName, azureBlobUrl: azureBlobUrl, uploadBucketName: "" }; mod.value('configuration', conf); })();