]>
Commit | Line | Data |
---|---|---|
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", " "); | |
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 | /* NOT cryptographically secure! */ | |
227 | qwebirc.util.randHexString = function(numBytes) { | |
228 | var getByte = function() { | |
229 | return (((1+Math.random())*0x100)|0).toString(16).substring(1); | |
230 | }; | |
231 | ||
232 | var l = []; | |
233 | for(var i=0;i<numBytes;i++) | |
234 | l.push(getByte()); | |
235 | ||
236 | return l.join(""); | |
237 | } | |
238 | ||
239 | qwebirc.util.importJS = function(name, watchFor, onload) { | |
240 | var script = document.createElement("script"); | |
241 | script.type = "text/javascript"; | |
242 | script.src = name; | |
243 | ||
244 | if(Browser.Engine.trident) { | |
245 | /* HORRID */ | |
246 | var checkFn = function() { | |
247 | if(eval("typeof " + watchFor) != "undefined") { | |
248 | onload(); | |
249 | } else { | |
250 | checkFn.delay(100); | |
251 | } | |
252 | } | |
253 | checkFn(); | |
254 | } else { | |
255 | script.onload = onload; | |
256 | } | |
257 | document.getElementsByTagName("head")[0].appendChild(script); | |
258 | } | |
259 | ||
260 | qwebirc.util.createInput = function(type, parent, name, selected, id) { | |
261 | var r; | |
262 | if(Browser.Engine.trident) { | |
263 | if(name) { | |
264 | name = " name=\"" + escape(name) + "\""; | |
265 | } else { | |
266 | name = ""; | |
267 | } | |
268 | if(id) { | |
269 | id = " id=\"" + escape(id) + "\""; | |
270 | } else { | |
271 | id = ""; | |
272 | } | |
273 | try { | |
274 | return $(document.createElement("<input type=\"" + type + "\"" + name + id + " " + (selected?" checked":"") + "/>")); | |
275 | } catch(e) { | |
276 | /* fallthough, trying it the proper way... */ | |
277 | } | |
278 | } | |
279 | ||
280 | r = new Element("input"); | |
281 | r.type = type; | |
282 | if(name) | |
283 | r.name = name; | |
284 | if(id) | |
285 | r.id = id; | |
286 | ||
287 | if(selected) | |
288 | r.checked = true; | |
289 | ||
290 | parent.appendChild(r); | |
291 | return r; | |
292 | } | |
293 | ||
294 | /* From: www.webtoolkit.info */ | |
295 | qwebirc.util.b64Table = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/="; | |
296 | qwebirc.util.b64Encode = function(data) { | |
297 | var output = []; | |
298 | var table = qwebirc.util.b64Table; | |
299 | for(var i=0;i<data.length;) { | |
300 | var chr1 = data.charCodeAt(i++); | |
301 | var chr2 = data.charCodeAt(i++); | |
302 | var chr3 = data.charCodeAt(i++); | |
303 | ||
304 | var enc1 = chr1 >> 2; | |
305 | var enc2 = ((chr1 & 3) << 4) | (chr2 >> 4); | |
306 | var enc3 = ((chr2 & 15) << 2) | (chr3 >> 6); | |
307 | var enc4 = chr3 & 63; | |
308 | ||
309 | if(isNaN(chr2)) { | |
310 | enc3 = enc4 = 64; | |
311 | } else if(isNaN(chr3)) { | |
312 | enc4 = 64; | |
313 | } | |
314 | ||
315 | output.push(table.charAt(enc1) + table.charAt(enc2) + table.charAt(enc3) + table.charAt(enc4)); | |
316 | } | |
317 | return output.join(""); | |
318 | } | |
319 | ||
320 | /* From: www.webtoolkit.info */ | |
321 | qwebirc.util.b64Decode = function(data) { | |
322 | data = data.replace(/[^A-Za-z0-9\+\/\=]/g, ""); | |
323 | ||
324 | var output = []; | |
325 | var table = qwebirc.util.b64Table; | |
326 | ||
327 | /* grossly inefficient... so sue me */ | |
328 | while(data.length % 4 != 0) | |
329 | data = data + "="; | |
330 | ||
331 | for(var i=0;i<data.length;) { | |
332 | var enc1 = table.indexOf(data.charAt(i++)); | |
333 | var enc2 = table.indexOf(data.charAt(i++)); | |
334 | var enc3 = table.indexOf(data.charAt(i++)); | |
335 | var enc4 = table.indexOf(data.charAt(i++)); | |
336 | ||
337 | var chr1 = (enc1 << 2) | (enc2 >> 4); | |
338 | var chr2 = ((enc2 & 15) << 4) | (enc3 >> 2); | |
339 | var chr3 = ((enc3 & 3) << 6) | enc4; | |
340 | ||
341 | output.push(String.fromCharCode(chr1)); | |
342 | if (enc3 != 64) | |
343 | output.push(String.fromCharCode(chr2)); | |
344 | if (enc4 != 64) | |
345 | output.push(String.fromCharCode(chr3)); | |
346 | } | |
347 | ||
348 | return output.join(""); | |
349 | } | |
350 | ||
351 | qwebirc.util.composeAnd = function() { | |
352 | var xargs = arguments; | |
353 | ||
354 | return function() { | |
355 | for(var i=0;i<xargs.length;i++) | |
356 | if(!xargs[i].apply(this, arguments)) | |
357 | return false; | |
358 | ||
359 | return true; | |
360 | } | |
361 | } | |
362 | ||
363 | qwebirc.util.invertFn = function(fn) { | |
364 | return function() { | |
365 | return !fn.apply(this, arguments); | |
366 | } | |
367 | } | |
368 | ||
369 | qwebirc.util.deviceHasKeyboard = function() { | |
370 | var determine = function() { | |
371 | if(Browser.Engine.ipod) | |
372 | return true; | |
373 | ||
374 | var MOBILE_UAs = ["Nintendo Wii", " PIE", "BlackBerry", "IEMobile", "Windows CE", "Nokia", "Opera Mini", "Mobile", "mobile", "Pocket", "pocket", "Android"]; | |
375 | /* safari not included because iphones/ipods send that, and we checked for iphone/ipod specifically above */ | |
376 | var DESKTOP_UAs = ["Chrome", "Firefox", "Camino", "Iceweasel", "K-Meleon", "Konqueror", "SeaMonkey", "Windows NT", "Windows 9"]; | |
377 | ||
378 | var ua = navigator.userAgent; | |
379 | ||
380 | var contains = function(v) { | |
381 | return ua.indexOf(v) > -1; | |
382 | } | |
383 | ||
384 | for(var i=0;i<MOBILE_UAs.length;i++) | |
385 | if(contains(MOBILE_UAs[i])) | |
386 | return false; | |
387 | ||
388 | for(var i=0;i<DESKTOP_UAs.length;i++) | |
389 | if(contains(DESKTOP_UAs[i])) | |
390 | return true; | |
391 | ||
392 | return false; | |
393 | }; | |
394 | var v = determine(); | |
395 | ||
396 | qwebirc.util.deviceHasKeyboard = function() { | |
397 | return v; | |
398 | } | |
399 | ||
400 | return v; | |
401 | } | |
402 | ||
403 | qwebirc.util.generateID_ID = 0; | |
404 | qwebirc.util.generateID = function() { | |
405 | return "qqa-" + qwebirc.util.generateID_ID++; | |
406 | } | |
407 | ||
408 | qwebirc.util.__log = function(x) { | |
409 | if(QWEBIRC_DEBUG) { | |
410 | if(typeof console == "undefined") { | |
411 | alert("log: " + x); | |
412 | } else { | |
413 | console.log(x); | |
414 | } | |
415 | } | |
416 | }; | |
417 | ||
418 | qwebirc.util.logger = { | |
419 | log: function(x) { qwebirc.util.__log("L " + x) }, | |
420 | info: function(x) { qwebirc.util.__log("I " + x) }, | |
421 | error: function(x) { qwebirc.util.__log("E " + x) }, | |
422 | warn: function(x) { qwebirc.util.__log("W " + x) } | |
423 | }; | |
424 | ||
425 | qwebirc.util.log = qwebirc.util.logger.log; |