Browse Source

feature(core): add just-clone

add config state type
add flattedRoutes to config state
pull/1746/head
mehmet-erim 7 years ago
parent
commit
2f3f411e34
  1. 1
      npm/ng-packs/packages/core/ng-package.json
  2. 1
      npm/ng-packs/packages/core/package.json
  3. 6
      npm/ng-packs/packages/core/src/lib/components/dynamic-layout.component.ts
  4. 3
      npm/ng-packs/packages/core/src/lib/directives/for.directive.ts
  5. 11
      npm/ng-packs/packages/core/src/lib/models/config.ts
  6. 21
      npm/ng-packs/packages/core/src/lib/plugins/config/config.plugin.ts
  7. 34
      npm/ng-packs/packages/core/src/lib/states/config.state.ts

1
npm/ng-packs/packages/core/ng-package.json

@ -11,6 +11,7 @@
"@ngxs/store", "@ngxs/store",
"angular-oauth2-oidc", "angular-oauth2-oidc",
"just-compare", "just-compare",
"just-clone",
"snq" "snq"
] ]
} }

1
npm/ng-packs/packages/core/package.json

@ -7,6 +7,7 @@
"@ngxs/store": "^3.5.0", "@ngxs/store": "^3.5.0",
"angular-oauth2-oidc": "^8.0.1", "angular-oauth2-oidc": "^8.0.1",
"just-compare": "^1.3.0", "just-compare": "^1.3.0",
"just-clone": "3.1.0",
"snq": "^1.0.3" "snq": "^1.0.3"
}, },
"peerDependencies": { "peerDependencies": {

6
npm/ng-packs/packages/core/src/lib/components/dynamic-layout.component.ts

@ -33,7 +33,7 @@ export class DynamicLayoutComponent implements OnDestroy {
if ((this.route.snapshot.data || {}).layout) { if ((this.route.snapshot.data || {}).layout) {
this.layout = layouts this.layout = layouts
.filter(l => !!l) .filter(l => !!l)
.find(l => snq(() => l.type.toLowerCase().indexOf(this.route.snapshot.data.layout), -1) > -1); .find((l: any) => snq(() => l.type.toLowerCase().indexOf(this.route.snapshot.data.layout), -1) > -1);
} }
this.router.events.pipe(takeUntilDestroy(this)).subscribe(event => { this.router.events.pipe(takeUntilDestroy(this)).subscribe(event => {
@ -42,7 +42,9 @@ export class DynamicLayoutComponent implements OnDestroy {
const layout = (this.route.snapshot.data || {}).layout || findLayout(segments, routes); const layout = (this.route.snapshot.data || {}).layout || findLayout(segments, routes);
this.layout = layouts.filter(l => !!l).find(l => snq(() => l.type.toLowerCase().indexOf(layout), -1) > -1); this.layout = layouts
.filter(l => !!l)
.find((l: any) => snq(() => l.type.toLowerCase().indexOf(layout), -1) > -1);
} }
}); });
} }

3
npm/ng-packs/packages/core/src/lib/directives/for.directive.ts

