Browse Source

enable/disable show/hide element if permission is true/false

tags/0.1.0
Robin Thoni 9 years ago
parent
commit
cf58e110aa
3 changed files with 89 additions and 23 deletions
  1. 59
    0
      src/luhaspermission.js
  2. 9
    8
      src/lupermissionsedit.js
  3. 21
    15
      src/lutable.js

+ 59
- 0
src/luhaspermission.js View File

1
+/**
2
+ * Created by robin on 11/5/15.
3
+ */
4
+
5
+angular.module('luticateUtils')
6
+    .directive('luShowPermission', ['luticateAuthCache',
7
+        function(luticateAuthCache) {
8
+            return {
9
+                restrict: 'A',
10
+                link: function ($scope, element, attrs) {
11
+
12
+                    $scope.$watch(function()
13
+                    {
14
+                        var permissions = luticateAuthCache.getEffectivePermissions();
15
+                        if (permissions == null) {
16
+                            return null;
17
+                        }
18
+                        var permission_name = attrs.luShowPermission;
19
+                        if ($scope[permission_name] != null) {
20
+                            permission_name = $scope[permission_name];
21
+                        }
22
+
23
+                        var value = permissions[permission_name];
24
+                        return value == null ? null : value;
25
+                    }, function(newValue, oldValue)
26
+                    {
27
+                        if (newValue == true) {
28
+                            element.show();
29
+                        }
30
+                        else {
31
+                            element.hide();
32
+                        }
33
+                    })
34
+                }
35
+            };
36
+        }])
37
+    .directive('luEnablePermission', ['luticateAuthCache',
38
+        function(luticateAuthCache) {
39
+            return {
40
+                restrict: 'A',
41
+                scope: {
42
+                    luEnablePermission: '@'
43
+                },
44
+                link: function ($scope, element, attrs) {
45
+
46
+                    $scope.$watch(function() {
47
+                        return luticateAuthCache.hasEffectivePermissions($scope.luEnablePermission);
48
+                    }, function(newValue)
49
+                    {
50
+                        if (newValue == true) {
51
+                            element.removeAttr("disabled");
52
+                        }
53
+                        else {
54
+                            element.attr("disabled", "disabled");
55
+                        }
56
+                    })
57
+                }
58
+            };
59
+        }]);

+ 9
- 8
src/lupermissionsedit.js View File

20
                         Name: "",
20
                         Name: "",
21
                         Value: true
21
                         Value: true
22
                     };
22
                     };
23
-                    var luModel = $scope.luModel();
23
+                    $scope.luModelOptions = $scope.luModel();
24
+                    console.log();
24
 
25
 
25
                     $scope.editPermission = function(permission, value)
26
                     $scope.editPermission = function(permission, value)
26
                     {
27
                     {
28
                             id: "promiseEditPermission_" + permission.Name,
29
                             id: "promiseEditPermission_" + permission.Name,
29
                             loaderGroups: ['permission_' + permission.Name]
30
                             loaderGroups: ['permission_' + permission.Name]
30
                         };
31
                         };
