// The function wrapper keeps global scope clear and avoids global naming collisions (function () { // define your controller and its logic in a local function variable var imageCropController = function ($scope, $http, $window, ServerValues, ModalConfirmService, WebApiUrlService, $upload, ModalOrgLayoutService) { $scope.selectedLogo = ''; $scope.logo = ''; $scope.backgroundPicture = ''; $scope.selectedBackgroundPicture = ''; $scope.myCroppedImage = ''; var handleFileSelect = function (evt) { var file = evt.currentTarget.files[0]; var type = file.type; var size = file.size; if (type == 'image/jpeg' || type == 'image/jpg' || type == 'image/png' || type == 'image/gif') { if (size <= 4000000) { var reader = new FileReader(); reader.onload = function (evt) { $scope.$apply(function ($scope) { $scope.logo = evt.target.result; $scope.selectedLogo = file; angular.element(document.querySelector('#logoImage')).attr('src', $scope.logo); }); }; reader.readAsDataURL(file); } else { var sizeFormat = formatBytes(size); ModalConfirmService.confirm('Filen er for stor.', 'Kunne ikke laste opp bildene dine. Bilder må være mindre enn 4 MB. Filen du lastet opp er: ' + sizeFormat); } } else { ModalConfirmService.confirm('Kan ikke lese fil.', 'Kunne ikke laste opp bildene dine. Bilder må være mindre enn 4 MB og lagret i JPG-, PNG- eller GIF-format. Filen du lastet opp er: ' + sizeFormat); } }; angular.element(document.querySelector('#upload1')).on('change', handleFileSelect); var handleFileSelect2 = function (evt) { var file = evt.currentTarget.files[0]; var type = file.type; var size = file.size; console.log("size = " + size + " " + (size < 4000000) + " formatsize = " + formatBytes(size)); if (type == 'image/jpeg' || type == 'image/jpg' || type == 'image/png' || type == 'image/gif') { //Less than 4MB if (size <= 4000000) { var reader = new FileReader(); reader.onload = function (evt) { $scope.$apply(function ($scope) { $scope.backgroundPicture = evt.target.result; $scope.cropBackground(); }); }; reader.readAsDataURL(file); } else { var sizeFormat = formatBytes(size); ModalConfirmService.confirm('Filen er for stor.', 'Kunne ikke laste opp bildene dine. Bilder må være mindre enn 4 MB. Filen du lastet opp er: ' + sizeFormat); } } else { ModalConfirmService.confirm('Kan ikke lese fil.', 'Kunne ikke laste opp bildene dine. Bilder må være mindre enn 4 MB og lagret i JPG-, PNG- eller GIF-format.'); } }; angular.element(document.querySelector('#upload2')).on('change', handleFileSelect2); $scope.saveCropped = function () { $scope.logo = $scope.myCroppedImage; $scope.selectedLogo = dataURItoBlob($scope.logo); return false; }; $scope.uploadLogo = function () { var asyncOps = []; var file = $scope.selectedLogo; if (file) { var url = WebApiUrlService.UploadApi.UploadOrgImage + '/' + ServerValues.organizationID; var promise = $upload.upload({ url: url, method: 'POST', file: file }); asyncOps.push(promise); ModalConfirmService.confirm('Lagring', 'Dine endringer ble lagret OK.'); } return false; }; $scope.uploadBackground = function () { var asyncOps = []; var file = $scope.selectedBackgroundPicture; if (file) { var url = WebApiUrlService.UploadApi.UploadBackgrundPicture + '/' + ServerValues.organizationID; var promise = $upload.upload({ url: url, method: 'POST', file: file }); asyncOps.push(promise); ModalConfirmService.confirm('Lagring', 'Dine endringer ble lagret OK.'); } return false; }; var dataURItoBlob = function (dataURI) { var binary = atob(dataURI.split(',')[1]); var mimeString = dataURI.split(',')[0].split(':')[1].split(';')[0]; var array = []; for (var i = 0; i < binary.length; i++) { array.push(binary.charCodeAt(i)); } return new Blob([new Uint8Array(array)], { type: mimeString }); }; $scope.cropBackground = function () { var element = angular.element(document.getElementsByClassName('jumbotron')); console.log(element[0].offsetHeight); var height = element[0].offsetHeight; ModalOrgLayoutService.cropBackground($scope.backgroundPicture, height).then(function (item1) { $scope.backgroundPicture = item1; $scope.selectedBackgroundPicture = dataURItoBlob($scope.backgroundPicture); angular.element(document.querySelector('#photo')).css('background-image', 'url('+item1+')'); }); } $scope.deleteBackground = function () { var url = WebApiUrlService.OrganizationsApi.DeleteBackgrundPicture + '/' + ServerValues.organizationID; ModalConfirmService.confirm('Sletting', 'Er du sikker på at du vil slette bildet?.', 'Ok', 'Avbryt').then(function () { return $http.delete(url).then(function (response) { if (response.data.success) { $window.location.href = "/Organization?organizationID=" + ServerValues.organizationID; } else { ModalConfirmService.confirm("Error", "Det oppstod en feil ved sletting.", "OK") } }); }, function () { }); } $scope.setBackgroundColor = function () { ModalOrgLayoutService.backgroundColor().then(function (color) { var colorTemp = color.replace('#', ''); var url = WebApiUrlService.OrganizationsApi.UpdateBackgroundColor + '/' + ServerValues.organizationID + '/' + colorTemp; return $http.put(url).then(function (response) { if (response.data.success) { angular.element(document.querySelector('#photo')).css('background-color', color); } else { ModalConfirmService.confirm("Error", "Noe har gått feil.", "OK") } }); }); } $scope.setTextColor = function () { ModalOrgLayoutService.textColor().then(function (color) { var colorTemp = color.replace('#', ''); var url = WebApiUrlService.OrganizationsApi.UpdateTextColor + '/' + ServerValues.organizationID + '/' + colorTemp; return $http.put(url).then(function (response) { if (response.data.success) { // Use header text color instead -> this setting does not affect the display of the header // angular.element(document.querySelector('#photo')).css('color', color); } else { ModalConfirmService.confirm("Error", "Noe har gått feil.", "OK") } }); }); } $scope.setHeaderTextColor = function () { ModalOrgLayoutService.textColor().then(function (color) { var colorTemp = color.replace('#', ''); var url = WebApiUrlService.OrganizationsApi.UpdateHeaderTextColor + '/' + ServerValues.organizationID + '/' + colorTemp; return $http.put(url).then(function (response) { if (response.data.success) { angular.element(document.querySelector('#photo')).css('color', color); } else { ModalConfirmService.confirm("Error", "Noe har gått feil.", "OK") } }); }); } $scope.cancel1 = function (logoImage) { angular.element(document.querySelector('#logoImage')).attr('src', logoImage); var input = angular.element(document.querySelector('#upload1')); input.replaceWith(input.val('').clone(true)); return false; } $scope.cancel2 = function (orgImage) { angular.element(document.querySelector('#photo')).css('background-image', 'url(data:image/png;base64,' + orgImage + ')'); var input = angular.element(document.querySelector('#upload2')); input.replaceWith(input.val('').clone(true)); return false; } var formatBytes = function (bytes) { var decimals = 3; if (bytes == 0) return '0 Byte'; var k = 1000; var dm = decimals + 1 || 3; var sizes = ['Bytes', 'KB', 'MB', 'GB', 'TB', 'PB', 'EB', 'ZB', 'YB']; var i = Math.floor(Math.log(bytes) / Math.log(k)); return (bytes / Math.pow(k, i)).toPrecision(dm) + ' ' + sizes[i]; } }; // Set up dependecy injections for dependencies used by controller function imageCropController.$inject = ['$scope', '$http', '$window', 'ServerValues', 'ModalConfirmService', 'WebApiUrlService', '$upload', 'ModalOrgLayoutService']; // Register the controller angular.module('rubicApp').controller('imageCropController', imageCropController); }());; // The function wrapper keeps global scope clear and avoids global naming collisions (function () { // define your controller and its logic in a local function variable var publicProjectAccreditationController = function ($scope, $http, $timeout, ServerValues, ModalConfirmService, CallRestApiService, CallApiService, $window, $q, WebApiUrlService, $upload, $window, $translate) { $scope.organizationID = ServerValues.organizationID; $scope.projectID = ServerValues.projectID; $scope.project = null; $scope.personID = ServerValues.personID; $scope.accreditationPerson = null; $scope.projectPerson = null; $scope.loading = false; $scope.translation = []; $scope.submitFeedback = ''; $scope.printFeeback = false; $scope.countryCodes = []; $scope.countries = []; $scope.showDays = false; if ($translate.proposedLanguage().indexOf("en") > -1) { $scope.locale = 'en'; } else { $scope.locale = 'no'; } var url = new URL($window.location.href); $scope.locale = url.searchParams.get("lang") || $scope.locale; if (!$scope.locale) { $scope.locale = 'en'; } $scope.categories = null; $scope.showCategories = true; $scope.categoriesRequired = false; $scope.nationForCategories = false; $scope.functions = null; $scope.functionsRequired = false; $scope.hierarchialCatFunc = false; $scope.showFunctions = true; $scope.organizationRequired = false; $scope.showOrganization = false; $scope.disciplines = null; $scope.disciplinesRequired = false; $scope.showDisciplines = false; $scope.profilePicRequired = false; $scope.getFunctions = function () { if ($scope.applicationConfig) { //const result = words.filter(word => word.length > 6); var filteredFunctions = $scope.applicationConfig.functions.filter(func => (func.parentName == null || func.parentName == $scope.accreditationPerson.group) && func.signupEnabled); /* if ($scope.accreditationPerson.group) { filteredFunctions.splice(filteredFunctions.length - 1, 0, filteredFunctions.filter(func => func.parentName == $scope.accreditationPerson.group)); }*/ return filteredFunctions; } /*if (category != null) { var categoryName = category.replace("/", ""); return $scope.functions[categoryName]; } else { return []; }*/ } $http.get("/rest/organizations/" + ServerValues.organizationID + "/projects/" + ServerValues.projectID + "/zoneapplicationconfig") .success(function (response) { $scope.applicationConfig = response.result; $scope.translation = []; $scope.applicationConfig.translation.forEach(function (trans) { if (!$scope.translation[trans.locale]) { $scope.translation[trans.locale] = trans.keys; } }) console.log("Configuration loaded."); $scope.loadingData = false; }) .error(function (response) { $scope.loadingData = false; var errorMsg = response.error != "" ? response.error : "En feil har oppstått."; ModalConfirmService.confirm("Feil", errorMsg, "OK"); }); $scope.getUrlParamValue = function (paramName, defaultValue) { const queryString = $window.location.search; const urlParams = new URLSearchParams(queryString); if (urlParams.has(paramName)) { const urlParamValue = urlParams.get(paramName); var list = []; switch (paramName) { case 'c': list = $scope.applicationConfig.categories; break; case 'f': list = $scope.applicationConfig.functions; break; case 'a': list = $scope.applicationConfig.disciplines; break; case 'o': list = $scope.applicationConfig.organizations; break; case 'n': list = $scope.countries; break; default: return ''; } if (paramName != 'n') { if (list.length > 0) { var search = list.filter(item => { return item.urlCode == urlParamValue || item.name == urlParamValue }); return search && search.length > 0 ? search[0].name : ''; } else { return urlParamValue; } } else { var search = list.filter(item => { return item.countryID == urlParamValue || item.countryName_en == urlParamValue || item.isO_Alpha_2 == urlParamValue || item.isO_Alpha_3 == urlParamValue }); return search && search.length > 0 ? search[0].countryName_en : ''; } } return defaultValue; } /* Statuser: Registered = 1, Approved = 2, Processing = 3, SignedOff = 4 */ $scope.setLocale = function (l) { $scope.locale = l; $translate.use('en-GB'); if ($scope.locale == 'no') { $translate.use('nb-NO'); } } $scope.resetForm = function () { $scope.accreditationPerson = { projectZonePersonID: 0, firstName: '', lastName: '', email: '', countryCode: '+47', mobile: null, personID: 0, days: [], group: $scope.getUrlParamValue('c', ''), role: $scope.getUrlParamValue('f', ''), roleSpecification: '', organization: $scope.getUrlParamValue('o', ''), area: $scope.getUrlParamValue('a', ''), nation: $scope.getUrlParamValue('n', ''), profilePicUrl: null, description: '' }; $scope.accreditationPerson.groupDisabled = $scope.accreditationPerson.group != ''; $scope.accreditationPerson.roleDisabled = $scope.accreditationPerson.role != ''; $scope.accreditationPerson.orgDisabled = $scope.accreditationPerson.organization != ''; $scope.accreditationPerson.areaDisabled = $scope.accreditationPerson.area != ''; $scope.accreditationPerson.nationDisabled = $scope.accreditationPerson.nation != ''; $scope.project.projectDays.forEach(function (pDay) { if (pDay.isOpen) { var d = new Date(pDay.dayDate); pDay.dateFormatted = d.getDate() + '.' + (d.getMonth() + 1) + '.' + d.getFullYear(); pDay.selected = false; $scope.accreditationPerson.days.push(pDay); } }); $scope.printFeeback = false; } $scope.getProject = function () { $scope.loading = true; return CallRestApiService(function () { return $http.get('/rest/projects/' + $scope.projectID); }) .then( function (project) { $scope.project = project; $scope.loading = false; $scope.resetForm(); }, function (error) { ModalConfirmService.confirm("Error", error.toString(), "OK"); }); } $scope.getProject(); $scope.getCountries = function () { $scope.loading = true; $http.get(WebApiUrlService.BasicDataApi.GetCountries) .success(function (data, status, headers, config) { data.data.forEach(function (country) { if (country.phoneCountryCode != '') { country.phoneCountryCode = '+' + country.phoneCountryCode; $scope.countryCodes.push(country); } $scope.countries.push(country); }); $scope.countryCodes.sort((a, b) => (a.phoneCountryCode > b.phoneCountryCode) ? 1 : -1); $scope.countries.sort((a, b) => (a.countryName_en > b.countryName_en) ? 1 : -1); }) .error(function (data, status, headers, config) { } ); } $scope.getCountries(); $scope.submitApplication = function () { debugger; if (!$scope.accreditationPerson.concent) { ModalConfirmService.confirm($scope.translation[$scope.locale].missingData, $scope.translation[$scope.locale].missingDataConcent, "OK"); return false; } if ($scope.accreditationPerson.firstName.length < 2) { ModalConfirmService.confirm($scope.translation[$scope.locale].missingData, $scope.translation[$scope.locale].missingDataFN, "OK"); return false; } if ($scope.accreditationPerson.lastName.length < 2) { ModalConfirmService.confirm($scope.translation[$scope.locale].missingData, $scope.translation[$scope.locale].missingDataLN, "OK"); return false; } if ($scope.accreditationPerson.mobile.length < 8) { ModalConfirmService.confirm($scope.translation[$scope.locale].missingData, $scope.translation[$scope.locale].missingDataMobile, "OK"); return false; } if ($scope.accreditationPerson.mobile.length < 8) { ModalConfirmService.confirm($scope.translation[$scope.locale].missingData, $scope.translation[$scope.locale].missingDataMobile, "OK"); return false; } if ($scope.applicationConfig.profilePicRequired && $scope.accreditationPerson.profilePicUrl == null) { ModalConfirmService.confirm($scope.translation[$scope.locale].missingData, $scope.translation[$scope.locale].missingProfilePic, "OK"); return false; } if ($scope.accreditationPerson.role.toLowerCase() == 'other' && $scope.accreditationPerson.roleSpecification != '') { $scope.accreditationPerson.role = $scope.accreditationPerson.roleSpecification; } var days = []; $scope.accreditationPerson.days.forEach(function (d) { if (d.selected) { var signupDay = { ID: d.projectDayID, date: d.dayDate, description: d.description, isProjectPerson: false }; days.push(signupDay); } }); $scope.accreditationPerson.days = days; $scope.loading = true; CallRestApiService(function () { return $http.post('/rest/public/projects/' + $scope.projectID + '/zonepersons', $scope.accreditationPerson); }) .then( function (accreditationPerson) { $scope.accreditationPerson = accreditationPerson; $scope.loading = false; //$scope.submitFeedback = $scope.translation.successfulSubmit[$scope.locale]; $scope.printFeeback = true; /* $timeout(function () { $scope.submitFeedback = ''; $scope.printFeeback = false; $scope.resetForm(); }, 5000); */ }, function (error) { ModalConfirmService.confirm("Error", error.toString(), "OK"); $scope.resetForm(); }); } $scope.getProjectApplication = function () { CallRestApiService(function () { return $http.get("/rest/projects/" + $scope.projectID + "/applications/" + $scope.personID); }) .then( function (projectPerson) { $scope.projectPerson = projectPerson; }, function (error) { ModalConfirmService.confirm("Error", error.toString(), "OK"); }); } //$scope.getProjectApplication(); $scope.getMyAccreditation = function () { return CallRestApiService(function () { return $http.get('/rest/public/projects/' + $scope.projectID + '/zoneperson'); }) .then(function (data) { $scope.accreditationPerson = data; $scope.validateAccreditation(); }, function (error) { ModalConfirmService.confirm("Error", error.toString(), "OK"); } ); } //Dont know why this was called earlier. Seems unnecessary. //$scope.getMyAccreditation(); $scope.validateAccreditation = function () { if ($scope.accreditationPerson) { $scope.accreditationPerson.statusText = "Ikke satt"; if ($scope.accreditationPerson.status == 1) { $scope.accreditationPerson.statusText = "Registrert"; } else if ($scope.accreditationPerson.status == 2) { $scope.accreditationPerson.statusText = "Godkjent"; } else if ($scope.accreditationPerson.status == 3) { $scope.accreditationPerson.statusText = "Under behandling"; } else if ($scope.accreditationPerson.status == 4) { $scope.accreditationPerson.statusText = "Avslått"; } else if ($scope.accreditationPerson.status == 7) { $scope.accreditationPerson.statusText = "Vaktseleksjon"; } if ($scope.accreditationPerson.zones == null) { $scope.accreditationPerson.zones = []; } if ($scope.accreditationPerson.days == null) { $scope.accreditationPerson.days = []; } } } $scope.fileReaderSupported = window.FileReader != null; $scope.readImageFiles = function (files) { var asyncOps = []; if (files && files.length > 0) { for (var i = 0; i < files.length; i++) { var file = files[i]; if (file != null) { if ($scope.fileReaderSupported && file.type.indexOf('image') > -1) { var deferred = $q.defer(); asyncOps.push(deferred.promise); $timeout(function () { var fileReader = new FileReader(); fileReader.readAsDataURL(file); fileReader.onload = function (e) { $timeout(function () { file.imageData = e.target.result; deferred.resolve(); }); }; }); } } } } return asyncOps; }; $scope.profileImageChange = function (files) { if (files && files.length > 0) { $q.all($scope.readImageFiles(files)). then(function () { var file = files[0]; $scope.selectedPicture = file.imageData; }); } } $scope.profileImageChange = function () { var asyncOps = []; $scope.uploadingImage = true; var asyncFileUploads = $scope.upload($scope.accreditationPerson.profilePic); $q.all(asyncFileUploads).then(function (uploadedFile) { $scope.uploadingImage = false; if (typeof uploadedFile[0] != "undefined") { $scope.accreditationPerson.profilePicUrl = uploadedFile[0].data.data[0]; } }); } $scope.upload = function (files) { var asyncOps = []; if (files && files.length > 0) { for (var i = 0; i < files.length; i++) { var file = files[i]; var file = $scope.accreditationPerson.profilePic[i]; var url = WebApiUrlService.UploadApi.UploadImage + '/ProfilePic'; var promise = $upload.upload({ url: url, method: 'POST', file: file, fileFormDataName: file.name }); asyncOps.push(promise); } } return asyncOps; }; }; // Set up dependecy injections for dependencies used by controller function publicProjectAccreditationController.$inject = ['$scope', '$http', '$timeout', 'ServerValues', 'ModalConfirmService', 'CallRestApiService', 'CallApiService', '$window', '$q', 'WebApiUrlService', '$upload', '$window', '$translate']; // Register the controller angular.module('rubicApp').controller('publicProjectAccreditationController', publicProjectAccreditationController); }());;