1
1
/** @internalapi @module vanilla */ /** */
2
- import { services , isDefined } from ' ../common/module' ;
3
- import { LocationConfig , LocationServices } from ' ../common/coreservices' ;
4
- import { splitQuery , trimHashVal , getParams , splitHash , locationPluginFactory } from ' ./utils' ;
5
- import { removeFrom , unnestR } from "../common/common" ;
2
+ import { isDefined } from " ../common/module" ;
3
+ import { LocationConfig , LocationServices } from " ../common/coreservices" ;
4
+ import { splitQuery , getParams , splitHash , locationPluginFactory } from " ./utils" ;
5
+ import { removeFrom , unnestR , deregAll } from "../common/common" ;
6
6
import { UIRouter } from "../router" ;
7
7
import { LocationPlugin } from "./interface" ;
8
8
import { isArray } from "../common/predicates" ;
9
+ import { Disposable } from "../interface" ;
9
10
10
- var mlc ;
11
11
/** A `LocationConfig` mock that gets/sets all config from an in-memory object */
12
- export const memoryLocationConfig : LocationConfig = mlc = {
13
- _hashPrefix : '' ,
14
- _baseHref : '' ,
15
- _port : 80 ,
16
- _protocol : "http" ,
17
- _host : "localhost" ,
12
+ export class MemoryLocationConfig implements LocationConfig {
13
+ _baseHref = '' ;
14
+ _port = 80 ;
15
+ _protocol = "http" ;
16
+ _host = "localhost" ;
18
17
19
- port : ( ) => mlc . _port ,
20
- protocol : ( ) => mlc . _protocol ,
21
- host : ( ) => mlc . _host ,
22
- baseHref : ( ) => mlc . _baseHref ,
23
- html5Mode : ( ) => false ,
24
- hashPrefix : ( newprefix ?: string ) : string => {
25
- if ( isDefined ( newprefix ) ) {
26
- mlc . _hashPrefix = newprefix ;
27
- }
28
- return mlc . _hashPrefix ;
29
- }
30
- } ;
18
+ port = ( ) => this . _port ;
19
+ protocol = ( ) => this . _protocol ;
20
+ host = ( ) => this . _host ;
21
+ baseHref = ( ) => this . _baseHref ;
22
+ }
31
23
32
- var mls ;
33
24
/** A `LocationServices` that gets/sets the current location from an in-memory object */
34
- export const memoryLocationService : LocationServices = mls = {
35
- _listeners : [ ] ,
36
- _url : {
25
+ export class MemoryLocationService implements LocationServices , Disposable {
26
+ _listeners : Function [ ] = [ ] ;
27
+ _hashPrefix = "" ;
28
+ _url = {
37
29
path : '' ,
38
30
search : { } ,
39
31
hash : ''
40
- } ,
41
- _changed : ( newval , oldval ) => {
32
+ } ;
33
+
34
+ private _urlChanged ( newval , oldval ) {
42
35
if ( newval === oldval ) return ;
43
36
let evt = new Event ( "locationchange" ) ;
44
37
evt [ 'url' ] = newval ;
45
- mls . _listeners . forEach ( cb => cb ( evt ) ) ;
46
- } ,
38
+ this . _listeners . forEach ( cb => cb ( evt ) ) ;
39
+ }
47
40
48
- url : ( ) => {
49
- let s = mls . _url . search ;
50
- let hash = mls . _url . hash ;
41
+ url ( ) {
42
+ let s = this . _url . search ;
43
+ let hash = this . _url . hash ;
51
44
let query = Object . keys ( s ) . map ( key => ( isArray ( s [ key ] ) ? s [ key ] : [ s [ key ] ] ) . map ( val => key + "=" + val ) )
52
45
. reduce ( unnestR , [ ] )
53
46
. join ( "&" ) ;
54
47
55
- return mls . _url . path +
48
+ return this . _url . path +
56
49
( query ? "?" + query : "" ) +
57
50
( hash ? "#" + hash : "" ) ;
58
- } ,
59
- hash : ( ) => mls . _url . hash ,
60
- path : ( ) => mls . _url . path ,
61
- search : ( ) => mls . _url . search ,
62
- setUrl : ( url : string , replace : boolean = false ) => {
51
+ }
52
+
53
+ hash ( ) {
54
+ return this . _url . hash ;
55
+ }
56
+
57
+ path ( ) {
58
+ return this . _url . path ;
59
+ }
60
+
61
+ search ( ) {
62
+ return this . _url . search ;
63
+ }
64
+
65
+ html5Mode ( ) {
66
+ return false ;
67
+ }
68
+
69
+ hashPrefix ( newprefix ?: string ) : string {
70
+ return isDefined ( newprefix ) ? this . _hashPrefix = newprefix : this . _hashPrefix ;
71
+ }
72
+
73
+ setUrl ( url : string , replace : boolean = false ) {
63
74
if ( isDefined ( url ) ) {
64
75
let path = splitHash ( splitQuery ( url ) [ 0 ] ) [ 0 ] ;
65
76
let hash = splitHash ( url ) [ 1 ] ;
66
77
let search = getParams ( splitQuery ( splitHash ( url ) [ 0 ] ) [ 1 ] ) ;
67
78
68
- let oldval = mls . url ( ) ;
69
- mls . _url = { path, search, hash } ;
70
- let newval = mls . url ( ) ;
71
- mls . _changed ( newval , oldval ) ;
79
+ let oldval = this . url ( ) ;
80
+ this . _url = { path, search, hash } ;
81
+ let newval = this . url ( ) ;
82
+ this . _urlChanged ( newval , oldval ) ;
72
83
}
73
- } ,
74
- onChange : ( cb : EventListener ) => ( mls . _listeners . push ( cb ) , ( ) => removeFrom ( mls . _listeners , cb ) )
75
- } ;
84
+ }
85
+
86
+ onChange ( cb : EventListener ) {
87
+ this . _listeners . push ( cb ) ;
88
+ return ( ) => removeFrom ( this . _listeners , cb ) ;
89
+ }
90
+
91
+ dispose ( ) {
92
+ deregAll ( this . _listeners ) ;
93
+ }
94
+ }
76
95
77
96
/** A `UIRouterPlugin` that gets/sets the current location from an in-memory object */
78
97
export const memoryLocationPlugin : ( router : UIRouter ) => LocationPlugin =
79
- locationPluginFactory ( "vanilla.memoryLocation" , memoryLocationService , memoryLocationConfig ) ;
98
+ locationPluginFactory ( "vanilla.memoryLocation" , MemoryLocationService , MemoryLocationConfig ) ;
0 commit comments