]> jfr.im git - irc/quakenet/qwebirc.git/blob - js/jslib.js
iframes should not have borders -- breaks page layout
[irc/quakenet/qwebirc.git] / js / jslib.js
1 Array.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
10 qwebirc.util.dictCopy = function(d) {
11 var n = {};
12 for(var k in d)
13 n[k] = d[k];
14
15 return n;
16 }
17
18 /* how horribly inefficient */
19 String.prototype.replaceAll = function(f, t) {
20 //return new RegExp("/" + RegExp.escape(f) + "/g").replace(f, RegExp.escape(t));
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) */
32 String.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 }
41
42 /* returns the arguments */
43 qwebirc.util.parseURI = function(uri) {
44 var result = new QHash();
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
54 for(var i=0;i<args.length;i++) {
55 var r = args[i].splitMax("=", 2);
56 if(r.length < 2)
57 continue;
58
59 result.put(unescape(r[0]), unescape(r[1]));
60 }
61
62 return result;
63 };
64
65 qwebirc.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
75 qwebirc.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 };
89
90 qwebirc.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
104 qwebirc.util.longtoduration = function(l) {
105 var seconds = l % 60;
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));
109
110 return days + " days " + hours + " hours " + minutes + " minutes " + seconds + " seconds";
111 }
112
113 qwebirc.util.pad = function(x) {
114 x = "" + x;
115 if(x.length == 1)
116 return "0" + x;
117 return x
118 }
119
120 RegExp.escape = function(text) {
121 return text.replace(/[-[\]{}()*+?.,\\^$|#\s]/g, "\\$&");
122 };
123
124 RegExp.fromIRCPattern = function(t) {
125 /* escape everything but ? . and * */
126 var t = t.replace(/[-[\]{}()+,\\^$|#\s]/g, "\\$&");
127 t = t.split("");
128 var out = [];
129
130 /* now process the rest */
131 for(var i=0;i<t.length;i++) {
132 var c = t[i];
133 switch(c) {
134 case '.':
135 out.push("\\.");
136 break;
137 case '?':
138 out.push(".");
139 break;
140 case '*':
141 out.push(".*");
142 break;
143 default:
144 out.push(c);
145 }
146 }
147 return out.join("");
148 }
149
150 qwebirc.ui.insertAt = function(position, parent, element) {
151 if(!parent.childNodes || (position >= parent.childNodes.length)) {
152 parent.appendChild(element);
153 } else {
154 parent.insertBefore(element, parent.childNodes[position]);
155 }
156 }
157
158 qwebirc.util.setCaretPos = function(obj, pos) {
159 if($defined(obj.selectionStart)) {
160 obj.focus();
161 obj.setSelectionRange(pos, pos);
162 } else if(obj.createTextRange) {
163 var range = obj.createTextRange();
164 range.move("character", pos);
165 range.select();
166 }
167 }
168
169 qwebirc.util.setAtEnd = function(obj) {
170 qwebirc.util.setCaretPos(obj.value.length);
171 }
172
173 qwebirc.util.getCaretPos = function(element) {
174 if($defined(element.selectionStart))
175 return element.selectionStart;
176
177 if(document.selection) {
178 element.focus();
179 var sel = document.selection.createRange();
180 sel.moveStart("character", -element.value.length);
181 return sel.text.length;
182 }
183 }
184
185 qwebirc.util.browserVersion = function() {
186 //return "engine: " + Browser.Engine.name + " platform: " + Browser.Platform.name + " user agent: " + navigator.userAgent;
187 return navigator.userAgent;
188 }
189
190 qwebirc.util.getEnclosedWord = function(text, position) {
191 var l = text.split("");
192 var buf = [];
193
194 if(text == "")
195 return;
196
197 var start = position - 1;
198 if(start < 0) {
199 /* special case: starting with space */
200 start = 0;
201 } else {
202 /* work back until we find the first space */
203 for(;start>=0;start--) {
204 if(l[start] == ' ') {
205 start = start + 1;
206 break;
207 }
208 }
209 }
210
211 if(start < 0)
212 start = 0;
213
214 var s = text.substring(start);
215 var pos = s.indexOf(" ");
216 if(pos != -1)
217 s = s.substring(0, pos);
218
219 return [start, s];
220 }
221
222 String.prototype.startsWith = function(what) {
223 return this.substring(0, what.length) == what;
224 }
225
226 String.prototype.endsWith = function(what) {
227 return this.substring(this.length - what.length, this.length) == what;
228 };
229
230 /* NOT cryptographically secure! */
231 qwebirc.util.randHexString = function(numBytes) {
232 var getByte = function() {
233 return (((1+Math.random())*0x100)|0).toString(16).substring(1);
234 };
235
236 var l = [];
237 for(var i=0;i<numBytes;i++)
238 l.push(getByte());
239
240 return l.join("");
241 }
242
243 qwebirc.util.importJS = function(name, watchFor, onload) {
244 var script = document.createElement("script");
245 script.type = "text/javascript";
246 script.src = name;
247
248 if(Browser.Engine.trident) {
249 /* HORRID */
250 var checkFn = function() {
251 if(eval("typeof " + watchFor) != "undefined") {
252 onload();
253 } else {
254 checkFn.delay(100);
255 }
256 }
257 checkFn();
258 } else {
259 script.onload = onload;
260 }
261 document.getElementsByTagName("head")[0].appendChild(script);
262 }
263
264 qwebirc.util.createInput = function(type, parent, name, selected, id) {
265 var created = false;
266 var r;
267 if (name)
268 name = "__input" + name;
269
270 if (Browser.Engine.trident) {
271 var name2;
272 if (name) {
273 name2 = " name=\"" + escape(name) + "\"";
274 } else {
275 name2 = "";
276 }
277 try {
278 var h = "<input type=\"" + type + "\"" + name2 + "/>";
279 r = $(document.createElement(h));
280 if (type == "radio") {
281 r.addEvent("click", function () {
282 $(document.body).getElements("input[name=" + name + "]").forEach(function (x) {
283 x.setAttribute("defaultChecked", x.checked ? "defaultChecked" : "");
284 });
285 });
286 }
287 created = true;
288 } catch (e) {
289 /* fallthough, trying it the proper way... */
290 }
291 }
292
293 if(!created) {
294 r = new Element("input");
295 r.setAttribute("type", type);
296 }
297 if(name)
298 r.setAttribute("name", name);
299 if(id)
300 r.setAttribute("id", id);
301 if(selected) {
302 r.setAttribute("checked", "checked");
303 if(type == "radio" && Browser.Engine.trident)
304 r.setAttribute("defaultChecked", "defaultChecked");
305 }
306
307 parent.appendChild(r);
308 return r;
309 }
310
311 /* From: www.webtoolkit.info */
312 qwebirc.util.b64Table = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=";
313 qwebirc.util.b64Encode = function(data) {
314 var output = [];
315 var table = qwebirc.util.b64Table;
316 for(var i=0;i<data.length;) {
317 var chr1 = data.charCodeAt(i++);
318 var chr2 = data.charCodeAt(i++);
319 var chr3 = data.charCodeAt(i++);
320
321 var enc1 = chr1 >> 2;
322 var enc2 = ((chr1 & 3) << 4) | (chr2 >> 4);
323 var enc3 = ((chr2 & 15) << 2) | (chr3 >> 6);
324 var enc4 = chr3 & 63;
325
326 if(isNaN(chr2)) {
327 enc3 = enc4 = 64;
328 } else if(isNaN(chr3)) {
329 enc4 = 64;
330 }
331
332 output.push(table.charAt(enc1) + table.charAt(enc2) + table.charAt(enc3) + table.charAt(enc4));
333 }
334 return output.join("");
335 }
336
337 /* From: www.webtoolkit.info */
338 qwebirc.util.b64Decode = function(data) {
339 data = data.replace(/[^A-Za-z0-9\+\/\=]/g, "");
340
341 var output = [];
342 var table = qwebirc.util.b64Table;
343
344 /* grossly inefficient... so sue me */
345 while(data.length % 4 != 0)
346 data = data + "=";
347
348 for(var i=0;i<data.length;) {
349 var enc1 = table.indexOf(data.charAt(i++));
350 var enc2 = table.indexOf(data.charAt(i++));
351 var enc3 = table.indexOf(data.charAt(i++));
352 var enc4 = table.indexOf(data.charAt(i++));
353
354 var chr1 = (enc1 << 2) | (enc2 >> 4);
355 var chr2 = ((enc2 & 15) << 4) | (enc3 >> 2);
356 var chr3 = ((enc3 & 3) << 6) | enc4;
357
358 output.push(String.fromCharCode(chr1));
359 if (enc3 != 64)
360 output.push(String.fromCharCode(chr2));
361 if (enc4 != 64)
362 output.push(String.fromCharCode(chr3));
363 }
364
365 return output.join("");
366 }
367
368 qwebirc.util.composeAnd = function() {
369 var xargs = arguments;
370
371 return function() {
372 for(var i=0;i<xargs.length;i++)
373 if(!xargs[i].apply(this, arguments))
374 return false;
375
376 return true;
377 }
378 }
379
380 qwebirc.util.invertFn = function(fn) {
381 return function() {
382 return !fn.apply(this, arguments);
383 }
384 }
385
386 qwebirc.util.deviceHasKeyboard = function() {
387 var determine = function() {
388 if(Browser.Engine.ipod)
389 return true;
390
391 var MOBILE_UAs = ["Nintendo Wii", " PIE", "BlackBerry", "IEMobile", "Windows CE", "Nokia", "Opera Mini", "Mobile", "mobile", "Pocket", "pocket", "Android"];
392 /* safari not included because iphones/ipods send that, and we checked for iphone/ipod specifically above */
393 var DESKTOP_UAs = ["Chrome", "Firefox", "Camino", "Iceweasel", "K-Meleon", "Konqueror", "SeaMonkey", "Windows NT", "Windows 9"];
394
395 var ua = navigator.userAgent;
396
397 var contains = function(v) {
398 return ua.indexOf(v) > -1;
399 }
400
401 for(var i=0;i<MOBILE_UAs.length;i++)
402 if(contains(MOBILE_UAs[i]))
403 return false;
404
405 for(var i=0;i<DESKTOP_UAs.length;i++)
406 if(contains(DESKTOP_UAs[i]))
407 return true;
408
409 return false;
410 };
411 var v = determine();
412
413 qwebirc.util.deviceHasKeyboard = function() {
414 return v;
415 }
416
417 return v;
418 }
419
420 qwebirc.util.generateID_ID = 0;
421 qwebirc.util.generateID = function() {
422 return "qqa-" + qwebirc.util.generateID_ID++;
423 };
424
425 qwebirc.util.arrayCmp = function(a, b) {
426 for(var p=0;p<a.length;p++) {
427 var ap = a[p];
428 var bp = b[p];
429 if(ap == bp)
430 continue;
431
432 if(ap < bp)
433 return -1;
434
435 return 1;
436 }
437 return 0;
438 };
439
440 qwebirc.util.__log = function(x) {
441 if(QWEBIRC_DEBUG) {
442 if(typeof console == "undefined") {
443 alert("log: " + x);
444 } else {
445 console.log(x);
446 }
447 }
448 };
449
450 qwebirc.util.logger = {
451 log: function(x) { qwebirc.util.__log("L " + x) },
452 info: function(x) { qwebirc.util.__log("I " + x) },
453 error: function(x) { qwebirc.util.__log("E " + x) },
454 warn: function(x) { qwebirc.util.__log("W " + x) }
455 };
456
457 qwebirc.util.log = qwebirc.util.logger.log;