]> jfr.im git - irc/quakenet/qwebirc.git/blame - js/jslib.js
Merge pull request #373 from retropc/kill_flash
[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) {
ea29e3d7 44 var result = new QHash();
66de775f
CP
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
ea29e3d7 59 result.put(unescape(r[0]), unescape(r[1]));
66de775f
CP
60 }
61
62 return result;
ea29e3d7 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, "\\$&");
b90b6d5f
CP
122};
123
124RegExp.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("");
52090a1f
CP
148}
149
150qwebirc.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 }
3c3ddb14
CP
156}
157
3184781b 158qwebirc.util.setCaretPos = function(obj, pos) {
3c3ddb14
CP
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
169qwebirc.util.setAtEnd = function(obj) {
3184781b 170 qwebirc.util.setCaretPos(obj.value.length);
3c3ddb14
CP
171}
172
173qwebirc.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
185qwebirc.util.browserVersion = function() {
186 //return "engine: " + Browser.Engine.name + " platform: " + Browser.Platform.name + " user agent: " + navigator.userAgent;
187 return navigator.userAgent;
188}
3184781b
CP
189
190qwebirc.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
222String.prototype.startsWith = function(what) {
223 return this.substring(0, what.length) == what;
224}
f59585a7 225
48a9e89a
CP
226String.prototype.endsWith = function(what) {
227 return this.substring(this.length - what.length, this.length) == what;
228};
229
f59585a7
CP
230/* NOT cryptographically secure! */
231qwebirc.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}
127631e0
CP
242
243qwebirc.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 {
5ad04c7b 254 checkFn.delay(100);
127631e0
CP
255 }
256 }
257 checkFn();
258 } else {
259 script.onload = onload;
260 }
261 document.getElementsByTagName("head")[0].appendChild(script);
262}
a1e826c7 263
a9cbd00d 264qwebirc.util.createInput = function(type, parent, name, selected, id) {
8d2bf7d0 265 var created = false;
a1e826c7 266 var r;
a4a71818 267 if (name)
8d2bf7d0
CP
268 name = "__input" + name;
269
a4a71818 270 if (Browser.Engine.trident) {
8d2bf7d0 271 var name2;
a4a71818 272 if (name) {
8d2bf7d0 273 name2 = " name=\"" + escape(name) + "\"";
a9cbd00d 274 } else {
8d2bf7d0 275 name2 = "";
a9cbd00d 276 }
f1398513 277 try {
8d2bf7d0
CP
278 var h = "<input type=\"" + type + "\"" + name2 + "/>";
279 r = $(document.createElement(h));
280 if (type == "radio") {
a4a71818
CP
281 r.addEvent("click", function () {
282 $(document.body).getElements("input[name=" + name + "]").forEach(function (x) {
8d2bf7d0
CP
283 x.setAttribute("defaultChecked", x.checked ? "defaultChecked" : "");
284 });
285 });
286 }
287 created = true;
288 } catch (e) {
f1398513
CP
289 /* fallthough, trying it the proper way... */
290 }
a1e826c7 291 }
8d2bf7d0 292
a4a71818
CP
293 if(!created) {
294 r = new Element("input");
295 r.setAttribute("type", type);
296 }
f1398513 297 if(name)
8d2bf7d0 298 r.setAttribute("name", name);
f1398513 299 if(id)
8d2bf7d0
CP
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
a1e826c7
CP
307 parent.appendChild(r);
308 return r;
309}
e516cc76 310
27add99a
CP
311qwebirc.util.composeAnd = function() {
312 var xargs = arguments;
313
314 return function() {
315 for(var i=0;i<xargs.length;i++)
316 if(!xargs[i].apply(this, arguments))
317 return false;
318
319 return true;
320 }
321}
322
323qwebirc.util.invertFn = function(fn) {
324 return function() {
325 return !fn.apply(this, arguments);
326 }
327}
c1416a8d
CP
328
329qwebirc.util.deviceHasKeyboard = function() {
330 var determine = function() {
331 if(Browser.Engine.ipod)
332 return true;
333
334 var MOBILE_UAs = ["Nintendo Wii", " PIE", "BlackBerry", "IEMobile", "Windows CE", "Nokia", "Opera Mini", "Mobile", "mobile", "Pocket", "pocket", "Android"];
335 /* safari not included because iphones/ipods send that, and we checked for iphone/ipod specifically above */
336 var DESKTOP_UAs = ["Chrome", "Firefox", "Camino", "Iceweasel", "K-Meleon", "Konqueror", "SeaMonkey", "Windows NT", "Windows 9"];
337
338 var ua = navigator.userAgent;
339
340 var contains = function(v) {
341 return ua.indexOf(v) > -1;
342 }
343
344 for(var i=0;i<MOBILE_UAs.length;i++)
345 if(contains(MOBILE_UAs[i]))
346 return false;
347
348 for(var i=0;i<DESKTOP_UAs.length;i++)
349 if(contains(DESKTOP_UAs[i]))
350 return true;
351
352 return false;
353 };
354 var v = determine();
355
356 qwebirc.util.deviceHasKeyboard = function() {
357 return v;
358 }
359
360 return v;
361}
a9cbd00d
CP
362
363qwebirc.util.generateID_ID = 0;
364qwebirc.util.generateID = function() {
365 return "qqa-" + qwebirc.util.generateID_ID++;
4b3dcc22
CP
366};
367
368qwebirc.util.arrayCmp = function(a, b) {
369 for(var p=0;p<a.length;p++) {
370 var ap = a[p];
371 var bp = b[p];
372 if(ap == bp)
373 continue;
374
375 if(ap < bp)
376 return -1;
377
378 return 1;
379 }
380 return 0;
381};
e87f9924
CP
382
383qwebirc.util.__log = function(x) {
384 if(QWEBIRC_DEBUG) {
385 if(typeof console == "undefined") {
386 alert("log: " + x);
387 } else {
388 console.log(x);
389 }
390 }
391};
392
393qwebirc.util.logger = {
394 log: function(x) { qwebirc.util.__log("L " + x) },
395 info: function(x) { qwebirc.util.__log("I " + x) },
396 error: function(x) { qwebirc.util.__log("E " + x) },
397 warn: function(x) { qwebirc.util.__log("W " + x) }
398};
399
400qwebirc.util.log = qwebirc.util.logger.log;