UNPKG

7.95 kBTypeScriptView Raw
1import { TypedMap } from '../common/common';
2import { PathNode } from '../path/pathNode';
3import { ActiveUIView, ViewContext, ViewConfig } from './interface';
4import { _ViewDeclaration } from '../state/interface';
5import { UIRouter } from '../router';
6export declare type ViewConfigFactory = (path: PathNode[], decl: _ViewDeclaration) => ViewConfig | ViewConfig[];
7export interface ViewServicePluginAPI {
8 _rootViewContext(context?: ViewContext): ViewContext;
9 _viewConfigFactory(viewType: string, factory: ViewConfigFactory): any;
10 /** @param id router.$id + "." + uiView.id */
11 _registeredUIView(id: string): ActiveUIView;
12 _registeredUIViews(): ActiveUIView[];
13 _activeViewConfigs(): ViewConfig[];
14 _onSync(listener: ViewSyncListener): Function;
15}
16export interface ViewTuple {
17 uiView: ActiveUIView;
18 viewConfig: ViewConfig;
19}
20export interface ViewSyncListener {
21 (viewTuples: ViewTuple[]): void;
22}
23/**
24 * The View service
25 *
26 * This service pairs existing `ui-view` components (which live in the DOM)
27 * with view configs (from the state declaration objects: [[StateDeclaration.views]]).
28 *
29 * - After a successful Transition, the views from the newly entered states are activated via [[activateViewConfig]].
30 * The views from exited states are deactivated via [[deactivateViewConfig]].
31 * (See: the [[registerActivateViews]] Transition Hook)
32 *
33 * - As `ui-view` components pop in and out of existence, they register themselves using [[registerUIView]].
34 *
35 * - When the [[sync]] function is called, the registered `ui-view`(s) ([[ActiveUIView]])
36 * are configured with the matching [[ViewConfig]](s)
37 *
38 */
39export declare class ViewService {
40 private router;
41 /** @internal */ private _uiViews;
42 /** @internal */ private _viewConfigs;
43 /** @internal */ private _rootContext;
44 /** @internal */ private _viewConfigFactories;
45 /** @internal */ private _listeners;
46 /** @internal */
47 _pluginapi: ViewServicePluginAPI;
48 /**
49 * Given a ui-view and a ViewConfig, determines if they "match".
50 *
51 * A ui-view has a fully qualified name (fqn) and a context object. The fqn is built from its overall location in
52 * the DOM, describing its nesting relationship to any parent ui-view tags it is nested inside of.
53 *
54 * A ViewConfig has a target ui-view name and a context anchor. The ui-view name can be a simple name, or
55 * can be a segmented ui-view path, describing a portion of a ui-view fqn.
56 *
57 * In order for a ui-view to match ViewConfig, ui-view's $type must match the ViewConfig's $type
58 *
59 * If the ViewConfig's target ui-view name is a simple name (no dots), then a ui-view matches if:
60 * - the ui-view's name matches the ViewConfig's target name
61 * - the ui-view's context matches the ViewConfig's anchor
62 *
63 * If the ViewConfig's target ui-view name is a segmented name (with dots), then a ui-view matches if:
64 * - There exists a parent ui-view where:
65 * - the parent ui-view's name matches the first segment (index 0) of the ViewConfig's target name
66 * - the parent ui-view's context matches the ViewConfig's anchor
67 * - And the remaining segments (index 1..n) of the ViewConfig's target name match the tail of the ui-view's fqn
68 *
69 * Example:
70 *
71 * DOM:
72 * <ui-view> <!-- created in the root context (name: "") -->
73 * <ui-view name="foo"> <!-- created in the context named: "A" -->
74 * <ui-view> <!-- created in the context named: "A.B" -->
75 * <ui-view name="bar"> <!-- created in the context named: "A.B.C" -->
76 * </ui-view>
77 * </ui-view>
78 * </ui-view>
79 * </ui-view>
80 *
81 * uiViews: [
82 * { fqn: "$default", creationContext: { name: "" } },
83 * { fqn: "$default.foo", creationContext: { name: "A" } },
84 * { fqn: "$default.foo.$default", creationContext: { name: "A.B" } }
85 * { fqn: "$default.foo.$default.bar", creationContext: { name: "A.B.C" } }
86 * ]
87 *
88 * These four view configs all match the ui-view with the fqn: "$default.foo.$default.bar":
89 *
90 * - ViewConfig1: { uiViewName: "bar", uiViewContextAnchor: "A.B.C" }
91 * - ViewConfig2: { uiViewName: "$default.bar", uiViewContextAnchor: "A.B" }
92 * - ViewConfig3: { uiViewName: "foo.$default.bar", uiViewContextAnchor: "A" }
93 * - ViewConfig4: { uiViewName: "$default.foo.$default.bar", uiViewContextAnchor: "" }
94 *
95 * Using ViewConfig3 as an example, it matches the ui-view with fqn "$default.foo.$default.bar" because:
96 * - The ViewConfig's segmented target name is: [ "foo", "$default", "bar" ]
97 * - There exists a parent ui-view (which has fqn: "$default.foo") where:
98 * - the parent ui-view's name "foo" matches the first segment "foo" of the ViewConfig's target name
99 * - the parent ui-view's context "A" matches the ViewConfig's anchor context "A"
100 * - And the remaining segments [ "$default", "bar" ].join("."_ of the ViewConfig's target name match
101 * the tail of the ui-view's fqn "default.bar"
102 *
103 * @internal
104 */
105 static matches: (uiViewsByFqn: TypedMap<ActiveUIView>, uiView: ActiveUIView) => (viewConfig: ViewConfig) => boolean;
106 /**
107 * Normalizes a view's name from a state.views configuration block.
108 *
109 * This should be used by a framework implementation to calculate the values for
110 * [[_ViewDeclaration.$uiViewName]] and [[_ViewDeclaration.$uiViewContextAnchor]].
111 *
112 * @param context the context object (state declaration) that the view belongs to
113 * @param rawViewName the name of the view, as declared in the [[StateDeclaration.views]]
114 *
115 * @returns the normalized uiViewName and uiViewContextAnchor that the view targets
116 */
117 static normalizeUIViewTarget(context: ViewContext, rawViewName?: string): {
118 uiViewName: string;
119 uiViewContextAnchor: string;
120 };
121 /** @internal */
122 constructor(/** @internal */ router: UIRouter);
123 /** @internal */
124 private _rootViewContext;
125 /** @internal */
126 private _viewConfigFactory;
127 createViewConfig(path: PathNode[], decl: _ViewDeclaration): ViewConfig[];
128 /**
129 * Deactivates a ViewConfig.
130 *
131 * This function deactivates a `ViewConfig`.
132 * After calling [[sync]], it will un-pair from any `ui-view` with which it is currently paired.
133 *
134 * @param viewConfig The ViewConfig view to deregister.
135 */
136 deactivateViewConfig(viewConfig: ViewConfig): void;
137 activateViewConfig(viewConfig: ViewConfig): void;
138 sync(): void;
139 /**
140 * Registers a `ui-view` component
141 *
142 * When a `ui-view` component is created, it uses this method to register itself.
143 * After registration the [[sync]] method is used to ensure all `ui-view` are configured with the proper [[ViewConfig]].
144 *
145 * Note: the `ui-view` component uses the `ViewConfig` to determine what view should be loaded inside the `ui-view`,
146 * and what the view's state context is.
147 *
148 * Note: There is no corresponding `deregisterUIView`.
149 * A `ui-view` should hang on to the return value of `registerUIView` and invoke it to deregister itself.
150 *
151 * @param uiView The metadata for a UIView
152 * @return a de-registration function used when the view is destroyed.
153 */
154 registerUIView(uiView: ActiveUIView): () => void;
155 /**
156 * Returns the list of views currently available on the page, by fully-qualified name.
157 *
158 * @return {Array} Returns an array of fully-qualified view names.
159 */
160 available(): any[];
161 /**
162 * Returns the list of views on the page containing loaded content.
163 *
164 * @return {Array} Returns an array of fully-qualified view names.
165 */
166 active(): any[];
167}