Skip to content

Commit e324973

Browse files
feat(plugin): Allow all plugins to be gotted.
docs(plugin): Update getPlugin docs
1 parent b9f4541 commit e324973

File tree

2 files changed

+47
-15
lines changed

2 files changed

+47
-15
lines changed

src/router.ts

+24-11
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,14 @@
11
/** @coreapi @module core */ /** */
2-
import {UrlMatcherFactory} from "./url/urlMatcherFactory";
3-
import {UrlRouterProvider} from "./url/urlRouter";
4-
import {UrlRouter} from "./url/urlRouter";
5-
import {TransitionService} from "./transition/transitionService";
6-
import {ViewService} from "./view/view";
7-
import {StateRegistry} from "./state/stateRegistry";
8-
import {StateService} from "./state/stateService";
9-
import {UIRouterGlobals, Globals} from "./globals";
10-
import {UIRouterPlugin} from "./interface";
2+
import { UrlMatcherFactory } from "./url/urlMatcherFactory";
3+
import { UrlRouterProvider } from "./url/urlRouter";
4+
import { UrlRouter } from "./url/urlRouter";
5+
import { TransitionService } from "./transition/transitionService";
6+
import { ViewService } from "./view/view";
7+
import { StateRegistry } from "./state/stateRegistry";
8+
import { StateService } from "./state/stateService";
9+
import { UIRouterGlobals, Globals } from "./globals";
10+
import { UIRouterPlugin } from "./interface";
11+
import { values } from "./common/common";
1112

1213
/**
1314
* The master class used to instantiate an instance of UI-Router.
@@ -103,14 +104,26 @@ export class UIRouter {
103104
plugin<T extends UIRouterPlugin>(plugin: { (router: UIRouter, options?: any): void }, options?: any): T;
104105
/** Allow javascript factory function */
105106
plugin<T extends UIRouterPlugin>(plugin: PluginFactory<T>, options?: any): T;
107+
/** Allow javascript factory function */
106108
plugin<T extends UIRouterPlugin>(plugin: any, options: any = {}): T {
107109
let pluginInstance = new plugin(this, options);
108110
if (!pluginInstance.name) throw new Error("Required property `name` missing on plugin: " + pluginInstance);
109111
return this._plugins[pluginInstance.name] = pluginInstance;
110112
}
111113

112-
getPlugin(pluginName: string): UIRouterPlugin {
113-
return this._plugins[pluginName];
114+
/**
115+
* Returns registered plugins
116+
*
117+
* Returns the registered plugin of the given `pluginName`.
118+
* If no `pluginName` is given, returns all registered plugins
119+
*
120+
* @param pluginName (optional) the name of the plugin to get
121+
* @return the named plugin (undefined if not found), or all plugins (if `pluginName` is omitted)
122+
*/
123+
getPlugin(): UIRouterPlugin[];
124+
getPlugin(pluginName?: string): UIRouterPlugin;
125+
getPlugin(pluginName?: string): UIRouterPlugin|UIRouterPlugin[] {
126+
return pluginName ? this._plugins[pluginName] : values(this._plugins);
114127
}
115128
}
116129

test/pluginSpec.ts

+23-4
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import * as vanilla from "../src/vanilla";
33
import { StateRegistry } from "../src/state/stateRegistry";
44
import { UrlRouter } from "../src/url/urlRouter";
55
import {UIRouterPlugin} from "../src/interface";
6+
import { isArray } from "../src/common/predicates";
67

78
describe('plugin api', function () {
89
let router: UIRouter;
@@ -23,23 +24,25 @@ describe('plugin api', function () {
2324
});
2425

2526
class FancyPluginClass implements UIRouterPlugin {
27+
name = "fancypluginclass";
2628
constructor(public router: UIRouter) { }
27-
name = "fancyplugin"
2829
}
2930

3031
function FancyPluginConstructor(router: UIRouter, options: any) {
31-
this.name = "fancyplugin";
32+
this.name = "fancypluginconstructor";
3233
}
3334

3435
describe('initialization', () => {
3536
it('should accept a plugin class', () => {
3637
let plugin = router.plugin(FancyPluginClass);
3738
expect(plugin instanceof FancyPluginClass).toBeTruthy();
39+
expect(plugin.name).toBe('fancypluginclass');
3840
});
3941

4042
it('should accept a constructor function', () => {
4143
let plugin = router.plugin(FancyPluginConstructor);
4244
expect(plugin instanceof FancyPluginConstructor).toBeTruthy();
45+
expect(plugin.name).toBe('fancypluginconstructor');
4346
});
4447

4548
it('should accept a factory function', () => {
@@ -48,6 +51,7 @@ describe('plugin api', function () {
4851
}
4952
let plugin = router.plugin(factoryFn);
5053
expect(plugin instanceof FancyPluginClass).toBeTruthy();
54+
expect(plugin.name).toBe('fancypluginclass');
5155
});
5256

5357
it('should return an instance of the plugin', () => {
@@ -77,9 +81,24 @@ describe('plugin api', function () {
7781

7882
describe('getPlugin', () => {
7983
it('should return the plugin instance', () => {
80-
router.plugin(() => new FancyPluginClass(router));
81-
let plugin = router.getPlugin('fancyplugin');
84+
router.plugin(FancyPluginClass);
85+
let plugin = router.getPlugin('fancypluginclass');
8286
expect(plugin instanceof FancyPluginClass).toBeTruthy();
8387
});
88+
89+
it('should return undefined if no pluginName is registered', () => {
90+
router.plugin(FancyPluginClass);
91+
let plugin = router.getPlugin('notexists');
92+
expect(plugin).toBeUndefined();
93+
});
94+
95+
it('should return all registered plugins when no pluginName is specified', () => {
96+
router.plugin(FancyPluginClass);
97+
router.plugin(FancyPluginConstructor);
98+
let plugins = router.getPlugin();
99+
expect(isArray(plugins)).toBeTruthy();
100+
expect(plugins.pop() instanceof FancyPluginConstructor).toBeTruthy();
101+
expect(plugins.pop() instanceof FancyPluginClass).toBeTruthy();
102+
});
84103
})
85104
});

0 commit comments

Comments
 (0)