(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.controller('PackAndLicensesCtrl', [ '$scope', '$http', 'toaster', '$store', '$L', function($scope, $http, toaster, $store, $L) { $scope.licenses = [ {id: 1, "code": "BP-SA-001-AKSJMS234", "user_fullname": "Johnny Belmonte", "user_email": "jb@curisit.net", "status": 3}, {id: 2, "code": "BP-SA-001-KAJSDHAJS", "user_fullname": "Walter Simons", "user_email": "ws@curisit.net", "status": 1}, {id: 3, "code": "BP-SA-001-ASKDGHKA", "user_fullname": "Frank Belmonte", "user_email": "fb@curisit.net", "status": 2}, {id: 4, "code": "BP-SA-001-BBBGGGG", "user_fullname": "John Dalton", "user_email": "jd@curisit.net", "status": 3}, {id: 5, "code": "BP-SA-001-AKADNAJANA", "user_fullname": "Walter Martins", "user_email": "wm@curisit.net", "status": 3}, {id: 6, "code": "BP-SA-001-AKANDAKS", "user_fullname": "Joe Bolton", "user_email": "jbol@curisit.net", "status": 2} ]; $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.currentPackId = $store.get('currentPackId'); }]); 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() { $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(packId) { $scope.$parent.currentPackId = packId; $store.put('currentPackId', packId); } } ]); })();