[ADF-2859] conditional evaluation of disabled state for content actions (#3450)

* react on [disabled] binding changes

* [disabled] binding updates for context menu items

* evaluating disabled state with a function

* unit test

* restore original description

* remove irrelevant test

* fix tests
This commit is contained in:
Denys Vuika
2018-06-07 23:28:01 +01:00
committed by Eugenio Romano
parent 08fd49c4e3
commit cb88a22a76
10 changed files with 199 additions and 93 deletions

View File

@@ -66,7 +66,7 @@ export class ContentActionComponent implements OnInit, OnChanges, OnDestroy {
/** Is the menu item disabled? */
@Input()
disabled: boolean = false;
disabled: boolean | Function = false;
/** Emitted when the user selects the action from the menu. */
@Output()
@@ -117,6 +117,15 @@ export class ContentActionComponent implements OnInit, OnChanges, OnDestroy {
this.folderActionModel.visible = changes.visible.currentValue;
}
}
if (changes.disabled && !changes.disabled.firstChange) {
if (this.documentActionModel) {
this.documentActionModel.disabled = changes.disabled.currentValue;
}
if (this.folderActionModel) {
this.folderActionModel.disabled = changes.disabled.currentValue;
}
}
}
ngOnDestroy() {

View File

@@ -359,6 +359,28 @@ describe('DocumentList', () => {
expect(actions[0].title).toBe('Action1');
});
it('should evaluate conditional disabled state for content action', () => {
documentList.actions = [
new ContentActionModel({
target: 'document',
title: 'Action1',
disabled: (): boolean => true
}),
new ContentActionModel({
target: 'document',
title: 'Action2',
disabled: (): boolean => false
})
];
const nodeFile = { entry: { isFile: true, name: 'xyz' } };
const actions = documentList.getNodeActions(nodeFile);
expect(actions.length).toBe(2);
expect(actions[0].disabled).toBeTruthy();
expect(actions[1].disabled).toBeFalsy();
});
it('should not disable the action if there is copy permission', () => {
let documentMenu = new ContentActionModel({
disableWithNoPermission: true,
@@ -418,7 +440,7 @@ describe('DocumentList', () => {
let actions = documentList.getNodeActions(nodeFile);
expect(actions.length).toBe(1);
expect(actions[0].title).toEqual('FileAction');
expect(actions[0].disabled).toBeUndefined();
expect(actions[0].disabled).toBeFalsy();
});
it('should not disable the action if there is the right permission for the folder', () => {
@@ -438,7 +460,7 @@ describe('DocumentList', () => {
let actions = documentList.getNodeActions(nodeFile);
expect(actions.length).toBe(1);
expect(actions[0].title).toEqual('FolderAction');
expect(actions[0].disabled).toBeUndefined();
expect(actions[0].disabled).toBeFalsy();
});
it('should not disable the action if there are no permissions for the file and disable with no permission is false', () => {
@@ -458,7 +480,7 @@ describe('DocumentList', () => {
let actions = documentList.getNodeActions(nodeFile);
expect(actions.length).toBe(1);
expect(actions[0].title).toEqual('FileAction');
expect(actions[0].disabled).toBeUndefined();
expect(actions[0].disabled).toBeFalsy();
});
it('should not disable the action if there are no permissions for the folder and disable with no permission is false', () => {
@@ -478,7 +500,7 @@ describe('DocumentList', () => {
let actions = documentList.getNodeActions(nodeFile);
expect(actions.length).toBe(1);
expect(actions[0].title).toEqual('FolderAction');
expect(actions[0].disabled).toBeUndefined();
expect(actions[0].disabled).toBeFalsy();
});
it('should disable the action if there are no permissions for the file and disable with no permission is true', () => {

View File

@@ -465,7 +465,7 @@ export class DocumentListComponent implements OnInit, OnChanges, OnDestroy, Afte
.map(action => new ContentActionModel(action));
actionsByTarget.forEach((action) => {
this.disableActionsWithNoPermissions(node, action);
action.disabled = this.isActionDisabled(action, node);
});
return actionsByTarget;
@@ -475,10 +475,16 @@ export class DocumentListComponent implements OnInit, OnChanges, OnDestroy, Afte
return [];
}
disableActionsWithNoPermissions(node: MinimalNodeEntity, action: ContentActionModel) {
if (action.permission && action.disableWithNoPermission && !this.contentService.hasPermission(node.entry, action.permission)) {
action.disabled = true;
private isActionDisabled(action: ContentActionModel, node: MinimalNodeEntity): boolean {
if (typeof action.disabled === 'function') {
return action.disabled(node);
}
if (action.permission && action.disableWithNoPermission && !this.contentService.hasPermission(node.entry, action.permission)) {
return true;
}
return false;
}
@HostListener('contextmenu', ['$event'])

View File

@@ -23,7 +23,7 @@ export class ContentActionModel {
target: string;
permission: string;
disableWithNoPermission: boolean = false;
disabled: boolean = false;
disabled: boolean | Function = false;
visible: boolean | Function = true;
constructor(obj?: any) {
@@ -35,7 +35,10 @@ export class ContentActionModel {
this.target = obj.target;
this.permission = obj.permission;
this.disableWithNoPermission = obj.disableWithNoPermission;
this.disabled = obj.disabled;
if (obj.hasOwnProperty('disabled')) {
this.disabled = obj.disabled;
}
if (obj.hasOwnProperty('visible')) {
this.visible = obj.visible;