tabellaFirmeDistinteCtrl.js 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211
  1. app.controller('tabellaFirmeDistinteCtrl',
  2. function($scope,$http,$cookies,$window, $location,$timeout, $mdDialog, $q, $routeParams, $mdBottomSheet, $mdMedia, Firme, utilsSvc) {
  3. var vm=this;
  4. vm.maxItems = 0;
  5. vm.itemsPerPage = 100;
  6. vm.selectedIndex=-1;
  7. vm.records=[];
  8. vm.filter={
  9. filterText: '',
  10. orderField:null
  11. }
  12. vm.setSelected=function(idx) {
  13. vm.selectedIndex = idx;
  14. }
  15. vm.columnWidths=[
  16. 120, //0
  17. 140, //1
  18. 120, //2
  19. 380 //3
  20. ];
  21. vm.getListHeight = function() {
  22. containerHeight = ($window.innerHeight - $('#container').position().top - $('#view').position().top);
  23. return {'height': containerHeight + 'px'};
  24. };
  25. vm.getGridItemHeight = function() {
  26. return utilsSvc.getGridWithButtonHeight();
  27. }
  28. $window.addEventListener('resize', onResize);
  29. function onResize() {
  30. $scope.$digest();
  31. }
  32. $scope.$on('$destroy', function() {
  33. $window.removeEventListener('resize', onResize);
  34. });
  35. vm.getColumnWidth = // }
  36. // this.lastStartIdx = index;
  37. function(idx) {
  38. return {
  39. 'width': vm.columnWidths[idx] + 'px',
  40. 'min-width':vm.columnWidths[idx] + 'px',
  41. 'margin-left':'8px',
  42. 'margin-right':'8px'
  43. };
  44. }
  45. vm.getMaxListWidth = function() {
  46. var res=0;
  47. for (var i=0;i<vm.columnWidths.length;i++)
  48. res += vm.columnWidths[i]+6+8+10;
  49. $('#container').css('width',($('#header').width()+96)+'px');
  50. return {'width': res+ 'px!important;'}
  51. }
  52. vm.setOrderField = function(field) {
  53. if (vm.filter.orderField!=field)
  54. vm.filter.orderField = field;
  55. else
  56. vm.filter.orderField = "-" +field;
  57. vm.getFirme();
  58. }
  59. vm.cancellaFirma=function(idx,event) {
  60. event.preventDefault();
  61. event.stopPropagation();
  62. utilsSvc.showConfirm("Conferma", "Confermi la cancellazione dell'elemento selezionato?")
  63. .then(function(res){
  64. Firme.Delete({id:vm.records[idx].id})
  65. .$promise.then(function(data){
  66. utilsSvc.showMessage("Cancellazione effettuata");
  67. vm.getFirme();
  68. })
  69. })
  70. }
  71. vm.nuovaFirma=function(event) {
  72. Firme.New()
  73. .$promise.then(function(data){
  74. vm.records.push(data);
  75. vm.editFirma(vm.records.length-1,event);
  76. })
  77. }
  78. vm.editFirma = function(idx, ev) {
  79. $mdDialog.show({
  80. targetEvent:ev,
  81. templateUrl: 'templates/firma.html',
  82. controller: 'firmaCtrl',
  83. locals: {firma: vm.records[idx], descrizione: vm.decodeTipologia(vm.records[idx].tipologia)},
  84. multiple: true
  85. }).then(function(res){
  86. vm.records[idx] = res;
  87. Firme.save({},res).$promise.then(
  88. function(res){
  89. vm.records[idx] = res;
  90. utilsSvc.showMessage('Salvataggio modifiche effettuato');
  91. vm.getFirme();
  92. },
  93. function(err){
  94. utilsSvc.handleHttpError(err);
  95. }
  96. );
  97. });
  98. }
  99. vm.decodeTipologia = function(tipologia) {
  100. return utilsSvc.decodeTipologia(tipologia);
  101. }
  102. vm.infiniteItems = {
  103. numLoaded_: 0,
  104. toLoad_: 0,
  105. loading_:false,
  106. lastStartIdx:-1,
  107. listPromise:null,
  108. canceler: $q.defer(),
  109. // Required.
  110. getItemAtIndex: function(index) {
  111. if (vm.maxItems==0 || index>vm.maxItems)
  112. return null;
  113. if (!vm.infiniteItems.loading_)
  114. if (index > vm.records.length) {
  115. this.fetchMoreItems_(index);
  116. return null;
  117. }
  118. return vm.records [index];
  119. },
  120. getLength: function() {
  121. return vm.maxItems;
  122. },
  123. fetchMoreItems_: function(index) {
  124. if (this.toLoad_ < index) {
  125. this.toLoad_ += vm.itemsPerPage;
  126. utilsSvc.showWaitMessage('Ricerca in corso...');
  127. vm.infiniteItems.loading_ = true;
  128. this.listPromise = Firme.List({start: vm.records.length,size:Math.max(vm.records.length+index,vm.itemsPerPage)}).$promise;
  129. var mod=this;
  130. this.listPromise.then(
  131. function(data){
  132. vm.infiniteItems.loading_ = false;
  133. vm.infiniteItems.toLoad_=0;
  134. utilsSvc.cancelWaitMessage();
  135. vm.records = vm.records.concat(data);
  136. mod.numLoaded_ = vm.records.length;
  137. mod.lastStartIdx = vm.records.length;
  138. },
  139. function(error) {
  140. vm.infiniteItems.loading_ = false;
  141. vm.infiniteItems.toLoad_=0;
  142. utilsSvc.handleHttpError(error);
  143. }
  144. )
  145. }
  146. }
  147. }
  148. vm.getFirme = function(){
  149. Firme.Count({filter:vm.filter.filterText}).$promise.then(
  150. function(data){
  151. vm.records=[];
  152. vm.maxItems = data.value;
  153. vm.infiniteItems.numLoaded_ = 0;
  154. vm.infiniteItems.toLoad_ = 0;
  155. }
  156. );
  157. }
  158. vm.init = function() {
  159. vm.getFirme();
  160. }
  161. vm.init();
  162. })
  163. .controller("firmaCtrl",function($scope, $mdDialog,utilsSvc,locals){
  164. $scope.firma = angular.copy(locals.firma);
  165. $scope.descrizione = locals.descrizione;
  166. $scope.confirmDialog = function(){
  167. if ($scope.firma.tipologia==null ||$scope.firma.tipologia.trim()=='' || $scope.firma.prefissoFlussoInput==null || $scope.firma.prefissoFlussoInput.trim()=='') {
  168. utilsSvc.show('I dati Tipologia e Prefisso Flusso sono obbligatori');
  169. return;
  170. }
  171. $mdDialog.hide($scope.firma);
  172. }
  173. $scope.cancelDialog=function(){
  174. $mdDialog.cancel();
  175. }
  176. })
  177. ;