]> jfr.im git - irc/quakenet/qwebirc.git/blame - js/jslib.js
Refactor isChannel, also push nicks onto the lastNick stack when we privmsg.
[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) {
20 var i = this.indexOf(f);
21 var c = this;
22
23 while(i > -1) {
24 c = c.replace(f, t);
25 i = c.indexOf(f);
26 }
27 return c;
28}
29
30/* how horribly inefficient (again) */
31String.prototype.splitMax = function(by, max) {
32 var items = this.split(by);
33 var newitems = items.slice(0, max-1);
34
35 if(items.length >= max)
36 newitems.push(items.slice(max-1).join(by));
37
38 return newitems;
39}
9b63b053 40
e20e5a6b 41qwebirc.util.setAtEnd = function(obj) {
9b63b053
CP
42 pos = obj.value.length;
43
44 if(obj.createTextRange) {
45 var range = obj.createTextRange();
46 range.move("character", pos);
47 range.select();
48 } else if(obj.selectionStart) {
49 obj.focus();
50 obj.setSelectionRange(pos, pos);
51 }
52}
66de775f
CP
53
54/* returns the arguments */
e20e5a6b 55qwebirc.util.parseURI = function(uri) {
66de775f
CP
56 var result = {}
57
58 var start = uri.indexOf('?');
59 if(start == -1)
60 return result;
61
62 var querystring = uri.substring(start + 1);
63
64 var args = querystring.split("&");
65
66 for(i=0;i<args.length;i++) {
67 var r = args[i].splitMax("=", 2);
68 if(r.length < 2)
69 continue;
70
71 result[unescape(r[0])] = unescape(r[1]);
72 }
73
74 return result;
75}
35155ba7 76
e20e5a6b
CP
77qwebirc.util.DaysOfWeek = {
78 0: "Sun",
79 1: "Mon",
80 2: "Tue",
81 3: "Wed",
82 4: "Thu",
83 5: "Fri",
84 6: "Sat"
85};
86
87qwebirc.util.MonthsOfYear = {
88 0: "Jan",
89 1: "Feb",
90 2: "Mar",
91 3: "Apr",
92 4: "May",
93 5: "Jun",
94 6: "Jul",
95 7: "Aug",
96 8: "Sep",
97 9: "Oct",
98 10: "Nov",
99 11: "Dec"
100};
1d6756bc
CP
101
102qwebirc.util.NBSPCreate = function(text, element) {
103 var e = text.split(" ");
104 for(var i=0;i<e.length;i++) {
105 var tn = document.createTextNode(e[i]);
106 element.appendChild(tn);
107
108 if(i != e.length - 1) {
109 var e2 = new Element("span");
110 e2.set("html", "&nbsp;&nbsp;");
111 element.appendChild(e2);
112 }
113 }
114};
115
116qwebirc.util.longtoduration = function(l) {
117 var seconds = l % 60;
4ad3011a
CP
118 var minutes = Math.round((l % 3600) / 60);
119 var hours = Math.round((l % (3600 * 24)) / 3600);
120 var days = Math.round(l / (24*3600));
1d6756bc
CP
121
122 return days + " days " + hours + " hours " + minutes + " minutes " + seconds + " seconds";
123}
8408e834
CP
124
125qwebirc.util.pad = function(x) {
126 x = "" + x;
127 if(x.length == 1)
128 return "0" + x;
129 return x
130}
96f28062
CP
131
132RegExp.escape = function(text) {
133 if(!arguments.callee.sRE) {
134 var specials = [
135 '/', '.', '*', '+', '?', '|',
136 '(', ')', '[', ']', '{', '}', '\\'
137 ];
138 arguments.callee.sRE = new RegExp('(\\' + specials.join('|\\') + ')', 'g');
139 }
140
141 return text.replace(arguments.callee.sRE, '\\$1');
52090a1f
CP
142}
143
144qwebirc.ui.insertAt = function(position, parent, element) {
145 if(!parent.childNodes || (position >= parent.childNodes.length)) {
146 parent.appendChild(element);
147 } else {
148 parent.insertBefore(element, parent.childNodes[position]);
149 }
96f28062 150}