(function () { 'use strict'; var app = angular.module('branding'); var templatePath = modulesSharedResourcesUrl + 'Modules/BrandingV2/Views/'; app.directive('branding', ['config', 'brandingDataContext', 'brandingService', 'common', '$location', '$modal', '$rootScope', '$q', '$upload', 'OrganisationAdminService', 'fileUpload', '$timeout', '$window', 'user', function (config, brandingDataContext, brandingService, common, $location, $modal, $rootScope, $q, $upload, OrganisationAdminService, fileUpload, $timeout, $window, user) { return { restrict: 'E', templateUrl: templatePath + 'branding.html', link: link }; function link($scope, elem, attrs) { var getLogFn = common.logger.getLogFn; var log = getLogFn('branding'); var logSuccess = getLogFn('branding', "success"); $scope.branding = {}; $scope.individualRegisterUrl = config.individualRegisterUrl; activate(); function activate() { NProgress.done(); log('Activated Branding View'); } $scope.isSaving = false; OrganisationAdminService.getTopLevelOrg().then(function (org) { $scope.topLevelOrg = org; $scope.branding.organisationId = org.id; getProfile(); }); function getProfile() { user.getProfile().then(function (theUser) { $scope.user = theUser; getBranding(); }); } function getBranding() { if (config.appCode == "CUPPA") { var org = $rootScope.currentRole.organisationId; console.log("rootscopeorg:" + org) return brandingDataContext.getBranding(org).then(function (data) { if (data.length > 0) { $scope.branding = data[0]; $scope.brandingLoaded = true; } else { return brandingDataContext.getBranding($scope.topLevelOrg.id).then(function (data) { if (data.length > 0) { $scope.branding = data[0]; $scope.brandingLoaded = true; } else { $scope.branding = brandingService.createBlankBranding(); } }); } }); } else { return brandingDataContext.getBranding($scope.topLevelOrg.id).then(function (data) { if (data.length > 0) { $scope.branding = data.reduce(function (obj, item) { obj[item['Key']] = item.Value return obj }, {}); } else { $scope.branding = brandingService.createBlankBranding(); } }); } }; $scope.saveBranding = function (integration) { $scope.isSaving = true; if (config.appCode === "CUPPA") { $scope.branding.organisationId = $rootScope.currentRole.organisationId; } else { $scope.branding.organisationId = $scope.topLevelOrg.id; } save($scope.branding); } $scope.resetBranding = function () { $scope.branding = brandingService.createBlankBranding(); } $scope.resetLogo = function () { $scope.branding.logoUrl = null; $scope.fileName = ""; }; $scope.resetIcon = function () { $scope.branding.iconUrl = null; $scope.fileName = ""; }; function save(branding) { if ($scope.logofile && !$scope.iconfile) { uploadLogo($scope.logofile, $scope.logofile); return; } if ($scope.iconfile && !$scope.logofile) { uploadIcon($scope.iconfile, $scope.iconfile); return; } if ($scope.iconfile && $scope.logofile) { uploadLogo($scope.logofile, $scope.logofile); uploadIcon($scope.iconfile, $scope.iconfile); return; } brandingDataContext.createBranding($scope.branding).then(function (data) { logSuccess("Branding updated."); logSuccess("Please note, if you have updated colours the email templates will need to be updated to reflect this change."); $scope.isSaving = false; $window.location.reload(); }); }; $scope.onOrgLogoFileSelect = function (files) { $scope.branding.logoUrl = null; $scope.logoErrorMessage == ''; $scope.logoPreview = null; $scope.logofile = files[0]; // we're not interested in multiple file uploads here if ($scope.logofile.type !== 'image/jpeg' && $scope.logofile.type !== 'image/png' && $scope.logofile.type !== 'image/gif') { $scope.logoErrorMessage = "Please upload a valid image file. jpeg, png or gif."; $scope.branding.logoUrl = null; return; } // check if the user has selected an image below our maximum file size of 20MB if ($scope.logofile.size > 20000000) { $scope.branding.logoUrl = null; $scope.logoErrorMessage == 'Image is too large. Please ensure size is less than 20MB.'; return; } var reader = new FileReader(); reader.onload = function (e) { $scope.$apply(function () { // get loaded data and render preview. $scope.logoPreview = e.target.result; }); }; // Generate base 64 image for preview reader.readAsDataURL($scope.logofile); $scope.fileName = $scope.topLevelOrg.id + '-logo'; $scope.logofile.fileName = $scope.topLevelOrg.id + '-logo'; $scope.logofile.originalImageFileName = $scope.topLevelOrg.id + '-logo'; $scope.logofile.imageFileSize = $scope.logofile.size; $scope.logofile.imageFileType = $scope.logofile.type; } $scope.onOrgIconFileSelect = function (files) { $scope.branding.iconUrl = null; $scope.iconPreview = null; $scope.iconErrorMessage == ''; $scope.iconfile = files[0]; // we're not interested in multiple file uploads here if ($scope.iconfile.type !== 'image/jpeg' && $scope.iconfile.type !== 'image/png' && $scope.iconfile.type !== 'image/gif') { $scope.iconErrorMessage = "Please upload a valid image file. jpeg, png or gif."; $scope.branding.iconUrl = null; return; } // check if the user has selected an image below our maximum file size of 20MB if ($scope.iconfile.size > 20000000) { $scope.branding.iconUrl = null; $scope.iconErrorMessage == 'Image is too large. Please ensure size is less than 20MB.'; return; } var reader = new FileReader(); reader.onload = function (e) { $scope.$apply(function () { // get loaded data and render preview. $scope.iconPreview = e.target.result; }); }; // Generate base 64 image for preview reader.readAsDataURL($scope.iconfile); $scope.fileName = $scope.topLevelOrg.id + '-icon'; $scope.iconfile.fileName = $scope.topLevelOrg.id + '-icon'; $scope.iconfile.originalImageFileName = $scope.topLevelOrg.id + '-icon'; $scope.iconfile.imageFileSize = $scope.iconfile.size; $scope.iconfile.imageFileType = $scope.iconfile.type; } //upload org logo function uploadIcon(obj, file) { //images are uploaded to blob storage var uload = fileUpload.uploadFile(file, file, iconUploadSuccess, UploadProgress, UploadError, config.brandingBucketName); } //upload org logo function uploadLogo(obj, file) { //images are uploaded to blob storage var uload = fileUpload.uploadFile(file, file, logoUploadSuccess, UploadProgress, UploadError, config.brandingBucketName); } //called when a logo is uploaded function logoUploadSuccess(files, data) { //ensure we always use https $scope.branding.logoUrl = files.fileUrl + '?version=' + new Date().valueOf(); $scope.progress = null; $scope.logoErrorMessage = ""; $scope.logoUploaded = true; checkUpload(); return; } //called when a logo is uploaded function iconUploadSuccess(files, data) { //ensure we always use https $scope.branding.iconUrl = files.fileUrl + '?version=' + new Date().valueOf(); $scope.progress = null; $scope.iconErrorMessage = ""; $scope.iconUploaded = true; checkUpload(); return; } function checkUpload() { if ($scope.logofile && !$scope.iconfile && $scope.logoUploaded) { doSave(); } if ($scope.iconfile && !$scope.logofile && $scope.iconUploaded) { doSave(); } if ($scope.iconfile && $scope.logofile && $scope.iconUploaded && $scope.logoUploaded) { doSave(); } } function doSave() { brandingDataContext.createBranding($scope.branding).then(function (data) { logSuccess("Branding updated."); logSuccess("Please note, if you have updated colours the email templates will need to be updated to reflect this change."); $scope.isSaving = false; $window.location.reload(); }); } // Generate the image upload process percentage function UploadProgress(percentComplete) { $scope.progress = percentComplete; $scope.uploadStatus = "Uploading... " + percentComplete + "%"; } function UploadError() { $scope.branding.logoUrl = null; $scope.errorMessage = "There is a problem uploading this image. Please try again."; } } }]); app.directive('landingPage', ['config', 'brandingDataContext', 'brandingService', 'common', '$location', '$modal', '$rootScope', '$q', '$upload', 'OrganisationAdminService', 'fileUpload', '$timeout', '$window', function (config, brandingDataContext, brandingService, common, $location, $modal, $rootScope, $q, $upload, OrganisationAdminService, fileUpload, $timeout, $window) { return { restrict: 'E', templateUrl: templatePath + 'landingpage.html', link: link }; function link($scope, elem, attrs) { var getLogFn = common.logger.getLogFn; var log = getLogFn('branding'); var logSuccess = getLogFn('branding', "success"); $scope.branding = {}; $scope.domain = config.sysDomain; activate(); function activate() { NProgress.done(); log('Activated Badges View'); } $scope.isSaving = false; OrganisationAdminService.getTopLevelOrg().then(function (org) { $scope.topLevelOrg = org; console.log(org); $scope.subDomain = org.subDomain; $scope.originalSubDomain = angular.copy($scope.subDomain); $scope.branding.organisationId = org.id; getBranding(); }); function getBranding() { return brandingDataContext.getBranding($scope.topLevelOrg.id).then(function (data) { if (data.length > 0) { $scope.branding = data.reduce(function (obj, item) { obj[item['Key']] = item.Value return obj }, {}); } else { $scope.branding = brandingService.createBlankBranding(); } $scope.brandingLoaded = true; }); }; $scope.saveBranding = function (integration) { $scope.isSaving = true; $scope.branding.organisationId = $scope.topLevelOrg.id; save($scope.branding); } $scope.resetLanding = function () { $scope.subDomain = null; $scope.branding.systemEmailAddress = ''; $scope.branding.systemName = ''; $scope.branding.backgroundUrl = null; $scope.branding.backgroundColour = null; } $scope.resetBackground = function () { $scope.branding.backgroundUrl = null; $scope.fileName = ""; }; function save(branding) { $scope.topLevelOrg.subDomain = $scope.subDomain; var sub = config.sysDomain; $scope.topLevelOrg.brandingUrl = "https://" + $scope.subDomain + "." + sub; OrganisationAdminService.updateOrganisation($scope.topLevelOrg).then(function (org) { logSuccess("Organisation updated."); $scope.originalSubDomain = angular.copy($scope.subDomain); }); brandingDataContext.createBranding($scope.branding).then(function (data) { logSuccess("Landing page updated."); $scope.isSaving = false; }); }; $scope.onOrgBackgroundFileSelect = function (files) { $scope.branding.backgroundUrl = null; $scope.backgroundErrorMessage == ''; $scope.file = files[0]; // we're not interested in multiple file uploads here if ($scope.file.type !== 'image/jpeg' && $scope.file.type !== 'image/png' && $scope.file.type !== 'image/gif') { $scope.backgroundErrorMessage = "Please upload a valid image file. jpeg, png or gif."; $scope.branding.backgroundUrl = null; return; } // check if the user has selected an image below our maximum file size of 20MB if ($scope.file.size > 20000000) { $scope.branding.backgroundUrl = null; $scope.backgroundErrorMessage == 'Image is too large. Please ensure size is less than 20MB.'; return; } var filename = $scope.file.name.replace(/ /g, ''); $scope.fileName = filename; $scope.file.fileName = filename; $scope.file.originalImageFileName = filename; $scope.file.imageFileSize = $scope.file.size; $scope.file.imageFileType = $scope.file.type; uploadBackground($scope.file, $scope.file); } //upload org logo function uploadBackground(obj, file) { //images are uploaded to blob storage var uload = fileUpload.uploadFile(file, file, backgroundUploadSuccess, UploadProgress, UploadError, config.brandingBucketName); } //called when a logo is uploaded function backgroundUploadSuccess(files, data) { //ensure we always use https $scope.branding.backgroundUrl = null; $scope.branding.backgroundUrl = files.fileUrl $scope.progress = null; logSuccess("Background Uploaded"); $scope.backgroundErrorMessage = ""; //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() { $scope.branding.logoUrl = null; $scope.errorMessage = "There is a problem uploading this image. Please try again."; } $scope.doesSubDomainExist = function (subDomain) { if ($scope.originalSubDomain !== $scope.subDomain) { OrganisationAdminService.doesSubDomainExist(subDomain).then(function (data) { if (data == "Exists" && subDomain != $scope.originalSubDomain || $scope.subDomain == 'app') { $scope.showSubDomainExists = true; return; } else { $scope.showSubDomainExists = false; return; } }); } if ($scope.subDomain == 'app') { $scope.showSubDomainExists = true; return; } $scope.showSubDomainExists = false; return; } } }]); app.directive('terminology', ['config', 'brandingDataContext', 'brandingService', 'common', '$location', '$modal', '$rootScope', '$q', '$upload', 'OrganisationAdminService', 'fileUpload', '$timeout', '$window', function (config, brandingDataContext, brandingService, common, $location, $modal, $rootScope, $q, $upload, OrganisationAdminService, fileUpload, $timeout, $window) { return { restrict: 'E', templateUrl: templatePath + 'terminology.html', link: link }; function link($scope, elem, attrs) { var getLogFn = common.logger.getLogFn; var log = getLogFn('branding'); var logSuccess = getLogFn('branding', "success"); $scope.defaultTerminology = brandingService.createBlankTerminology(); $scope.terminology = {}; $scope.isSaving = false; OrganisationAdminService.getTopLevelOrg().then(function (org) { $scope.topLevelOrg = org; getTerminology(); }); function getTerminology() { brandingDataContext.getTerminology($scope.topLevelOrg.id).then(function (data) { if (data.length > 0) { $scope.terminology = data.reduce(function (obj, item) { obj[item['Key']] = item.Value return obj }, {}); } else { $scope.terminology = brandingService.createBlankTerminology(); } }); }; $scope.saveTerminology = function () { $scope.isSaving = true; save($scope.terminology); } $scope.resetTerminology= function () { $scope.terminology = brandingService.createBlankTerminology(); } function save(branding) { brandingDataContext.createTerminology($scope.terminology, $scope.topLevelOrg.id).then(function (data) { logSuccess("Terminolgy updated."); $scope.isSaving = false; $window.location.reload(); }); }; } }]); })();