UNPKG

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