| .. | .. |
|---|
| 1 | 1 | (function() { |
|---|
| 2 | 2 | 'use strict'; |
|---|
| 3 | 3 | |
|---|
| 4 | + var HTTP_ERRORS = { |
|---|
| 5 | + 401: "Unathorized action", |
|---|
| 6 | + 403: "Forbidden action", |
|---|
| 7 | + 500: "Server error", |
|---|
| 8 | + 404: "Element not found" |
|---|
| 9 | + } |
|---|
| 10 | + |
|---|
| 4 | 11 | var app = angular.module('securis'); |
|---|
| 12 | + |
|---|
| 13 | + app.controller('PackAndLicensesCtrl', [ |
|---|
| 14 | + '$scope', |
|---|
| 15 | + '$http', |
|---|
| 16 | + 'toaster', |
|---|
| 17 | + '$store', |
|---|
| 18 | + '$L', |
|---|
| 19 | + function($scope, $http, toaster, $store, $L) { |
|---|
| 20 | + $scope.licenses = [ |
|---|
| 21 | + {id: 1, |
|---|
| 22 | + "code": "BP-SA-001-AKSJMS234", |
|---|
| 23 | + "user_fullname": "Johnny Belmonte", |
|---|
| 24 | + "user_email": "jb@curisit.net", |
|---|
| 25 | + "status": 3}, |
|---|
| 26 | + {id: 2, |
|---|
| 27 | + "code": "BP-SA-001-KAJSDHAJS", |
|---|
| 28 | + "user_fullname": "Walter Simons", |
|---|
| 29 | + "user_email": "ws@curisit.net", |
|---|
| 30 | + "status": 1}, |
|---|
| 31 | + {id: 3, |
|---|
| 32 | + "code": "BP-SA-001-ASKDGHKA", |
|---|
| 33 | + "user_fullname": "Frank Belmonte", |
|---|
| 34 | + "user_email": "fb@curisit.net", |
|---|
| 35 | + "status": 2}, |
|---|
| 36 | + {id: 4, |
|---|
| 37 | + "code": "BP-SA-001-BBBGGGG", |
|---|
| 38 | + "user_fullname": "John Dalton", |
|---|
| 39 | + "user_email": "jd@curisit.net", |
|---|
| 40 | + "status": 3}, |
|---|
| 41 | + {id: 5, |
|---|
| 42 | + "code": "BP-SA-001-AKADNAJANA", |
|---|
| 43 | + "user_fullname": "Walter Martins", |
|---|
| 44 | + "user_email": "wm@curisit.net", |
|---|
| 45 | + "status": 3}, |
|---|
| 46 | + {id: 6, |
|---|
| 47 | + "code": "BP-SA-001-AKANDAKS", |
|---|
| 48 | + "user_fullname": "Joe Bolton", |
|---|
| 49 | + "user_email": "jbol@curisit.net", |
|---|
| 50 | + "status": 2} |
|---|
| 51 | + ]; |
|---|
| 52 | + |
|---|
| 53 | + $scope.maxLengthErrorMsg = function(displayname, fieldMaxlength) { |
|---|
| 54 | + return $L.get("{0} length is too long (max: {1}).", $L.get(displayname), fieldMaxlength); |
|---|
| 55 | + } |
|---|
| 56 | + $scope.mandatoryFieldErrorMsg = function(displayname) { |
|---|
| 57 | + return $L.get("'{0}' is required.", $L.get(displayname)); |
|---|
| 58 | + } |
|---|
| 59 | + $scope.ellipsis = function(txt, len) { |
|---|
| 60 | + if (!txt || txt.length <= len) return txt; |
|---|
| 61 | + return txt.substring(0, len) + '...'; |
|---|
| 62 | + } |
|---|
| 63 | + $scope.currentPackId = $store.get('currentPackId'); |
|---|
| 64 | + |
|---|
| 65 | + }]); |
|---|
| 5 | 66 | |
|---|
| 6 | | - app.controller('LicensesCtrl', [ |
|---|
| 67 | + app.controller('PacksCtrl', [ |
|---|
| 7 | 68 | '$scope', |
|---|
| 8 | 69 | '$http', |
|---|
| 70 | + '$resource', |
|---|
| 9 | 71 | 'toaster', |
|---|
| 10 | 72 | 'Catalogs', |
|---|
| 11 | 73 | '$store', |
|---|
| 12 | 74 | '$L', |
|---|
| 13 | | - function($scope, $http, toaster, Catalogs, $store, $L) { |
|---|
| 14 | | - $scope.currentPack = $store.get('currentPack'); |
|---|
| 15 | | - $scope.packs = [ |
|---|
| 16 | | - {id: 1, |
|---|
| 17 | | - "organization_name": "BP-Spain", |
|---|
| 18 | | - "application_name": "CurisIntegrity", |
|---|
| 19 | | - "licensetype_code": "CIBS", |
|---|
| 20 | | - "code": "BP-SA-0001", |
|---|
| 21 | | - "licenses": 50, |
|---|
| 22 | | - "lic_available": 23}, |
|---|
| 23 | | - {id: 2, |
|---|
| 24 | | - "organization_name": "Exxon", |
|---|
| 25 | | - "application_name": "CurisData", |
|---|
| 26 | | - "licensetype_code": "CDL1", |
|---|
| 27 | | - "code": "EX-SA-0001", |
|---|
| 28 | | - "licenses": 1, |
|---|
| 29 | | - "lic_available": 0}, |
|---|
| 30 | | - {id: 3, |
|---|
| 31 | | - "organization_name": "Repsol S.A. empresa con nombre muy largo", |
|---|
| 32 | | - "application_name": "CurisData", |
|---|
| 33 | | - "licensetype_code": "CDL2", |
|---|
| 34 | | - "code": "RE-SA-0001", |
|---|
| 35 | | - "licenses": 5, |
|---|
| 36 | | - "lic_available": 2}, |
|---|
| 37 | | - {id: 4, |
|---|
| 38 | | - "organization_name": "BP-Spain", |
|---|
| 39 | | - "application_name": "CurisIntegrity v3.0", |
|---|
| 40 | | - "code": "BP-SA-0002", |
|---|
| 41 | | - "licensetype_code": "CISA", |
|---|
| 42 | | - "licenses": 150, |
|---|
| 43 | | - "lic_available": 13}, |
|---|
| 44 | | - ]; |
|---|
| 75 | + function($scope, $http, $resource, toaster, Catalogs, $store, $L) { |
|---|
| 76 | + var packResource = $resource('/pack/:packId', { |
|---|
| 77 | + packId : '@id' |
|---|
| 78 | + }); |
|---|
| 79 | + $scope.mandatory = { |
|---|
| 80 | + code: true, |
|---|
| 81 | + num_licenses: true, |
|---|
| 82 | + organization_id: true, |
|---|
| 83 | + license_type_id: true |
|---|
| 84 | + } |
|---|
| 85 | + $scope.maxlength = { |
|---|
| 86 | + code: 50, |
|---|
| 87 | + comments: 1024 |
|---|
| 88 | + } |
|---|
| 89 | + $scope.refs = {}; |
|---|
| 90 | + Catalogs.init().then(function() { |
|---|
| 91 | + Catalogs.loadRefs($scope.refs, [{resource: 'organization', name: 'organization_id'},{resource: 'licensetype', name: 'license_type_id'}]) |
|---|
| 92 | + }); |
|---|
| 45 | 93 | |
|---|
| 46 | | - $scope.licenses = [ |
|---|
| 47 | | - {id: 1, |
|---|
| 48 | | - "code": "BP-SA-001-AKSJMS234", |
|---|
| 49 | | - "user_fullname": "Johnny Belmonte", |
|---|
| 50 | | - "user_email": "jb@curisit.net", |
|---|
| 51 | | - "status": 3}, |
|---|
| 52 | | - {id: 2, |
|---|
| 53 | | - "code": "BP-SA-001-KAJSDHAJS", |
|---|
| 54 | | - "user_fullname": "Walter Simons", |
|---|
| 55 | | - "user_email": "ws@curisit.net", |
|---|
| 56 | | - "status": 1}, |
|---|
| 57 | | - {id: 3, |
|---|
| 58 | | - "code": "BP-SA-001-ASKDGHKA", |
|---|
| 59 | | - "user_fullname": "Frank Belmonte", |
|---|
| 60 | | - "user_email": "fb@curisit.net", |
|---|
| 61 | | - "status": 2}, |
|---|
| 62 | | - {id: 4, |
|---|
| 63 | | - "code": "BP-SA-001-BBBGGGG", |
|---|
| 64 | | - "user_fullname": "John Dalton", |
|---|
| 65 | | - "user_email": "jd@curisit.net", |
|---|
| 66 | | - "status": 3}, |
|---|
| 67 | | - {id: 5, |
|---|
| 68 | | - "code": "BP-SA-001-AKADNAJANA", |
|---|
| 69 | | - "user_fullname": "Walter Martins", |
|---|
| 70 | | - "user_email": "wm@curisit.net", |
|---|
| 71 | | - "status": 3}, |
|---|
| 72 | | - {id: 6, |
|---|
| 73 | | - "code": "BP-SA-001-AKANDAKS", |
|---|
| 74 | | - "user_fullname": "Joe Bolton", |
|---|
| 75 | | - "user_email": "jbol@curisit.net", |
|---|
| 76 | | - "status": 2} |
|---|
| 77 | | - ]; |
|---|
| 78 | | - |
|---|
| 94 | + // Used to create the form with the appropriate data |
|---|
| 95 | + $scope.isNew = undefined; |
|---|
| 79 | 96 | |
|---|
| 97 | + // Selected pack from listing |
|---|
| 98 | + // pack is the edited pack, in creation contains the data for the new pack |
|---|
| 99 | + $scope.pack = null; |
|---|
| 100 | + |
|---|
| 101 | + $scope.packs = packResource.query(); |
|---|
| 80 | 102 | |
|---|
| 81 | | - $scope.ellipsis = function(txt, len) { |
|---|
| 82 | | - if (!txt || txt.length <= len) return txt; |
|---|
| 83 | | - return txt.substring(0, len) + '...'; |
|---|
| 103 | + $scope.save = function() { |
|---|
| 104 | + var _success = function() { |
|---|
| 105 | + $scope.packs = packResource.query(); |
|---|
| 106 | + } |
|---|
| 107 | + packResource.save($scope.pack, _success) |
|---|
| 84 | 108 | } |
|---|
| 85 | 109 | |
|---|
| 86 | | - $scope.selectPack = function(pack) { |
|---|
| 87 | | - console.log('Pack selected: ' + JSON.stringify(pack)); |
|---|
| 88 | | - $scope.currentPack = pack; |
|---|
| 89 | | - $store.put('currentPack', pack); |
|---|
| 110 | + $scope.newPack = function() { |
|---|
| 111 | + $scope.isNew = true; |
|---|
| 112 | + $scope.showForm = true; |
|---|
| 113 | + $scope.pack = { |
|---|
| 114 | + num_licenses: 1, |
|---|
| 115 | + license_type_id: !$scope.refs.license_type_id || !$scope.refs.license_type_id.length ? null : $scope.refs.license_type_id[0].id, |
|---|
| 116 | + organization_id: !$scope.refs.organization_id || !$scope.refs.organization_id.length ? null : $scope.refs.organization_id[0].id |
|---|
| 117 | + } |
|---|
| 118 | + setTimeout(function() { |
|---|
| 119 | + $('#code').focus(); |
|---|
| 120 | + }, 0); |
|---|
| 121 | + } |
|---|
| 122 | + |
|---|
| 123 | + $scope.editPack = function(selectedPack) { |
|---|
| 124 | + $scope.isNew = false; |
|---|
| 125 | + $scope.showForm = true; |
|---|
| 126 | + $scope.pack = selectedPack; |
|---|
| 127 | + setTimeout(function() { |
|---|
| 128 | + $('#code').focus(); |
|---|
| 129 | + }, 0); |
|---|
| 130 | + } |
|---|
| 131 | + |
|---|
| 132 | + $scope.deletePack = function(selectedPack) { |
|---|
| 133 | + $scope.showForm = false; |
|---|
| 134 | + BootstrapDialog.confirm($L.get("The pack '{0}' will be deleted, are you sure?", selectedPack.code), function(result){ |
|---|
| 135 | + if(result) { |
|---|
| 136 | + var promise = packResource.remove({}, {id: selectedPack.id}).$promise; |
|---|
| 137 | + promise.then(function(data) { |
|---|
| 138 | + $scope.selectPack(null); |
|---|
| 139 | + $scope.packs = packResource.query(); |
|---|
| 140 | + toaster.pop('success', Catalogs.getName(), $L.get("Pack '{0}' deleted successfully", selectedPack.code)); |
|---|
| 141 | + },function(error) { |
|---|
| 142 | + console.log(error); |
|---|
| 143 | + 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); |
|---|
| 144 | + }); |
|---|
| 145 | + } |
|---|
| 146 | + }); |
|---|
| 147 | + $scope.isNew = false; |
|---|
| 148 | + } |
|---|
| 149 | + |
|---|
| 150 | + |
|---|
| 151 | + $scope.cancel = function() { |
|---|
| 152 | + $scope.showForm = false; |
|---|
| 153 | + } |
|---|
| 154 | + |
|---|
| 155 | + $scope.selectPack = function(packId) { |
|---|
| 156 | + $scope.$parent.currentPackId = packId; |
|---|
| 157 | + $store.put('currentPackId', packId); |
|---|
| 90 | 158 | } |
|---|
| 91 | 159 | |
|---|
| 92 | 160 | } ]); |
|---|