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 6.6KB

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