mirror of
https://github.com/Alfresco/alfresco-ng2-components.git
synced 2025-07-24 17:32:15 +00:00
@@ -0,0 +1,79 @@
|
||||
/*!
|
||||
* @license
|
||||
* Copyright 2016 Alfresco Software, Ltd.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
import { CUSTOM_ELEMENTS_SCHEMA } from '@angular/core';
|
||||
import { async, TestBed } from '@angular/core/testing';
|
||||
import { DataTableModule } from '@alfresco/core';
|
||||
import { MaterialModule } from '../../../material.module';
|
||||
import { DocumentListService } from '../../services/document-list.service';
|
||||
import { ContentActionModel } from './../../models/content-action.model';
|
||||
import { DocumentListComponent } from './../document-list.component';
|
||||
import { ContentActionListComponent } from './content-action-list.component';
|
||||
|
||||
describe('ContentColumnList', () => {
|
||||
|
||||
let documentList: DocumentListComponent;
|
||||
let actionList: ContentActionListComponent;
|
||||
|
||||
beforeEach(async(() => {
|
||||
TestBed.configureTestingModule({
|
||||
imports: [
|
||||
DataTableModule,
|
||||
MaterialModule
|
||||
],
|
||||
declarations: [
|
||||
DocumentListComponent
|
||||
],
|
||||
providers: [
|
||||
DocumentListService
|
||||
],
|
||||
schemas: [
|
||||
CUSTOM_ELEMENTS_SCHEMA
|
||||
]
|
||||
}).compileComponents();
|
||||
}));
|
||||
|
||||
beforeEach(() => {
|
||||
documentList = (TestBed.createComponent(DocumentListComponent).componentInstance as DocumentListComponent);
|
||||
actionList = new ContentActionListComponent(documentList);
|
||||
});
|
||||
|
||||
it('should register action', () => {
|
||||
spyOn(documentList.actions, 'push').and.callThrough();
|
||||
|
||||
let action = new ContentActionModel();
|
||||
let result = actionList.registerAction(action);
|
||||
|
||||
expect(result).toBeTruthy();
|
||||
expect(documentList.actions.push).toHaveBeenCalledWith(action);
|
||||
});
|
||||
|
||||
it('should require document list instance to register action', () => {
|
||||
actionList = new ContentActionListComponent(null);
|
||||
let action = new ContentActionModel();
|
||||
expect(actionList.registerAction(action)).toBeFalsy();
|
||||
});
|
||||
|
||||
it('should require action instance to register', () => {
|
||||
spyOn(documentList.actions, 'push').and.callThrough();
|
||||
let result = actionList.registerAction(null);
|
||||
|
||||
expect(result).toBeFalsy();
|
||||
expect(documentList.actions.push).not.toHaveBeenCalled();
|
||||
});
|
||||
|
||||
});
|
@@ -0,0 +1,45 @@
|
||||
/*!
|
||||
* @license
|
||||
* Copyright 2016 Alfresco Software, Ltd.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
/* tslint:disable:component-selector */
|
||||
|
||||
import { Component } from '@angular/core';
|
||||
|
||||
import { ContentActionModel } from './../../models/content-action.model';
|
||||
import { DocumentListComponent } from './../document-list.component';
|
||||
|
||||
@Component({
|
||||
selector: 'content-actions',
|
||||
template: ''
|
||||
})
|
||||
export class ContentActionListComponent {
|
||||
|
||||
constructor(private documentList: DocumentListComponent) {
|
||||
}
|
||||
|
||||
/**
|
||||
* Registers action handler within the parent document list component.
|
||||
* @param action Action model to register.
|
||||
*/
|
||||
registerAction(action: ContentActionModel): boolean {
|
||||
if (this.documentList && action) {
|
||||
this.documentList.actions.push(action);
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
}
|
@@ -0,0 +1,294 @@
|
||||
/*!
|
||||
* @license
|
||||
* Copyright 2016 Alfresco Software, Ltd.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
import { CUSTOM_ELEMENTS_SCHEMA } from '@angular/core';
|
||||
import { EventEmitter } from '@angular/core';
|
||||
import { async, TestBed } from '@angular/core/testing';
|
||||
import { ContentService, TranslationService, NotificationService } from '@alfresco/core';
|
||||
import { DataTableModule } from '@alfresco/core';
|
||||
import { MaterialModule } from '../../../material.module';
|
||||
|
||||
import { DocumentListService } from '../../services/document-list.service';
|
||||
import { FileNode } from '../../../mock';
|
||||
import { ContentActionHandler } from './../../models/content-action.model';
|
||||
import { DocumentActionsService } from './../../services/document-actions.service';
|
||||
import { FolderActionsService } from './../../services/folder-actions.service';
|
||||
import { NodeActionsService } from './../../services/node-actions.service';
|
||||
import { DocumentListComponent } from './../document-list.component';
|
||||
import { ContentActionListComponent } from './content-action-list.component';
|
||||
import { ContentActionComponent } from './content-action.component';
|
||||
|
||||
describe('ContentAction', () => {
|
||||
|
||||
let documentList: DocumentListComponent;
|
||||
let actionList: ContentActionListComponent;
|
||||
let documentActions: DocumentActionsService;
|
||||
let folderActions: FolderActionsService;
|
||||
|
||||
let contentService: ContentService;
|
||||
let translateService: TranslationService;
|
||||
let notificationService: NotificationService;
|
||||
let nodeActionsService: NodeActionsService;
|
||||
|
||||
beforeEach(async(() => {
|
||||
TestBed.configureTestingModule({
|
||||
imports: [
|
||||
DataTableModule,
|
||||
MaterialModule
|
||||
],
|
||||
providers: [
|
||||
DocumentListService
|
||||
],
|
||||
declarations: [
|
||||
DocumentListComponent
|
||||
],
|
||||
schemas: [
|
||||
CUSTOM_ELEMENTS_SCHEMA
|
||||
]
|
||||
}).compileComponents();
|
||||
}));
|
||||
|
||||
beforeEach(() => {
|
||||
contentService = TestBed.get(ContentService);
|
||||
translateService = <TranslationService> { addTranslationFolder: () => {}};
|
||||
nodeActionsService = new NodeActionsService(null, null, null);
|
||||
notificationService = new NotificationService(null);
|
||||
documentActions = new DocumentActionsService(nodeActionsService);
|
||||
folderActions = new FolderActionsService(nodeActionsService, null, contentService);
|
||||
|
||||
documentList = (TestBed.createComponent(DocumentListComponent).componentInstance as DocumentListComponent);
|
||||
actionList = new ContentActionListComponent(documentList);
|
||||
});
|
||||
|
||||
it('should register within parent actions list', () => {
|
||||
spyOn(actionList, 'registerAction').and.stub();
|
||||
|
||||
let action = new ContentActionComponent(actionList, null, null);
|
||||
action.ngOnInit();
|
||||
|
||||
expect(actionList.registerAction).toHaveBeenCalled();
|
||||
});
|
||||
|
||||
it('should setup and register model', () => {
|
||||
let action = new ContentActionComponent(actionList, null, null);
|
||||
action.target = 'document';
|
||||
action.title = '<title>';
|
||||
action.icon = '<icon>';
|
||||
|
||||
expect(documentList.actions.length).toBe(0);
|
||||
action.ngOnInit();
|
||||
|
||||
expect(documentList.actions.length).toBe(1);
|
||||
|
||||
let model = documentList.actions[0];
|
||||
expect(model.target).toBe(action.target);
|
||||
expect(model.title).toBe(action.title);
|
||||
expect(model.icon).toBe(action.icon);
|
||||
});
|
||||
|
||||
it('should get action handler from document actions service', () => {
|
||||
|
||||
let handler = function () {
|
||||
};
|
||||
spyOn(documentActions, 'getHandler').and.returnValue(handler);
|
||||
|
||||
let action = new ContentActionComponent(actionList, documentActions, null);
|
||||
action.target = 'document';
|
||||
action.handler = '<handler>';
|
||||
action.ngOnInit();
|
||||
|
||||
expect(documentActions.getHandler).toHaveBeenCalledWith(action.handler);
|
||||
expect(documentList.actions.length).toBe(1);
|
||||
|
||||
let model = documentList.actions[0];
|
||||
expect(model.handler).toBe(handler);
|
||||
});
|
||||
|
||||
it('should get action handler from folder actions service', () => {
|
||||
let handler = function () {
|
||||
};
|
||||
spyOn(folderActions, 'getHandler').and.returnValue(handler);
|
||||
|
||||
let action = new ContentActionComponent(actionList, null, folderActions);
|
||||
action.target = 'folder';
|
||||
action.handler = '<handler>';
|
||||
action.ngOnInit();
|
||||
|
||||
expect(folderActions.getHandler).toHaveBeenCalledWith(action.handler);
|
||||
expect(documentList.actions.length).toBe(1);
|
||||
|
||||
let model = documentList.actions[0];
|
||||
expect(model.handler).toBe(handler);
|
||||
});
|
||||
|
||||
it('should require target to get system handler', () => {
|
||||
spyOn(folderActions, 'getHandler').and.stub();
|
||||
spyOn(documentActions, 'getHandler').and.stub();
|
||||
|
||||
let action = new ContentActionComponent(actionList, documentActions, folderActions);
|
||||
action.handler = '<handler>';
|
||||
|
||||
action.ngOnInit();
|
||||
expect(documentList.actions.length).toBe(1);
|
||||
expect(folderActions.getHandler).not.toHaveBeenCalled();
|
||||
expect(documentActions.getHandler).not.toHaveBeenCalled();
|
||||
|
||||
action.target = 'document';
|
||||
action.ngOnInit();
|
||||
expect(documentActions.getHandler).toHaveBeenCalled();
|
||||
|
||||
action.target = 'folder';
|
||||
action.ngOnInit();
|
||||
expect(folderActions.getHandler).toHaveBeenCalled();
|
||||
});
|
||||
|
||||
it('should be case insensitive for document target', () => {
|
||||
spyOn(documentActions, 'getHandler').and.stub();
|
||||
|
||||
let action = new ContentActionComponent(actionList, documentActions, null);
|
||||
action.target = 'DoCuMeNt';
|
||||
action.handler = '<handler>';
|
||||
|
||||
action.ngOnInit();
|
||||
expect(documentActions.getHandler).toHaveBeenCalledWith(action.handler);
|
||||
});
|
||||
|
||||
it('should be case insensitive for folder target', () => {
|
||||
spyOn(folderActions, 'getHandler').and.stub();
|
||||
|
||||
let action = new ContentActionComponent(actionList, null, folderActions);
|
||||
action.target = 'FoLdEr';
|
||||
action.handler = '<handler>';
|
||||
|
||||
action.ngOnInit();
|
||||
expect(folderActions.getHandler).toHaveBeenCalledWith(action.handler);
|
||||
});
|
||||
|
||||
it('should use custom "execute" emitter', (done) => {
|
||||
let emitter = new EventEmitter();
|
||||
|
||||
emitter.subscribe(e => {
|
||||
expect(e.value).toBe('<obj>');
|
||||
done();
|
||||
});
|
||||
|
||||
let action = new ContentActionComponent(actionList, null, null);
|
||||
action.target = 'document';
|
||||
action.execute = emitter;
|
||||
|
||||
action.ngOnInit();
|
||||
expect(documentList.actions.length).toBe(1);
|
||||
|
||||
let model = documentList.actions[0];
|
||||
model.execute('<obj>');
|
||||
});
|
||||
|
||||
it('should sync localizable fields with model', () => {
|
||||
|
||||
let action = new ContentActionComponent(actionList, null, null);
|
||||
action.title = 'title1';
|
||||
action.ngOnInit();
|
||||
|
||||
expect(action.model.title).toBe(action.title);
|
||||
|
||||
action.title = 'title2';
|
||||
action.ngOnChanges(null);
|
||||
|
||||
expect(action.model.title).toBe('title2');
|
||||
});
|
||||
|
||||
it('should not find document action handler with missing service', () => {
|
||||
let action = new ContentActionComponent(actionList, null, null);
|
||||
expect(action.getSystemHandler('document', 'name')).toBeNull();
|
||||
});
|
||||
|
||||
it('should not find folder action handler with missing service', () => {
|
||||
let action = new ContentActionComponent(actionList, null, null);
|
||||
expect(action.getSystemHandler('folder', 'name')).toBeNull();
|
||||
});
|
||||
|
||||
it('should find document action handler via service', () => {
|
||||
let handler = <ContentActionHandler> function (obj: any, target?: any) {
|
||||
};
|
||||
let action = new ContentActionComponent(actionList, documentActions, null);
|
||||
spyOn(documentActions, 'getHandler').and.returnValue(handler);
|
||||
expect(action.getSystemHandler('document', 'name')).toBe(handler);
|
||||
});
|
||||
|
||||
it('should find folder action handler via service', () => {
|
||||
let handler = <ContentActionHandler> function (obj: any, target?: any) {
|
||||
};
|
||||
let action = new ContentActionComponent(actionList, null, folderActions);
|
||||
spyOn(folderActions, 'getHandler').and.returnValue(handler);
|
||||
expect(action.getSystemHandler('folder', 'name')).toBe(handler);
|
||||
});
|
||||
|
||||
it('should not find actions for unknown target type', () => {
|
||||
spyOn(folderActions, 'getHandler').and.stub();
|
||||
spyOn(documentActions, 'getHandler').and.stub();
|
||||
|
||||
let action = new ContentActionComponent(actionList, documentActions, folderActions);
|
||||
|
||||
expect(action.getSystemHandler('unknown', 'name')).toBeNull();
|
||||
expect(folderActions.getHandler).not.toHaveBeenCalled();
|
||||
expect(documentActions.getHandler).not.toHaveBeenCalled();
|
||||
|
||||
});
|
||||
|
||||
it('should wire model with custom event handler', (done) => {
|
||||
let action = new ContentActionComponent(actionList, documentActions, folderActions);
|
||||
let file = new FileNode();
|
||||
|
||||
let handler = new EventEmitter();
|
||||
handler.subscribe((e) => {
|
||||
expect(e.value).toBe(file);
|
||||
done();
|
||||
});
|
||||
|
||||
action.execute = handler;
|
||||
|
||||
action.ngOnInit();
|
||||
action.model.execute(file);
|
||||
});
|
||||
|
||||
it('should allow registering model without handler', () => {
|
||||
let action = new ContentActionComponent(actionList, documentActions, folderActions);
|
||||
|
||||
spyOn(actionList, 'registerAction').and.callThrough();
|
||||
action.execute = null;
|
||||
action.ngOnInit();
|
||||
|
||||
expect(action.model.handler).toBeUndefined();
|
||||
expect(actionList.registerAction).toHaveBeenCalledWith(action.model);
|
||||
});
|
||||
|
||||
it('should register on init', () => {
|
||||
let action = new ContentActionComponent(actionList, null, null);
|
||||
spyOn(action, 'register').and.callThrough();
|
||||
|
||||
action.ngOnInit();
|
||||
expect(action.register).toHaveBeenCalled();
|
||||
});
|
||||
|
||||
it('should require action list to register action with', () => {
|
||||
let action = new ContentActionComponent(actionList, null, null);
|
||||
expect(action.register()).toBeTruthy();
|
||||
|
||||
action = new ContentActionComponent(null, null, null);
|
||||
expect(action.register()).toBeFalsy();
|
||||
});
|
||||
});
|
@@ -0,0 +1,159 @@
|
||||
/*!
|
||||
* @license
|
||||
* Copyright 2016 Alfresco Software, Ltd.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
/* tslint:disable:component-selector */
|
||||
|
||||
import { Component, EventEmitter, Input, OnChanges, OnInit, Output } from '@angular/core';
|
||||
|
||||
import { ContentActionHandler } from '../../models/content-action.model';
|
||||
import { DocumentActionsService } from '../../services/document-actions.service';
|
||||
import { FolderActionsService } from '../../services/folder-actions.service';
|
||||
import { ContentActionModel } from './../../models/content-action.model';
|
||||
import { ContentActionListComponent } from './content-action-list.component';
|
||||
|
||||
@Component({
|
||||
selector: 'content-action',
|
||||
template: '',
|
||||
providers: [
|
||||
DocumentActionsService,
|
||||
FolderActionsService
|
||||
]
|
||||
})
|
||||
export class ContentActionComponent implements OnInit, OnChanges {
|
||||
|
||||
@Input()
|
||||
title: string = 'Action';
|
||||
|
||||
@Input()
|
||||
icon: string;
|
||||
|
||||
@Input()
|
||||
handler: string;
|
||||
|
||||
@Input()
|
||||
target: string;
|
||||
|
||||
@Input()
|
||||
permission: string;
|
||||
|
||||
@Input()
|
||||
disableWithNoPermission: boolean;
|
||||
|
||||
@Input()
|
||||
disabled: boolean = false;
|
||||
|
||||
@Output()
|
||||
execute = new EventEmitter();
|
||||
|
||||
@Output()
|
||||
permissionEvent = new EventEmitter();
|
||||
|
||||
@Output()
|
||||
error = new EventEmitter();
|
||||
|
||||
@Output()
|
||||
success = new EventEmitter();
|
||||
|
||||
model: ContentActionModel;
|
||||
|
||||
constructor(
|
||||
private list: ContentActionListComponent,
|
||||
private documentActions: DocumentActionsService,
|
||||
private folderActions: FolderActionsService) {
|
||||
this.model = new ContentActionModel();
|
||||
}
|
||||
|
||||
ngOnInit() {
|
||||
this.model = new ContentActionModel({
|
||||
title: this.title,
|
||||
icon: this.icon,
|
||||
permission: this.permission,
|
||||
disableWithNoPermission: this.disableWithNoPermission,
|
||||
target: this.target,
|
||||
disabled: this.disabled
|
||||
});
|
||||
|
||||
if (this.handler) {
|
||||
this.model.handler = this.getSystemHandler(this.target, this.handler);
|
||||
}
|
||||
|
||||
if (this.execute) {
|
||||
this.model.execute = (value: any): void => {
|
||||
this.execute.emit({ value });
|
||||
};
|
||||
}
|
||||
|
||||
this.register();
|
||||
}
|
||||
|
||||
register(): boolean {
|
||||
if (this.list) {
|
||||
return this.list.registerAction(this.model);
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
ngOnChanges(changes) {
|
||||
// update localizable properties
|
||||
this.model.title = this.title;
|
||||
}
|
||||
|
||||
getSystemHandler(target: string, name: string): ContentActionHandler {
|
||||
if (target) {
|
||||
let ltarget = target.toLowerCase();
|
||||
|
||||
if (ltarget === 'document') {
|
||||
if (this.documentActions) {
|
||||
this.documentActions.permissionEvent.subscribe((permision) => {
|
||||
this.permissionEvent.emit(permision);
|
||||
});
|
||||
|
||||
this.documentActions.error.subscribe((errors) => {
|
||||
this.error.emit(errors);
|
||||
});
|
||||
|
||||
this.documentActions.success.subscribe((message) => {
|
||||
this.success.emit(message);
|
||||
});
|
||||
|
||||
return this.documentActions.getHandler(name);
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
if (ltarget === 'folder') {
|
||||
if (this.folderActions) {
|
||||
this.folderActions.permissionEvent.subscribe((permision) => {
|
||||
this.permissionEvent.emit(permision);
|
||||
});
|
||||
|
||||
this.folderActions.error.subscribe((errors) => {
|
||||
this.error.emit(errors);
|
||||
});
|
||||
|
||||
this.folderActions.success.subscribe((message) => {
|
||||
this.success.emit(message);
|
||||
});
|
||||
|
||||
return this.folderActions.getHandler(name);
|
||||
}
|
||||
return null;
|
||||
}
|
||||
}
|
||||
return null;
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user