@base-framework/base 2.6.0 → 2.6.1

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.
Files changed (190) hide show
  1. package/base.min.js +1 -0
  2. package/es5/base.js +2968 -0
  3. package/es5/modules/ajax.js +663 -0
  4. package/es5/modules/animation.js +188 -0
  5. package/es5/modules/animations.js +1080 -0
  6. package/es5/modules/atom.js +65 -0
  7. package/es5/modules/component.js +1310 -0
  8. package/es5/modules/data-binder.js +1131 -0
  9. package/es5/modules/data.js +1808 -0
  10. package/es5/modules/date.js +525 -0
  11. package/es5/modules/form-validator.js +324 -0
  12. package/es5/modules/history.js +126 -0
  13. package/es5/modules/html-builder.js +461 -0
  14. package/es5/modules/layout.js +1679 -0
  15. package/es5/modules/mouse.js +124 -0
  16. package/es5/modules/nav-link.js +123 -0
  17. package/es5/modules/olderversions/animations-ease.js +1095 -0
  18. package/es5/modules/olderversions/animations-update.js +1048 -0
  19. package/es5/modules/olderversions/base-animations.js +636 -0
  20. package/es5/modules/olderversions/base-component-class.js +100 -0
  21. package/es5/modules/olderversions/base-data-binder-1.js +407 -0
  22. package/es5/modules/olderversions/base-data-binder-class.js +358 -0
  23. package/es5/modules/olderversions/base-layout-parser-class.js +172 -0
  24. package/es5/modules/olderversions/base-mode-1.js +777 -0
  25. package/es5/modules/olderversions/base-model-class.js +585 -0
  26. package/es5/modules/olderversions/data-binder/element-binding/base-data-binder-class.js +358 -0
  27. package/es5/modules/olderversions/data-binder/element-binding/base-model-class.js +585 -0
  28. package/es5/modules/olderversions/data-binder/model-binding/base-data-binder-class.js +353 -0
  29. package/es5/modules/olderversions/data-binder/model-binding/base-model-class.js +604 -0
  30. package/es5/modules/olderversions/data-binder-update-watcher.js +640 -0
  31. package/es5/modules/olderversions/data-tracker.js +187 -0
  32. package/es5/modules/olderversions/event-update.js +666 -0
  33. package/es5/modules/olderversions/nav-link.js +119 -0
  34. package/es5/modules/olderversions/router-with-templates-1.js +785 -0
  35. package/es5/modules/olderversions/router-with-templates.js +701 -0
  36. package/es5/modules/prototypes/ajax.js +657 -0
  37. package/es5/modules/prototypes/atom.js +65 -0
  38. package/es5/modules/prototypes/component.js +972 -0
  39. package/es5/modules/prototypes/data-binder.js +1089 -0
  40. package/es5/modules/prototypes/data.js +1290 -0
  41. package/es5/modules/prototypes/html-builder.js +414 -0
  42. package/es5/modules/prototypes/layout.js +879 -0
  43. package/es5/modules/router.js +1680 -0
  44. package/es5/modules/state.js +274 -0
  45. package/es6/.jshintrc +3 -0
  46. package/es6/base.js +41 -0
  47. package/es6/core.js +1 -0
  48. package/es6/data-tracker.js +351 -0
  49. package/es6/events.js +602 -0
  50. package/es6/legacy/es5/base.js +2968 -0
  51. package/es6/legacy/es5/modules/ajax.js +663 -0
  52. package/es6/legacy/es5/modules/animation.js +188 -0
  53. package/es6/legacy/es5/modules/animations.js +1080 -0
  54. package/es6/legacy/es5/modules/atom.js +65 -0
  55. package/es6/legacy/es5/modules/component.js +1310 -0
  56. package/es6/legacy/es5/modules/data-binder.js +1131 -0
  57. package/es6/legacy/es5/modules/data.js +1808 -0
  58. package/es6/legacy/es5/modules/date.js +525 -0
  59. package/es6/legacy/es5/modules/form-validator.js +324 -0
  60. package/es6/legacy/es5/modules/history.js +126 -0
  61. package/es6/legacy/es5/modules/html-builder.js +461 -0
  62. package/es6/legacy/es5/modules/layout.js +1679 -0
  63. package/es6/legacy/es5/modules/mouse.js +124 -0
  64. package/es6/legacy/es5/modules/nav-link.js +123 -0
  65. package/es6/legacy/es5/modules/olderversions/animations-ease.js +1095 -0
  66. package/es6/legacy/es5/modules/olderversions/animations-update.js +1048 -0
  67. package/es6/legacy/es5/modules/olderversions/base-animations.js +636 -0
  68. package/es6/legacy/es5/modules/olderversions/base-component-class.js +100 -0
  69. package/es6/legacy/es5/modules/olderversions/base-data-binder-1.js +407 -0
  70. package/es6/legacy/es5/modules/olderversions/base-data-binder-class.js +358 -0
  71. package/es6/legacy/es5/modules/olderversions/base-layout-parser-class.js +172 -0
  72. package/es6/legacy/es5/modules/olderversions/base-mode-1.js +777 -0
  73. package/es6/legacy/es5/modules/olderversions/base-model-class.js +585 -0
  74. package/es6/legacy/es5/modules/olderversions/data-binder/element-binding/base-data-binder-class.js +358 -0
  75. package/es6/legacy/es5/modules/olderversions/data-binder/element-binding/base-model-class.js +585 -0
  76. package/es6/legacy/es5/modules/olderversions/data-binder/model-binding/base-data-binder-class.js +353 -0
  77. package/es6/legacy/es5/modules/olderversions/data-binder/model-binding/base-model-class.js +604 -0
  78. package/es6/legacy/es5/modules/olderversions/data-binder-update-watcher.js +640 -0
  79. package/es6/legacy/es5/modules/olderversions/data-tracker.js +187 -0
  80. package/es6/legacy/es5/modules/olderversions/event-update.js +666 -0
  81. package/es6/legacy/es5/modules/olderversions/nav-link.js +119 -0
  82. package/es6/legacy/es5/modules/olderversions/router-with-templates-1.js +785 -0
  83. package/es6/legacy/es5/modules/olderversions/router-with-templates.js +701 -0
  84. package/es6/legacy/es5/modules/prototypes/ajax.js +657 -0
  85. package/es6/legacy/es5/modules/prototypes/atom.js +65 -0
  86. package/es6/legacy/es5/modules/prototypes/component.js +972 -0
  87. package/es6/legacy/es5/modules/prototypes/data-binder.js +1089 -0
  88. package/es6/legacy/es5/modules/prototypes/data.js +1290 -0
  89. package/es6/legacy/es5/modules/prototypes/html-builder.js +414 -0
  90. package/es6/legacy/es5/modules/prototypes/layout.js +879 -0
  91. package/es6/legacy/es5/modules/router.js +1680 -0
  92. package/es6/legacy/es5/modules/state.js +274 -0
  93. package/es6/main.js +1331 -0
  94. package/es6/modules/ajax/ajax.js +514 -0
  95. package/es6/modules/animation/animation.js +236 -0
  96. package/es6/modules/animations/animation-controller.js +231 -0
  97. package/es6/modules/animations/animation.js +64 -0
  98. package/es6/modules/animations/attr-movement.js +66 -0
  99. package/es6/modules/animations/css-movement.js +170 -0
  100. package/es6/modules/animations/movement.js +131 -0
  101. package/es6/modules/animations/value.js +187 -0
  102. package/es6/modules/atom/atom.js +54 -0
  103. package/es6/modules/component/component.js +230 -0
  104. package/es6/modules/component/event-helper.js +119 -0
  105. package/es6/modules/component/jot.js +144 -0
  106. package/es6/modules/component/state-helper.js +262 -0
  107. package/es6/modules/component/unit.js +551 -0
  108. package/es6/modules/data/attrs.js +40 -0
  109. package/es6/modules/data/basic-data.js +500 -0
  110. package/es6/modules/data/data-utils.js +29 -0
  111. package/es6/modules/data/data.js +3 -0
  112. package/es6/modules/data/deep-data.js +541 -0
  113. package/es6/modules/data/model-service.js +528 -0
  114. package/es6/modules/data/model.js +133 -0
  115. package/es6/modules/data/simple-data.js +33 -0
  116. package/es6/modules/data-binder/connection-tracker.js +113 -0
  117. package/es6/modules/data-binder/connection.js +16 -0
  118. package/es6/modules/data-binder/data-binder.js +352 -0
  119. package/es6/modules/data-binder/data-pub-sub.js +141 -0
  120. package/es6/modules/data-binder/data-source.js +56 -0
  121. package/es6/modules/data-binder/element-source.js +219 -0
  122. package/es6/modules/data-binder/one-way-connection.js +46 -0
  123. package/es6/modules/data-binder/one-way-source.js +43 -0
  124. package/es6/modules/data-binder/source.js +36 -0
  125. package/es6/modules/data-binder/two-way-connection.js +75 -0
  126. package/es6/modules/data-binder/two-way-source.js +41 -0
  127. package/es6/modules/date/date.js +544 -0
  128. package/es6/modules/history/history.js +89 -0
  129. package/es6/modules/html-builder/html-builder.js +434 -0
  130. package/es6/modules/import/import.js +390 -0
  131. package/es6/modules/layout/layout-builder.js +1269 -0
  132. package/es6/modules/layout/layout-parser.js +134 -0
  133. package/es6/modules/layout/watcher-helper.js +282 -0
  134. package/es6/modules/mouse/mouse.js +114 -0
  135. package/es6/modules/router/component-helper.js +163 -0
  136. package/es6/modules/router/history-controller.js +216 -0
  137. package/es6/modules/router/nav-link.js +124 -0
  138. package/es6/modules/router/route.js +401 -0
  139. package/es6/modules/router/router.js +789 -0
  140. package/es6/modules/router/utils.js +31 -0
  141. package/es6/modules/state/state-target.js +91 -0
  142. package/es6/modules/state/state.js +171 -0
  143. package/es6/package-lock.json +13 -0
  144. package/es6/package.json +28 -0
  145. package/es6/shared/objects.js +99 -0
  146. package/legacy/es5/base.js +2968 -0
  147. package/legacy/es5/modules/ajax.js +663 -0
  148. package/legacy/es5/modules/animation.js +188 -0
  149. package/legacy/es5/modules/animations.js +1080 -0
  150. package/legacy/es5/modules/atom.js +65 -0
  151. package/legacy/es5/modules/component.js +1310 -0
  152. package/legacy/es5/modules/data-binder.js +1131 -0
  153. package/legacy/es5/modules/data.js +1808 -0
  154. package/legacy/es5/modules/date.js +525 -0
  155. package/legacy/es5/modules/form-validator.js +324 -0
  156. package/legacy/es5/modules/history.js +126 -0
  157. package/legacy/es5/modules/html-builder.js +461 -0
  158. package/legacy/es5/modules/layout.js +1679 -0
  159. package/legacy/es5/modules/mouse.js +124 -0
  160. package/legacy/es5/modules/nav-link.js +123 -0
  161. package/legacy/es5/modules/olderversions/animations-ease.js +1095 -0
  162. package/legacy/es5/modules/olderversions/animations-update.js +1048 -0
  163. package/legacy/es5/modules/olderversions/base-animations.js +636 -0
  164. package/legacy/es5/modules/olderversions/base-component-class.js +100 -0
  165. package/legacy/es5/modules/olderversions/base-data-binder-1.js +407 -0
  166. package/legacy/es5/modules/olderversions/base-data-binder-class.js +358 -0
  167. package/legacy/es5/modules/olderversions/base-layout-parser-class.js +172 -0
  168. package/legacy/es5/modules/olderversions/base-mode-1.js +777 -0
  169. package/legacy/es5/modules/olderversions/base-model-class.js +585 -0
  170. package/legacy/es5/modules/olderversions/data-binder/element-binding/base-data-binder-class.js +358 -0
  171. package/legacy/es5/modules/olderversions/data-binder/element-binding/base-model-class.js +585 -0
  172. package/legacy/es5/modules/olderversions/data-binder/model-binding/base-data-binder-class.js +353 -0
  173. package/legacy/es5/modules/olderversions/data-binder/model-binding/base-model-class.js +604 -0
  174. package/legacy/es5/modules/olderversions/data-binder-update-watcher.js +640 -0
  175. package/legacy/es5/modules/olderversions/data-tracker.js +187 -0
  176. package/legacy/es5/modules/olderversions/event-update.js +666 -0
  177. package/legacy/es5/modules/olderversions/nav-link.js +119 -0
  178. package/legacy/es5/modules/olderversions/router-with-templates-1.js +785 -0
  179. package/legacy/es5/modules/olderversions/router-with-templates.js +701 -0
  180. package/legacy/es5/modules/prototypes/ajax.js +657 -0
  181. package/legacy/es5/modules/prototypes/atom.js +65 -0
  182. package/legacy/es5/modules/prototypes/component.js +972 -0
  183. package/legacy/es5/modules/prototypes/data-binder.js +1089 -0
  184. package/legacy/es5/modules/prototypes/data.js +1290 -0
  185. package/legacy/es5/modules/prototypes/html-builder.js +414 -0
  186. package/legacy/es5/modules/prototypes/layout.js +879 -0
  187. package/legacy/es5/modules/router.js +1680 -0
  188. package/legacy/es5/modules/state.js +274 -0
  189. package/package.json +8 -3
  190. package/update +16 -0
