@base-framework/base 2.6.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.
Files changed (57) hide show
  1. package/.jshintrc +3 -0
  2. package/base.js +41 -0
  3. package/core.js +1 -0
  4. package/data-tracker.js +351 -0
  5. package/events.js +602 -0
  6. package/main.js +1331 -0
  7. package/modules/ajax/ajax.js +514 -0
  8. package/modules/animation/animation.js +236 -0
  9. package/modules/animations/animation-controller.js +231 -0
  10. package/modules/animations/animation.js +64 -0
  11. package/modules/animations/attr-movement.js +66 -0
  12. package/modules/animations/css-movement.js +170 -0
  13. package/modules/animations/movement.js +131 -0
  14. package/modules/animations/value.js +187 -0
  15. package/modules/atom/atom.js +54 -0
  16. package/modules/component/component.js +230 -0
  17. package/modules/component/event-helper.js +119 -0
  18. package/modules/component/jot.js +144 -0
  19. package/modules/component/state-helper.js +262 -0
  20. package/modules/component/unit.js +551 -0
  21. package/modules/data/attrs.js +40 -0
  22. package/modules/data/basic-data.js +500 -0
  23. package/modules/data/data-utils.js +29 -0
  24. package/modules/data/data.js +3 -0
  25. package/modules/data/deep-data.js +541 -0
  26. package/modules/data/model-service.js +528 -0
  27. package/modules/data/model.js +133 -0
  28. package/modules/data/simple-data.js +33 -0
  29. package/modules/data-binder/connection-tracker.js +113 -0
  30. package/modules/data-binder/connection.js +16 -0
  31. package/modules/data-binder/data-binder.js +352 -0
  32. package/modules/data-binder/data-pub-sub.js +141 -0
  33. package/modules/data-binder/data-source.js +56 -0
  34. package/modules/data-binder/element-source.js +219 -0
  35. package/modules/data-binder/one-way-connection.js +46 -0
  36. package/modules/data-binder/one-way-source.js +43 -0
  37. package/modules/data-binder/source.js +36 -0
  38. package/modules/data-binder/two-way-connection.js +75 -0
  39. package/modules/data-binder/two-way-source.js +41 -0
  40. package/modules/date/date.js +544 -0
  41. package/modules/history/history.js +89 -0
  42. package/modules/html-builder/html-builder.js +434 -0
  43. package/modules/import/import.js +390 -0
  44. package/modules/layout/layout-builder.js +1269 -0
  45. package/modules/layout/layout-parser.js +134 -0
  46. package/modules/layout/watcher-helper.js +282 -0
  47. package/modules/mouse/mouse.js +114 -0
  48. package/modules/router/component-helper.js +163 -0
  49. package/modules/router/history-controller.js +216 -0
  50. package/modules/router/nav-link.js +124 -0
  51. package/modules/router/route.js +401 -0
  52. package/modules/router/router.js +789 -0
  53. package/modules/router/utils.js +31 -0
  54. package/modules/state/state-target.js +91 -0
  55. package/modules/state/state.js +171 -0
  56. package/package.json +23 -0
  57. package/shared/objects.js +99 -0
