X-Git-Url: https://gerrit.onap.org/r/gitweb?a=blobdiff_plain;f=sdnr%2Fwireless-transport%2Fcode-Carbon-SR1%2Fapps%2Fdlux%2Fdlux-web%2Fsrc%2Fcommon%2Fgeneral%2Fcommon.navigation.directives.js;fp=sdnr%2Fwireless-transport%2Fcode-Carbon-SR1%2Fapps%2Fdlux%2Fdlux-web%2Fsrc%2Fcommon%2Fgeneral%2Fcommon.navigation.directives.js;h=9c0dd7598fa7746ccbe4748445f9cd5514e2fac6;hb=27fb2d06608fbb070ae2c15a5580a4f5b2423d15;hp=0000000000000000000000000000000000000000;hpb=60315525ab5e7c12a9f47c409092e8dba6ad656d;p=ccsdk%2Fapps.git diff --git a/sdnr/wireless-transport/code-Carbon-SR1/apps/dlux/dlux-web/src/common/general/common.navigation.directives.js b/sdnr/wireless-transport/code-Carbon-SR1/apps/dlux/dlux-web/src/common/general/common.navigation.directives.js new file mode 100644 index 00000000..9c0dd759 --- /dev/null +++ b/sdnr/wireless-transport/code-Carbon-SR1/apps/dlux/dlux-web/src/common/general/common.navigation.directives.js @@ -0,0 +1,232 @@ +define(['common/general/common.navigation.module'], function(common) { + + /* + * Helper to set CSS class to active via ng-class using $location.path() + * or $state.includes() + */ + common.directive('isActive', function($compile) { + return { + restrict: 'A', + replace: false, + scope: { + state: '@', + stateParams: '=', + stateActive: '@', + url: '@' + }, + + controller: ['$scope', '$location', '$state', function ($scope, $location, $state) { + $scope.$state = $state; + $scope.$location = $location; + }], + compile: function() { + return function (scope, iElement, iAttrs, controller) { + var active; + if (scope.state) { + var state = scope.stateActive || scope.$state.current.name.split('.')[0]; + active = 'active: $state.includes(\'' + scope.state + '\')'; + } else if (scope.url) { + active = 'active: url === $location.path()'; + } else { + active = "false"; + } + iElement.attr('ng-class', '{ ' + active + ' }'); // Adding the ngClass + iElement.removeAttr('is-active'); // Avoid infinite loop + $compile(iElement)(scope); + }; + } + }; + }); + + + common.directive('brdAnchor', function ($compile, $rootScope) { + return { + restrict: 'E', + replace: true, + scope: { + label: '@', + state: '@', + stateParams: '=', + url: '@' + }, + + /* The idea is to support both state and url, to be able to set {active} either + if stateActive matches via $state.includes() or if the url matches + Change this into a actual href later on ? - see https://github.com/angular-ui/ui-router/issues/395 + */ + template: '{{label}}', + controller: ['$scope', '$rootScope', '$location', '$state', function ($scope, $rootScope, $location, $state) { + $scope.$location = $location; + $scope.$state = $state; + + $scope.doClick = function () { + var args = { + label: $scope.label, + state: $scope.state, + stateParams: $scope.stateParams, + url: $scope.url + }; + + $rootScope.$broadcast('event:navigation', args); + + if (!$scope.url && $scope.state) { + var params = $scope.stateParams || {}; + $state.transitionTo($scope.state, params, { location: true, inherit: true, relative: $state.$current, notify: true }); + } else if ($scope.url) { + $location.path($scope.url); + } + }; + }] + }; + }); + + + common.directive('buttonCancel', function() { + // Runs during compile + return { + restrict: 'E', + replace: true, + scope: { + 'btnLabel': '@label', + 'btnSize': '@size', + 'btnGlyph': '@glyph', + 'cancelFunc': '=function', + 'state': '@', + 'stateParams': '=', + }, + template: '', + controller: ['$scope', '$state', function ($scope, $state) { + $scope.label = $scope.btnLabel || 'Cancel'; + $scope.size = $scope.btnSize || 'md'; + $scope.glyph = $scope.btnGlyph || 'remove-circle'; + + $scope.doCancel = function () { + if (angular.isFunction($scope.cancelFunc)) { + $scope.cancelFunc(); + return; + } + + var params = $scope.stateParams || {}; + $state.transitionTo($scope.state, params, { location: true, inherit: true, relative: $state.$current, notify: true }); + + }; + }] + }; + }); + + common.directive('buttonSubmit', function(){ + // Runs during compile + return { + restrict: 'E', + replace: true, + scope: { + 'btnLabel': '@label', + 'btnSize': '@size', + 'btnGlyph': '@glyph', + 'submitFunc': '=function', + 'form': '=form', + 'validator': '=' + }, + template: '', + controller: ['$scope', function ($scope) { + $scope.label = $scope.btnLabel || 'Submit'; + $scope.size = $scope.btnSize || 'md'; + $scope.glyph = $scope.btnGlyph || 'ok-circle'; + + $scope.submitDisabled = true; + + $scope.doSubmit = function () { + if ($scope.submitFunc) { + $scope.submitFunc(); + } + }; + + $scope.toggle = function (newVal) { + $scope.submitDisabled = newVal ? false : true; + }; + + + // Setup a watch for form.$valid if it's passed + if (!$scope.validator && $scope.form) { + $scope.$watch('form.$valid', function (newVal, oldVal) { + $scope.toggle(newVal); + }); + } + + // This overrules the form watch if set - use with cauthion! + if ($scope.validator && angular.isFunction($scope.validator)) { + $scope.$watch( + function() { + return $scope.validator(); + }, + function(newVal, oldVal) { + $scope.toggle(newVal); + } + ); + } + + // Lastly if none of the above goes we'll just enable ourselves + if (!$scope.form && !$scope.validator) { + $scope.submitDisabled = false; + } + }] + }; + }); + + + common.directive('showSelected', function() { + // Runs during compile + return { + restrict: 'E', + replace: true, + scope: { + 'data': '=' + }, + template: 'Selected: {{data.length}}' + }; + }); + + common.directive('ctrlReload', function() { + // Runs during compile + return { + replace: true, + restrict: 'E', + scope: { + svc: '=service' + }, + template: '', + link: function ($scope, iElm, iAttrs, controller) { + $scope.$on('evt:refresh', function() { + $scope.svc.getAll(); + }); + } + }; + }); + + common.directive('ctrlDelete', function($rootScope) { + // Runs during compile + return { + replace: true, + restrict: 'E', + template: '', + link: function($scope, iElm, iAttrs, controller) { + var i = 0; + var selected = $scope.gridOptions.selectedItems; + + // Fire up a evt:refresh event once done. + $scope.deleteSelected = function () { + angular.forEach(selected, function(value, key) { + $scope.svc.delete(value).then( + function () { + i++; + if (i == selected.length) { + $rootScope.$broadcast('evt:refresh'); + } + } + ); + }); + }; + } + }; + }); +});