@@ -7,6 +7,7 @@ import {ViewService} from "./view/view";
7
7
import { StateRegistry } from "./state/stateRegistry" ;
8
8
import { StateService } from "./state/stateService" ;
9
9
import { UIRouterGlobals , Globals } from "./globals" ;
10
+ import { UIRouterPlugin } from "./interface" ;
10
11
11
12
/**
12
13
* The master class used to instantiate an instance of UI-Router.
@@ -41,5 +42,65 @@ export class UIRouter {
41
42
this . globals . $current = this . stateRegistry . root ( ) ;
42
43
this . globals . current = this . globals . $current . self ;
43
44
}
44
- }
45
45
46
+ private _plugins : { [ key : string ] : UIRouterPlugin } = { } ;
47
+
48
+ /**
49
+ * Adds a plugin to UI-Router
50
+ *
51
+ * This method adds a UI-Router Plugin.
52
+ * A plugin can enhance or change UI-Router behavior using any public API.
53
+ *
54
+ * #### Example:
55
+ * ```js
56
+ * import { MyCoolPlugin } from "ui-router-cool-plugin";
57
+ *
58
+ * var plugin = router.addPlugin(MyCoolPlugin);
59
+ * ```
60
+ *
61
+ * ### Plugin authoring
62
+ *
63
+ * A plugin is simply a class (or constructor function) which accepts a [[UIRouter]] instance and (optionally) an options object.
64
+ *
65
+ * The plugin can implement its functionality using any of the public APIs of [[UIRouter]].
66
+ * For example, it may configure router options or add a Transition Hook.
67
+ *
68
+ * The plugin can then be published as a separate module.
69
+ *
70
+ * #### Example:
71
+ * ```js
72
+ * export class MyAuthPlugin {
73
+ * constructor(router: UIRouter, options: any) {
74
+ * let $transitions = router.transitionService;
75
+ * let $state = router.stateService;
76
+ *
77
+ * let authCriteria = {
78
+ * to: (state) => state.data && state.data.requiresAuth
79
+ * };
80
+ *
81
+ * function authHook(transition: Transition) {
82
+ * let authService = transition.injector().get('AuthService');
83
+ * if (!authService.isAuthenticated()) {
84
+ * return $state.target('login');
85
+ * }
86
+ * }
87
+ *
88
+ * $transitions.onStart(authCriteria, authHook);
89
+ * }
90
+ * }
91
+ * ```
92
+ *
93
+ * @param PluginClass a UI-Router Plugin class (or constructor function).
94
+ * @param options options to pass to the plugin
95
+ * @returns {T }
96
+ */
97
+ addPlugin < T extends UIRouterPlugin > ( PluginClass : { new ( router : UIRouter , options ?: any ) : T } , options : any = { } ) : T {
98
+ let pluginInstance = new PluginClass ( this , options ) ;
99
+ var pluginName = pluginInstance . name ( ) ;
100
+ return this . _plugins [ pluginName ] = pluginInstance ;
101
+ }
102
+
103
+ getPlugin ( pluginName : string ) : UIRouterPlugin {
104
+ return this . _plugins [ pluginName ] ;
105
+ }
106
+ }
0 commit comments