-
Notifications
You must be signed in to change notification settings - Fork 237
Expand file tree
/
Copy pathmisc.js
More file actions
181 lines (162 loc) · 5.48 KB
/
misc.js
File metadata and controls
181 lines (162 loc) · 5.48 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
(function ($) {
AblePlayer.prototype.getNextHeadingLevel = function ($element) {
// Finds the nearest heading in the ancestor tree
// Loops over each parent of the current element until a heading is found
// If multiple headings are found beneath a given parent, get the closest
// Returns an integer (1-6) representing the next available heading level
var $parents, $foundHeadings, numHeadings, headingType, headingNumber;
$parents = $element.parents();
$parents.each(function () {
$foundHeadings = $(this).children(":header");
numHeadings = $foundHeadings.length;
if (numHeadings) {
headingType = $foundHeadings.eq(numHeadings - 1).prop("tagName");
return false;
}
});
if (typeof headingType === "undefined") {
// page has no headings
headingNumber = 1;
} else {
// Increment closest heading by one if less than 6.
headingNumber = parseInt(headingType[1]);
headingNumber += 1;
if (headingNumber > 6) {
headingNumber = 6;
}
}
return headingNumber;
};
AblePlayer.prototype.countProperties = function (obj) {
// returns the number of properties in an object
var count, prop;
count = 0;
for (prop in obj) {
if (obj.hasOwnProperty(prop)) {
++count;
}
}
return count;
};
AblePlayer.prototype.formatSecondsAsColonTime = function (
seconds,
showFullTime
) {
// Takes seconds and converts to string of form hh:mm:ss
// If showFullTime is true, shows 00 for hours if time is less than an hour
// and show milliseconds (e.g., 00:00:04.123 as in Video Track Sorter)
// Otherwise, omits empty hours and milliseconds (e.g., 00:04 as in timer on controller)
var dHours, dMinutes, dSeconds, parts, milliSeconds, numShort, i;
if (showFullTime) {
// preserve milliseconds, if included in seconds
parts = seconds.toString().split(".");
if (parts.length === 2) {
milliSeconds = parts[1];
if (milliSeconds.length < 3) {
numShort = 3 - milliSeconds.length;
for (i = 1; i <= numShort; i++) {
milliSeconds += "0";
}
}
} else {
milliSeconds = "000";
}
}
dHours = Math.floor(seconds / 3600);
dMinutes = Math.floor(seconds / 60) % 60;
dSeconds = Math.floor(seconds % 60);
if (dSeconds < 10) {
dSeconds = "0" + dSeconds;
}
if (dHours > 0) {
if (dMinutes < 10) {
dMinutes = "0" + dMinutes;
}
if (showFullTime) {
return dHours + ":" + dMinutes + ":" + dSeconds + "." + milliSeconds;
} else {
return dHours + ":" + dMinutes + ":" + dSeconds;
}
} else {
if (showFullTime) {
if (dHours < 1) {
dHours = "00";
} else if (dHours < 10) {
dHours = "0" + dHours;
}
if (dMinutes < 1) {
dMinutes = "00";
} else if (dMinutes < 10) {
dMinutes = "0" + dMinutes;
}
return dHours + ":" + dMinutes + ":" + dSeconds + "." + milliSeconds;
} else {
return dMinutes + ":" + dSeconds;
}
}
};
AblePlayer.prototype.getSecondsFromColonTime = function (timeStr) {
// Converts string of form hh:mm:ss to seconds
var timeParts, hours, minutes, seconds;
timeParts = timeStr.split(":");
if (timeParts.length === 3) {
hours = parseInt(timeParts[0]);
minutes = parseInt(timeParts[1]);
seconds = parseFloat(timeParts[2]);
return hours * 3600 + minutes * 60 + seconds;
} else if (timeParts.length === 2) {
minutes = parseInt(timeParts[0]);
seconds = parseFloat(timeParts[1]);
return minutes * 60 + seconds;
} else if (timeParts.length === 1) {
seconds = parseFloat(timeParts[0]);
return seconds;
}
};
AblePlayer.prototype.capitalizeFirstLetter = function (string) {
return string.charAt(0).toUpperCase() + string.slice(1);
};
AblePlayer.prototype.roundDown = function (value, decimals) {
// round value down to the nearest X decimal points
// where X is the value of the decimals parameter
return Number(Math.floor(value + "e" + decimals) + "e-" + decimals);
};
AblePlayer.prototype.defer = function() {
const self = this;
const promise = new Promise((resolve, reject) => {
self.resolve = resolve;
self.reject = reject;
self.promise = () => promise;
});
}
AblePlayer.prototype.getScript = function( source, callback ) {
var script = document.createElement('script');
var prior = document.getElementsByTagName('script')[0];
script.async = 1;
script.onload = script.onreadystatechange = function( _, isAbort ) {
if ( isAbort || !script.readyState || /loaded|complete/.test(script.readyState) ) {
script.onload = script.onreadystatechange = null;
script = undefined;
if ( !isAbort && callback ) {
setTimeout(callback, 0);
}
}
};
script.src = source;
prior.parentNode.insertBefore(script, prior);
}
AblePlayer.prototype.hasAttr = function (object, attribute) {
// surprisingly, there is no hasAttr() function in Jquery as of 3.2.1
// return true if object has attribute; otherwise false
// selector is a Jquery object
// attribute is a string
var attr = object.attr(attribute);
// For some browsers, `attr` is undefined; for others,
// `attr` is false. Check for both.
if (typeof attr !== typeof undefined && attr !== false) {
return true;
} else {
return false;
}
};
})(jQuery);