@@ -0,0 +1,789 @@
1
+ import {base} from '../../core.js';
2
+ import {Data} from '../data/data.js';
3
+ export {NavLink} from './nav-link.js';
4
+ import {Utils} from './utils.js';
5
+ import {Route} from './route.js';
6
+ import {HistoryController} from './history-controller.js';
7
+
8
+ /* this will register the route system to the
9
+ data tracker to remove routes that have been
10
+ nested in layouts. */
11
+ base.dataTracker.addType('routes', (data) =>
12
+ {
13
+ if(!data)
14
+ {
15
+ return false;
16
+ }
17
+
18
+ const route = data.route;
19
+ if(route)
20
+ {
21
+ router.removeRoute(route);
22
+ }
23
+ });
24
+
25
+ base.dataTracker.addType('switch', (data) =>
26
+ {
27
+ if(!data)
28
+ {
29
+ return false;
30
+ }
31
+
32
+ let id = data.id;
33
+ router.removeSwitch(id);
34
+ });
35
+
36
+ /**
37
+ * Router
38
+ *
39
+ * This will create a browser router.
40
+ * @class
41
+ */
42
+ export class Router
43
+ {
44
+ constructor()
45
+ {
46
+ /**
47
+ * @member {string} version
48
+ */
49
+ this.version = '1.0.2';
50
+
51
+ /* this is the root of the uri for the routing object
52
+ and the base title */
53
+ this.baseURI = '/';
54
+ this.title = '';
55
+
56
+ this.lastPath = null;
57
+ this.path = null;
58
+
59
+ /* this will be used to access our history object */
60
+ this.history = null;
61
+ this.callBackLink = null;
62
+ this.location = window.location;
63
+
64
+ /* this will store each route added to the
65
+ router. */
66
+ this.routes = [];
67
+ this.switches = {};
68
+ this.switchCount = 0;
69
+
70
+ /**
71
+ * @member {object} data
72
+ */
73
+ this.data = new Data(
74
+ {
75
+ path: ''
76
+ });
77
+ }
78
+
79
+ /**
80
+ * This will setup our history object.
81
+ */
82
+ setupHistory()
83
+ {
84
+ this.history = HistoryController.setup(this);
85
+ }
86
+
87
+ /**
88
+ * This will create a new route.
89
+ *
90
+ * @protected
91
+ * @param {object} settings
92
+ * @return {object}
93
+ */
94
+ createRoute(settings)
95
+ {
96
+ let uri = settings.uri || '*';
97
+ settings.baseUri = this.createURI(uri);
98
+
99
+ let route = new Route(settings);
100
+ return route;
101
+ }
102
+
103
+ /**
104
+ * This will add a new route to the router.
105
+ *
106
+ * @param {object} settings
107
+ * @return {object}
108
+ */
109
+ add(settings)
110
+ {
111
+ if(typeof settings !== 'object')
112
+ {
113
+ let args = arguments;
114
+ settings =
115
+ {
116
+ uri: args[0],
117
+ component: args[1],
118
+ callBack: args[2],
119
+ title: args[3],
120
+ id: args[4],
121
+ container: args[5]
122
+ };
123
+ }
124
+
125
+ const route = this.createRoute(settings);
126
+ this.addRoute(route);
127
+ return route;
128
+ }
129
+
130
+ addRoute(route)
131
+ {
132
+ this.routes.push(route);
133
+ this.checkRoute(route, this.getPath());
134
+ }
135
+
136
+ /**
137
+ * This will resume a route.
138
+ *
139
+ * @param {object} route
140
+ * @param {object} container
141
+ */
142
+ resume(route, container)
143
+ {
144
+ route.resume(container);
145
+ this.addRoute(route);
146
+ }
147
+
148
+ /**
149
+ * This will get the base path.
150
+ *
151
+ * @protected
152
+ * @return {string}
153
+ */
154
+ getBasePath()
155
+ {
156
+ if(!this.basePath)
157
+ {
158
+ let pathURI = this.baseURI || '';
159
+ if((pathURI[pathURI.length - 1] !== '/'))
160
+ {
161
+ pathURI += '/';
162
+ }
163
+ this.basePath = pathURI;
164
+ }
165
+ return this.basePath;
166
+ }
167
+
168
+ /**
169
+ * This will create a uri.
170
+ *
171
+ * @protected
172
+ * @param {string} uri
173
+ * @return {string}
174
+ */
175
+ createURI(uri)
176
+ {
177
+ let baseUri = this.getBasePath();
178
+ return (baseUri + Utils.removeSlashes(uri));
179
+ }
180
+
181
+ /**
182
+ * This will get a route by uri.
183
+ *
184
+ * @param {string} uri
185
+ * @return {(object|boolean)}
186
+ */
187
+ getRoute(uri)
188
+ {
189
+ let routes = this.routes,
190
+ length = routes.length;
191
+ if(length > 0)
192
+ {
193
+ for(var i = 0; i < length; i++)
194
+ {
195
+ var route = routes[i];
196
+ if(route.uri === uri)
197
+ {
198
+ return route;
199
+ }
200
+ }
201
+ }
202
+ return false;
203
+ }
204
+
205
+ /**
206
+ * This will get a route by id.
207
+ *
208
+ * @param {string} id
209
+ * @return {(object|boolean)}
210
+ */
211
+ getRouteById(id)
212
+ {
213
+ let routes = this.routes,
214
+ length = routes.length;
215
+ if(length > 0)
216
+ {
217
+ for(var i = 0; i < length; i++)
218
+ {
219
+ var route = routes[i];
220
+ if(route.id === id)
221
+ {
222
+ return route;
223
+ }
224
+ }
225
+ }
226
+ return false;
227
+ }
228
+
229
+ /**
230
+ * This will remove a route.
231
+ *
232
+ * @param {object} route
233
+ */
234
+ removeRoute(route)
235
+ {
236
+ let routes = this.routes,
237
+ index = routes.indexOf(route);
238
+ if(index > -1)
239
+ {
240
+ routes.splice(index, 1);
241
+ }
242
+ }
243
+
244
+ /**
245
+ * This will add a switch.
246
+ *
247
+ * @param {array} group
248
+ * @return {string} the switch id.
249
+ */
250
+ addSwitch(group)
251
+ {
252
+ let id = this.switchCount++;
253
+ let switchArray = this.getSwitchGroup(id);
254
+
255
+ for(var i = 0, length = group.length; i < length; i++)
256
+ {
257
+ var route = this.createRoute(group[i]);
258
+ switchArray.push(route);
259
+ }
260
+
261
+ this.checkGroup(switchArray, this.getPath());
262
+ return id;
263
+ }
264
+
265
+ /**
266
+ * This will resume a switch.
267
+ *
268
+ * @param {object} group
269
+ * @param {object} container
270
+ * @return {int} the switch id.
271
+ */
272
+ resumeSwitch(group, container)
273
+ {
274
+ let id = this.switchCount++;
275
+ let switchArray = this.getSwitchGroup(id);
276
+
277
+ for(var i = 0, length = group.length; i < length; i++)
278
+ {
279
+ var route = group[i].component.route;
280
+ route.resume(container);
281
+ switchArray.push(route);
282
+ }
283
+
284
+ this.checkGroup(switchArray, this.getPath());
285
+ return id;
286
+ }
287
+
288
+ getSwitchGroup(id)
289
+ {
290
+ return (this.switches[id] = []);
291
+ }
292
+
293
+ /**
294
+ * This will remove a switch by id.
295
+ *
296
+ * @param {string} id
297
+ */
298
+ removeSwitch(id)
299
+ {
300
+ let switches = this.switches;
301
+ if(switches[id])
302
+ {
303
+ delete switches[id];
304
+ }
305
+ }
306
+
307
+ /**
308
+ * This will remove a route by uri.
309
+ *
310
+ * @param {string} uri
311
+ * @return {object} a reference to the router object.
312
+ */
313
+ remove(uri)
314
+ {
315
+ uri = this.createURI(uri);
316
+
317
+ let route = this.getRoute(uri);
318
+ if(route !== false)
319
+ {
320
+ this.removeRoute(route);
321
+ }
322
+ return this;
323
+ }
324
+
325
+ /**
326
+ * This will setup the router.
327
+ *
328
+ * @param {string} [baseURI]
329
+ * @param {string} [title]
330
+ * @return {object} a reference to the router object.
331
+ */
332
+ setup(baseURI, title)
333
+ {
334
+ this.baseURI = baseURI || '/';
335
+ this.updateBaseTag(this.baseURI);
336
+ this.title = (typeof title !== 'undefined')? title : '';
337
+
338
+ this.setupHistory();
339
+
340
+ this.data.set('path', this.getPath());
341
+
342
+ this.callBackLink = this.checkLink.bind(this);
343
+ base.on('click', document, this.callBackLink);
344
+
345
+ /* this will route to the first url entered
346
+ when the router loads. this will fix the issue
347
+ that stopped the first endpoint from being
348
+ added to the history */
349
+ let endPoint = this.getEndPoint();
350
+ this.navigate(endPoint, null, true);
351
+ return this;
352
+ }
353
+
354
+ updateBaseTag(url)
355
+ {
356
+ /* this will modify the base tag to ref from
357
+ the base url for all xhr */
358
+ let ele = document.getElementsByTagName('base');
359
+ if(ele.length)
360
+ {
361
+ ele[0].href = url;
362
+ }
363
+ }
364
+
365
+ /**
366
+ * This will get the parent element link.
367
+ *
368
+ * @param {object} ele
369
+ * @return {(object|boolean)}
370
+ */
371
+ getParentLink(ele)
372
+ {
373
+ let target = ele.parentNode;
374
+ while(target !== null)
375
+ {
376
+ if(target.nodeName.toLowerCase() === 'a')
377
+ {
378
+ return target;
379
+ }
380
+
381
+ target = target.parentNode;
382
+ }
383
+ return false;
384
+ }
385
+
386
+ /**
387
+ * This will check if a link was routed.
388
+ *
389
+ * @protected
390
+ * @param {object} evt
391
+ */
392
+ checkLink(evt)
393
+ {
394
+ if(evt.ctrlKey === true)
395
+ {
396
+ return true;
397
+ }
398
+
399
+ let target = evt.target || evt.srcElement;
400
+ if(target.nodeName.toLowerCase() !== 'a')
401
+ {
402
+ /* this will check to get the parent to check
403
+ if the child is contained in a link */
404
+ target = this.getParentLink(target);
405
+ if(target === false)
406
+ {
407
+ return true;
408
+ }
409
+ }
410
+
411
+ if(target.target === '_blank' || base.data(target, 'cancel-route'))
412
+ {
413
+ return true;
414
+ }
415
+
416
+ let href = target.getAttribute('href');
417
+ if(typeof href !== 'undefined')
418
+ {
419
+ let baseUri = this.baseURI,
420
+ path = (baseUri !== '/')? href.replace(baseUri, '') : href;
421
+ this.navigate(path);
422
+
423
+ evt.preventDefault();
424
+ evt.stopPropagation();
425
+ return false;
426
+ }
427
+ }
428
+
429
+ /**
430
+ * This will reset the router.
431
+ *
432
+ * @return {object} a reference to the router object.
433
+ */
434
+ reset()
435
+ {
436
+ this.routes = [];
437
+ this.switches = [];
438
+ this.switchCount = 0;
439
+
440
+ return this;
441
+ }
442
+
443
+ /**
444
+ * This will check the active routes.
445
+ *
446
+ * @return {object} a reference to the router object.
447
+ */
448
+ activate()
449
+ {
450
+ this.checkActiveRoutes();
451
+ return this;
452
+ }
453
+
454
+ /**
455
+ * This will navigate the router.
456
+ *
457
+ * @param {string} uri
458
+ * @param {object} [data]
459
+ * @param {boolean} [replace]
460
+ * @return {object} a reference to the router object.
461
+ */
462
+ navigate(uri, data, replace)
463
+ {
464
+ uri = this.createURI(uri);
465
+ this.history.addState(uri, data, replace);
466
+ this.activate();
467
+
468
+ return this;
469
+ }
470
+
471
+ /**
472
+ * This will update the data path.
473
+ * @protected
474
+ */
475
+ updatePath()
476
+ {
477
+ let path = this.getPath();
478
+ this.data.set('path', path);
479
+ }
480
+
481
+ /**
482
+ * This will update the title.
483
+ *
484
+ * @protected
485
+ * @param {object} route
486
+ */
487
+ updateTitle(route)
488
+ {
489
+ if(!route || !route.title)
490
+ {
491
+ return this;
492
+ }
493
+
494
+ let getTitle = (title) =>
495
+ {
496
+ /* this will uppercase each word in a string
497
+ @param (string) str = the string to uppercase
498
+ @return (string) the uppercase string */
499
+ let toTitleCase = (str) =>
500
+ {
501
+ let pattern = /\w\S*/;
502
+ return str.replace(pattern, (txt) =>
503
+ {
504
+ return txt.charAt(0).toUpperCase() + txt.substring(1).toLowerCase();
505
+ });
506
+ };
507
+
508
+
509
+ /* this will replace the params in the title
510
+ @param (string) str = the route title
511
+ @return (string) the title string */
512
+ let replaceParams = (str) =>
513
+ {
514
+ if(str.indexOf(':') > -1)
515
+ {
516
+ let params = route.stage;
517
+ for(var prop in params)
518
+ {
519
+ if(params.hasOwnProperty(prop))
520
+ {
521
+ var param = params[prop],
522
+ pattern = new RegExp(':' + prop, 'gi');
523
+ str = str.replace(pattern, param);
524
+ }
525
+ }
526
+ }
527
+ return str;
528
+ };
529
+
530
+ if(title)
531
+ {
532
+ if(typeof title === 'function')
533
+ {
534
+ title = title(route.stage);
535
+ }
536
+
537
+ /* we want to replace any params in the title
538
+ and uppercase the title */
539
+ title = replaceParams(title);
540
+ title = toTitleCase(title);
541
+
542
+ /* we want to check to add the base title to the
543
+ to the end of the title */
544
+ if(this.title !== '')
545
+ {
546
+ title += " - " + this.title;
547
+ }
548
+ }
549
+ return title;
550
+ };
551
+
552
+ let title = route.title;
553
+ document.title = getTitle(title);
554
+ }
555
+
556
+ /**
557
+ * This will check the routes to match the path.
558
+ *
559
+ * @protected
560
+ * @param {string} [path]
561
+ */
562
+ checkActiveRoutes(path)
563
+ {
564
+ this.lastPath = path;
565
+
566
+ path = path || this.getPath();
567
+ this.path = path;
568
+
569
+ let routes = this.routes,
570
+ length = routes.length;
571
+
572
+ let route;
573
+ for(var i = 0; i < length; i++)
574
+ {
575
+ route = routes[i];
576
+ if(typeof route === 'undefined')
577
+ {
578
+ continue;
579
+ }
580
+
581
+ this.checkRoute(route, path);
582
+ }
583
+
584
+ this.checkSwitches(path);
585
+ this.updatePath();
586
+ }
587
+
588
+ /**
589
+ * This will check the switches to match the path.
590
+ *
591
+ * @protected
592
+ * @param {string} [path]
593
+ */
594
+ checkSwitches(path)
595
+ {
596
+ let switches = this.switches;
597
+ for(var id in switches)
598
+ {
599
+ if(switches.hasOwnProperty(id) === false)
600
+ {
601
+ continue;
602
+ }
603
+
604
+ var group = switches[id];
605
+ this.checkGroup(group, path);
606
+ }
607
+ }
608
+
609
+ /**
610
+ * This will check a group to match a path.
611
+ *
612
+ * @protected
613
+ * @param {object} group
614
+ * @param {string} path
615
+ */
616
+ checkGroup(group, path)
617
+ {
618
+ let check = false,
619
+ route, firstRoute, lastSelected, selected, hasController = false;
620
+
621
+ for(var i = 0, length = group.length; i < length; i++)
622
+ {
623
+ route = group[i];
624
+ if(typeof route === 'undefined')
625
+ {
626
+ continue;
627
+ }
628
+
629
+ /* we want to save the first route in the switch
630
+ so it can be selected if no route is active */
631
+ if(i === 0)
632
+ {
633
+ firstRoute = route;
634
+ }
635
+
636
+ if(!lastSelected && route.get('active'))
637
+ {
638
+ lastSelected = route;
639
+ }
640
+
641
+ if(check !== false)
642
+ {
643
+ if(hasController)
644
+ {
645
+ route.deactivate();
646
+ }
647
+ continue;
648
+ }
649
+
650
+ /* we will break the loop on the first match */
651
+ check = route.match(path);
652
+ if(check !== false)
653
+ {
654
+ selected = route;
655
+
656
+ if(route.controller)
657
+ {
658
+ this.select(route);
659
+ hasController = true;
660
+ }
661
+ }
662
+ }
663
+
664
+ if(selected === undefined)
665
+ {
666
+ this.select(firstRoute);
667
+
668
+ if(lastSelected && firstRoute !== lastSelected)
669
+ {
670
+ lastSelected.deactivate();
671
+ }
672
+ }
673
+ else
674
+ {
675
+ if(lastSelected)
676
+ {
677
+ if(hasController && selected !== lastSelected)
678
+ {
679
+ lastSelected.deactivate();
680
+ }
681
+ }
682
+ else if(firstRoute && hasController === false)
683
+ {
684
+ this.select(firstRoute);
685
+ }
686
+ }
687
+ }
688
+
689
+ /**
690
+ * This will check if a route matches the path.
691
+ *
692
+ * @param {object} route
693
+ * @param {string} path
694
+ * @return {boolean}
695
+ */
696
+ checkRoute(route, path)
697
+ {
698
+ let check = this.check(route, path);
699
+ if(check !== false)
700
+ {
701
+ this.select(route);
702
+ }
703
+ else
704
+ {
705
+ route.deactivate();
706
+ }
707
+ return check;
708
+ }
709
+
710
+ /**
711
+ * This will select a route if the route matches the path.
712
+ *
713
+ * @param {object} route
714
+ * @param {string} [path]
715
+ */
716
+ check(route, path)
717
+ {
718
+ /* we want to check if the route has been
719
+ deleted from the routes */
720
+ if(!route)
721
+ {
722
+ return false;
723
+ }
724
+
725
+ /* we want to check to use the supplied uri or get the
726
+ current uri if not setup */
727
+ path = path || this.getPath();
728
+
729
+ /* we want to check if the route uri matches the path uri */
730
+ return (route.match(path) !== false);
731
+ }
732
+
733
+ /**
734
+ * This will select the route.
735
+ *
736
+ * @param {object} route
737
+ */
738
+ select(route)
739
+ {
740
+ if(!route)
741
+ {
742
+ return false;
743
+ }
744
+
745
+ route.setPath(this.path, this.lastPath);
746
+ route.select();
747
+ this.updateTitle(route);
748
+ }
749
+
750
+ /**
751
+ * This will get the endpoint.
752
+ *
753
+ * @return {string}
754
+ */
755
+ getEndPoint()
756
+ {
757
+ let path = this.getPath();
758
+ return (path.replace(this.baseURI, '') || '/');
759
+ }
760
+
761
+ /**
762
+ * This will remove the router events.
763
+ */
764
+ destroy()
765
+ {
766
+ base.off('click', document, this.callBackLink);
767
+ }
768
+
769
+ /**
770
+ * This will get the location pathname.
771
+ *
772
+ * @return {string}
773
+ */
774
+ getPath()
775
+ {
776
+ /* we want to get the window location path */
777
+ let location = this.location,
778
+ path = this.path = location.pathname;
779
+
780
+ if(this.history.type === 'hash')
781
+ {
782
+ return location.hash.replace('#', '');
783
+ }
784
+
785
+ return path + location.search + location.hash;
786
+ }
787
+ }
788
+
789
+ export const router = new Router();