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.5KB

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. var onItemClicked = $scope.tableOptions.onItemClicked;
  32. $scope.tableOptions.onItemClicked = function (item) {
  33. if (onItemClicked != null) {
  34. onItemClicked(item);
  35. }
  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. if ($scope.tableOptions.canDel == null) {
  44. $scope.tableOptions.canDel = function()
  45. {
  46. return false;
  47. }
  48. }
  49. if (typeof $scope.tableOptions.canDel == 'string') {
  50. var permDel = $scope.tableOptions.canDel;
  51. $scope.tableOptions.canDel = function()
  52. {
  53. return luticateAuthCache.hasEffectivePermission(permDel);
  54. }
  55. }
  56. $scope.tableOptions.canCheck = $scope.tableOptions.canDel;
  57. if ($scope.tableOptions.canEdit == null) {
  58. $scope.tableOptions.canEdit = function()
  59. {
  60. return false;
  61. }
  62. }
  63. if (typeof $scope.tableOptions.canEdit == 'string') {
  64. var permEdit = $scope.tableOptions.canEdit;
  65. $scope.tableOptions.canEdit = function()
  66. {
  67. return luticateAuthCache.hasEffectivePermission(permEdit);
  68. }
  69. }
  70. $scope.tableOptions.canClick = $scope.tableOptions.canEdit;
  71. if ($scope.tableOptions.canAdd == null) {
  72. $scope.tableOptions.canAdd = function()
  73. {
  74. return false;
  75. }
  76. }
  77. if (typeof $scope.tableOptions.canAdd == 'string') {
  78. var permAdd = $scope.tableOptions.canAdd;
  79. $scope.tableOptions.canAdd = function()
  80. {
  81. return luticateAuthCache.hasEffectivePermission(permAdd);
  82. }
  83. }
  84. $scope.deleteItems = function () {
  85. if ($scope.tableOptions.checkedItems.length == 0) {
  86. $scope.tableOptions.luBasicTableScope.loadPage($scope.tableOptions.page);
  87. return;
  88. }
  89. var promiseDelItems = {
  90. id: "promiseDelItems",
  91. loaderGroups: [$scope.tableOptions.luBusy.group]
  92. };
  93. var item = $scope.tableOptions.checkedItems[0];
  94. $scope.tableOptions.getDelPromise(item, promiseDelItems)
  95. .then(function (data) {
  96. $scope.tableOptions.onItemDeleted(item);
  97. $scope.tableOptions.checkedItems.splice(0, 1);
  98. $scope.deleteItems();
  99. }, function (error) {
  100. luticateDialogErrorHelper.errorDialog(error)
  101. .result.then(function (data) {
  102. $scope.tableOptions.luBasicTableScope.loadPage($scope.tableOptions.page);
  103. }, function (error) {
  104. });
  105. });
  106. };
  107. $scope.addItem = function () {
  108. var ctrl = $scope.tableOptions.getEditController();
  109. dialogs.create('views/modals/' + ctrl.toLowerCase() + '.html', ctrl + 'Controller', null)
  110. .result.then(function (data) {
  111. $scope.tableOptions.luBasicTableScope.loadPage($scope.tableOptions.page);
  112. $scope.tableOptions.onItemAdded(data);
  113. });
  114. };
  115. }
  116. };
  117. }
  118. ]);
  119. angular.module('luticateUtils').run(['$templateCache', function($templateCache)
  120. {
  121. $templateCache.put('/luticate/lutable.html',
  122. ' <lu-basic-table options="tableOptions"></lu-basic-table>' +
  123. '<div class="col-sm-12">' +
  124. ' <button class="btn btn-default" type="button" ng-click="deleteItems()"' +
  125. 'ng-disabled="tableOptions.checkedItems.length == 0" ng-show="tableOptions.canDel()">' +
  126. ' <span class="glyphicon glyphicon-remove" aria-hidden="true"></span> Delete' +
  127. ' </button>' +
  128. ' <button class="btn btn-default" type="button" ng-click="addItem()" ng-show="tableOptions.canAdd()">' +
  129. ' <span class="glyphicon glyphicon-plus" aria-hidden="true"></span> Add' +
  130. ' </button>' +
  131. ' </div>');
  132. }]);