(function() { 'use strict'; var HTTP_ERRORS = { 401: "Unathorized action", 403: "Forbidden action", 500: "Server error", 404: "Element not found" } var app = angular.module('securis'); app.directive('fileLoader', function($timeout, $parse) { return { restrict : 'A', // only activate on element attribute require : '', link : function(scope, element, attrs) { console.log('scope.license: ' + scope.$parent.license); var setter = $parse(attrs.fileLoader).assign; element.bind('change', function(evt) { console.log('scope.license: ' + scope.$parent.license); var field = $parse(attrs.fileLoader); console.log('field: ' + field); var fileList = evt.target.files; if (fileList != null && fileList[0]) { var reader = new FileReader(); reader.onerror = function(data) { setter(scope.$parent, 'ERROR'); scope.$apply(); } reader.onload = function(data) { setter(scope.$parent, reader.result); scope.$apply(); } reader.readAsText(fileList[0]); } else { console.log('NO FILE: '); field = ''; scope.$apply(); } }); } }; }); console.log(' OK ????? '); app.controller('PackAndLicensesCtrl', [ '$scope', '$http', 'toaster', '$store', '$L', function($scope, $http, toaster, $store, $L) { $scope.maxLengthErrorMsg = function(displayname, fieldMaxlength) { return $L.get("{0} length is too long (max: {1}).", $L.get(displayname), fieldMaxlength); } $scope.mandatoryFieldErrorMsg = function(displayname) { return $L.get("'{0}' is required.", $L.get(displayname)); } $scope.ellipsis = function(txt, len) { if (!txt || txt.length <= len) return txt; return txt.substring(0, len) + '...'; } $scope.currentPack = $store.get('currentPack'); }]); app.controller('PacksCtrl', [ '$scope', '$http', '$resource', 'toaster', 'Catalogs', '$store', '$L', function($scope, $http, $resource, toaster, Catalogs, $store, $L) { var packResource = $resource('/pack/:packId', { packId : '@id' }); $scope.mandatory = { code: true, num_licenses: true, organization_id: true, license_type_id: true } $scope.maxlength = { code: 50, comments: 1024 } $scope.refs = {}; Catalogs.init().then(function() { Catalogs.loadRefs($scope.refs, [{resource: 'organization', name: 'organization_id'},{resource: 'licensetype', name: 'license_type_id'}]) }); // Used to create the form with the appropriate data $scope.isNew = undefined; // Selected pack from listing // pack is the edited pack, in creation contains the data for the new pack $scope.pack = null; $scope.packs = packResource.query(); $scope.save = function() { var _success = function() { if (!$scope.isNew) $scope.showForm = false; $scope.packs = packResource.query(); } packResource.save($scope.pack, _success) } $scope.newPack = function() { $scope.isNew = true; $scope.showForm = true; $scope.pack = { num_licenses: 1, license_type_id: !$scope.refs.license_type_id || !$scope.refs.license_type_id.length ? null : $scope.refs.license_type_id[0].id, organization_id: !$scope.refs.organization_id || !$scope.refs.organization_id.length ? null : $scope.refs.organization_id[0].id } setTimeout(function() { $('#code').focus(); }, 0); } $scope.editPack = function(selectedPack) { $scope.isNew = false; $scope.showForm = true; $scope.pack = selectedPack; setTimeout(function() { $('#code').focus(); }, 0); } $scope.deletePack = function(selectedPack) { $scope.showForm = false; BootstrapDialog.confirm($L.get("The pack '{0}' will be deleted, are you sure?", selectedPack.code), function(result){ if(result) { var promise = packResource.remove({}, {id: selectedPack.id}).$promise; promise.then(function(data) { $scope.selectPack(null); $scope.packs = packResource.query(); toaster.pop('success', Catalogs.getName(), $L.get("Pack '{0}' deleted successfully", selectedPack.code)); },function(error) { console.log(error); toaster.pop('error', Catalogs.getName(), $L.get("Error deleting pack, reason: {0}. Details: {1}", $L.get(HTTP_ERRORS[error.status]), error.headers('X-SECURIS-ERROR')), 10000); }); } }); $scope.isNew = false; } $scope.cancel = function() { $scope.showForm = false; } $scope.selectPack = function(pack) { $scope.$parent.currentPack = pack; $store.put('currentPack', pack); $scope.$parent.$broadcast('pack_changed', pack); } } ]); app.controller('LicensesCtrl', [ '$scope', '$http', '$resource', 'toaster', '$store', '$L', function($scope, $http, $resource, toaster, $store, $L) { $scope.$on('pack_changed', function(evt, message) { $scope.licenses = licenseResource.query({packId: $scope.currentPack.id}); if ($scope.showForm) { if ($scope.isNew) { $scope.license.pack_id = $scope.currentPack.id } else { $scope.showForm = false; } } }) var licenseResource = $resource('/license/:licenseId', { licenseId : '@id' }, { save_w_upload: { method: "POST", transformRequest: function(data, headersGetter) { // To use an object without FormData, follow: https://developer.mozilla.org/en-US/docs/Web/API/XMLHttpRequest/Using_XMLHttpRequest?redirectlocale=en-US&redirectslug=DOM%2FXMLHttpRequest%2FUsing_XMLHttpRequest#Submitting_forms_and_uploading_files var formData = new FormData(); angular.forEach(data, function(value, key) { if (key !== 'request_data') formData.append(key, value) else formData.append(key, $('input#request_data_file').get(0).files[0]); }) return formData; }, headers: { 'Content-Type': undefined } } }); $scope.mandatory = { code: true } $scope.maxlength = { code: 50, comments: 1024 } $scope.refs = {}; // Used to create the form with the appropriate data $scope.isNew = undefined; // Selected license from listing // license is the edited license, in creation contains the data for the new license $scope.license = null; if ($scope.currentPack) $scope.licenses = licenseResource.query({packId: $scope.currentPack.id}); // $(document).on('change', '#request_data_file', function(newValue, oldValue) { // console.log('File changed!!!!'); // var reader = new FileReader(); // reader.onload = function(data) { // console.log('LOAD complete: ' + data); // console.log('LOAD reader.result: ' + reader.result); // $('input#request_data').val(reader.result) // } // console.log('file: ' + $('input#request_data_file').get(0).files[0]); // console.log('file2: ' + $scope.request_data_file); // reader.readAsText($('input#request_data_file').get(0).files[0]); // }); $scope.save = function() { $( "form#licenseForm" ) .attr( "enctype", "multipart/form-data" ) .attr( "encoding", "multipart/form-data" ); var _success = function() { if (!$scope.isNew) $scope.showForm = false; $scope.licenses = licenseResource.query({packId: $scope.currentPack.id}); } licenseResource.save_w_upload($scope.license, _success) } $scope.newLicense = function() { if (!$scope.currentPack) { BootstrapDialog.show({ title: $L.get('New license'), type: BootstrapDialog.TYPE_WARNING, message: $L.get('Please, select a pack before to create a new license'), buttons: [{ label: 'OK', action: function(dialog) { dialog.close(); } }] }); return; } $scope.isNew = true; $scope.showForm = true; $scope.license = { pack_id: $scope.currentPack.id } setTimeout(function() { $('#licenseForm * #code').focus(); }, 0); } $scope.editLicense = function(selectedlicense) { $scope.isNew = false; $scope.showForm = true; $scope.license = selectedlicense; setTimeout(function() { $('#licenseForm * #code').focus(); }, 0); } $scope.deletelicense = function(selectedlicense) { $scope.showForm = false; BootstrapDialog.confirm($L.get("The license '{0}' will be deleted, are you sure?", selectedlicense.code), function(result){ if(result) { var promise = licenseResource.remove({}, {id: selectedlicense.id}).$promise; promise.then(function(data) { $scope.selectlicense(null); $scope.licenses = licenseResource.query({packId: $scope.currentPack.id}); toaster.pop('success', Catalogs.getName(), $L.get("License '{0}' deleted successfully", selectedlicense.code)); },function(error) { console.log(error); toaster.pop('error', Catalogs.getName(), $L.get("Error deleting license, reason: {0}. Details: {1}", $L.get(HTTP_ERRORS[error.status]), error.headers('X-SECURIS-ERROR')), 10000); }); } }); $scope.isNew = false; } $scope.cancel = function() { $scope.showForm = false; } $scope.showStatus = function(lic) { } $scope.showStatusComplete = function(license) { } $scope.isActionVisible = function(actionMask, lic) { } } ]); })();