forked from gabrielsroka/gabrielsroka.github.io
-
Notifications
You must be signed in to change notification settings - Fork 0
/
js.js
324 lines (286 loc) · 10.2 KB
/
js.js
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
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
// Copyright 2008-2014 Gabriel Sroka
// see http://weblogs.asp.net/bleroy/archive/2007/04/23/the-format-for-javascript-doc-comments.aspx
function $(id) {
return document.getElementById(id);
}
function comparer(o1, o2, asc) {
/// <summary>Compares two objects: anArray.sort(comparer).</summary>
/// <param name="o1" type="Object">An object.</param>
/// <param name="o2" type="Object">An object.</param>
/// <param name="asc" type="Boolean">Sort ascending? [optional]</param>
/// <returns type="Number"></returns>
var order = asc == undefined || asc ? 1 : -1; // sort ascending or descending
var comparison;
if (o1 == undefined) {
comparison = -1;
} else if (o2 == undefined) {
comparison = 1;
} else if (o1 < o2) {
comparison = -1;
} else if (o1 > o2) {
comparison = 1;
} else {
comparison = 0;
}
return order * comparison;
}
function caseInsensitiveComparer(s1, s2, asc) {
/// <summary>Compares two strings: anArray.sort(caseInsensitiveComparer).</summary>
/// <param name="s1" type="String">A string.</param>
/// <param name="s2" type="String">A string.</param>
/// <param name="asc" type="Boolean" optional="true">Sort ascending?</param>
/// <returns type="Number"></returns>
return comparer(s1.toLowerCase(), s2.toLowerCase(), asc);
}
function objectPropertyComparer(o1, o2, prop, asc) {
return comparer(o1[prop], o2[prop], asc);
}
Date.prototype.toDateTimeString = function () {
/// <summary>Formats a Date as "ddd mmm dd yyyy hh:mm:ss ampm".</summary>
/// <returns type="String">Format: "ddd mmm dd yyyy hh:mm:ss ampm"</returns>
var days = ["Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"];
var months = ["Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"];
var hr24 = this.getHours(); // range: 0-23 (24-hour clock)
var hr12; // range: 1-12 (12-hour clock. use with ampm)
if (hr24 == 0) {
hr12 = 12;
} else if (hr24 <= 12) {
hr12 = hr24;
} else {
hr12 = hr24 - 12;
}
var min = this.getMinutes().zeroPad();
var sec = this.getSeconds().zeroPad();
var ampm = hr24 < 12 ? "AM" : "PM";
return days[this.getDay()] + " " + months[this.getMonth()] + " " + this.getDate() + " " + this.getFullYear() + " " +
hr12 + ":" + min + ":" + sec + " " + ampm;
};
function getText(element) {
/// <param name="element" type="domElement" />
if (element.textContent) {
return element.textContent; // FF
} else {
return element.innerText; // IE, WebKit
}
}
function setText(element, text) {
if (element.textContent) {
element.textContent = text; // FF
} else {
element.innerText = text; // IE, WebKit
}
}
function handleMousewheel(element, listener) {
if (element.addEventListener) {
element.addEventListener("DOMMouseScroll", listener, false); // FF
element.addEventListener("mousewheel", listener, false); // WebKit, IE 9
} else {
element.attachEvent("onmousewheel", listener); // IE < 9
}
}
// Don't use Number.toLocaleString()!
Number.prototype.format = function () {
/// <summary>Add "," every 3 digits.</summary>
var ts = this.toString(), s = "";
for (var i = 1; i <= ts.length; i++) {
s += ts.charAt(i - 1);
if (i != ts.length && (ts.length - i) % 3 == 0) {
s += ",";
}
}
return s;
};
Number.prototype.zeroPad = function () {
/// <summary>Add a leading zero.</summary>
if (this < 10) {
return "0" + this;
} else {
return this.toString();
}
};
Math.pmt = function (rate, nper, pv) {
return pv * rate / (1 - Math.pow(1 + rate, -nper));
};
// Bookmarklet
/*
javascript:
(function () {
var scripts = ["csu", "campuses", "js", "table", "dialog", "Request"];
function loadScript(path, filename, scriptLoaded) {
var id = "calstate_" + filename;
if (document.getElementById(id)) {
scriptLoaded();
return;
}
var script = document.body.appendChild(document.createElement("script"));
script.type = "text/javascript";
script.src = path + filename + ".js";
script.id = id;
if (script.attachEvent) {
script.attachEvent("onreadystatechange", function () {
if (script.readyState == "loaded") {
scriptLoaded();
}
});
} else {
script.addEventListener("load", scriptLoaded, false);
}
}
function scriptLoaded() {
scriptsLoaded++;
if (scriptsLoaded == scripts.length) showCampuses();
}
var scriptsLoaded = 0;
var path = "https://mysite.calstate.edu/personal/csuco_gsroka/scripts/";
for (var s = 0; s < scripts.length; s++) {
loadScript(path, scripts[s], scriptLoaded);
}
}
)()
// - or -
javascript:
(function () {
var s = document.body.appendChild(document.createElement("script")), p = "https://mysite.calstate.edu/personal/csuco_gsroka/scripts/";
s.type = "text/javascript";
s.src = p + "js.js";
var src = p + "SharePoint.js", callback = function () {doSendLink();};
if (s.attachEvent) {
s.attachEvent("onreadystatechange", function () {
if (s.readyState == "loaded") {
loadScript(src, callback);
}
});
} else {
s.addEventListener("load", function () {
loadScript(src, callback);
}, false);
}
}
)()
*/
function loadScript(src, callback) {
var script = document.body.appendChild(document.createElement("script"));
script.type = "text/javascript";
script.src = src;
if (callback) {
if (script.addEventListener) {
script.addEventListener("load", function () {
callback();
}, false);
} else {
script.attachEvent("onreadystatechange", function () { // IE (use "onreadystatechange" since "onload" doesn't work)
if (script.readyState == "loaded") {
callback();
}
});
}
}
}
// regExp.test(string) returns Boolean
// regExp.exec(string) returns Array or null
// string.search(regExp) returns position Number
// string.match(regExp) return Array or null
String.prototype.left = function (length) {
return this.substr(0, length);
};
String.prototype.right = function (length) {
return this.substr(this.length - length);
};
String.prototype.startsWith = function (value) {
/// <param name="value" type="String"></param>
/// <returns type="Boolean">true if string starts with value</returns>
return this.left(value.length) == value;
};
String.prototype.endsWith = function (value) {
/// <param name="value" type="String"></param>
/// <returns type="Boolean">true if string ends with value</returns>
return this.right(value.length) == value;
};
String.prototype.contains = function (value) {
/// <param name="value" type="String"></param>
/// <returns type="Boolean">true if value occurs within this string</returns>
return this.indexOf(value) >= 0;
};
String.prototype.parseQueryString = function (delim, assign) {
/// <summary>Convert query string to hashtable, e.g. "a1=v1&a2=v2&a3=v3" to {a1: "v1", a2: "v2", a3: "v3"}.</summary>
/// <returns type="Object">Hashtable</returns>
delim = delim || "&";
assign = assign || "=";
var args = this.split(delim), nameValueHash = {}, nameValue, name, value;
for (var arg = 0; arg < args.length; arg++) {
nameValue = args[arg].split(assign);
name = nameValue[0];
value = nameValue[1];
nameValueHash[name] = value;
}
return nameValueHash;
};
function Namespace(prefix, uri) {
this.prefix = prefix;
this.uri = uri;
}
Namespace.prototype.getElements = function (x, tagName) {
if (x.getElementsByTagNameNS) {
return x.getElementsByTagNameNS(this.uri, tagName); // Firefox, Chrome
} else {
return x.getElementsByTagName(this.prefix + ":" + tagName); // IE
}
};
Namespace.prototype.getElement = function (x, tagName) {
return this.getElements(x, tagName)[0].firstChild;
};
Namespace.prototype.getElementValue = function (x, tagName) {
return this.getElement(x, tagName).nodeValue;
};
if (window.ActiveXObject) {
window.XML = function (s) {
var xmlDoc = new ActiveXObject("Msxml2.DOMDocument.3.0");
xmlDoc.async = false;
xmlDoc.loadXML(s.replace(/></g, "> <")); // Adding a space makes MSXML prettify the document with tabs.
if (xmlDoc.parseError.errorCode != 0) {
alert("(js.js) XML Parse Error: " + xmlDoc.parseError.reason + ", line: " + xmlDoc.parseError.line);
}
this.toString = function () {
return xmlDoc.xml.replace(/\t/g, " ");
};
this.xmlDoc = xmlDoc;
};
} else if (window.XML) {
XML.fixFirefox = function (s) {
return s.replace(/^<\?xml\s+version\s*=\s*(["'])[^\1]+\1[^?]*\?>/, ""); // Mozilla bug 336551
};
}
/*
// Test script:
var o = {n: 1, d: new Date, s: "a", f: function () {var a=2}, b: false, o2: {p: 3.14},
re: /[r]/g, ud: undefined, nu: null, a: [1, 2, "a",[1,2,"a"],{a:"b",c:[3,4,"d"]}]};
*/
// Save these only once so they can't be overwritten.
/*
Object.prototype._toString = Object.prototype.toString;
Array.prototype._toString = Array.prototype.toString;
Object.prototype.startDump = function () {
function toString (open, close, showProp) {
return function () {
var props = [];
for (var prop in this) {
if (!this.hasOwnProperty(prop)) break;
var val = this[prop];
if (typeof val == "string") {
val = '"' + val.replace(/(["\\])/g, "\\$1") + '"'; // Escape " and \
} else if (val instanceof Date) {
val = 'new Date("' + val + '")';
}
props.push((showProp ? prop + ": " : "") + val);
}
return open + props.join(", ") + close;
};
}
Array.prototype.toString = toString("[", "]", false);
Object.prototype.toString = toString("{", "}", true);
};
Object.prototype.endDump = function () {
Object.prototype.toString = Object.prototype._toString;
Array.prototype.toString = Array.prototype._toString;
};
*/