@ -12,6 +12,7 @@ import {
ViewContainerRef, ViewContainerRef,
} from '@angular/core'; } from '@angular/core';
import compare from 'just-compare'; import compare from 'just-compare';
import clone from 'just-clone';
export type CompareFn<T = any> = (value: T, comparison: T) => boolean; export type CompareFn<T = any> = (value: T, comparison: T) => boolean;
@ -146,7 +147,7 @@ export class ForDirective implements OnChanges {
} }
ngOnChanges() { ngOnChanges() {
let items = [...this.items] as any[]; let items = clone(this.items) as any[];
if (!Array.isArray(items)) return; if (!Array.isArray(items)) return;
const compareFn = this.compareFn; const compareFn = this.compareFn;

11
npm/ng-packs/packages/core/src/lib/models/config.ts

@ -1,16 +1,21 @@
import { AuthConfig } from 'angular-oauth2-oidc'; import { AuthConfig } from 'angular-oauth2-oidc';
import { Type } from '@angular/core'; import { Type } from '@angular/core';
import { ApplicationConfiguration } from './application-configuration';
import { ABP } from './common';
export namespace Config { export namespace Config {
export interface State { export type State = ApplicationConfiguration.Response &
[key: string]: any; ABP.Root & { environment: Environment } & {
} routes: ABP.FullRoute[];
flattedRoutes: ABP.FullRoute[];
};
export interface Environment { export interface Environment {
application: Application; application: Application;
production: boolean; production: boolean;
oAuthConfig: AuthConfig; oAuthConfig: AuthConfig;
apis: Apis; apis: Apis;
localization: { defaultResourceName: string };
} }
export interface Application { export interface Application {

21
npm/ng-packs/packages/core/src/lib/plugins/config/config.plugin.ts

@ -4,6 +4,7 @@ import { actionMatcher, InitState, NgxsNextPluginFn, NgxsPlugin, setValue, Updat
import snq from 'snq'; import snq from 'snq';
import { ABP } from '../../models'; import { ABP } from '../../models';
import { organizeRoutes } from '../../utils/route-utils'; import { organizeRoutes } from '../../utils/route-utils';
import clone from 'just-clone';
export const NGXS_CONFIG_PLUGIN_OPTIONS = new InjectionToken('NGXS_CONFIG_PLUGIN_OPTIONS'); export const NGXS_CONFIG_PLUGIN_OPTIONS = new InjectionToken('NGXS_CONFIG_PLUGIN_OPTIONS');
@ -21,11 +22,12 @@ export class ConfigPlugin implements NgxsPlugin {
if (isInitAction && !this.initialized) { if (isInitAction && !this.initialized) {
let { routes, wrappers } = transformRoutes(this.router.config); let { routes, wrappers } = transformRoutes(this.router.config);
routes = organizeRoutes(routes, wrappers); routes = organizeRoutes(routes, wrappers);
const flattedRoutes = flatRoutes(clone(routes));
state = setValue(state, 'ConfigState', { state = setValue(state, 'ConfigState', {
...(state.ConfigState && { ...state.ConfigState }), ...(state.ConfigState && { ...state.ConfigState }),
...this.options, ...this.options,
routes, routes,
flattedRoutes,
}); });
this.initialized = true; this.initialized = true;
@ -92,3 +94,20 @@ function setUrls(routes: ABP.FullRoute[], parentUrl?: string): ABP.FullRoute[] {
}), }),
})); }));
} }
function flatRoutes(routes: ABP.FullRoute[]): ABP.FullRoute[] {
const flat = (r: ABP.FullRoute[]) => {
return r.reduce((acc, val) => {
let value: ABP.FullRoute[] = [val];
if (val.children) {
const { children } = val;
delete val.children;
value = [val, ...flat(children)];
}
return [...acc, ...value];
}, []);
};
return flat(routes);
}

34
npm/ng-packs/packages/core/src/lib/states/config.state.ts

@ -20,8 +20,8 @@ export class ConfigState {
} }
@Selector() @Selector()
static getApplicationInfo(state: Config.State) { static getApplicationInfo(state: Config.State): Config.Application {
return state.environment.application || {}; return state.environment.application || ({} as Config.Application);
} }
static getOne(key: string) { static getOne(key: string) {
@ -64,7 +64,14 @@ export class ConfigState {
const selector = createSelector( const selector = createSelector(
[ConfigState], [ConfigState],
function(state: Config.State) { function(state: Config.State) {
return findRoute(state.routes, path, name); const { flattedRoutes } = state;
return (flattedRoutes as ABP.FullRoute[]).find(route => {
if (path && route.path === path) {
return route;
} else if (name && route.name === name) {
return route;
}
});
}, },
); );
@ -131,7 +138,7 @@ export class ConfigState {
keys[0] = snq(() => defaultResourceName); keys[0] = snq(() => defaultResourceName);
} }
let copy = keys.reduce((acc, val) => { let copy = (keys as any).reduce((acc, val) => {
if (acc) { if (acc) {
return acc[val]; return acc[val];
} }
@ -223,22 +230,3 @@ function patchRouteDeep(
return organizeRoutes(routes); return organizeRoutes(routes);
} }
function findRoute(routes: ABP.FullRoute[], path?: string, name?: string) {
let foundRoute;
routes.forEach(route => {
if (foundRoute) return;
if (path && route.path === path) {
foundRoute = route;
} else if (name && route.name === name) {
foundRoute = route;
return;
} else if (route.children && route.children.length) {
foundRoute = findRoute(route.children, path, name);
return;
}
});
return foundRoute;
}

Loading…
Cancel
Save