]> jfr.im git - irc/quakenet/qwebirc.git/blob - js/jslib.js
Fix idle time display.
[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 /* how horribly inefficient */
11 String.prototype.replaceAll = function(f, t) {
12 var i = this.indexOf(f);
13 var c = this;
14
15 while(i > -1) {
16 c = c.replace(f, t);
17 i = c.indexOf(f);
18 }
19 return c;
20 }
21
22 /* how horribly inefficient (again) */
23 String.prototype.splitMax = function(by, max) {
24 var items = this.split(by);
25 var newitems = items.slice(0, max-1);
26
27 if(items.length >= max)
28 newitems.push(items.slice(max-1).join(by));
29
30 return newitems;
31 }
32
33 qwebirc.util.setAtEnd = function(obj) {
34 pos = obj.value.length;
35
36 if(obj.createTextRange) {
37 var range = obj.createTextRange();
38 range.move("character", pos);
39 range.select();
40 } else if(obj.selectionStart) {
41 obj.focus();
42 obj.setSelectionRange(pos, pos);
43 }
44 }
45
46 /* returns the arguments */
47 qwebirc.util.parseURI = function(uri) {
48 var result = {}
49
50 var start = uri.indexOf('?');
51 if(start == -1)
52 return result;
53
54 var querystring = uri.substring(start + 1);
55
56 var args = querystring.split("&");
57
58 for(i=0;i<args.length;i++) {
59 var r = args[i].splitMax("=", 2);
60 if(r.length < 2)
61 continue;
62
63 result[unescape(r[0])] = unescape(r[1]);
64 }
65
66 return result;
67 }
68
69 qwebirc.util.DaysOfWeek = {
70 0: "Sun",
71 1: "Mon",
72 2: "Tue",
73 3: "Wed",
74 4: "Thu",
75 5: "Fri",
76 6: "Sat"
77 };
78
79 qwebirc.util.MonthsOfYear = {
80 0: "Jan",
81 1: "Feb",
82 2: "Mar",
83 3: "Apr",
84 4: "May",
85 5: "Jun",
86 6: "Jul",
87 7: "Aug",
88 8: "Sep",
89 9: "Oct",
90 10: "Nov",
91 11: "Dec"
92 };
93
94 qwebirc.util.NBSPCreate = function(text, element) {
95 var e = text.split(" ");
96 for(var i=0;i<e.length;i++) {
97 var tn = document.createTextNode(e[i]);
98 element.appendChild(tn);
99
100 if(i != e.length - 1) {
101 var e2 = new Element("span");
102 e2.set("html", "&nbsp;&nbsp;");
103 element.appendChild(e2);
104 }
105 }
106 };
107
108 qwebirc.util.longtoduration = function(l) {
109 var seconds = l % 60;
110 var minutes = Math.round(l / 60);
111 var hours = Math.round(minutes / 60);
112 var days = Math.round(hours / 24);
113
114 return days + " days " + hours + " hours " + minutes + " minutes " + seconds + " seconds";
115 }
116
117 qwebirc.util.pad = function(x) {
118 x = "" + x;
119 if(x.length == 1)
120 return "0" + x;
121 return x
122 }