|
1
|
// Core JavaScript helper functions |
|
2
|
'use strict'; |
|
3
|
|
|
4
|
// quickElement(tagType, parentReference [, textInChildNode, attribute, attributeValue ...]); |
|
5
|
function quickElement() { |
|
6
|
const obj = document.createElement(arguments[0]); |
|
7
|
if (arguments[2]) { |
|
8
|
const textNode = document.createTextNode(arguments[2]); |
|
9
|
obj.appendChild(textNode); |
|
10
|
} |
|
11
|
const len = arguments.length; |
|
12
|
for (let i = 3; i < len; i += 2) { |
|
13
|
obj.setAttribute(arguments[i], arguments[i + 1]); |
|
14
|
} |
|
15
|
arguments[1].appendChild(obj); |
|
16
|
return obj; |
|
17
|
} |
|
18
|
|
|
19
|
// "a" is reference to an object |
|
20
|
function removeChildren(a) { |
|
21
|
while (a.hasChildNodes()) { |
|
22
|
a.removeChild(a.lastChild); |
|
23
|
} |
|
24
|
} |
|
25
|
|
|
26
|
// ---------------------------------------------------------------------------- |
|
27
|
// Find-position functions by PPK |
|
28
|
// See https://www.quirksmode.org/js/findpos.html |
|
29
|
// ---------------------------------------------------------------------------- |
|
30
|
function findPosX(obj) { |
|
31
|
let curleft = 0; |
|
32
|
if (obj.offsetParent) { |
|
33
|
while (obj.offsetParent) { |
|
34
|
curleft += obj.offsetLeft - obj.scrollLeft; |
|
35
|
obj = obj.offsetParent; |
|
36
|
} |
|
37
|
} else if (obj.x) { |
|
38
|
curleft += obj.x; |
|
39
|
} |
|
40
|
return curleft; |
|
41
|
} |
|
42
|
|
|
43
|
function findPosY(obj) { |
|
44
|
let curtop = 0; |
|
45
|
if (obj.offsetParent) { |
|
46
|
while (obj.offsetParent) { |
|
47
|
curtop += obj.offsetTop - obj.scrollTop; |
|
48
|
obj = obj.offsetParent; |
|
49
|
} |
|
50
|
} else if (obj.y) { |
|
51
|
curtop += obj.y; |
|
52
|
} |
|
53
|
return curtop; |
|
54
|
} |
|
55
|
|
|
56
|
//----------------------------------------------------------------------------- |
|
57
|
// Date object extensions |
|
58
|
// ---------------------------------------------------------------------------- |
|
59
|
{ |
|
60
|
Date.prototype.getTwelveHours = function() { |
|
61
|
return this.getHours() % 12 || 12; |
|
62
|
}; |
|
63
|
|
|
64
|
Date.prototype.getTwoDigitMonth = function() { |
|
65
|
return (this.getMonth() < 9) ? '0' + (this.getMonth() + 1) : (this.getMonth() + 1); |
|
66
|
}; |
|
67
|
|
|
68
|
Date.prototype.getTwoDigitDate = function() { |
|
69
|
return (this.getDate() < 10) ? '0' + this.getDate() : this.getDate(); |
|
70
|
}; |
|
71
|
|
|
72
|
Date.prototype.getTwoDigitTwelveHour = function() { |
|
73
|
return (this.getTwelveHours() < 10) ? '0' + this.getTwelveHours() : this.getTwelveHours(); |
|
74
|
}; |
|
75
|
|
|
76
|
Date.prototype.getTwoDigitHour = function() { |
|
77
|
return (this.getHours() < 10) ? '0' + this.getHours() : this.getHours(); |
|
78
|
}; |
|
79
|
|
|
80
|
Date.prototype.getTwoDigitMinute = function() { |
|
81
|
return (this.getMinutes() < 10) ? '0' + this.getMinutes() : this.getMinutes(); |
|
82
|
}; |
|
83
|
|
|
84
|
Date.prototype.getTwoDigitSecond = function() { |
|
85
|
return (this.getSeconds() < 10) ? '0' + this.getSeconds() : this.getSeconds(); |
|
86
|
}; |
|
87
|
|
|
88
|
Date.prototype.getAbbrevDayName = function() { |
|
89
|
return typeof window.CalendarNamespace === "undefined" |
|
90
|
? '0' + this.getDay() |
|
91
|
: window.CalendarNamespace.daysOfWeekAbbrev[this.getDay()]; |
|
92
|
}; |
|
93
|
|
|
94
|
Date.prototype.getFullDayName = function() { |
|
95
|
return typeof window.CalendarNamespace === "undefined" |
|
96
|
? '0' + this.getDay() |
|
97
|
: window.CalendarNamespace.daysOfWeek[this.getDay()]; |
|
98
|
}; |
|
99
|
|
|
100
|
Date.prototype.getAbbrevMonthName = function() { |
|
101
|
return typeof window.CalendarNamespace === "undefined" |
|
102
|
? this.getTwoDigitMonth() |
|
103
|
: window.CalendarNamespace.monthsOfYearAbbrev[this.getMonth()]; |
|
104
|
}; |
|
105
|
|
|
106
|
Date.prototype.getFullMonthName = function() { |
|
107
|
return typeof window.CalendarNamespace === "undefined" |
|
108
|
? this.getTwoDigitMonth() |
|
109
|
: window.CalendarNamespace.monthsOfYear[this.getMonth()]; |
|
110
|
}; |
|
111
|
|
|
112
|
Date.prototype.strftime = function(format) { |
|
113
|
const fields = { |
|
114
|
a: this.getAbbrevDayName(), |
|
115
|
A: this.getFullDayName(), |
|
116
|
b: this.getAbbrevMonthName(), |
|
117
|
B: this.getFullMonthName(), |
|
118
|
c: this.toString(), |
|
119
|
d: this.getTwoDigitDate(), |
|
120
|
H: this.getTwoDigitHour(), |
|
121
|
I: this.getTwoDigitTwelveHour(), |
|
122
|
m: this.getTwoDigitMonth(), |
|
123
|
M: this.getTwoDigitMinute(), |
|
124
|
p: (this.getHours() >= 12) ? 'PM' : 'AM', |
|
125
|
S: this.getTwoDigitSecond(), |
|
126
|
w: '0' + this.getDay(), |
|
127
|
x: this.toLocaleDateString(), |
|
128
|
X: this.toLocaleTimeString(), |
|
129
|
y: ('' + this.getFullYear()).substr(2, 4), |
|
130
|
Y: '' + this.getFullYear(), |
|
131
|
'%': '%' |
|
132
|
}; |
|
133
|
let result = '', i = 0; |
|
134
|
while (i < format.length) { |
|
135
|
if (format.charAt(i) === '%') { |
|
136
|
result += fields[format.charAt(i + 1)]; |
|
137
|
++i; |
|
138
|
} |
|
139
|
else { |
|
140
|
result += format.charAt(i); |
|
141
|
} |
|
142
|
++i; |
|
143
|
} |
|
144
|
return result; |
|
145
|
}; |
|
146
|
|
|
147
|
// ---------------------------------------------------------------------------- |
|
148
|
// String object extensions |
|
149
|
// ---------------------------------------------------------------------------- |
|
150
|
String.prototype.strptime = function(format) { |
|
151
|
const split_format = format.split(/[.\-/]/); |
|
152
|
const date = this.split(/[.\-/]/); |
|
153
|
let i = 0; |
|
154
|
let day, month, year; |
|
155
|
while (i < split_format.length) { |
|
156
|
switch (split_format[i]) { |
|
157
|
case "%d": |
|
158
|
day = date[i]; |
|
159
|
break; |
|
160
|
case "%m": |
|
161
|
month = date[i] - 1; |
|
162
|
break; |
|
163
|
case "%Y": |
|
164
|
year = date[i]; |
|
165
|
break; |
|
166
|
case "%y": |
|
167
|
// A %y value in the range of [00, 68] is in the current |
|
168
|
// century, while [69, 99] is in the previous century, |
|
169
|
// according to the Open Group Specification. |
|
170
|
if (parseInt(date[i], 10) >= 69) { |
|
171
|
year = date[i]; |
|
172
|
} else { |
|
173
|
year = (new Date(Date.UTC(date[i], 0))).getUTCFullYear() + 100; |
|
174
|
} |
|
175
|
break; |
|
176
|
} |
|
177
|
++i; |
|
178
|
} |
|
179
|
// Create Date object from UTC since the parsed value is supposed to be |
|
180
|
// in UTC, not local time. Also, the calendar uses UTC functions for |
|
181
|
// date extraction. |
|
182
|
return new Date(Date.UTC(year, month, day)); |
|
183
|
}; |
|
184
|
} |
|
185
|
|