You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

luedittable.js 7.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  1. /**
  2. * Created by robin on 11/3/15.
  3. */
  4. angular.module('luticateUtils')
  5. .directive('luEditTable', ['dialogs', 'luticateDialogErrorHelper', 'luticateAuthCache', 'luticateDialogOkCancelHelper',
  6. function(dialogs, luticateDialogErrorHelper, luticateAuthCache, luticateDialogOkCancelHelper) {
  7. return {
  8. restrict: 'E',
  9. scope: {
  10. options: '&'
  11. },
  12. transclude: {
  13. 'lu-basic-table-filter': '?lu-basic-table-filter'
  14. },
  15. templateUrl: "/luticate/lutable.html",
  16. link: function ($scope, element, attrs) {
  17. $scope.tableOptions = $scope.options();
  18. $scope.tableOptions.luEditTableScope = $scope;
  19. if ($scope.tableOptions.onItemDeleted == null) {
  20. $scope.tableOptions.onItemDeleted = function(item)
  21. {
  22. }
  23. }
  24. if ($scope.tableOptions.onItemEdited == null) {
  25. $scope.tableOptions.onItemEdited = function(item)
  26. {
  27. }
  28. }
  29. if ($scope.tableOptions.onItemAdded == null) {
  30. $scope.tableOptions.onItemAdded = function(item)
  31. {
  32. }
  33. }
  34. if ($scope.tableOptions.onItemClicked == null) {
  35. $scope.tableOptions.onItemClicked = function (item) {
  36. var ctrl = $scope.tableOptions.getEditController();
  37. dialogs.create('views/modals/' + ctrl.toLowerCase() + '.html', ctrl + 'Controller', item)
  38. .result.then(function (data) {
  39. $scope.tableOptions.luBasicTableScope.loadPage($scope.tableOptions.page);
  40. $scope.tableOptions.onItemEdited(data);
  41. });
  42. };
  43. }
  44. if ($scope.tableOptions.addItem == null) {
  45. $scope.tableOptions.addItem = function()
  46. {
  47. var ctrl = $scope.tableOptions.getEditController();
  48. dialogs.create('views/modals/' + ctrl.toLowerCase() + '.html', ctrl + 'Controller', $scope.tableOptions.editData)
  49. .result.then(function (data) {
  50. $scope.tableOptions.luBasicTableScope.loadPage($scope.tableOptions.page);
  51. $scope.tableOptions.onItemAdded(data);
  52. });
  53. }
  54. }
  55. if ($scope.tableOptions.canDel == null) {
  56. $scope.tableOptions.canDel = function()
  57. {
  58. return false;
  59. }
  60. }
  61. if (typeof $scope.tableOptions.canDel == 'string') {
  62. var permDel = $scope.tableOptions.canDel;
  63. $scope.tableOptions.canDel = function()
  64. {
  65. return luticateAuthCache.hasEffectivePermission(permDel);
  66. }
  67. }
  68. $scope.tableOptions.canCheck = $scope.tableOptions.canDel;
  69. if ($scope.tableOptions.canEdit == null) {
  70. $scope.tableOptions.canEdit = function()
  71. {
  72. return false;
  73. }
  74. }
  75. if (typeof $scope.tableOptions.canEdit == 'string') {
  76. var permEdit = $scope.tableOptions.canEdit;
  77. $scope.tableOptions.canEdit = function(item)
  78. {
  79. return luticateAuthCache.hasEffectivePermission(permEdit);
  80. }
  81. }
  82. $scope.tableOptions.canClick = $scope.tableOptions.canEdit;
  83. if ($scope.tableOptions.canAdd == null) {
  84. $scope.tableOptions.canAdd = function()
  85. {
  86. return false;
  87. }
  88. }
  89. if (typeof $scope.tableOptions.canAdd == 'string') {
  90. var permAdd = $scope.tableOptions.canAdd;
  91. $scope.tableOptions.canAdd = function()
  92. {
  93. return luticateAuthCache.hasEffectivePermission(permAdd);
  94. }
  95. }
  96. $scope.askDeleteItems = function()
  97. {
  98. luticateDialogOkCancelHelper.okCancelDialog({
  99. title: "Delete items",
  100. text: "Do you really want to delete selected items?"
  101. }).result.then(function()
  102. {
  103. $scope.deleteItems();
  104. });
  105. };
  106. $scope.deleteItems = function () {
  107. if ($scope.tableOptions.checkedItems.length == 0) {
  108. $scope.tableOptions.luBasicTableScope.loadPage($scope.tableOptions.page);
  109. return;
  110. }
  111. var promiseDelItems = {
  112. id: "promiseDelItems",
  113. loaderGroups: [$scope.tableOptions.luBusy.group]
  114. };
  115. var item = $scope.tableOptions.checkedItems[0];
  116. $scope.tableOptions.getDelPromise(item, promiseDelItems)
  117. .then(function (data) {
  118. $scope.tableOptions.onItemDeleted(item);
  119. $scope.tableOptions.checkedItems.splice(0, 1);
  120. $scope.deleteItems();
  121. }, function (error) {
  122. luticateDialogErrorHelper.errorDialog(error)
  123. .result.then(function (data) {
  124. $scope.tableOptions.luBasicTableScope.loadPage($scope.tableOptions.page);
  125. }, function (error) {
  126. });
  127. });
  128. };
  129. }
  130. };
  131. }
  132. ]);
  133. angular.module('luticateUtils').run(['$templateCache', function($templateCache)
  134. {
  135. $templateCache.put('/luticate/lutable.html',
  136. '<lu-basic-table options="tableOptions"><lu-basic-table-filter><ng-transclude ng-transclude="lu-basic-table-filter"></ng-transclude></lu-basic-table-filter></lu-basic-table>' +
  137. '<div class="col-sm-12">' +
  138. ' <button class="btn btn-danger" type="button" ng-click="askDeleteItems()"' +
  139. ' ng-disabled="tableOptions.checkedItems.length == 0" ng-show="tableOptions.canDel()">' +
  140. ' <span class="glyphicon glyphicon-remove" aria-hidden="true"></span> Delete' +
  141. ' </button>' +
  142. ' <button class="btn btn-primary" type="button" ng-click="tableOptions.addItem()" ng-show="tableOptions.canAdd()">' +
  143. ' <span class="glyphicon glyphicon-plus" aria-hidden="true"></span> Add' +
  144. ' </button>' +
  145. '</div>');
  146. }]);