split extension actions into separate service (#512)

* move action management to a separate ActionService

* code improvements and registration chaining

* code fixes
This commit is contained in:
Denys Vuika
2018-07-18 05:16:10 +01:00
committed by Cilibiu Bogdan
parent 5d8a9057bc
commit 79a20c65fb
10 changed files with 262 additions and 159 deletions

View File

@@ -78,13 +78,11 @@ import { MaterialModule } from './material.module';
import { ExperimentalDirective } from './directives/experimental.directive';
import { ContentApiService } from './services/content-api.service';
import { ExtensionsModule } from './extensions.module';
import { ExtensionService } from './extensions/extension.service';
import { CoreExtensionsModule } from './extensions/core.extensions';
import { CoreExtensionsModule } from './extensions/core.extensions.module';
import { SearchResultsRowComponent } from './components/search/search-results-row/search-results-row.component';
import { NodePermissionsDialogComponent } from './dialogs/node-permissions/node-permissions.dialog';
import { NodePermissionsDirective } from './common/directives/node-permissions.directive';
import { PermissionsManagerComponent } from './components/permission-manager/permissions-manager.component';
import { RuleService } from './extensions/rules/rule.service';
@NgModule({
imports: [
@@ -159,9 +157,7 @@ import { RuleService } from './extensions/rules/rule.service';
NodePermissionService,
ProfileResolver,
ExperimentalGuard,
ContentApiService,
ExtensionService,
RuleService
ContentApiService
],
entryComponents: [
LibraryDialogComponent,

View File

@@ -23,7 +23,7 @@
* along with Alfresco. If not, see <http://www.gnu.org/licenses/>.
*/
export interface ActionExtension {
export interface ActionRef {
id: string;
type: string;
payload?: string;

View File

@@ -0,0 +1,141 @@
/*!
* @license
* Alfresco Example Content Application
*
* Copyright (C) 2005 - 2018 Alfresco Software Limited
*
* This file is part of the Alfresco Example Content Application.
* If the software was purchased under a paid Alfresco license, the terms of
* the paid license agreement will prevail. Otherwise, the software is
* provided under the following open source license terms:
*
* The Alfresco Example Content Application is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* The Alfresco Example Content Application is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with Alfresco. If not, see <http://www.gnu.org/licenses/>.
*/
import { AppConfigService } from '@alfresco/adf-core';
import { ActionService } from './action.service';
import { Store } from '@ngrx/store';
import { AppStore } from '../../store/states';
import { TestBed } from '@angular/core/testing';
import { AppTestingModule } from '../../testing/app-testing.module';
describe('ActionService', () => {
let config: AppConfigService;
let actions: ActionService;
let store: Store<AppStore>;
beforeEach(() => {
TestBed.configureTestingModule({
imports: [AppTestingModule]
});
actions = TestBed.get(ActionService);
store = TestBed.get(Store);
config = TestBed.get(AppConfigService);
config.config['extensions'] = {};
});
describe('actions', () => {
beforeEach(() => {
config.config.extensions = {
core: {
actions: [
{
id: 'aca:actions/create-folder',
type: 'CREATE_FOLDER',
payload: 'folder-name'
}
]
}
};
});
it('should load actions from the config', () => {
actions.init();
expect(actions.actions.length).toBe(1);
});
it('should have an empty action list if config provides nothing', () => {
config.config.extensions = {};
actions.init();
expect(actions.actions).toEqual([]);
});
it('should find action by id', () => {
actions.init();
const action = actions.getActionById(
'aca:actions/create-folder'
);
expect(action).toBeTruthy();
expect(action.type).toBe('CREATE_FOLDER');
expect(action.payload).toBe('folder-name');
});
it('should not find action by id', () => {
actions.init();
const action = actions.getActionById('missing');
expect(action).toBeFalsy();
});
it('should run the action via store', () => {
actions.init();
spyOn(store, 'dispatch').and.stub();
actions.runActionById('aca:actions/create-folder');
expect(store.dispatch).toHaveBeenCalledWith({
type: 'CREATE_FOLDER',
payload: 'folder-name'
});
});
it('should not use store if action is missing', () => {
actions.init();
spyOn(store, 'dispatch').and.stub();
actions.runActionById('missing');
expect(store.dispatch).not.toHaveBeenCalled();
});
});
describe('expressions', () => {
it('should eval static value', () => {
const value = actions.runExpression('hello world');
expect(value).toBe('hello world');
});
it('should eval string as an expression', () => {
const value = actions.runExpression('$( "hello world" )');
expect(value).toBe('hello world');
});
it('should eval expression with no context', () => {
const value = actions.runExpression('$( 1 + 1 )');
expect(value).toBe(2);
});
it('should eval expression with context', () => {
const context = {
a: 'hey',
b: 'there'
};
const expression = '$( context.a + " " + context.b + "!" )';
const value = actions.runExpression(expression, context);
expect(value).toBe('hey there!');
});
});
});

View File

@@ -0,0 +1,76 @@
/*!
* @license
* Alfresco Example Content Application
*
* Copyright (C) 2005 - 2018 Alfresco Software Limited
*
* This file is part of the Alfresco Example Content Application.
* If the software was purchased under a paid Alfresco license, the terms of
* the paid license agreement will prevail. Otherwise, the software is
* provided under the following open source license terms:
*
* The Alfresco Example Content Application is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* The Alfresco Example Content Application is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with Alfresco. If not, see <http://www.gnu.org/licenses/>.
*/
import { Injectable } from '@angular/core';
import { AppConfigService } from '@alfresco/adf-core';
import { Store } from '@ngrx/store';
import { AppStore } from '../../store/states';
import { ActionRef } from './action-ref';
@Injectable()
export class ActionService {
actions: Array<ActionRef> = [];
constructor(
private config: AppConfigService,
private store: Store<AppStore>
) {}
init() {
this.actions = this.config.get<Array<ActionRef>>(
'extensions.core.actions',
[]
);
}
getActionById(id: string): ActionRef {
return this.actions.find(action => action.id === id);
}
runActionById(id: string, context?: any) {
const action = this.getActionById(id);
if (action) {
const { type, payload } = action;
const expression = this.runExpression(payload, context);
this.store.dispatch({ type, payload: expression });
}
}
runExpression(value: string, context?: any) {
const pattern = new RegExp(/\$\((.*\)?)\)/g);
const matches = pattern.exec(value);
if (matches && matches.length > 1) {
const expression = matches[1];
const fn = new Function('context', `return ${expression}`);
const result = fn(context);
return result;
}
return value;
}
}

View File

@@ -30,20 +30,21 @@ import { AboutComponent } from '../components/about/about.component';
import { LayoutComponent } from '../components/layout/layout.component';
import { ToolbarActionComponent } from './components/toolbar-action/toolbar-action.component';
import { CommonModule } from '@angular/common';
import { RuleService } from './rules/rule.service';
import { ActionService } from './actions/action.service';
@NgModule({
imports: [
CommonModule,
CoreModule.forChild()
],
imports: [CommonModule, CoreModule.forChild()],
declarations: [ToolbarActionComponent],
exports: [ToolbarActionComponent],
entryComponents: [AboutComponent]
entryComponents: [AboutComponent],
providers: [ExtensionService, RuleService, ActionService]
})
export class CoreExtensionsModule {
constructor(extensions: ExtensionService) {
extensions.components['aca:layouts/main'] = LayoutComponent;
extensions.components['aca:components/about'] = AboutComponent;
extensions.authGuards['aca:auth'] = AuthGuardEcm;
extensions
.setComponent('aca:layouts/main', LayoutComponent)
.setComponent('aca:components/about', AboutComponent)
.setAuthGuard('aca:auth', AuthGuardEcm);
}
}

View File

@@ -23,13 +23,13 @@
* along with Alfresco. If not, see <http://www.gnu.org/licenses/>.
*/
import { RouteExtension } from './route.extension';
import { ActionExtension } from './action.extension';
import { RuleRef } from './rules/rule-ref';
import { ActionRef } from './actions/action-ref';
import { RouteRef } from './route-ref';
export interface ExtensionConfig {
rules?: Array<RuleRef>;
routes?: Array<RouteExtension>;
actions?: Array<ActionExtension>;
routes?: Array<RouteRef>;
actions?: Array<ActionRef>;
features?: { [key: string]: any };
}

View File

@@ -27,14 +27,11 @@ import { TestBed } from '@angular/core/testing';
import { AppTestingModule } from '../testing/app-testing.module';
import { ExtensionService } from './extension.service';
import { AppConfigService } from '@alfresco/adf-core';
import { Store } from '@ngrx/store';
import { AppStore } from '../store/states';
import { ContentActionType } from './content-action.extension';
describe('ExtensionService', () => {
let config: AppConfigService;
let extensions: ExtensionService;
let store: Store<AppStore>;
beforeEach(() => {
TestBed.configureTestingModule({
@@ -42,7 +39,6 @@ describe('ExtensionService', () => {
});
extensions = TestBed.get(ExtensionService);
store = TestBed.get(Store);
config = TestBed.get(AppConfigService);
config.config['extensions'] = {};
@@ -168,71 +164,6 @@ describe('ExtensionService', () => {
});
});
describe('actions', () => {
beforeEach(() => {
config.config.extensions = {
core: {
actions: [
{
id: 'aca:actions/create-folder',
type: 'CREATE_FOLDER',
payload: 'folder-name'
}
]
}
};
});
it('should load actions from the config', () => {
extensions.init();
expect(extensions.actions.length).toBe(1);
});
it('should have an empty action list if config provides nothing', () => {
config.config.extensions = {};
extensions.init();
expect(extensions.actions).toEqual([]);
});
it('should find action by id', () => {
extensions.init();
const action = extensions.getActionById(
'aca:actions/create-folder'
);
expect(action).toBeTruthy();
expect(action.type).toBe('CREATE_FOLDER');
expect(action.payload).toBe('folder-name');
});
it('should not find action by id', () => {
extensions.init();
const action = extensions.getActionById('missing');
expect(action).toBeFalsy();
});
it('should run the action via store', () => {
extensions.init();
spyOn(store, 'dispatch').and.stub();
extensions.runActionById('aca:actions/create-folder');
expect(store.dispatch).toHaveBeenCalledWith({
type: 'CREATE_FOLDER',
payload: 'folder-name'
});
});
it('should not use store if action is missing', () => {
extensions.init();
spyOn(store, 'dispatch').and.stub();
extensions.runActionById('missing');
expect(store.dispatch).not.toHaveBeenCalled();
});
});
describe('content actions', () => {
it('should load content actions from the config', () => {
config.config.extensions = {
@@ -464,33 +395,6 @@ describe('ExtensionService', () => {
});
});
describe('expressions', () => {
it('should eval static value', () => {
const value = extensions.runExpression('hello world');
expect(value).toBe('hello world');
});
it('should eval string as an expression', () => {
const value = extensions.runExpression('$( "hello world" )');
expect(value).toBe('hello world');
});
it('should eval expression with no context', () => {
const value = extensions.runExpression('$( 1 + 1 )');
expect(value).toBe(2);
});
it('should eval expression with context', () => {
const context = {
a: 'hey',
b: 'there'
};
const expression = '$( context.a + " " + context.b + "!" )';
const value = extensions.runExpression(expression, context);
expect(value).toBe('hey there!');
});
});
describe('sorting', () => {
it('should sort by provided order', () => {
const sorted = [

View File

@@ -24,52 +24,45 @@
*/
import { Injectable, Type } from '@angular/core';
import { RouteExtension } from './route.extension';
import { ActionExtension } from './action.extension';
import { AppConfigService } from '@alfresco/adf-core';
import {
ContentActionExtension,
ContentActionType
} from './content-action.extension';
import { OpenWithExtension } from './open-with.extension';
import { AppStore } from '../store/states';
import { Store } from '@ngrx/store';
import { NavigationExtension } from './navigation.extension';
import { Route } from '@angular/router';
import { Node } from 'alfresco-js-api';
import { RuleService } from './rules/rule.service';
import { ActionService } from './actions/action.service';
import { ActionRef } from './actions/action-ref';
import { RouteRef } from './route-ref';
@Injectable()
export class ExtensionService {
routes: Array<RouteExtension> = [];
actions: Array<ActionExtension> = [];
contentActions: Array<ContentActionExtension> = [];
openWithActions: Array<OpenWithExtension> = [];
createActions: Array<ContentActionExtension> = [];
routes: Array<RouteRef> = [];
authGuards: { [key: string]: Type<{}> } = {};
components: { [key: string]: Type<{}> } = {};
constructor(
private config: AppConfigService,
private store: Store<AppStore>,
private ruleService: RuleService
private ruleService: RuleService,
private actionService: ActionService
) {}
// initialise extension service
// in future will also load and merge data from the external plugins
init() {
this.routes = this.config.get<Array<RouteExtension>>(
this.routes = this.config.get<Array<RouteRef>>(
'extensions.core.routes',
[]
);
this.actions = this.config.get<Array<ActionExtension>>(
'extensions.core.actions',
[]
);
this.contentActions = this.config
.get<Array<ContentActionExtension>>(
'extensions.core.features.content.actions',
@@ -93,24 +86,30 @@ export class ExtensionService {
.sort(this.sortByOrder);
this.ruleService.init();
this.actionService.init();
}
getRouteById(id: string): RouteExtension {
setAuthGuard(key: string, value: Type<{}>): ExtensionService {
this.authGuards[key] = value;
return this;
}
getRouteById(id: string): RouteRef {
return this.routes.find(route => route.id === id);
}
getActionById(id: string): ActionExtension {
return this.actions.find(action => action.id === id);
getActionById(id: string): ActionRef {
return this.actionService.getActionById(id);
}
runActionById(id: string, context?: any) {
const action = this.getActionById(id);
if (action) {
const { type, payload } = action;
const expression = this.runExpression(payload, context);
this.actionService.runActionById(id, context);
}
this.store.dispatch({ type, payload: expression });
}
getAuthGuards(ids: string[]): Array<Type<{}>> {
return (ids || [])
.map(id => this.authGuards[id])
.filter(guard => guard);
}
getNavigationGroups(): Array<NavigationExtension[]> {
@@ -140,31 +139,15 @@ export class ExtensionService {
return [];
}
getAuthGuards(ids: string[]): Array<Type<{}>> {
return (ids || [])
.map(id => this.authGuards[id])
.filter(guard => guard);
setComponent(id: string, value: Type<{}>): ExtensionService {
this.components[id] = value;
return this;
}
getComponentById(id: string): Type<{}> {
return this.components[id];
}
runExpression(value: string, context?: any) {
const pattern = new RegExp(/\$\((.*\)?)\)/g);
const matches = pattern.exec(value);
if (matches && matches.length > 1) {
const expression = matches[1];
const fn = new Function('context', `return ${expression}`);
const result = fn(context);
return result;
}
return value;
}
getApplicationRoutes(): Array<Route> {
return this.routes.map(route => {
const guards = this.getAuthGuards(route.auth);

View File

@@ -23,7 +23,7 @@
* along with Alfresco. If not, see <http://www.gnu.org/licenses/>.
*/
export interface RouteExtension {
export interface RouteRef {
id: string;
path: string;
component: string;

View File

@@ -61,6 +61,7 @@ import { NodePermissionService } from '../common/services/node-permission.servic
import { ContentApiService } from '../services/content-api.service';
import { ExtensionService } from '../extensions/extension.service';
import { RuleService } from '../extensions/rules/rule.service';
import { ActionService } from '../extensions/actions/action.service';
@NgModule({
imports: [
@@ -114,7 +115,8 @@ import { RuleService } from '../extensions/rules/rule.service';
NodePermissionService,
ContentApiService,
ExtensionService,
RuleService
RuleService,
ActionService
]
})
export class AppTestingModule {}