Skip to content

Commit 6533b51

Browse files
feat(view): Add _pluginapi._registeredUIView() to get a ui-view by id
1 parent 10b7fde commit 6533b51

File tree

3 files changed

+17
-3
lines changed

3 files changed

+17
-3
lines changed

src/router.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ export class UIRouter {
4343
trace: Trace = trace;
4444

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

4848
/** Global router state */
4949
globals: UIRouterGlobals = new UIRouterGlobals();

src/view/view.ts

+6-2
Original file line numberDiff line numberDiff line change
@@ -2,19 +2,22 @@
22
* @coreapi
33
* @module view
44
*/ /** for typedoc */
5-
import { equals, applyPairs, removeFrom, TypedMap, inArray } from '../common/common';
5+
import { equals, applyPairs, removeFrom, TypedMap, inArray, find } from '../common/common';
66
import { curry, prop } from '../common/hof';
77
import { isString, isArray } from '../common/predicates';
88
import { trace } from '../common/trace';
99
import { PathNode } from '../path/pathNode';
1010
import { ActiveUIView, ViewContext, ViewConfig } from './interface';
1111
import { _ViewDeclaration } from '../state/interface';
12+
import { UIRouter } from '../router';
1213

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

1516
export interface ViewServicePluginAPI {
1617
_rootViewContext(context?: ViewContext): ViewContext;
1718
_viewConfigFactory(viewType: string, factory: ViewConfigFactory);
19+
/** @param id router.$id + "." + uiView.id */
20+
_registeredUIView(id: string): ActiveUIView;
1821
_registeredUIViews(): ActiveUIView[];
1922
_activeViewConfigs(): ViewConfig[];
2023
_onSync(listener: ViewSyncListener): Function;
@@ -56,6 +59,7 @@ export class ViewService {
5659
public _pluginapi: ViewServicePluginAPI = {
5760
_rootViewContext: this._rootViewContext.bind(this),
5861
_viewConfigFactory: this._viewConfigFactory.bind(this),
62+
_registeredUIView: (id: string) => find(this._uiViews, view => `${this.router.$id}.${view.id}` === id),
5963
_registeredUIViews: () => this._uiViews,
6064
_activeViewConfigs: () => this._viewConfigs,
6165
_onSync: (listener: ViewSyncListener) => {
@@ -187,7 +191,7 @@ export class ViewService {
187191
return { uiViewName, uiViewContextAnchor };
188192
}
189193

190-
constructor() {}
194+
constructor(private router: UIRouter) {}
191195

192196
private _rootViewContext(context?: ViewContext): ViewContext {
193197
return (this._rootContext = context || this._rootContext);

test/viewServiceSpec.ts

+10
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,16 @@ describe('View Service', () => {
5454
});
5555
});
5656

57+
describe('_pluginapi._registeredUIView', () => {
58+
it('should return a ui-view from an id', () => {
59+
expect($view._pluginapi._registeredUIView(`${router.$id}.0`)).toBeUndefined();
60+
61+
const uiView = makeUIView();
62+
$view.registerUIView(uiView);
63+
expect($view._pluginapi._registeredUIView(`${router.$id}.${uiView.id}`)).toBe(uiView);
64+
});
65+
});
66+
5767
describe('onSync', () => {
5868
it('registers view sync listeners', () => {
5969
function listener(tuples: ViewTuple[]) {}

0 commit comments

Comments
 (0)