9c0dd7598fa7746ccbe4748445f9cd5514e2fac6
[ccsdk/apps.git] / sdnr / wireless-transport / code-Carbon-SR1 / apps / dlux / dlux-web / src / common / general / common.navigation.directives.js
1 define(['common/general/common.navigation.module'], function(common) {
2
3   /*
4   * Helper to set CSS class to active via ng-class using $location.path()
5   * or $state.includes()
6   */
7   common.directive('isActive', function($compile) {
8     return {
9       restrict: 'A',
10       replace: false,
11       scope: {
12         state: '@',
13         stateParams: '=',
14         stateActive: '@',
15         url: '@'
16       },
17
18       controller: ['$scope', '$location', '$state', function ($scope, $location, $state) {
19         $scope.$state = $state;
20         $scope.$location = $location;
21       }],
22       compile: function() {
23         return function (scope, iElement, iAttrs, controller) {
24           var active;
25           if (scope.state) {
26             var state = scope.stateActive || scope.$state.current.name.split('.')[0];
27             active = 'active: $state.includes(\'' + scope.state + '\')';
28           } else if (scope.url) {
29             active = 'active: url === $location.path()';
30           } else {
31             active = "false";
32           }
33           iElement.attr('ng-class', '{ ' + active  + ' }'); // Adding the ngClass
34           iElement.removeAttr('is-active'); // Avoid infinite loop
35           $compile(iElement)(scope);
36         };
37       }
38     };
39   });
40
41
42   common.directive('brdAnchor', function ($compile, $rootScope) {
43     return {
44       restrict: 'E',
45       replace: true,
46       scope: {
47         label: '@',
48         state: '@',
49         stateParams: '=',
50         url: '@'
51       },
52
53       /* The idea is to support both state and url, to be able to set {active} either
54       if stateActive matches via $state.includes() or if the url matches
55       Change this into a actual href later on ? - see https://github.com/angular-ui/ui-router/issues/395
56       */
57       template: '<a href="" ng-click="doClick()">{{label}}</a>',
58       controller: ['$scope', '$rootScope', '$location', '$state', function ($scope, $rootScope, $location, $state) {
59         $scope.$location = $location;
60         $scope.$state = $state;
61
62         $scope.doClick = function () {
63           var args = {
64             label: $scope.label,
65             state: $scope.state,
66             stateParams: $scope.stateParams,
67             url: $scope.url
68           };
69
70           $rootScope.$broadcast('event:navigation', args);
71
72           if (!$scope.url && $scope.state) {
73             var params = $scope.stateParams || {};
74             $state.transitionTo($scope.state, params, { location: true, inherit: true, relative: $state.$current, notify: true });
75           } else if ($scope.url) {
76             $location.path($scope.url);
77           }
78         };
79       }]
80     };
81   });
82
83
84   common.directive('buttonCancel', function() {
85       // Runs during compile
86       return {
87           restrict: 'E',
88           replace: true,
89           scope: {
90               'btnLabel': '@label',
91               'btnSize': '@size',
92               'btnGlyph': '@glyph',
93               'cancelFunc': '=function',
94               'state': '@',
95               'stateParams': '=',
96           },
97           template: '<button class="btn btn-{{size}} btn-danger" ng-click="doCancel()"><i class="icon-remove-sign"></i> {{label}}</button>',
98           controller: ['$scope', '$state', function ($scope, $state) {
99             $scope.label = $scope.btnLabel || 'Cancel';
100             $scope.size = $scope.btnSize || 'md';
101             $scope.glyph = $scope.btnGlyph || 'remove-circle';
102
103             $scope.doCancel = function () {
104               if (angular.isFunction($scope.cancelFunc)) {
105                 $scope.cancelFunc();
106                 return;
107               }
108
109               var params = $scope.stateParams || {};
110               $state.transitionTo($scope.state, params, { location: true, inherit: true, relative: $state.$current, notify: true });
111
112             };
113           }]
114       };
115   });
116
117   common.directive('buttonSubmit', function(){
118     // Runs during compile
119     return {
120       restrict: 'E',
121       replace: true,
122       scope: {
123         'btnLabel': '@label',
124         'btnSize': '@size',
125         'btnGlyph': '@glyph',
126         'submitFunc': '=function',
127         'form': '=form',
128         'validator': '='
129       },
130       template: '<button class="btn btn-{{size}} btn-orange" ng-click="doSubmit()" ng-disabled="submitDisabled"><i class="icon-ok-sign"></i> {{label}}</button>',
131       controller: ['$scope', function ($scope) {
132         $scope.label = $scope.btnLabel || 'Submit';
133         $scope.size = $scope.btnSize || 'md';
134         $scope.glyph = $scope.btnGlyph || 'ok-circle';
135
136         $scope.submitDisabled = true;
137
138         $scope.doSubmit = function () {
139           if ($scope.submitFunc) {
140             $scope.submitFunc();
141           }
142         };
143
144         $scope.toggle = function (newVal) {
145           $scope.submitDisabled = newVal ? false : true;
146         };
147
148
149         // Setup a watch for form.$valid if it's passed
150         if (!$scope.validator && $scope.form) {
151           $scope.$watch('form.$valid', function (newVal, oldVal) {
152             $scope.toggle(newVal);
153           });
154         }
155
156         // This overrules the form watch if set - use with cauthion!
157         if ($scope.validator && angular.isFunction($scope.validator)) {
158           $scope.$watch(
159             function() {
160               return $scope.validator();
161             },
162             function(newVal, oldVal) {
163               $scope.toggle(newVal);
164             }
165           );
166         }
167
168         // Lastly if none of the above goes we'll just enable ourselves
169         if (!$scope.form && !$scope.validator) {
170           $scope.submitDisabled = false;
171         }
172       }]
173     };
174   });
175
176
177   common.directive('showSelected', function() {
178     // Runs during compile
179     return {
180       restrict: 'E',
181       replace: true,
182       scope: {
183         'data': '='
184       },
185       template: '<span>Selected: {{data.length}}</span>'
186     };
187   });
188
189   common.directive('ctrlReload', function() {
190     // Runs during compile
191     return {
192       replace: true,
193       restrict: 'E',
194       scope: {
195         svc: '=service'
196       },
197       template: '<button class="btn btn-primary btn-xs" ng-click="svc.getAll()"><i class="icon-refresh"></i></button>',
198       link: function ($scope, iElm, iAttrs, controller) {
199         $scope.$on('evt:refresh', function() {
200           $scope.svc.getAll();
201         });
202       }
203     };
204   });
205
206   common.directive('ctrlDelete', function($rootScope) {
207     // Runs during compile
208     return {
209       replace: true,
210       restrict: 'E',
211       template: '<button class="btn btn-danger btn-xs" ng-click="deleteSelected()" ng-disabled="gridOptions.selectedItems.length == 0"><i class="icon-remove"></i></button>',
212       link: function($scope, iElm, iAttrs, controller) {
213         var i = 0;
214         var selected = $scope.gridOptions.selectedItems;
215
216         // Fire up a evt:refresh event once done.
217         $scope.deleteSelected = function () {
218           angular.forEach(selected, function(value, key) {
219             $scope.svc.delete(value).then(
220               function () {
221                 i++;
222                 if (i == selected.length) {
223                   $rootScope.$broadcast('evt:refresh');
224                 }
225               }
226             );
227           });
228         };
229       }
230     };
231   });
232 });