UNPKG

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