/** * Created by robin on 11/3/15. */ angular.module('luticateUtils') .directive('luEditTable', ['dialogs', 'luticateDialogErrorHelper', 'luticateAuthCache', 'luticateDialogOkCancelHelper', function(dialogs, luticateDialogErrorHelper, luticateAuthCache, luticateDialogOkCancelHelper) { return { restrict: 'E', scope: { options: '&' }, transclude: { 'lu-basic-table-filter': '?lu-basic-table-filter' }, templateUrl: "/luticate/lutable.html", link: function ($scope, element, attrs) { $scope.tableOptions = $scope.options(); $scope.tableOptions.luEditTableScope = $scope; if ($scope.tableOptions.onItemDeleted == null) { $scope.tableOptions.onItemDeleted = function(item) { } } if ($scope.tableOptions.onItemEdited == null) { $scope.tableOptions.onItemEdited = function(item) { } } if ($scope.tableOptions.onItemAdded == null) { $scope.tableOptions.onItemAdded = function(item) { } } if ($scope.tableOptions.onItemClicked == null) { $scope.tableOptions.onItemClicked = function (item) { var ctrl = $scope.tableOptions.getEditController(); dialogs.create('views/modals/' + ctrl.toLowerCase() + '.html', ctrl + 'Controller', item) .result.then(function (data) { $scope.tableOptions.luBasicTableScope.loadPage($scope.tableOptions.page); $scope.tableOptions.onItemEdited(data); }); }; } if ($scope.tableOptions.addItem == null) { $scope.tableOptions.addItem = function() { var ctrl = $scope.tableOptions.getEditController(); dialogs.create('views/modals/' + ctrl.toLowerCase() + '.html', ctrl + 'Controller', $scope.tableOptions.editData) .result.then(function (data) { $scope.tableOptions.luBasicTableScope.loadPage($scope.tableOptions.page); $scope.tableOptions.onItemAdded(data); }); } } if ($scope.tableOptions.canDel == null) { $scope.tableOptions.canDel = function() { return false; } } if (typeof $scope.tableOptions.canDel == 'string') { var permDel = $scope.tableOptions.canDel; $scope.tableOptions.canDel = function() { return luticateAuthCache.hasEffectivePermission(permDel); } } $scope.tableOptions.canCheck = $scope.tableOptions.canDel; if ($scope.tableOptions.canEdit == null) { $scope.tableOptions.canEdit = function() { return false; } } if (typeof $scope.tableOptions.canEdit == 'string') { var permEdit = $scope.tableOptions.canEdit; $scope.tableOptions.canEdit = function(item) { return luticateAuthCache.hasEffectivePermission(permEdit); } } $scope.tableOptions.canClick = $scope.tableOptions.canEdit; if ($scope.tableOptions.canAdd == null) { $scope.tableOptions.canAdd = function() { return false; } } if (typeof $scope.tableOptions.canAdd == 'string') { var permAdd = $scope.tableOptions.canAdd; $scope.tableOptions.canAdd = function() { return luticateAuthCache.hasEffectivePermission(permAdd); } } $scope.askDeleteItems = function() { luticateDialogOkCancelHelper.okCancelDialog({ title: "Delete items", text: "Do you really want to delete selected items?" }).result.then(function() { $scope.deleteItems(); }); }; $scope.deleteItems = function () { if ($scope.tableOptions.checkedItems.length == 0) { $scope.tableOptions.luBasicTableScope.loadPage($scope.tableOptions.page); return; } var promiseDelItems = { id: "promiseDelItems", loaderGroups: [$scope.tableOptions.luBusy.group] }; var item = $scope.tableOptions.checkedItems[0]; $scope.tableOptions.getDelPromise(item, promiseDelItems) .then(function (data) { $scope.tableOptions.onItemDeleted(item); $scope.tableOptions.checkedItems.splice(0, 1); $scope.deleteItems(); }, function (error) { luticateDialogErrorHelper.errorDialog(error) .result.then(function (data) { $scope.tableOptions.luBasicTableScope.loadPage($scope.tableOptions.page); }, function (error) { }); }); }; } }; } ]); angular.module('luticateUtils').run(['$templateCache', function($templateCache) { $templateCache.put('/luticate/lutable.html', '' + '
' + ' ' + ' ' + '
'); }]);