1
+ import { services , isDefined } from '../common/module' ;
2
+ import { LocationConfig , LocationServices } from '../common/coreservices' ;
3
+ import { splitQuery , trimHashVal , getParams , splitHash , locationPluginFactory } from './utils' ;
4
+ import { removeFrom , unnestR } from "../common/common" ;
5
+ import { UIRouter } from "../router" ;
6
+ import { LocationPlugin } from "./interface" ;
7
+ import { isArray } from "../common/predicates" ;
8
+
9
+ var mlc ;
10
+ export const memoryLocationConfig : LocationConfig = mlc = {
11
+ _hashPrefix : '' ,
12
+ _baseHref : '' ,
13
+ _port : 80 ,
14
+ _protocol : "http" ,
15
+ _host : "localhost" ,
16
+
17
+ port : ( ) => mlc . _port ,
18
+ protocol : ( ) => mlc . _protocol ,
19
+ host : ( ) => mlc . _host ,
20
+ baseHref : ( ) => mlc . _baseHref ,
21
+ html5Mode : ( ) => false ,
22
+ hashPrefix : ( newprefix ?: string ) : string => {
23
+ if ( isDefined ( newprefix ) ) {
24
+ mlc . _hashPrefix = newprefix ;
25
+ }
26
+ return mlc . _hashPrefix ;
27
+ }
28
+ } ;
29
+
30
+ var mls ;
31
+ export const memoryLocationService : LocationServices = mls = {
32
+ _listeners : [ ] ,
33
+ _url : {
34
+ path : '' ,
35
+ search : { } ,
36
+ hash : ''
37
+ } ,
38
+ _changed : ( newval , oldval ) => {
39
+ if ( newval === oldval ) return ;
40
+ let evt = new Event ( "locationchange" ) ;
41
+ evt [ 'url' ] = newval ;
42
+ mls . _listeners . forEach ( cb => cb ( evt ) ) ;
43
+ } ,
44
+
45
+ url : ( ) => {
46
+ let s = mls . _url . search ;
47
+ let hash = mls . _url . hash ;
48
+ let query = Object . keys ( s ) . map ( key => ( isArray ( s [ key ] ) ? s [ key ] : [ s [ key ] ] ) . map ( val => key + "=" + val ) )
49
+ . reduce ( unnestR , [ ] )
50
+ . join ( "&" ) ;
51
+
52
+ return mls . _url . path +
53
+ ( query ? "?" + query : "" ) +
54
+ ( hash ? "#" + hash : "" ) ;
55
+ } ,
56
+ hash : ( ) => mls . _url . hash ,
57
+ path : ( ) => mls . _url . path ,
58
+ search : ( ) => mls . _url . search ,
59
+ setUrl : ( url : string , replace : boolean = false ) => {
60
+ if ( isDefined ( url ) ) {
61
+ let path = splitHash ( splitQuery ( url ) [ 0 ] ) [ 0 ] ;
62
+ let hash = splitHash ( url ) [ 1 ] ;
63
+ let search = getParams ( splitQuery ( splitHash ( url ) [ 0 ] ) [ 1 ] ) ;
64
+
65
+ let oldval = mls . url ( ) ;
66
+ mls . _url = { path, search, hash } ;
67
+ let newval = mls . url ( ) ;
68
+ mls . _changed ( newval , oldval ) ;
69
+ }
70
+ } ,
71
+ onChange : ( cb : EventListener ) => ( mls . _listeners . push ( cb ) , ( ) => removeFrom ( mls . _listeners , cb ) )
72
+ } ;
73
+
74
+ export const memoryLocationPlugin : ( router : UIRouter ) => LocationPlugin =
75
+ locationPluginFactory ( "vanilla.memoryLocation" , memoryLocationService , memoryLocationConfig ) ;
0 commit comments