UNPKG

7.37 kBJavaScriptView Raw
1/**
2 * @coreapi
3 * @module core
4 */ /** */
5import { UrlMatcherFactory } from "./url/urlMatcherFactory";
6import { UrlRouter } from "./url/urlRouter";
7import { TransitionService } from "./transition/transitionService";
8import { ViewService } from "./view/view";
9import { StateRegistry } from "./state/stateRegistry";
10import { StateService } from "./state/stateService";
11import { UIRouterGlobals } from "./globals";
12import { values, removeFrom } from "./common/common";
13import { isFunction } from "./common/predicates";
14import { UrlService } from "./url/urlService";
15import { trace } from "./common/trace";
16/** @hidden */
17var _routerInstance = 0;
18/**
19 * The master class used to instantiate an instance of UI-Router.
20 *
21 * UI-Router (for each specific framework) will create an instance of this class during bootstrap.
22 * This class instantiates and wires the UI-Router services together.
23 *
24 * After a new instance of the UIRouter class is created, it should be configured for your app.
25 * For instance, app states should be registered with the [[UIRouter.stateRegistry]].
26 *
27 * ---
28 *
29 * Normally the framework code will bootstrap UI-Router.
30 * If you are bootstrapping UIRouter manually, tell it to monitor the URL by calling
31 * [[UrlService.listen]] then [[UrlService.sync]].
32 */
33var UIRouter = (function () {
34 /**
35 * Creates a new `UIRouter` object
36 *
37 * @param locationService a [[LocationServices]] implementation
38 * @param locationConfig a [[LocationConfig]] implementation
39 * @internalapi
40 */
41 function UIRouter(locationService, locationConfig) {
42 if (locationService === void 0) { locationService = UrlService.locationServiceStub; }
43 if (locationConfig === void 0) { locationConfig = UrlService.locationConfigStub; }
44 this.locationService = locationService;
45 this.locationConfig = locationConfig;
46 /** @hidden */ this.$id = _routerInstance++;
47 /** @hidden */ this._disposed = false;
48 /** @hidden */ this._disposables = [];
49 /** Provides trace information to the console */
50 this.trace = trace;
51 /** Provides services related to ui-view synchronization */
52 this.viewService = new ViewService();
53 /** Provides services related to Transitions */
54 this.transitionService = new TransitionService(this);
55 /** Global router state */
56 this.globals = new UIRouterGlobals();
57 /**
58 * Deprecated for public use. Use [[urlService]] instead.
59 * @deprecated Use [[urlService]] instead
60 */
61 this.urlMatcherFactory = new UrlMatcherFactory();
62 /**
63 * Deprecated for public use. Use [[urlService]] instead.
64 * @deprecated Use [[urlService]] instead
65 */
66 this.urlRouter = new UrlRouter(this);
67 /** Provides a registry for states, and related registration services */
68 this.stateRegistry = new StateRegistry(this);
69 /** Provides services related to states */
70 this.stateService = new StateService(this);
71 /** Provides services related to the URL */
72 this.urlService = new UrlService(this);
73 /** @hidden */
74 this._plugins = {};
75 this.viewService._pluginapi._rootViewContext(this.stateRegistry.root());
76 this.globals.$current = this.stateRegistry.root();
77 this.globals.current = this.globals.$current.self;
78 this.disposable(this.globals);
79 this.disposable(this.stateService);
80 this.disposable(this.stateRegistry);
81 this.disposable(this.transitionService);
82 this.disposable(this.urlRouter);
83 this.disposable(locationService);
84 this.disposable(locationConfig);
85 }
86 /** Registers an object to be notified when the router is disposed */
87 UIRouter.prototype.disposable = function (disposable) {
88 this._disposables.push(disposable);
89 };
90 /**
91 * Disposes this router instance
92 *
93 * When called, clears resources retained by the router by calling `dispose(this)` on all
94 * registered [[disposable]] objects.
95 *
96 * Or, if a `disposable` object is provided, calls `dispose(this)` on that object only.
97 *
98 * @param disposable (optional) the disposable to dispose
99 */
100 UIRouter.prototype.dispose = function (disposable) {
101 var _this = this;
102 if (disposable && isFunction(disposable.dispose)) {
103 disposable.dispose(this);
104 return undefined;
105 }
106 this._disposed = true;
107 this._disposables.slice().forEach(function (d) {
108 try {
109 typeof d.dispose === 'function' && d.dispose(_this);
110 removeFrom(_this._disposables, d);
111 }
112 catch (ignored) { }
113 });
114 };
115 /**
116 * Adds a plugin to UI-Router
117 *
118 * This method adds a UI-Router Plugin.
119 * A plugin can enhance or change UI-Router behavior using any public API.
120 *
121 * #### Example:
122 * ```js
123 * import { MyCoolPlugin } from "ui-router-cool-plugin";
124 *
125 * var plugin = router.addPlugin(MyCoolPlugin);
126 * ```
127 *
128 * ### Plugin authoring
129 *
130 * A plugin is simply a class (or constructor function) which accepts a [[UIRouter]] instance and (optionally) an options object.
131 *
132 * The plugin can implement its functionality using any of the public APIs of [[UIRouter]].
133 * For example, it may configure router options or add a Transition Hook.
134 *
135 * The plugin can then be published as a separate module.
136 *
137 * #### Example:
138 * ```js
139 * export class MyAuthPlugin implements UIRouterPlugin {
140 * constructor(router: UIRouter, options: any) {
141 * this.name = "MyAuthPlugin";
142 * let $transitions = router.transitionService;
143 * let $state = router.stateService;
144 *
145 * let authCriteria = {
146 * to: (state) => state.data && state.data.requiresAuth
147 * };
148 *
149 * function authHook(transition: Transition) {
150 * let authService = transition.injector().get('AuthService');
151 * if (!authService.isAuthenticated()) {
152 * return $state.target('login');
153 * }
154 * }
155 *
156 * $transitions.onStart(authCriteria, authHook);
157 * }
158 * }
159 * ```
160 *
161 * @param plugin one of:
162 * - a plugin class which implements [[UIRouterPlugin]]
163 * - a constructor function for a [[UIRouterPlugin]] which accepts a [[UIRouter]] instance
164 * - a factory function which accepts a [[UIRouter]] instance and returns a [[UIRouterPlugin]] instance
165 * @param options options to pass to the plugin class/factory
166 * @returns the registered plugin instance
167 */
168 UIRouter.prototype.plugin = function (plugin, options) {
169 if (options === void 0) { options = {}; }
170 var pluginInstance = new plugin(this, options);
171 if (!pluginInstance.name)
172 throw new Error("Required property `name` missing on plugin: " + pluginInstance);
173 this._disposables.push(pluginInstance);
174 return this._plugins[pluginInstance.name] = pluginInstance;
175 };
176 UIRouter.prototype.getPlugin = function (pluginName) {
177 return pluginName ? this._plugins[pluginName] : values(this._plugins);
178 };
179 return UIRouter;
180}());
181export { UIRouter };
182//# sourceMappingURL=router.js.map
\No newline at end of file