31
-                        luModel.getEditPermissionPromise(permission.Name, value, promiseEditPermission)
32
+                        $scope.luModelOptions.getEditPermissionPromise(permission.Name, value, promiseEditPermission)
32
                             .then(function(data)
33
                             .then(function(data)
33
                             {
34
                             {
34
                                 permission.Value = value;
35
                                 permission.Value = value;
41
                             id: "promiseDelPermission_" + permission.Name,
42
                             id: "promiseDelPermission_" + permission.Name,
42
                             loaderGroups: ['permission_' + permission.Name]
43
                             loaderGroups: ['permission_' + permission.Name]
43
                         };
44
                         };
44
-                        luModel.getDelPermissionPromise(permission.Name, promiseDelPermission)
45
+                        $scope.luModelOptions.getDelPermissionPromise(permission.Name, promiseDelPermission)
45
                             .then(function(data)
46
                             .then(function(data)
46
                             {
47
                             {
47
                                 $scope.loadPermissions();
48
                                 $scope.loadPermissions();
54
                             id: "promiseAddPermission_" + permission.Name,
55
                             id: "promiseAddPermission_" + permission.Name,
55
                             loaderGroups: ['permission_add']
56
                             loaderGroups: ['permission_add']
56
                         };
57
                         };
57
-                        luModel.getAddPermissionPromise(permission.Name, permission.Value, promiseAddPermission)
58
+                        $scope.luModelOptions.getAddPermissionPromise(permission.Name, permission.Value, promiseAddPermission)
58
                             .then(function(data)
59
                             .then(function(data)
59
                             {
60
                             {
60
                                 $scope.loadPermissions();
61
                                 $scope.loadPermissions();
67
                             id: "promiseLoadPermissions_",
68
                             id: "promiseLoadPermissions_",
68
                             groups: ['permissionList']
69
                             groups: ['permissionList']
69
                         };
70
                         };
70
-                        luModel.getGetAllPromise(null, null, promiseLoadPermissions)
71
+                        $scope.luModelOptions.getGetAllPromise(null, null, promiseLoadPermissions)
71
                             .then(function (permissions) {
72
                             .then(function (permissions) {
72
                                 $scope.permissions = permissions.Data;
73
                                 $scope.permissions = permissions.Data;
73
                                 $scope.allPermissionsCleared = [];
74
                                 $scope.allPermissionsCleared = [];
115
 '    <label class="col-sm-6 control-label">{{ permission.Name }}</label>' +
116
 '    <label class="col-sm-6 control-label">{{ permission.Name }}</label>' +
116
 '<div class="col-sm-6">' +
117
 '<div class="col-sm-6">' +
117
 '    <div class="btn-group">' +
118
 '    <div class="btn-group">' +
118
-'    <a class="btn btn-default dropdown-toggle btn-select" data-toggle="dropdown" href="">' +
119
+'    <a class="btn btn-default dropdown-toggle btn-select" data-toggle="dropdown" href="" lu-enable-permission="{{ luModelOptions.permissions.edit }}">' +
119
 '    {{ permission.Value ? \'Allow\' : \'Disallow\' }} <span class="caret"></span>' +
120
 '    {{ permission.Value ? \'Allow\' : \'Disallow\' }} <span class="caret"></span>' +
120
 '    </a>' +
121
 '    </a>' +
121
 '    <ul class="dropdown-menu">' +
122
 '    <ul class="dropdown-menu">' +
124
 '</ul>' +
125
 '</ul>' +
125
 '</div>' +
126
 '</div>' +
126
 '<div class="btn-group">' +
127
 '<div class="btn-group">' +
127
-'    <button type="button" class="btn btn-default" ng-click="delPermission(permission)">' +
128
+'    <button type="button" class="btn btn-default" ng-click="delPermission(permission)" lu-show-permission="{{ luModelOptions.permissions.del }}">' +
128
 '    <span class="glyphicon glyphicon-remove" aria-hidden="true"></span>' +
129
 '    <span class="glyphicon glyphicon-remove" aria-hidden="true"></span>' +
129
 '    </button>' +
130
 '    </button>' +
130
 '    </div>' +
131
 '    </div>' +
132
 '    </div>' +
133
 '    </div>' +
133
 '    </div>' +
134
 '    </div>' +
134
 '    <div class="clearfix"></div>' +
135
 '    <div class="clearfix"></div>' +
135
-'    <div class="col-sm-12" lu-busy="permission_add">' +
136
+'    <div class="col-sm-12" lu-busy="permission_add" lu-show-permission="{{ luModelOptions.permissions.add }}">' +
136
 '    <div class="btn-group">' +
137
 '    <div class="btn-group">' +
137
 '    <a class="btn btn-default dropdown-toggle btn-select" data-toggle="dropdown" href="">' +
138
 '    <a class="btn btn-default dropdown-toggle btn-select" data-toggle="dropdown" href="">' +
138
 '    {{ tmpPermission.Name }} <span class="caret"></span>' +
139
 '    {{ tmpPermission.Name }} <span class="caret"></span>' +

+ 21
- 15
src/lutable.js View File

7
         function(dialogs, luticateDialogErrorHelper) {
7
         function(dialogs, luticateDialogErrorHelper) {
8
             return {
8
             return {
9
                 restrict: 'E',
9
                 restrict: 'E',
10
+                scope: {
11
+                    options: '&'
12
+                },
10
                 templateUrl: "/luticate/lutable.html",
13
                 templateUrl: "/luticate/lutable.html",
11
                 link: function ($scope, element, attrs) {
14
                 link: function ($scope, element, attrs) {
12
                     $scope.page = 0;
15
                     $scope.page = 0;
15
                     $scope.pages = [];
18
                     $scope.pages = [];
16
                     $scope.selectedItems = [];
19
                     $scope.selectedItems = [];
17
 
20
 
21
+                    $scope.tableOptions = $scope.options();
22
+
18
                     var promiseLoadItems = {
23
                     var promiseLoadItems = {
19
                         id: "promiseLoadItems",
24
                         id: "promiseLoadItems",
20
                         groups: ['itemList']
25
                         groups: ['itemList']
25
                     };
30
                     };
26
 
31
 
27
                     $scope.loadPage = function (page) {
32
                     $scope.loadPage = function (page) {
28
-                        $scope.getLoadPagePromise(page, $scope.perPage, promiseLoadItems)
33
+                        $scope.tableOptions.getLoadPagePromise(page, $scope.perPage, promiseLoadItems)
29
                             .then(function (items) {
34
                             .then(function (items) {
30
                                 $scope.page = page;
35
                                 $scope.page = page;
31
                                 $scope.items = items;
36
                                 $scope.items = items;
40
                     };
45
                     };
41
 
46
 
42
                     $scope.displayItem = function (item) {
47
                     $scope.displayItem = function (item) {
43
-                        var ctrl = $scope.getEditController();
48
+                        var ctrl = $scope.tableOptions.getEditController();
44
                         dialogs.create('views/modals/' + ctrl.toLowerCase() + '.html', ctrl + 'Controller', item)
49
                         dialogs.create('views/modals/' + ctrl.toLowerCase() + '.html', ctrl + 'Controller', item)
45
                             .result.then(function (data) {
50
                             .result.then(function (data) {
46
                                 $scope.loadPage($scope.page);
51
                                 $scope.loadPage($scope.page);
52
                             $scope.loadPage($scope.page);
57
                             $scope.loadPage($scope.page);
53
                             return;
58
                             return;
54
                         }
59
                         }
55
-                        $scope.getDelPromise($scope.selectedItems[0], promiseDelItems)
60
+                        $scope.tableOptions.getDelPromise($scope.selectedItems[0], promiseDelItems)
56
                             .then(function (data) {
61
                             .then(function (data) {
57
                                 $scope.selectedItems.splice(0, 1);
62
                                 $scope.selectedItems.splice(0, 1);
58
                                 $scope.removeItems();
63
                                 $scope.removeItems();
66
                     };
71
                     };
67
 
72
 
68
                     $scope.addItem = function () {
73
                     $scope.addItem = function () {
69
-                        var ctrl = $scope.getEditController();
74
+                        var ctrl = $scope.tableOptions.getEditController();
70
                         dialogs.create('views/modals/' + ctrl.toLowerCase() + '.html', ctrl + 'Controller', null)
75
                         dialogs.create('views/modals/' + ctrl.toLowerCase() + '.html', ctrl + 'Controller', null)
71
                             .result.then(function (data) {
76
                             .result.then(function (data) {
72
                                 $scope.loadPage($scope.page);
77
                                 $scope.loadPage($scope.page);
74
                     };
79
                     };
75
 
80
 
76
                     $scope.toggleSelectedItem = function (item) {
81
                     $scope.toggleSelectedItem = function (item) {
77
-                        var id = $scope.getItemId(item);
82
+                        var id = $scope.tableOptions.getItemId(item);
78
                         var idx = $scope.selectedItems.indexOf(id);
83
                         var idx = $scope.selectedItems.indexOf(id);
79
                         if (idx > -1) {
84
                         if (idx > -1) {
80
                             $scope.selectedItems.splice(idx, 1);
85
                             $scope.selectedItems.splice(idx, 1);
91
                         else {
96
                         else {
92
                             $scope.selectedItems = [];
97
                             $scope.selectedItems = [];
93
                             for (var i = 0; i < $scope.items.Data.length; ++i) {
98
                             for (var i = 0; i < $scope.items.Data.length; ++i) {
94
-                                $scope.selectedItems.push($scope.getItemId($scope.items.Data[i]));
99
+                                $scope.selectedItems.push($scope.tableOptions.getItemId($scope.items.Data[i]));
95
                             }
100
                             }
96
                         }
101
                         }
97
                     };
102
                     };
98
 
103
 
99
                     $scope.isItemChecked = function(item)
104
                     $scope.isItemChecked = function(item)
100
                     {
105
                     {
101
-                        return $scope.selectedItems.indexOf($scope.getItemId(item)) > -1;
106
+                        return $scope.selectedItems.indexOf($scope.tableOptions.getItemId(item)) > -1;
102
                     };
107
                     };
103
 
108
 
104
-                    if (!$scope.getItemId) {
105
-                        $scope.getItemId = function(item)
109
+                    if (!$scope.tableOptions.getItemId) {
110
+                        $scope.tableOptions.getItemId = function(item)
106
                         {
111
                         {
107
                             return item.Id;
112
                             return item.Id;
108
                         };
113
                         };
120
 '        <table class="col-sm-12 table table-hover">' +
125
 '        <table class="col-sm-12 table table-hover">' +
121
 '    <thead>' +
126
 '    <thead>' +
122
 '    <tr>' +
127
 '    <tr>' +
123
-'    <th>' +
128
+'    <th lu-show-permission="{{ tableOptions.permissions.del }}">' +
124
 '    <input type="checkbox" ng-click="toggleSelectAll()"' +
129
 '    <input type="checkbox" ng-click="toggleSelectAll()"' +
125
 '    ng-checked="selectedItems.length == items.Data.length && items.Data.length != 0">' +
130
 '    ng-checked="selectedItems.length == items.Data.length && items.Data.length != 0">' +
126
 '        </th>' +
131
 '        </th>' +
127
-'        <th class="col-sm-{{ col.width }}" ng-repeat="col in columns">{{ col.name }}</th>' +
132
+'        <th class="col-sm-{{ col.width }}" ng-repeat="col in tableOptions.columns">{{ col.name }}</th>' +
128
 '</tr>' +
133
 '</tr>' +
129
 '</thead>' +
134
 '</thead>' +
130
 '<tbody>' +
135
 '<tbody>' +
131
 '<tr ng-repeat="item in items.Data" style="cursor: pointer" ng-click="displayItem(item)">' +
136
 '<tr ng-repeat="item in items.Data" style="cursor: pointer" ng-click="displayItem(item)">' +
132
-'    <td>' +
137
+'    <td lu-show-permission="{{ tableOptions.permissions.del }}">' +
133
 '    <input name="selectedItems[]" type="checkbox" ng-checked="isItemChecked(item)"' +
138
 '    <input name="selectedItems[]" type="checkbox" ng-checked="isItemChecked(item)"' +
134
  '   ng-click="$event.stopPropagation();toggleSelectedItem(item)" >' +
139
  '   ng-click="$event.stopPropagation();toggleSelectedItem(item)" >' +
135
 '        </td>' +
140
 '        </td>' +
136
-'        <td ng-repeat="col in columns">{{ col.getValue(item) }}</td>' +
141
+'        <td ng-repeat="col in tableOptions.columns">{{ col.getValue(item) }}</td>' +
137
 '</tr>' +
142
 '</tr>' +
138
 '</tbody>' +
143
 '</tbody>' +
139
 '</table>' +
144
 '</table>' +
143
 '</div>' +
148
 '</div>' +
144
 
149
 
145
 '<div class="col-sm-12">' +
150
 '<div class="col-sm-12">' +
146
-'    <button class="btn btn-default" type="button" ng-click="removeItems()" ng-disabled="selectedItems.length == 0">' +
151
+'    <button class="btn btn-default" type="button" ng-click="removeItems()"' +
152
+'ng-disabled="selectedItems.length == 0" lu-show-permission="{{ tableOptions.permissions.del }}">' +
147
 '    <span class="glyphicon glyphicon-remove" aria-hidden="true"></span> Remove' +
153
 '    <span class="glyphicon glyphicon-remove" aria-hidden="true"></span> Remove' +
148
 '    </button>' +
154
 '    </button>' +
149
-'    <button class="btn btn-default" type="button" ng-click="addItem()">' +
155
+'    <button class="btn btn-default" type="button" ng-click="addItem()" lu-show-permission="{{ tableOptions.permissions.add }}">' +
150
 '    <span class="glyphicon glyphicon-plus" aria-hidden="true"></span> Add' +
156
 '    <span class="glyphicon glyphicon-plus" aria-hidden="true"></span> Add' +
151
 '    </button>' +
157
 '    </button>' +
152
 '    </div>' +
158
 '    </div>' +

Loading…
Cancel
Save