@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,544 @@
1
+ /**
2
+ * This will add date functions to the base framework.
3
+ *
4
+ */
5
+ export const date = {
6
+
7
+ /**
8
+ * @member {array} monthName
9
+ */
10
+ monthNames: ["January","February","March","April","May","June","July","August","September","October","November","December"],
11
+
12
+ /**
13
+ * @member {array} dayNames
14
+ */
15
+ dayNames: ["Sunday","Monday","Tuesday","Wednesday","Thursday","Friday","Saturday"],
16
+
17
+ /**
18
+ * This will get the day name.
19
+ *
20
+ * @param {int} [day]
21
+ * @param {boolean} [shortenName=false]
22
+ * @return {string}
23
+ */
24
+ getDayName(day = new Date().getDay(), shortenName = false)
25
+ {
26
+ let days = this.dayNames;
27
+ if(day > days.length)
28
+ {
29
+ return false;
30
+ }
31
+
32
+ /* we want to check to shorten name */
33
+ let dayName = days[day];
34
+ return (shortenName)? dayName.substring(0, 3) : dayName;
35
+ },
36
+
37
+ /**
38
+ * This will convert month to js.
39
+ *
40
+ * @param {int} month
41
+ * @return {int}
42
+ */
43
+ convertJsMonth(month)
44
+ {
45
+ return this.padNumber(month + 1);
46
+ },
47
+
48
+ /**
49
+ * This will convert a date.
50
+ *
51
+ * @param {string} dateString
52
+ * @param {bool} addYear
53
+ * @returns {string}
54
+ */
55
+ convertDate(dateString, addYear)
56
+ {
57
+ var baseDate = base.date;
58
+ dateString = (dateString)? dateString.replace(/\s/, 'T'): ''; //For safari
59
+
60
+ var date = new Date(dateString),
61
+ year = (addYear === true)? ' ' + date.getFullYear() : '';
62
+ return baseDate.getDayName(date.getDay()) + ', ' + baseDate.getMonthName(date.getMonth(), true) + ' ' + baseDate.padNumber(date.getDate()) + year;
63
+ },
64
+
65
+ /**
66
+ * This will add leading zero to number less than 10.
67
+ *
68
+ * @param {int} number
69
+ * @return {string}
70
+ */
71
+ padNumber(number)
72
+ {
73
+ return (number <= 9)? '0' + number : String(number);
74
+ },
75
+
76
+ /**
77
+ * This will create a new date object.
78
+ *
79
+ * @param {string} dateString
80
+ * @returns {Date}
81
+ */
82
+ createDate(dateString)
83
+ {
84
+ if(!dateString)
85
+ {
86
+ return new Date();
87
+ }
88
+
89
+ if(typeof dateString === 'string' && dateString.indexOf('-') > -1)
90
+ {
91
+ dateString = dateString.replace(/\s/, 'T'); //For safari
92
+ dateString = (dateString.indexOf(':') > -1)? dateString : dateString + "T00:00:00";
93
+ }
94
+ return new Date(dateString);
95
+ },
96
+
97
+ /**
98
+ * This will format a date.
99
+ *
100
+ * @param {string} type
101
+ * @param {string} dateString
102
+ * @return {string}
103
+ */
104
+ format(type, dateString)
105
+ {
106
+ let date = this.createDate(dateString);
107
+ switch(type)
108
+ {
109
+ case 'sql':
110
+ dateString = date.getFullYear() + '-' + this.convertJsMonth(date.getMonth()) + '-' + this.padNumber(date.getDate());
111
+ break;
112
+ default:
113
+ dateString = this.convertJsMonth(date.getMonth()) + '/' + this.padNumber(date.getDate()) + '/' + date.getFullYear();
114
+ break;
115
+
116
+ }
117
+ return dateString;
118
+ },
119
+
120
+ /**
121
+ * This will format time.
122
+ *
123
+ * @param {string} dateString
124
+ * @param {int} format
125
+ * @returns {stirng}
126
+ */
127
+ formatTime(dateString, format)
128
+ {
129
+ let date = this.createDate(dateString);
130
+ if(format === 24)
131
+ {
132
+ return this.padNumber(date.getHours()) + ':' + this.padNumber(date.getMinutes()) + ':' + this.padNumber(date.getSeconds());
133
+ }
134
+
135
+ let hours = date.getHours(),
136
+ meridian = 'AM';
137
+
138
+ if(hours >= 12)
139
+ {
140
+ meridian = 'PM';
141
+ }
142
+
143
+ if(hours > 12)
144
+ {
145
+ hours = hours - 12;
146
+ }
147
+ return (hours + ':' + this.padNumber(date.getMinutes()) + ' ' + meridian);
148
+ },
149
+
150
+ /**
151
+ * This will check for leap year.
152
+ *
153
+ * @param {int} year
154
+ * @return {boolean}
155
+ */
156
+ leapYear(year)
157
+ {
158
+ return ((year % 400 === 0) || (year % 100 !== 0 && year % 4 === 0));
159
+ },
160
+
161
+ /**
162
+ * This will get a month name.
163
+ *
164
+ * @param {int} [month]
165
+ * @param {boolean} [shortenName]
166
+ * @return {string}
167
+ */
168
+ getMonthName(month = new Date().getMonth(), shortenName = false)
169
+ {
170
+ let months = this.monthNames;
171
+ if(month > months.length)
172
+ {
173
+ return false;
174
+ }
175
+
176
+ let monthName = months[month];
177
+ return (shortenName)? monthName.substring(0, 3) : monthName;
178
+ },
179
+
180
+ /**
181
+ * This will return the month length.
182
+ *
183
+ * @param {int} [month]
184
+ * @param {int} [year]
185
+ * @return {int}
186
+ */
187
+ getMonthLength(month, year)
188
+ {
189
+ /* we want to check to use params or use
190
+ default */
191
+ let date = new Date();
192
+ month = (typeof month !== 'undefined')? month : date.getMonth();
193
+ year = (typeof year !== 'undefined')? year : date.getFullYear();
194
+
195
+ /* we need to get the month lengths for
196
+ the year */
197
+ let yearMonthLengths = this.getMonthsLength(year);
198
+
199
+ /* we can select the month length from
200
+ the yearMonthLengths array */
201
+ let monthLength = yearMonthLengths[month];
202
+ return monthLength;
203
+ },
204
+
205
+ /**
206
+ * This will get the length of all the months.
207
+ *
208
+ * @param {int} year
209
+ * @return {array}
210
+ */
211
+ getMonthsLength(year = new Date().getFullYear())
212
+ {
213
+ let isLeapYear = this.leapYear(year);
214
+ let days = (isLeapYear === true)?
215
+ [31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31]
216
+ :
217
+ [31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31];
218
+
219
+ return days;
220
+ },
221
+
222
+ /**
223
+ * This will get the difference from now.
224
+ *
225
+ * @param {string} date
226
+ * @param {bool} setHours
227
+ * @returns {int}
228
+ */
229
+ getDiffFromNow(date, setHours)
230
+ {
231
+ date = date.replace(/\s/, 'T'); //For safari
232
+ date = new Date(date);
233
+
234
+ let now = new Date();
235
+ if(setHours === true)
236
+ {
237
+ now.setHours(0,0,0,0);
238
+ }
239
+
240
+ let timeDiff = now.getTime() - date.getTime();
241
+ return timeDiff;
242
+ },
243
+
244
+ /**
245
+ * This will get the age.
246
+ *
247
+ * @param {string} date
248
+ * @returns {string}
249
+ */
250
+ getAge(date)
251
+ {
252
+ const milliseconds = this.getDiffFromNow(date);
253
+
254
+ let age = '';
255
+ switch(true)
256
+ {
257
+ case milliseconds < 86400000:
258
+ age = '1 day';
259
+ break;
260
+ case milliseconds < 604800000:
261
+ var days = this.toDays(milliseconds);
262
+ age = (days) + ' days';
263
+ break;
264
+ case milliseconds < 1209600000:
265
+ age = '1 week';
266
+ break;
267
+ case milliseconds < 2592000000:
268
+ var days = this.toDays(milliseconds),
269
+ weeks = Math.floor(days / 7);
270
+ age = weeks + ' weeks';
271
+ break;
272
+ case milliseconds < 5184000000:
273
+ age = '1 month';
274
+ break;
275
+ case milliseconds < 31104000000:
276
+ var months = this.toMonths(milliseconds);
277
+ age = months + ' months';
278
+ break;
279
+ default:
280
+ var years = this.toYears(milliseconds);
281
+ age = years;
282
+ }
283
+
284
+ return age;
285
+ },
286
+
287
+ /**
288
+ * This will get the time frame.
289
+ *
290
+ * @param {string} date
291
+ * @returns {string}
292
+ */
293
+ getTimeFrame(date)
294
+ {
295
+ let timeDiff = this.getDiffFromNow(date);
296
+ return this.convertToEstimate(timeDiff);
297
+ },
298
+
299
+ /**
300
+ *
301
+ * @param {int} milliseconds
302
+ * @returns {string}
303
+ */
304
+ convertToEstimate(milliseconds)
305
+ {
306
+ let timeFrame = '';
307
+
308
+ if(milliseconds <= 0)
309
+ {
310
+ switch(true)
311
+ {
312
+ case milliseconds < -63072000000:
313
+ var years = this.toYears(Math.abs(milliseconds));
314
+ timeFrame = 'in ' + years + ' years';
315
+ break;
316
+ case milliseconds < -31536000000:
317
+ timeFrame = 'in a year';
318
+ break;
319
+ case milliseconds < -5184000000:
320
+ var months = this.toMonths(Math.abs(milliseconds));
321
+ timeFrame = 'in ' + months + ' months';
322
+ break;
323
+ case milliseconds < -2592000000:
324
+ timeFrame = 'in a month';
325
+ break;
326
+ case milliseconds < -1209600000:
327
+ var days = this.toDays(Math.abs(milliseconds)),
328
+ weeks = Math.floor(days / 7);
329
+ timeFrame = 'in ' + weeks + ' weeks';
330
+ break;
331
+ case milliseconds < -604800000:
332
+ timeFrame = 'in a week';
333
+ break;
334
+ case milliseconds < -172800000:
335
+ var days = this.toDays(Math.abs(milliseconds));
336
+ timeFrame = 'in ' + (days) + ' days';
337
+ break;
338
+ case milliseconds < -86400000:
339
+ timeFrame = 'tomorrow';
340
+ break;
341
+ case milliseconds < -7200000:
342
+ var hours = this.toHours(Math.abs(milliseconds));
343
+ timeFrame = 'in ' + hours + ' hours';
344
+ break;
345
+ case milliseconds <= -3600000:
346
+ timeFrame = 'in an hour';
347
+ break;
348
+ case milliseconds < -120000:
349
+ var minutes = this.toMinutes(Math.abs(milliseconds));
350
+ timeFrame = 'in ' + minutes + ' minutes';
351
+ break;
352
+ case milliseconds < -60000:
353
+ timeFrame = 'in a minute';
354
+ break;
355
+ case milliseconds < -2000:
356
+ var seconds = this.toSeconds(Math.abs(milliseconds));
357
+ timeFrame = 'in ' + seconds + ' seconds';
358
+ break;
359
+ case milliseconds < -1:
360
+ timeFrame = 'in 1 second';
361
+ break;
362
+ default:
363
+ timeFrame = 'now';
364
+ }
365
+ }
366
+ else
367
+ {
368
+ switch(true)
369
+ {
370
+ case milliseconds < 1000:
371
+ timeFrame = '1 second ago';
372
+ break;
373
+ case milliseconds < 60000:
374
+ var seconds = this.toSeconds(milliseconds);
375
+ timeFrame = seconds + ' seconds ago';
376
+ break;
377
+ case milliseconds < 120000:
378
+ timeFrame = '1 minute ago';
379
+ break;
380
+ case milliseconds < 3600000:
381
+ var minutes = this.toMinutes(milliseconds);
382
+ timeFrame = minutes + ' minutes ago';
383
+ break;
384
+ case milliseconds < 7200000:
385
+ timeFrame = '1 hour ago';
386
+ break;
387
+ case milliseconds < 86400000:
388
+ var hours = this.toHours(milliseconds);
389
+ timeFrame = hours + ' hours ago';
390
+ break;
391
+ case milliseconds < 172800000:
392
+ timeFrame = 'yesterday';
393
+ break;
394
+ case milliseconds < 604800000:
395
+ var days = this.toDays(milliseconds);
396
+ timeFrame = (days) + ' days ago';
397
+ break;
398
+ case milliseconds < 1209600000:
399
+ timeFrame = 'a week ago';
400
+ break;
401
+ case milliseconds < 2592000000:
402
+ var days = this.toDays(milliseconds),
403
+ weeks = Math.floor(days / 7);
404
+ timeFrame = weeks + ' weeks ago';
405
+ break;
406
+ case milliseconds < 5184000000:
407
+ timeFrame = 'a month ago';
408
+ break;
409
+ case milliseconds < 31536000000:
410
+ var months = this.toMonths(milliseconds);
411
+ timeFrame = months + ' months ago';
412
+ break;
413
+ case milliseconds < 63072000000:
414
+ timeFrame = 'a year ago';
415
+ break;
416
+ default:
417
+ var years = this.toYears(milliseconds);
418
+ timeFrame = years + ' years ago';
419
+ }
420
+ }
421
+
422
+ return timeFrame;
423
+ },
424
+
425
+ /**
426
+ * This will convert to years.
427
+ *
428
+ * @param {int} milliseconds
429
+ * @return {int}
430
+ */
431
+ toYears(milliseconds)
432
+ {
433
+ if(typeof milliseconds !== 'number')
434
+ {
435
+ return false;
436
+ }
437
+
438
+ return Math.floor(milliseconds / (1000 * 60 * 60 * 24 * 365.26));
439
+ },
440
+
441
+ /**
442
+ * This will convert to months.
443
+ *
444
+ * @param {int} milliseconds
445
+ * @returns {int}
446
+ */
447
+ toMonths(milliseconds)
448
+ {
449
+ if(typeof milliseconds === 'number')
450
+ {
451
+ return Math.floor(milliseconds / (1000 * 60 * 60 * 24 * 30));
452
+ }
453
+ return false;
454
+ },
455
+
456
+ /**
457
+ * This will convert to days.
458
+ *
459
+ * @param {int} milliseconds
460
+ * @return {int}
461
+ */
462
+ toDays(milliseconds)
463
+ {
464
+ if(typeof milliseconds !== 'number')
465
+ {
466
+ return false;
467
+ }
468
+
469
+ return Math.floor(milliseconds / (60 * 60 * 1000 * 24) * 1);
470
+ },
471
+
472
+ /**
473
+ * This will convert to hours.
474
+ *
475
+ * @param {int} milliseconds
476
+ * @return {int}
477
+ */
478
+ toHours(milliseconds)
479
+ {
480
+ if(typeof milliseconds !== 'number')
481
+ {
482
+ return false;
483
+ }
484
+
485
+ return Math.floor((milliseconds % (60 * 60 * 1000 * 24)) / (60 * 60 * 1000) * 1);
486
+ },
487
+
488
+ /**
489
+ * This will convert to minutes.
490
+ *
491
+ * @param {int} milliseconds
492
+ * @return {int}
493
+ */
494
+ toMinutes(milliseconds)
495
+ {
496
+ if(typeof milliseconds !== 'number')
497
+ {
498
+ return false;
499
+ }
500
+
501
+ return Math.floor(((milliseconds % (60 * 60 * 1000 * 24)) % (60 * 60 * 1000)) / (60 * 1000) * 1);
502
+ },
503
+
504
+ /**
505
+ * This will convert to seconds.
506
+ *
507
+ * @param {int} milliseconds
508
+ * @return {int}
509
+ */
510
+ toSeconds(milliseconds)
511
+ {
512
+ if(typeof milliseconds !== 'number')
513
+ {
514
+ return false;
515
+ }
516
+
517
+ return Math.floor((((milliseconds % (60 * 60 * 1000 * 24)) % (60 * 60 * 1000)) % (60 * 1000)) / 1000 * 1);
518
+ },
519
+
520
+ /**
521
+ * This will get the difference between two dates.
522
+ *
523
+ * @param {string} startDate
524
+ * @param {string} endDate
525
+ * @return {object}
526
+ */
527
+ getDifference(startDate, endDate)
528
+ {
529
+ /* we want to convert the dates to objects */
530
+ let start = new Date(startDate),
531
+ end = new Date(endDate),
532
+
533
+ /* we want to subtract the start time from the end */
534
+ difference = (end.getTime() - start.getTime());
535
+
536
+ return {
537
+ years: this.toYears(difference),
538
+ days: this.toDays(difference),
539
+ hours: this.toHours(difference),
540
+ minutes: this.toMinutes(difference),
541
+ seconds: this.toSeconds(difference)
542
+ };
543
+ }
544
+ };
@@ -0,0 +1,89 @@
1
+ import {base} from '../../core.js';
2
+
3
+ const updateTitle = (title) =>
4
+ {
5
+ if(typeof title === 'string')
6
+ {
7
+ document.title = title;
8
+ }
9
+ };
10
+
11
+ const getStateTitle = (title, stateObj) =>
12
+ {
13
+ return (title === null && (stateObj && stateObj.title))? stateObj.title : title;
14
+ };
15
+
16
+ export const history =
17
+ {
18
+ isSupported()
19
+ {
20
+ return ('history' in window && 'pushState' in window.history);
21
+ },
22
+
23
+ addEvent(fn, capture)
24
+ {
25
+ const popEvent = (e) =>
26
+ {
27
+ let state = e.state;
28
+ if(state && state.title)
29
+ {
30
+ updateTitle(state.title);
31
+ }
32
+
33
+ fn(e);
34
+ };
35
+
36
+ base.events.add('popstate', window, popEvent, capture, true, fn);
37
+ },
38
+
39
+ removeEvent(fn, capture)
40
+ {
41
+ base.off('popstate', window, fn, capture);
42
+ },
43
+
44
+ pushState(object, title, url)
45
+ {
46
+ const history = window.history,
47
+ lastState = history.state;
48
+
49
+ /* we want to check if the object is not already
50
+ the last saved state */
51
+ if(!lastState || base.equals(lastState, object) === false)
52
+ {
53
+ title = getStateTitle(title, object);
54
+ updateTitle(title);
55
+
56
+ /* we want to add the new state to the window history*/
57
+ history.pushState(object, title, url);
58
+ }
59
+ },
60
+
61
+ replaceState(object, title, url)
62
+ {
63
+ title = getStateTitle(title, object);
64
+ updateTitle(title);
65
+
66
+ /* we want to add the new state to the window history*/
67
+ window.history.replaceState(object, title, url);
68
+ },
69
+
70
+ /* this will go to the next state in the window history */
71
+ nextState()
72
+ {
73
+ window.history.forward();
74
+ },
75
+
76
+ /* this will go to the previous state in the window history */
77
+ prevState()
78
+ {
79
+ window.history.back();
80
+ },
81
+
82
+ /* this will take you to a specified number in the history
83
+ index.
84
+ @param (int) indexNumber= the number to go to */
85
+ goTo(indexNumber)
86
+ {
87
+ window.history.go(indexNumber);
88
+ }
89
+ };