mirror of
https://github.com/Alfresco/alfresco-content-app.git
synced 2025-07-31 17:38:28 +00:00
shared library (#1080)
* shared project scaffold * rules package * move evaluators to shared lib * add rxjs peer dependency * use dedicated material namespaces * create store package, move actions * move selectors to shared library * move generic effects to shared lib * move routing extensions * minor code reorg * fix unit tests * move content-api service * move permission service * update tests * update plint config * move page layout * css variables * use dedicated css property * move generic error component to shared lib * fix test
This commit is contained in:
@@ -0,0 +1,86 @@
|
||||
/*!
|
||||
* @license
|
||||
* Alfresco Example Content Application
|
||||
*
|
||||
* Copyright (C) 2005 - 2019 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 { AppRouteReuseStrategy } from './app.routes.strategy';
|
||||
import { TestBed } from '@angular/core/testing';
|
||||
|
||||
describe('AppRouteReuseStrategy', () => {
|
||||
let appRouteReuse: AppRouteReuseStrategy;
|
||||
|
||||
beforeEach(() => {
|
||||
TestBed.configureTestingModule({
|
||||
providers: [AppRouteReuseStrategy]
|
||||
});
|
||||
|
||||
appRouteReuse = TestBed.get(AppRouteReuseStrategy);
|
||||
});
|
||||
|
||||
it('should allow detach if route is configured to be reused', () => {
|
||||
const route = <any>{
|
||||
routeConfig: {
|
||||
data: {
|
||||
reuse: true
|
||||
},
|
||||
path: 'tested-path'
|
||||
}
|
||||
};
|
||||
expect(appRouteReuse.shouldDetach(<any>route)).toBe(true);
|
||||
});
|
||||
|
||||
it('should store on routeCache', () => {
|
||||
const route = <any>{
|
||||
url: [],
|
||||
routeConfig: {
|
||||
data: {
|
||||
reuse: true
|
||||
},
|
||||
path: 'tested-path',
|
||||
component: {}
|
||||
},
|
||||
firstChild: null,
|
||||
children: []
|
||||
};
|
||||
appRouteReuse.store(route, { route: {} });
|
||||
expect(appRouteReuse.shouldAttach(<any>route)).toBe(true);
|
||||
});
|
||||
|
||||
it('should clear routeCache on resetCache', () => {
|
||||
const route = <any>{
|
||||
url: [],
|
||||
routeConfig: {
|
||||
data: {
|
||||
reuse: true
|
||||
},
|
||||
path: 'tested-path',
|
||||
component: {}
|
||||
},
|
||||
firstChild: null,
|
||||
children: []
|
||||
};
|
||||
appRouteReuse.store(route, { route: {} });
|
||||
appRouteReuse.resetCache();
|
||||
expect(appRouteReuse.shouldAttach(<any>route)).toBe(false);
|
||||
});
|
||||
});
|
142
projects/aca-shared/src/lib/routing/app.routes.strategy.ts
Normal file
142
projects/aca-shared/src/lib/routing/app.routes.strategy.ts
Normal file
@@ -0,0 +1,142 @@
|
||||
/*!
|
||||
* @license
|
||||
* Alfresco Example Content Application
|
||||
*
|
||||
* Copyright (C) 2005 - 2019 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 {
|
||||
RouteReuseStrategy,
|
||||
DetachedRouteHandle,
|
||||
ActivatedRouteSnapshot
|
||||
} from '@angular/router';
|
||||
import { ComponentRef } from '@angular/core';
|
||||
|
||||
interface RouteData {
|
||||
reuse: boolean;
|
||||
}
|
||||
|
||||
interface RouteInfo {
|
||||
handle: DetachedRouteHandle;
|
||||
data: RouteData;
|
||||
}
|
||||
|
||||
export class AppRouteReuseStrategy implements RouteReuseStrategy {
|
||||
private routeCache = new Map<string, RouteInfo>();
|
||||
|
||||
resetCache() {
|
||||
this.routeCache.forEach(value => {
|
||||
this.deactivateComponent(value.handle);
|
||||
});
|
||||
this.routeCache.clear();
|
||||
}
|
||||
|
||||
private deactivateComponent(handle: DetachedRouteHandle): void {
|
||||
if (!handle) {
|
||||
return;
|
||||
}
|
||||
const componentRef: ComponentRef<any> = handle['componentRef'];
|
||||
if (componentRef) {
|
||||
componentRef.destroy();
|
||||
}
|
||||
}
|
||||
|
||||
shouldReuseRoute(
|
||||
future: ActivatedRouteSnapshot,
|
||||
curr: ActivatedRouteSnapshot
|
||||
): boolean {
|
||||
const ret = future.routeConfig === curr.routeConfig;
|
||||
if (ret) {
|
||||
this.addRedirectsRecursively(future); // update redirects
|
||||
}
|
||||
return ret;
|
||||
}
|
||||
|
||||
shouldDetach(route: ActivatedRouteSnapshot): boolean {
|
||||
const data = this.getRouteData(route);
|
||||
return data && data.reuse;
|
||||
}
|
||||
|
||||
store(route: ActivatedRouteSnapshot, handle: DetachedRouteHandle): void {
|
||||
const url = this.getFullRouteUrl(route);
|
||||
const data = this.getRouteData(route);
|
||||
this.routeCache.set(url, { handle, data });
|
||||
this.addRedirectsRecursively(route);
|
||||
}
|
||||
|
||||
shouldAttach(route: ActivatedRouteSnapshot): boolean {
|
||||
const url = this.getFullRouteUrl(route);
|
||||
return this.routeCache.has(url);
|
||||
}
|
||||
|
||||
retrieve(route: ActivatedRouteSnapshot): DetachedRouteHandle {
|
||||
const url = this.getFullRouteUrl(route);
|
||||
const data = this.getRouteData(route);
|
||||
return data && data.reuse && this.routeCache.has(url)
|
||||
? this.routeCache.get(url).handle
|
||||
: null;
|
||||
}
|
||||
|
||||
private addRedirectsRecursively(route: ActivatedRouteSnapshot): void {
|
||||
const config = route.routeConfig;
|
||||
if (config) {
|
||||
if (!config.loadChildren) {
|
||||
const routeFirstChild = route.firstChild;
|
||||
const routeFirstChildUrl = routeFirstChild
|
||||
? this.getRouteUrlPaths(routeFirstChild).join('/')
|
||||
: '';
|
||||
const childConfigs = config.children;
|
||||
if (childConfigs) {
|
||||
const childConfigWithRedirect = childConfigs.find(
|
||||
c => c.path === '' && !!c.redirectTo
|
||||
);
|
||||
if (childConfigWithRedirect) {
|
||||
childConfigWithRedirect.redirectTo = routeFirstChildUrl;
|
||||
}
|
||||
}
|
||||
}
|
||||
route.children.forEach(childRoute =>
|
||||
this.addRedirectsRecursively(childRoute)
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
private getFullRouteUrl(route: ActivatedRouteSnapshot): string {
|
||||
return this.getFullRouteUrlPaths(route)
|
||||
.filter(Boolean)
|
||||
.join('/');
|
||||
}
|
||||
|
||||
private getFullRouteUrlPaths(route: ActivatedRouteSnapshot): string[] {
|
||||
const paths = this.getRouteUrlPaths(route);
|
||||
return route.parent
|
||||
? [...this.getFullRouteUrlPaths(route.parent), ...paths]
|
||||
: paths;
|
||||
}
|
||||
|
||||
private getRouteUrlPaths(route: ActivatedRouteSnapshot): string[] {
|
||||
return route.url.map(urlSegment => urlSegment.path);
|
||||
}
|
||||
|
||||
private getRouteData(route: ActivatedRouteSnapshot): RouteData {
|
||||
return route.routeConfig && (route.routeConfig.data as RouteData);
|
||||
}
|
||||
}
|
54
projects/aca-shared/src/lib/routing/shared.guard.ts
Normal file
54
projects/aca-shared/src/lib/routing/shared.guard.ts
Normal file
@@ -0,0 +1,54 @@
|
||||
/*!
|
||||
* @license
|
||||
* Alfresco Example Content Application
|
||||
*
|
||||
* Copyright (C) 2005 - 2019 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 { CanActivate } from '@angular/router';
|
||||
import { Observable } from 'rxjs';
|
||||
import { ActivatedRouteSnapshot } from '@angular/router';
|
||||
import { Store } from '@ngrx/store';
|
||||
import { AppStore, isQuickShareEnabled } from '@alfresco/aca-shared/store';
|
||||
|
||||
@Injectable({
|
||||
providedIn: 'root'
|
||||
})
|
||||
export class AppSharedRuleGuard implements CanActivate {
|
||||
isQuickShareEnabled$: Observable<boolean>;
|
||||
|
||||
constructor(store: Store<AppStore>) {
|
||||
this.isQuickShareEnabled$ = store.select(isQuickShareEnabled);
|
||||
}
|
||||
|
||||
canActivate(
|
||||
_: ActivatedRouteSnapshot
|
||||
): Observable<boolean> | Promise<boolean> | boolean {
|
||||
return this.isQuickShareEnabled$;
|
||||
}
|
||||
|
||||
canActivateChild(
|
||||
route: ActivatedRouteSnapshot
|
||||
): Observable<boolean> | Promise<boolean> | boolean {
|
||||
return this.canActivate(route);
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user