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

View File

@@ -23,7 +23,7 @@
* along with Alfresco. If not, see <http://www.gnu.org/licenses/>. * along with Alfresco. If not, see <http://www.gnu.org/licenses/>.
*/ */
export interface ActionExtension { export interface ActionRef {
id: string; id: string;
type: string; type: string;
payload?: 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 { LayoutComponent } from '../components/layout/layout.component';
import { ToolbarActionComponent } from './components/toolbar-action/toolbar-action.component'; import { ToolbarActionComponent } from './components/toolbar-action/toolbar-action.component';
import { CommonModule } from '@angular/common'; import { CommonModule } from '@angular/common';
import { RuleService } from './rules/rule.service';
import { ActionService } from './actions/action.service';
@NgModule({ @NgModule({
imports: [ imports: [CommonModule, CoreModule.forChild()],
CommonModule,
CoreModule.forChild()
],
declarations: [ToolbarActionComponent], declarations: [ToolbarActionComponent],
exports: [ToolbarActionComponent], exports: [ToolbarActionComponent],
entryComponents: [AboutComponent] entryComponents: [AboutComponent],
providers: [ExtensionService, RuleService, ActionService]
}) })
export class CoreExtensionsModule { export class CoreExtensionsModule {
constructor(extensions: ExtensionService) { constructor(extensions: ExtensionService) {
extensions.components['aca:layouts/main'] = LayoutComponent; extensions
extensions.components['aca:components/about'] = AboutComponent; .setComponent('aca:layouts/main', LayoutComponent)
extensions.authGuards['aca:auth'] = AuthGuardEcm; .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/>. * 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 { RuleRef } from './rules/rule-ref';
import { ActionRef } from './actions/action-ref';
import { RouteRef } from './route-ref';
export interface ExtensionConfig { export interface ExtensionConfig {
rules?: Array<RuleRef>; rules?: Array<RuleRef>;
routes?: Array<RouteExtension>; routes?: Array<RouteRef>;
actions?: Array<ActionExtension>; actions?: Array<ActionRef>;
features?: { [key: string]: any }; features?: { [key: string]: any };
} }

View File

@@ -27,14 +27,11 @@ import { TestBed } from '@angular/core/testing';
import { AppTestingModule } from '../testing/app-testing.module'; import { AppTestingModule } from '../testing/app-testing.module';
import { ExtensionService } from './extension.service'; import { ExtensionService } from './extension.service';
import { AppConfigService } from '@alfresco/adf-core'; import { AppConfigService } from '@alfresco/adf-core';
import { Store } from '@ngrx/store';
import { AppStore } from '../store/states';
import { ContentActionType } from './content-action.extension'; import { ContentActionType } from './content-action.extension';
describe('ExtensionService', () => { describe('ExtensionService', () => {
let config: AppConfigService; let config: AppConfigService;
let extensions: ExtensionService; let extensions: ExtensionService;
let store: Store<AppStore>;
beforeEach(() => { beforeEach(() => {
TestBed.configureTestingModule({ TestBed.configureTestingModule({
@@ -42,7 +39,6 @@ describe('ExtensionService', () => {
}); });
extensions = TestBed.get(ExtensionService); extensions = TestBed.get(ExtensionService);
store = TestBed.get(Store);
config = TestBed.get(AppConfigService); config = TestBed.get(AppConfigService);
config.config['extensions'] = {}; 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', () => { describe('content actions', () => {
it('should load content actions from the config', () => { it('should load content actions from the config', () => {
config.config.extensions = { 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', () => { describe('sorting', () => {
it('should sort by provided order', () => { it('should sort by provided order', () => {
const sorted = [ const sorted = [

View File

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

View File

@@ -23,7 +23,7 @@
* along with Alfresco. If not, see <http://www.gnu.org/licenses/>. * along with Alfresco. If not, see <http://www.gnu.org/licenses/>.
*/ */
export interface RouteExtension { export interface RouteRef {
id: string; id: string;
path: string; path: string;
component: 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 { ContentApiService } from '../services/content-api.service';
import { ExtensionService } from '../extensions/extension.service'; import { ExtensionService } from '../extensions/extension.service';
import { RuleService } from '../extensions/rules/rule.service'; import { RuleService } from '../extensions/rules/rule.service';
import { ActionService } from '../extensions/actions/action.service';
@NgModule({ @NgModule({
imports: [ imports: [
@@ -114,7 +115,8 @@ import { RuleService } from '../extensions/rules/rule.service';
NodePermissionService, NodePermissionService,
ContentApiService, ContentApiService,
ExtensionService, ExtensionService,
RuleService RuleService,
ActionService
] ]
}) })
export class AppTestingModule {} export class AppTestingModule {}