(function () { 'use strict'; var app = angular.module('issuers'); var templatePath = modulesSharedResourcesUrl + 'Modules/Issuers/Views/'; app.directive('issuersList', ['config', 'issuersDataContext', 'common', '$location', '$modal', '$q', '$upload', 'OrganisationAdminService', 'myUsersDataContext', 'features', 'virusScanModalService', function (config, issuersDataContext, common, $location, $modal, $q, $upload, OrganisationAdminService, myUsersDataContext, features, virusScanModalService) { return { restrict: 'E', templateUrl: templatePath + 'issuerslist.html', scope: { hideTitle: '=', }, link: link }; function link($scope, elem, attrs) { var getLogFn = common.logger.getLogFn; var log = getLogFn('branding'); var logSuccess = getLogFn('branding', "success"); $scope.orderByPredicate = 'name'; $scope.selectedOrg = ''; activate(); function activate() { NProgress.done(); log('Activated Badges View'); getOrganisations(); getFeatures(); } function getOrganisations() { myUsersDataContext.getOrganisationsFullFlat(0).then(function (data) { $scope.organisations = data; $scope.getIssuers(); $scope.organisationsLoaded = true; }); } function getFeatures() { OrganisationAdminService.getTopLevelOrg().then(function (org) { features.getCurrentFeatures(org.id).then(function (features) { $scope.features = features; }); }); } $scope.getIssuers = function () { issuersDataContext.getIssuersForCurrentUser().then(function (data) { $scope.issuers = data.issuers; $scope.issuersLoaded = true; }); } $scope.canDeleteIssuer = function (orgId) { for (i in $scope.organisations) { if ($scope.organisations[i].id == orgId) { return true; } } return false; } //manage the users for an organisation $scope.manageIssuer = function (issuer) { if (issuer) { for (i in $scope.organisations) { if ($scope.organisations[i].id == issuer.organisationId) { $scope.issuer = angular.copy(issuer); openIssueModal(); } } } else { $scope.issuer = angular.copy(issuer); openIssueModal(); } } $scope.createOrgIssuer = function (issuer) { issuersDataContext.createOrganisationIssuer(issuer).then(function (data) { logSuccess('Badge issuer successfully added'); $scope.getIssuers(); return data; }).catch(function (errorData) { if (errorData === "Virus scan positive.") { virusScanModalService.virusModal(); } else { logError("An error occured saving the item."); } }); } $scope.deleteOrgIssuer = function (issuer) { issuersDataContext.deleteOrganisationIssuer(issuer).then(function (data) { logSuccess('Badge issuer successfully deleted'); $scope.getIssuers(); return data; }); } $scope.updateOrgIssuer = function (issuer) { if (issuer.id == null) issuersDataContext.createOrganisationIssuer(issuer).then(function (data) { logSuccess('Badge issuer successfully added'); $scope.getIssuers(); return data; }).catch(function (errorData) { if (errorData === "Virus scan positive.") { virusScanModalService.virusModal(); } else { logError("An error occured saving the item."); } }); else { issuersDataContext.updateOrganisationIssuer(issuer).then(function (data) { logSuccess('Badge issuer successfully updated'); $scope.getIssuers(); return data; }).catch(function (errorData) { if (errorData === "Virus scan positive.") { virusScanModalService.virusModal(); } else { logError("An error occured saving the item."); } }); } } function openIssueModal() { $modal.open({ templateUrl: templatePath + 'orgissuer.html', controller: manageIssuerController, size: 'sm', backdrop: 'static', resolve: { organisations: function () { return $scope.organisations; }, 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; } } }); } //Controller for managing issuer var manageIssuerController = function ($scope, $modalInstance, organisations, Issuer, createOrgIssuer, deleteOrgIssuer, updateOrgIssuer) { $scope.organisations = organisations; $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: '', name: '', 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.issuer.fileAsBase64 = reader.result; $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; } // save the organisation issuer $scope.saveOrganisationIssuer = function () { // Upload the issuer and any image to cloudinary $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'); }; }; } }]); })();