@@ -0,0 +1,514 @@
1
+ import {base} from '../../core.js';
2
+
3
+ /**
4
+ * This is the default xhr (ajax) settings.
5
+ */
6
+ let XhrDefaultSettings =
7
+ {
8
+ url: '',
9
+
10
+ /* this is the responseType of the server
11
+ response (string) */
12
+ responseType: 'json',
13
+
14
+ /* this is the server method */
15
+ method: 'POST',
16
+
17
+ /* this can fix a param string to be added
18
+ to every ajax request */
19
+ fixedParams: '',
20
+
21
+ /* headers (object) */
22
+ headers:
23
+ {
24
+ 'Content-Type': 'application/x-www-form-urlencoded; charset=UTF-8'
25
+ },
26
+
27
+ beforeSend: [],
28
+
29
+ /* this will set the ajax request to async (bool) */
30
+ async: true,
31
+
32
+ /* cross domain (bool) */
33
+ crossDomain: false,
34
+
35
+ /* cors with credentials (bool) */
36
+ withCredentials: false,
37
+
38
+ /* events (function) */
39
+ completed: null,
40
+ failed: null,
41
+ aborted: null,
42
+ progress: null
43
+ };
44
+
45
+ /* this will add ajax settings to the base class */
46
+ base.augment(
47
+ {
48
+ /**
49
+ * @member {object} xhrSettings
50
+ */
51
+ xhrSettings: XhrDefaultSettings,
52
+
53
+ /**
54
+ * This will add fixed params to each xhr request.
55
+ *
56
+ * @param {(string|object)} params
57
+ */
58
+ addFixedParams(params)
59
+ {
60
+ this.xhrSettings.fixedParams = params;
61
+ },
62
+
63
+ /**
64
+ * This will add a callback that will be called before
65
+ * each request.
66
+ *
67
+ * @param {function} callBack
68
+ */
69
+ beforeSend(callBack)
70
+ {
71
+ this.xhrSettings.beforeSend.push(callBack);
72
+ },
73
+
74
+ /**
75
+ * This will update the xhr settings.
76
+ *
77
+ * @param {object} settingsObj
78
+ */
79
+ ajaxSettings(settingsObj)
80
+ {
81
+ if(typeof settingsObj === 'object')
82
+ {
83
+ this.xhrSettings = this.extendClass(base.xhrSettings, settingsObj);
84
+ }
85
+ },
86
+
87
+ /**
88
+ * This will reset the xhr settings.
89
+ */
90
+ resetAjaxSettings()
91
+ {
92
+ this.xhrSettings = XhrDefaultSettings;
93
+ }
94
+ });
95
+
96
+ /**
97
+ * This will make an xhr (ajax) request.
98
+ *
99
+ * @param {string} url
100
+ * @param {string} params
101
+ * @param {function} callBackFn
102
+ * @param {string} responseType
103
+ * @param {string} [method=POST]
104
+ * @param {boolean} async
105
+ *
106
+ * or
107
+ *
108
+ * @param {object} settings
109
+ * @example
110
+ * {
111
+ * url: '',
112
+ * params: '',
113
+ * completed(response)
114
+ * {
115
+ *
116
+ * }
117
+ * }
118
+ *
119
+ * @return {object} xhr object.
120
+ */
121
+ export const ajax = (...args) =>
122
+ {
123
+ /* we want to save the args so we can check
124
+ which way we are adding the ajax settings */
125
+ const ajax = new XhrRequest(args);
126
+ return ajax.xhr;
127
+ };
128
+
129
+ /**
130
+ * XhrRequest
131
+ *
132
+ * This will create an xhr request object.
133
+ * @class
134
+ */
135
+ export class XhrRequest
136
+ {
137
+ /**
138
+ * @constructor
139
+ * @param {*} args
140
+ */
141
+ constructor(args)
142
+ {
143
+ this.settings = null;
144
+ this.xhr = null;
145
+ this.setup(args);
146
+ }
147
+
148
+ /**
149
+ * This will setup the xhr request.
150
+ *
151
+ * @protected
152
+ * @param {*} args
153
+ * @return {(object|boolean)}
154
+ */
155
+ setup(args)
156
+ {
157
+ this.getXhrSettings(args);
158
+
159
+ let xhr = this.xhr = this.createXHR();
160
+ if(xhr === false)
161
+ {
162
+ return false;
163
+ }
164
+
165
+ const {method, url, async} = this.settings;
166
+ xhr.open(method, url, async);
167
+
168
+ this.setupHeaders();
169
+ this.addXhrEvents();
170
+ this.beforeSend();
171
+
172
+ /* this will setup the params and send the
173
+ xhr request */
174
+ xhr.send(this.getParams());
175
+ }
176
+
177
+ /**
178
+ * This will call all before send callbacks.
179
+ *
180
+ * @returns {void}
181
+ */
182
+ beforeSend()
183
+ {
184
+ let items = base.xhrSettings.beforeSend;
185
+ if(items.length < 1)
186
+ {
187
+ return;
188
+ }
189
+
190
+ let xhr = this.xhr;
191
+ let settings = this.settings;
192
+ for(var i = 0, length = items.length; i < length; i++)
193
+ {
194
+ var callBack = items[i];
195
+ if(callBack)
196
+ {
197
+ callBack(xhr, settings);
198
+ }
199
+ }
200
+ }
201
+
202
+ /**
203
+ * This will convert an object to a string.
204
+ *
205
+ * @protected
206
+ * @param {object} object
207
+ * @return {string}
208
+ */
209
+ objectToString(object)
210
+ {
211
+ let params = [];
212
+ for (var prop in object)
213
+ {
214
+ if(object.hasOwnProperty(prop))
215
+ {
216
+ params.push(prop + '=' + object[prop]);
217
+ }
218
+ }
219
+ return params.join('&');
220
+ }
221
+
222
+ /**
223
+ * This will add the base params to the request params.
224
+ *
225
+ * @protected
226
+ * @param {*} params
227
+ * @param {*} addingParams
228
+ * @return {*}
229
+ */
230
+ setupParams(params, addingParams)
231
+ {
232
+ let paramsType = typeof params;
233
+ if(addingParams)
234
+ {
235
+ /* this will convert the adding params to match
236
+ the params type */
237
+ let addingType = typeof addingParams;
238
+ if(paramsType === 'string')
239
+ {
240
+ if(addingType !== 'string')
241
+ {
242
+ addingParams = this.objectToString(addingParams);
243
+ }
244
+
245
+ const char = (params === '')? '?' : '&';
246
+ params += char + addingParams;
247
+ }
248
+ else
249
+ {
250
+ if(addingType === 'string')
251
+ {
252
+ addingParams = base.parseQueryString(addingParams, false);
253
+ }
254
+
255
+ if(params instanceof FormData)
256
+ {
257
+ for(var key in addingParams)
258
+ {
259
+ if(addingParams.hasOwnProperty(key))
260
+ {
261
+ params.append(key, addingParams[key]);
262
+ }
263
+ }
264
+ }
265
+ else if(paramsType === 'object')
266
+ {
267
+ /* we don't want to modify the original object
268
+ so we need to clone the object before extending */
269
+ params = base.cloneObject(params);
270
+
271
+ params = Object.assign(addingParams, params);
272
+ params = this.objectToString(params);
273
+ }
274
+ }
275
+ }
276
+ else
277
+ {
278
+ if((params instanceof FormData) === false && paramsType === 'object')
279
+ {
280
+ params = this.objectToString(params);
281
+ }
282
+ }
283
+ return params;
284
+ }
285
+
286
+ /**
287
+ * This will get the params.
288
+ * @protected
289
+ * @return {*}
290
+ */
291
+ getParams()
292
+ {
293
+ let settings = this.settings,
294
+ params = settings.params;
295
+
296
+ let fixedParams = settings.fixedParams;
297
+ if(params)
298
+ {
299
+ params = this.setupParams(params, fixedParams);
300
+ }
301
+ else if(fixedParams)
302
+ {
303
+ params = this.setupParams(fixedParams);
304
+ }
305
+
306
+ return params;
307
+ }
308
+
309
+ /**
310
+ * This will set the settings from the args.
311
+ *
312
+ * @protected
313
+ * @param {array} args
314
+ */
315
+ getXhrSettings(args)
316
+ {
317
+ /* we want to create a clone of the default
318
+ settings before adding the new settings */
319
+ let settings = this.settings = Object.create(base.xhrSettings);
320
+
321
+ /* we want to check if we are adding the ajax settings by
322
+ individual args or by a settings object */
323
+ if(args.length >= 2 && typeof args[0] !== 'object')
324
+ {
325
+ for(var i = 0, maxLength = args.length; i < maxLength; i++)
326
+ {
327
+ var arg = args[i];
328
+
329
+ switch(i)
330
+ {
331
+ case 0:
332
+ settings.url = arg;
333
+ break;
334
+ case 1:
335
+ settings.params = arg;
336
+ break;
337
+ case 2:
338
+ settings.completed = arg;
339
+ settings.failed = arg;
340
+ break;
341
+ case 3:
342
+ settings.responseType = arg || 'json';
343
+ break;
344
+ case 4:
345
+ settings.method = (arg)? arg.toUpperCase() : 'POST';
346
+ break;
347
+ case 5:
348
+ settings.async = (typeof arg !== 'undefined')? arg : true;
349
+ break;
350
+ }
351
+ }
352
+ }
353
+ else
354
+ {
355
+ /* override the default settings with the args
356
+ settings object */
357
+ settings = this.settings = base.extendClass(this.settings, args[0]);
358
+
359
+ /* we want to check to add the completed callback
360
+ as the error and aborted if not set */
361
+ if(typeof settings.completed === 'function')
362
+ {
363
+ if(typeof settings.failed !== 'function')
364
+ {
365
+ settings.failed = settings.completed;
366
+ }
367
+
368
+ if(typeof settings.aborted !== 'function')
369
+ {
370
+ settings.aborted = settings.failed;
371
+ }
372
+ }
373
+ }
374
+ }
375
+
376
+ /**
377
+ * This will create the xhr object.
378
+ *
379
+ * @protected
380
+ * @return {(object|boolean)}
381
+ */
382
+ createXHR()
383
+ {
384
+ /* we want to check to setup the xhr by
385
+ the crossDomain settings */
386
+ let settings = this.settings,
387
+
388
+ xhr = new XMLHttpRequest();
389
+ xhr.responseType = settings.responseType;
390
+
391
+ if(settings.withCredentials === true)
392
+ {
393
+ xhr.withCredentials = true;
394
+ }
395
+
396
+ return xhr;
397
+ }
398
+
399
+ /**
400
+ * This will setup the request headers.
401
+ */
402
+ setupHeaders()
403
+ {
404
+ let settings = this.settings;
405
+ if(settings && typeof settings.headers === 'object')
406
+ {
407
+ /* we want to add a header for each
408
+ property in the object */
409
+ let headers = settings.headers;
410
+ for(var header in headers)
411
+ {
412
+ if(headers.hasOwnProperty(header))
413
+ {
414
+ this.xhr.setRequestHeader(header, headers[header]);
415
+ }
416
+ }
417
+ }
418
+ }
419
+
420
+ /**
421
+ * This will update the request status.
422
+ *
423
+ * @param {object} e
424
+ * @param {string} [overrideType]
425
+ */
426
+ update(e, overrideType)
427
+ {
428
+ let xhr = this.xhr;
429
+
430
+ /* this will remove the xhr events from the active events
431
+ after the events are completed, aborted, or errored */
432
+ let removeEvents = () =>
433
+ {
434
+ let events = base.events;
435
+ events.removeEvents(xhr.upload);
436
+ events.removeEvents(xhr);
437
+ };
438
+
439
+ let settings = this.settings;
440
+ if(!settings)
441
+ {
442
+ return false;
443
+ }
444
+
445
+ let type = overrideType || e.type;
446
+ switch(type)
447
+ {
448
+ case 'load':
449
+ if(typeof settings.completed === 'function')
450
+ {
451
+ let response = this.getResponseData();
452
+ settings.completed(response, this.xhr);
453
+ }
454
+ removeEvents();
455
+ break;
456
+ case 'error':
457
+ if(typeof settings.failed === 'function')
458
+ {
459
+ settings.failed(false, this.xhr);
460
+ }
461
+ removeEvents();
462
+ break;
463
+ case 'progress':
464
+ if(typeof settings.progress === 'function')
465
+ {
466
+ settings.progress(e);
467
+ }
468
+ break;
469
+ case 'abort':
470
+ if(typeof settings.aborted === 'function')
471
+ {
472
+ settings.aborted(false, this.xhr);
473
+ }
474
+ removeEvents();
475
+ break;
476
+ }
477
+ }
478
+
479
+ /**
480
+ * This will get the response data.
481
+ *
482
+ * @return {*}
483
+ */
484
+ getResponseData()
485
+ {
486
+ let xhr = this.xhr,
487
+ response = xhr.response;
488
+
489
+ const responseType = xhr.responseType;
490
+ if(!responseType || responseType === 'text')
491
+ {
492
+ return xhr.responseText;
493
+ }
494
+
495
+ return response;
496
+ }
497
+
498
+ /**
499
+ * This will add the xhr events.
500
+ */
501
+ addXhrEvents()
502
+ {
503
+ let settings = this.settings;
504
+ if(!settings)
505
+ {
506
+ return;
507
+ }
508
+
509
+ let xhr = this.xhr,
510
+ callBack = base.bind(this, this.update);
511
+ base.on(['load', 'error', 'abort'], xhr, callBack);
512
+ base.on('progress', xhr.upload, callBack);
513
+ }
514
+ }