@dharmax/state-router 3.0.0 → 3.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/ReadMe.md CHANGED
@@ -29,6 +29,8 @@ you can use the hash notation or not (set the router's mode to 'history' or 'has
29
29
  stateManager.addState('inbox')
30
30
  stateManager.addState('about')
31
31
  stateManager.addState('discussion', 'discussion-page', 'discussion/%')
32
+ stateManager.addState('article', 'article-page', 'article/%','article-mode')
33
+ stateManager.addState('blog', 'article-page', 'blog/%','blog-mode')
32
34
  ```
33
35
 
34
36
  ## usage in the gui
@@ -45,4 +47,11 @@ stateManager.onChange( event => {
45
47
  })
46
48
 
47
49
 
48
- ```
50
+ ```
51
+
52
+ ## More
53
+ * pageChangeHandler - it's a global optional page change listener that receives ('send', 'pageview', `/${state.name}/${context || ''}`);
54
+ * google analytics (ga) is automatically used if it was found
55
+ * query parameters are passed to the state context under queryParams object
56
+ * on page change, an event state:changed is fired with the new state (that include the context)
57
+ * in addState, the last (optional) parameter can contain either a string an array of strings and it can be used for in-state special logic, or sub-states within the same parent-component, for example
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@dharmax/state-router",
3
- "version": "3.0.0",
3
+ "version": "3.1.0",
4
4
  "description": "A cute and tight router and application state controller",
5
5
  "main": "dist/index.js",
6
6
  "types": "dist/index.d.ts",
package/src/router.ts CHANGED
@@ -76,7 +76,8 @@ class Router {
76
76
  const match = path.match(route.pattern);
77
77
  if (match) {
78
78
  match.shift(); // Remove the full match element
79
- route.handler.apply({}, match);
79
+ const queryParams = Object.fromEntries(new URLSearchParams(window.location.search));
80
+ route.handler.apply({queryParams}, match);
80
81
  return true
81
82
  }
82
83
  }
@@ -21,14 +21,14 @@ export class StateManager {
21
21
  public static dispatcher = dispatcher
22
22
  private changeAuthorities: ChangeAuthority[] = [];
23
23
 
24
- constructor(private mode: RoutingMode = 'hash', autostart= true) {
24
+ constructor(private mode: RoutingMode = 'hash', autostart = true) {
25
25
 
26
26
  if (autostart)
27
27
  router.listen(mode)
28
28
  }
29
29
 
30
30
  start() {
31
- router.listen( this.mode )
31
+ router.listen(this.mode)
32
32
  }
33
33
 
34
34
  onChange(handler: (event: PubSubEvent, data: any) => void): IPubSubHandle {
@@ -61,16 +61,17 @@ export class StateManager {
61
61
  * set current page state
62
62
  * @param state can be either just a state or a state and context (which can be sub-state, or anything else)
63
63
  */
64
- set state(state: ApplicationStateName | [ApplicationStateName, any]) {
65
- if (Array.isArray(state))
66
- this.setState(state[0], state[1])
67
- else
64
+ set state(state: ApplicationStateName | [ApplicationStateName, ...any]) {
65
+ if (Array.isArray(state)) {
66
+ const sName = state.shift()
67
+ this.setState(sName,state)
68
+ } else
68
69
  this.setState(state)
69
70
  }
70
71
 
71
72
  /** attempts to restore state from current url. Currently, works only in hash mode */
72
73
  restoreState(defaultState: ApplicationStateName) {
73
- if ( router.navigate(window.location.pathname))
74
+ if (router.navigate(window.location.pathname))
74
75
  return
75
76
  router.navigate(defaultState)
76
77
  }
@@ -130,10 +131,9 @@ export class StateManager {
130
131
  if (await this.setState(state.name, context)) {
131
132
 
132
133
  // @ts-ignore
133
- if (window.ga) {
134
- // @ts-ignore
135
- window.ga('send', 'pageview', `/${state.name}/${context || ''}`);
136
- }
134
+ window.pageChangeHandler && window.pageChangeHandler('send', 'pageview', `/${state.name}/${context || ''}`);
135
+ // @ts-ignore
136
+ window.ga && window.ga('send', 'pageview', `/${state.name}/${context || ''}`);
137
137
  }
138
138
  })
139
139
  }