Skip to content

Commit

Permalink
feat(view): Add _pluginapi._registeredUIView() to get a ui-view by id
Browse files Browse the repository at this point in the history
  • Loading branch information
christopherthielen committed May 30, 2018
1 parent 10b7fde commit 6533b51
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 3 deletions.
2 changes: 1 addition & 1 deletion src/router.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ export class UIRouter {
trace: Trace = trace;

/** Provides services related to ui-view synchronization */
viewService = new ViewService();
viewService = new ViewService(this);

/** Global router state */
globals: UIRouterGlobals = new UIRouterGlobals();
Expand Down
8 changes: 6 additions & 2 deletions src/view/view.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,22 @@
* @coreapi
* @module view
*/ /** for typedoc */
import { equals, applyPairs, removeFrom, TypedMap, inArray } from '../common/common';
import { equals, applyPairs, removeFrom, TypedMap, inArray, find } from '../common/common';
import { curry, prop } from '../common/hof';
import { isString, isArray } from '../common/predicates';
import { trace } from '../common/trace';
import { PathNode } from '../path/pathNode';
import { ActiveUIView, ViewContext, ViewConfig } from './interface';
import { _ViewDeclaration } from '../state/interface';
import { UIRouter } from '../router';

export type ViewConfigFactory = (path: PathNode[], decl: _ViewDeclaration) => ViewConfig | ViewConfig[];

export interface ViewServicePluginAPI {
_rootViewContext(context?: ViewContext): ViewContext;
_viewConfigFactory(viewType: string, factory: ViewConfigFactory);
/** @param id router.$id + "." + uiView.id */
_registeredUIView(id: string): ActiveUIView;
_registeredUIViews(): ActiveUIView[];
_activeViewConfigs(): ViewConfig[];
_onSync(listener: ViewSyncListener): Function;
Expand Down Expand Up @@ -56,6 +59,7 @@ export class ViewService {
public _pluginapi: ViewServicePluginAPI = {
_rootViewContext: this._rootViewContext.bind(this),
_viewConfigFactory: this._viewConfigFactory.bind(this),
_registeredUIView: (id: string) => find(this._uiViews, view => `${this.router.$id}.${view.id}` === id),
_registeredUIViews: () => this._uiViews,
_activeViewConfigs: () => this._viewConfigs,
_onSync: (listener: ViewSyncListener) => {
Expand Down Expand Up @@ -187,7 +191,7 @@ export class ViewService {
return { uiViewName, uiViewContextAnchor };
}

constructor() {}
constructor(private router: UIRouter) {}

private _rootViewContext(context?: ViewContext): ViewContext {
return (this._rootContext = context || this._rootContext);
Expand Down
10 changes: 10 additions & 0 deletions test/viewServiceSpec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,16 @@ describe('View Service', () => {
});
});

describe('_pluginapi._registeredUIView', () => {
it('should return a ui-view from an id', () => {
expect($view._pluginapi._registeredUIView(`${router.$id}.0`)).toBeUndefined();

const uiView = makeUIView();
$view.registerUIView(uiView);
expect($view._pluginapi._registeredUIView(`${router.$id}.${uiView.id}`)).toBe(uiView);
});
});

describe('onSync', () => {
it('registers view sync listeners', () => {
function listener(tuples: ViewTuple[]) {}
Expand Down

0 comments on commit 6533b51

Please sign in to comment.