From cbfe9207ad7c9bba96b39c550d250d12097fd06f Mon Sep 17 00:00:00 2001
From: Roberto Sánchez <roberto.sanchez@curisit.net>
Date: Thu, 23 Jan 2014 19:21:10 +0000
Subject: [PATCH] #395 feature - Implemented license section at 75%

---
 securis/src/main/resources/static/js/licenses.js |  128 +++++++++++++++++++++++++++++++++++++++++-
 1 files changed, 124 insertions(+), 4 deletions(-)

diff --git a/securis/src/main/resources/static/js/licenses.js b/securis/src/main/resources/static/js/licenses.js
index 50afef0..67bf7b4 100644
--- a/securis/src/main/resources/static/js/licenses.js
+++ b/securis/src/main/resources/static/js/licenses.js
@@ -60,7 +60,7 @@
         					if (!txt || txt.length <= len) return txt;
         					return txt.substring(0, len) + '...';
         				}
-                        $scope.currentPackId = $store.get('currentPackId');
+                        $scope.currentPack = $store.get('currentPack');
 
 	   			}]);
 	
@@ -153,11 +153,131 @@
                     $scope.showForm = false;
                 }
 
-				$scope.selectPack = function(packId) {
-					$scope.$parent.currentPackId = packId;
-					$store.put('currentPackId', packId);
+				$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});
+	                                           console.log('on pack_changed');
+	                                           if ($scope.showForm) {
+	                                               if ($scope.isNew) {
+	                                                   $scope.license.pack_id = $scope.currentPack.id
+	                                               } else {
+	                                                   $scope.showForm = false;    
+	                                               }
+	                                           }
+	                                       })
+	                                           
+	                                       var licenseResource = $resource('/license/:licenseId', {
+	                                           licenseId : '@id'
+	                                       });
+	                                       $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});
+	                                       
+	                                       $scope.save = function() {
+	                                           var _success = function() {
+	                                               if (!$scope.isNew) $scope.showForm = false;
+	                                               $scope.licenses = licenseResource.query({packId: $scope.currentPack.id});
+	                                           }
+	                                           licenseResource.save($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) {
+                                               
+                                           }
+	                                       
+	                                   } ]);
+
 })();

--
Gitblit v1.3.2