Skip to content

Commit

Permalink
feat: #387 - Configuration of default directive behaviour
Browse files Browse the repository at this point in the history
  • Loading branch information
masterspambot committed Mar 15, 2017
1 parent 5c1d0bd commit daf9be9
Show file tree
Hide file tree
Showing 5 changed files with 107 additions and 3 deletions.
1 change: 1 addition & 0 deletions config/grunt/concat.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ module.exports = function () {
files: {
'<%= paths.dist %>/<%= pkg.name %>.js': [
'<%= paths.src %>/permission/permission.js',
'<%= paths.src %>/permission/permissionConfig.js',
'<%= paths.src %>/permission/decorators/$q.js',
'<%= paths.src %>/permission/strategies/PermissionStrategies.js',
'<%= paths.src %>/permission/transition/TransitionProperties.js',
Expand Down
8 changes: 5 additions & 3 deletions src/permission/directives/permission.js
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ function PermissionDirective($log, $injector, PermPermissionMap, PermPermissionS
onUnauthorized: '&?permissionOnUnauthorized'
},
controllerAs: 'permission',
controller: function ($scope, $element) {
controller: function ($scope, $element, permissionConfig) {
var permission = this;

$scope.$watchGroup(['permission.only', 'permission.except', 'sref'],
Expand Down Expand Up @@ -118,7 +118,8 @@ function PermissionDirective($log, $injector, PermPermissionMap, PermPermissionS
if (angular.isFunction(permission.onAuthorized)) {
permission.onAuthorized()($element);
} else {
PermPermissionStrategies.showElement($element);
var onAuthorizedMethodName = permissionConfig.defaultOnAuthorizedMethod;
PermPermissionStrategies[onAuthorizedMethodName]($element);
}
}

Expand All @@ -130,7 +131,8 @@ function PermissionDirective($log, $injector, PermPermissionMap, PermPermissionS
if (angular.isFunction(permission.onUnauthorized)) {
permission.onUnauthorized()($element);
} else {
PermPermissionStrategies.hideElement($element);
var onUnauthorizedMethodName = permissionConfig.defaultOnUnauthorizedMethod;
PermPermissionStrategies[onUnauthorizedMethodName]($element);
}
}
}
Expand Down
1 change: 1 addition & 0 deletions src/permission/permission.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

var permission = angular.module('permission', []);

/* istanbul ignore if */
if (typeof module !== 'undefined' && typeof exports !== 'undefined' && module.exports === exports) {
module.exports = permission.name;
}
39 changes: 39 additions & 0 deletions src/permission/permissionConfig.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
'use strict';

/**
* Permission module configuration provider
*
* @name permission.permissionConfigProvider
*/
function permissionConfig() {
'ngInject';

this.defaultOnAuthorizedMethod = 'showElement';
this.defaultOnUnauthorizedMethod = 'hideElement';

/**
* Methods allowing to alter default directive onAuthorized behaviour in permission directive
*
* @param onAuthorizedMethod {String} One of permission.PermPermissionStrategies method names
*/
this.setDefaultOnAuthorizedMethod = function (onAuthorizedMethod) {
this.defaultOnAuthorizedMethod = onAuthorizedMethod;
};

/**
* Methods allowing to alter default directive onUnauthorized behaviour in permission directive
*
* @param onUnauthorizedMethod {String} One of permission.PermPermissionStrategies method names
*/
this.setDefaultOnUnauthorizedMethod = function (onUnauthorizedMethod) {
this.defaultOnUnauthorizedMethod = onUnauthorizedMethod;
};

this.$get = function () {
return this;
}
}

angular
.module('permission')
.provider('permissionConfig', permissionConfig);
61 changes: 61 additions & 0 deletions test/unit/permission/configuration/permissionConfig.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
describe('permission', function () {
'use strict';

describe('configuration', function () {
describe('provider: permissionConfig', function () {
var permissionConfigProvider;

beforeEach(function () {
module('permission');
});

beforeEach(function () {
module(['permissionConfigProvider', function (_permissionConfigProvider) {
permissionConfigProvider = _permissionConfigProvider;
}]);
});

beforeEach(inject());

describe('method: setDefaultOnAuthorizedMethod', function () {
it('should have default authorized method set to showElement', function () {
// GIVEN
// WHEN
//THEN
expect(permissionConfigProvider.$get().defaultOnAuthorizedMethod).toBe('showElement');
});

it('should set custom default authorized method when provided', function () {
// GIVEN
var onAuthorizedMethodName = 'customOnAuthorizedMethod';

// WHEN
permissionConfigProvider.setDefaultOnAuthorizedMethod(onAuthorizedMethodName);

//THEN
expect(permissionConfigProvider.$get().defaultOnAuthorizedMethod).toBe(onAuthorizedMethodName);
});
});

describe('method: setDefaultOnUnauthorizedMethod', function () {
it('should have default unauthorized method set to hideElement', function () {
// GIVEN
// WHEN
//THEN
expect(permissionConfigProvider.$get().defaultOnUnauthorizedMethod).toBe('hideElement');
});

it('should set custom default unauthorized method when provided', function () {
// GIVEN
var onUnauthorizedMethodName = 'customOnUnauthorizedMethod';

// WHEN
permissionConfigProvider.setDefaultOnUnauthorizedMethod(onUnauthorizedMethodName);

//defaultOnUnauthorizedMethod
expect(permissionConfigProvider.$get().defaultOnUnauthorizedMethod).toBe(onUnauthorizedMethodName);
});
});
});
});
});

0 comments on commit daf9be9

Please sign in to comment.