]> jfr.im git - irc/quakenet/qwebirc.git/blame - js/jslib.js
add some stupid logging
[irc/quakenet/qwebirc.git] / js / jslib.js
CommitLineData
9e769c12
CP
1Array.prototype.indexFromEnd = function(d) {
2 var p = this;
3
4 if(d < 0)
5 return p[p.length + d];
6
7 return p[d];
8}
9
96f28062
CP
10qwebirc.util.dictCopy = function(d) {
11 var n = {};
12 for(var k in d)
13 n[k] = d[k];
14
15 return n;
16}
17
9e769c12
CP
18/* how horribly inefficient */
19String.prototype.replaceAll = function(f, t) {
6f8a20df 20 //return new RegExp("/" + RegExp.escape(f) + "/g").replace(f, RegExp.escape(t));
9e769c12
CP
21 var i = this.indexOf(f);
22 var c = this;
23
24 while(i > -1) {
25 c = c.replace(f, t);
26 i = c.indexOf(f);
27 }
28 return c;
29}
30
31/* how horribly inefficient (again) */
32String.prototype.splitMax = function(by, max) {
33 var items = this.split(by);
34 var newitems = items.slice(0, max-1);
35
36 if(items.length >= max)
37 newitems.push(items.slice(max-1).join(by));
38
39 return newitems;
40}
9b63b053 41
66de775f 42/* returns the arguments */
e20e5a6b 43qwebirc.util.parseURI = function(uri) {
66de775f
CP
44 var result = {}
45
46 var start = uri.indexOf('?');
47 if(start == -1)
48 return result;
49
50 var querystring = uri.substring(start + 1);
51
52 var args = querystring.split("&");
53
29453513 54 for(var i=0;i<args.length;i++) {
66de775f
CP
55 var r = args[i].splitMax("=", 2);
56 if(r.length < 2)
57 continue;
58
59 result[unescape(r[0])] = unescape(r[1]);
60 }
61
62 return result;
63}
35155ba7 64
e20e5a6b
CP
65qwebirc.util.DaysOfWeek = {
66 0: "Sun",
67 1: "Mon",
68 2: "Tue",
69 3: "Wed",
70 4: "Thu",
71 5: "Fri",
72 6: "Sat"
73};
74
75qwebirc.util.MonthsOfYear = {
76 0: "Jan",
77 1: "Feb",
78 2: "Mar",
79 3: "Apr",
80 4: "May",
81 5: "Jun",
82 6: "Jul",
83 7: "Aug",
84 8: "Sep",
85 9: "Oct",
86 10: "Nov",
87 11: "Dec"
88};
1d6756bc
CP
89
90qwebirc.util.NBSPCreate = function(text, element) {
91 var e = text.split(" ");
92 for(var i=0;i<e.length;i++) {
93 var tn = document.createTextNode(e[i]);
94 element.appendChild(tn);
95
96 if(i != e.length - 1) {
97 var e2 = new Element("span");
98 e2.set("html", "&nbsp;&nbsp;");
99 element.appendChild(e2);
100 }
101 }
102};
103
104qwebirc.util.longtoduration = function(l) {
105 var seconds = l % 60;
32d0e6f5
CP
106 var minutes = Math.floor((l % 3600) / 60);
107 var hours = Math.floor((l % (3600 * 24)) / 3600);
108 var days = Math.floor(l / (24*3600));
1d6756bc
CP
109
110 return days + " days " + hours + " hours " + minutes + " minutes " + seconds + " seconds";
111}
8408e834
CP
112
113qwebirc.util.pad = function(x) {
114 x = "" + x;
115 if(x.length == 1)
116 return "0" + x;
117 return x
118}
96f28062
CP
119
120RegExp.escape = function(text) {
6f8a20df 121 return text.replace(/[-[\]{}()*+?.,\\^$|#\s]/g, "\\$&");
52090a1f
CP
122}
123
124qwebirc.ui.insertAt = function(position, parent, element) {
125 if(!parent.childNodes || (position >= parent.childNodes.length)) {
126 parent.appendChild(element);
127 } else {
128 parent.insertBefore(element, parent.childNodes[position]);
129 }
3c3ddb14
CP
130}
131
3184781b 132qwebirc.util.setCaretPos = function(obj, pos) {
3c3ddb14
CP
133 if($defined(obj.selectionStart)) {
134 obj.focus();
135 obj.setSelectionRange(pos, pos);
136 } else if(obj.createTextRange) {
137 var range = obj.createTextRange();
138 range.move("character", pos);
139 range.select();
140 }
141}
142
143qwebirc.util.setAtEnd = function(obj) {
3184781b 144 qwebirc.util.setCaretPos(obj.value.length);
3c3ddb14
CP
145}
146
147qwebirc.util.getCaretPos = function(element) {
148 if($defined(element.selectionStart))
149 return element.selectionStart;
150
151 if(document.selection) {
152 element.focus();
153 var sel = document.selection.createRange();
154 sel.moveStart("character", -element.value.length);
155 return sel.text.length;
156 }
157}
158
159qwebirc.util.browserVersion = function() {
160 //return "engine: " + Browser.Engine.name + " platform: " + Browser.Platform.name + " user agent: " + navigator.userAgent;
161 return navigator.userAgent;
162}
3184781b
CP
163
164qwebirc.util.getEnclosedWord = function(text, position) {
165 var l = text.split("");
166 var buf = [];
167
168 if(text == "")
169 return;
170
171 var start = position - 1;
172 if(start < 0) {
173 /* special case: starting with space */
174 start = 0;
175 } else {
176 /* work back until we find the first space */
177 for(;start>=0;start--) {
178 if(l[start] == ' ') {
179 start = start + 1;
180 break;
181 }
182 }
183 }
184
185 if(start < 0)
186 start = 0;
187
188 var s = text.substring(start);
189 var pos = s.indexOf(" ");
190 if(pos != -1)
191 s = s.substring(0, pos);
192
193 return [start, s];
194}
195
196String.prototype.startsWith = function(what) {
197 return this.substring(0, what.length) == what;
198}
f59585a7
CP
199
200/* NOT cryptographically secure! */
201qwebirc.util.randHexString = function(numBytes) {
202 var getByte = function() {
203 return (((1+Math.random())*0x100)|0).toString(16).substring(1);
204 };
205
206 var l = [];
207 for(var i=0;i<numBytes;i++)
208 l.push(getByte());
209
210 return l.join("");
211}
127631e0
CP
212
213qwebirc.util.importJS = function(name, watchFor, onload) {
214 var script = document.createElement("script");
215 script.type = "text/javascript";
216 script.src = name;
217
218 if(Browser.Engine.trident) {
219 /* HORRID */
220 var checkFn = function() {
221 if(eval("typeof " + watchFor) != "undefined") {
222 onload();
223 } else {
5ad04c7b 224 checkFn.delay(100);
127631e0
CP
225 }
226 }
227 checkFn();
228 } else {
229 script.onload = onload;
230 }
231 document.getElementsByTagName("head")[0].appendChild(script);
232}
a1e826c7 233
a9cbd00d 234qwebirc.util.createInput = function(type, parent, name, selected, id) {
a1e826c7
CP
235 var r;
236 if(Browser.Engine.trident) {
237 if(name) {
238 name = " name=\"" + escape(name) + "\"";
239 } else {
240 name = "";
241 }
a9cbd00d
CP
242 if(id) {
243 id = " id=\"" + escape(id) + "\"";
244 } else {
245 id = "";
246 }
f1398513
CP
247 try {
248 return $(document.createElement("<input type=\"" + type + "\"" + name + id + " " + (selected?" checked":"") + "/>"));
249 } catch(e) {
250 /* fallthough, trying it the proper way... */
251 }
a1e826c7 252 }
f1398513
CP
253
254 r = new Element("input");
255 r.type = type;
256 if(name)
257 r.name = name;
258 if(id)
259 r.id = id;
260
261 if(selected)
262 r.checked = true;
a1e826c7
CP
263
264 parent.appendChild(r);
265 return r;
266}
e516cc76
CP
267
268/* From: www.webtoolkit.info */
269qwebirc.util.b64Table = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=";
270qwebirc.util.b64Encode = function(data) {
271 var output = [];
272 var table = qwebirc.util.b64Table;
2a802692 273 for(var i=0;i<data.length;) {
e516cc76
CP
274 var chr1 = data.charCodeAt(i++);
275 var chr2 = data.charCodeAt(i++);
276 var chr3 = data.charCodeAt(i++);
277
278 var enc1 = chr1 >> 2;
279 var enc2 = ((chr1 & 3) << 4) | (chr2 >> 4);
280 var enc3 = ((chr2 & 15) << 2) | (chr3 >> 6);
281 var enc4 = chr3 & 63;
282
283 if(isNaN(chr2)) {
284 enc3 = enc4 = 64;
285 } else if(isNaN(chr3)) {
286 enc4 = 64;
287 }
288
289 output.push(table.charAt(enc1) + table.charAt(enc2) + table.charAt(enc3) + table.charAt(enc4));
290 }
291 return output.join("");
292}
293
294/* From: www.webtoolkit.info */
295qwebirc.util.b64Decode = function(data) {
296 data = data.replace(/[^A-Za-z0-9\+\/\=]/g, "");
297
298 var output = [];
299 var table = qwebirc.util.b64Table;
8d1217eb
CP
300
301 /* grossly inefficient... so sue me */
302 while(data.length % 4 != 0)
303 data = data + "=";
304
e516cc76
CP
305 for(var i=0;i<data.length;) {
306 var enc1 = table.indexOf(data.charAt(i++));
307 var enc2 = table.indexOf(data.charAt(i++));
308 var enc3 = table.indexOf(data.charAt(i++));
309 var enc4 = table.indexOf(data.charAt(i++));
310
311 var chr1 = (enc1 << 2) | (enc2 >> 4);
312 var chr2 = ((enc2 & 15) << 4) | (enc3 >> 2);
313 var chr3 = ((enc3 & 3) << 6) | enc4;
314
315 output.push(String.fromCharCode(chr1));
316 if (enc3 != 64)
317 output.push(String.fromCharCode(chr2));
318 if (enc4 != 64)
319 output.push(String.fromCharCode(chr3));
320 }
321
322 return output.join("");
323}
27add99a
CP
324
325qwebirc.util.composeAnd = function() {
326 var xargs = arguments;
327
328 return function() {
329 for(var i=0;i<xargs.length;i++)
330 if(!xargs[i].apply(this, arguments))
331 return false;
332
333 return true;
334 }
335}
336
337qwebirc.util.invertFn = function(fn) {
338 return function() {
339 return !fn.apply(this, arguments);
340 }
341}
c1416a8d
CP
342
343qwebirc.util.deviceHasKeyboard = function() {
344 var determine = function() {
345 if(Browser.Engine.ipod)
346 return true;
347
348 var MOBILE_UAs = ["Nintendo Wii", " PIE", "BlackBerry", "IEMobile", "Windows CE", "Nokia", "Opera Mini", "Mobile", "mobile", "Pocket", "pocket", "Android"];
349 /* safari not included because iphones/ipods send that, and we checked for iphone/ipod specifically above */
350 var DESKTOP_UAs = ["Chrome", "Firefox", "Camino", "Iceweasel", "K-Meleon", "Konqueror", "SeaMonkey", "Windows NT", "Windows 9"];
351
352 var ua = navigator.userAgent;
353
354 var contains = function(v) {
355 return ua.indexOf(v) > -1;
356 }
357
358 for(var i=0;i<MOBILE_UAs.length;i++)
359 if(contains(MOBILE_UAs[i]))
360 return false;
361
362 for(var i=0;i<DESKTOP_UAs.length;i++)
363 if(contains(DESKTOP_UAs[i]))
364 return true;
365
366 return false;
367 };
368 var v = determine();
369
370 qwebirc.util.deviceHasKeyboard = function() {
371 return v;
372 }
373
374 return v;
375}
a9cbd00d
CP
376
377qwebirc.util.generateID_ID = 0;
378qwebirc.util.generateID = function() {
379 return "qqa-" + qwebirc.util.generateID_ID++;
e87f9924
CP
380}
381
382qwebirc.util.__log = function(x) {
383 if(QWEBIRC_DEBUG) {
384 if(typeof console == "undefined") {
385 alert("log: " + x);
386 } else {
387 console.log(x);
388 }
389 }
390};
391
392qwebirc.util.logger = {
393 log: function(x) { qwebirc.util.__log("L " + x) },
394 info: function(x) { qwebirc.util.__log("I " + x) },
395 error: function(x) { qwebirc.util.__log("E " + x) },
396 warn: function(x) { qwebirc.util.__log("W " + x) }
397};
398
399qwebirc.util.log = qwebirc.util.logger.log;