Polishing join-fix
[KiwiIRC.git] / client / assets / src / helpers / utils.js
CommitLineData
2dd6a025
D
1/*jslint devel: true, browser: true, continue: true, sloppy: true, forin: true, plusplus: true, maxerr: 50, indent: 4, nomen: true, regexp: true*/
2/*globals $, front, gateway, Utilityview */
3
2dd6a025
D
4
5
6/**
7* Suppresses console.log
8* @param {Boolean} debug Whether to re-enable console.log or not
9*/
10function manageDebug(debug) {
2dd6a025
D
11 var log, consoleBackUp;
12 if (window.console) {
13 consoleBackUp = window.console.log;
14 window.console.log = function () {
15 if (debug) {
16 consoleBackUp.apply(console, arguments);
17 }
18 };
19 } else {
20 log = window.opera ? window.opera.postError : alert;
21 window.console = {};
22 window.console.log = function (str) {
23 if (debug) {
24 log(str);
25 }
26 };
27 }
28}
29
30/**
31* Generate a random string of given length
32* @param {Number} string_length The length of the random string
33* @returns {String} The random string
34*/
35function randomString(string_length) {
36 var chars = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXTZabcdefghiklmnopqrstuvwxyz",
37 randomstring = '',
38 i,
39 rnum;
40 for (i = 0; i < string_length; i++) {
41 rnum = Math.floor(Math.random() * chars.length);
42 randomstring += chars.substring(rnum, rnum + 1);
43 }
44 return randomstring;
45}
46
47/**
48* String.trim shim
49*/
50if (typeof String.prototype.trim === 'undefined') {
51 String.prototype.trim = function () {
52 return this.replace(/^\s+|\s+$/g, "");
53 };
54}
55
56/**
57* String.lpad shim
58* @param {Number} length The length of padding
59* @param {String} characher The character to pad with
60* @returns {String} The padded string
61*/
62if (typeof String.prototype.lpad === 'undefined') {
63 String.prototype.lpad = function (length, character) {
64 var padding = "",
65 i;
66 for (i = 0; i < length; i++) {
67 padding += character;
68 }
69 return (padding + this).slice(-length);
70 };
71}
72
73
74/**
75* Convert seconds into hours:minutes:seconds
76* @param {Number} secs The number of seconds to converts
77* @returns {Object} An object representing the hours/minutes/second conversion of secs
78*/
79function secondsToTime(secs) {
80 var hours, minutes, seconds, divisor_for_minutes, divisor_for_seconds, obj;
81 hours = Math.floor(secs / (60 * 60));
82
83 divisor_for_minutes = secs % (60 * 60);
84 minutes = Math.floor(divisor_for_minutes / 60);
85
86 divisor_for_seconds = divisor_for_minutes % 60;
87 seconds = Math.ceil(divisor_for_seconds);
88
89 obj = {
90 "h": hours,
91 "m": minutes,
92 "s": seconds
93 };
94 return obj;
95}
96
97
98
99
64c6bcb4
D
100
101
102/* Command input Alias + re-writing */
103function InputPreProcessor () {
104 this.recursive_depth = 3;
105
106 this.aliases = {};
107 this.vars = {version: 1};
108
109 // Current recursive depth
110 var depth = 0;
111
112
113 // Takes an array of words to process!
114 this.processInput = function (input) {
115 var words = input || [],
116 alias = this.aliases[words[0]],
117 alias_len,
118 current_alias_word = '',
119 compiled = [];
120
121 // If an alias wasn't found, return the original input
122 if (!alias) return input;
123
124 // Split the alias up into useable words
125 alias = alias.split(' ');
126 alias_len = alias.length;
127
128 // Iterate over each word and pop them into the final compiled array.
129 // Any $ words are processed with the result ending into the compiled array.
130 for (var i=0; i<alias_len; i++) {
131 current_alias_word = alias[i];
132
133 // Non $ word
134 if (current_alias_word[0] !== '$') {
135 compiled.push(current_alias_word);
136 continue;
137 }
138
139 // Refering to an input word ($N)
140 if (!isNaN(current_alias_word[1])) {
141 var num = current_alias_word.match(/\$(\d+)(\+)?(\d+)?/);
142
143 // Did we find anything or does the word it refers to non-existant?
144 if (!num || !words[num[1]]) continue;
145
146 if (num[2] === '+' && num[3]) {
147 // Add X number of words
148 compiled = compiled.concat(words.slice(parseInt(num[1], 10), parseInt(num[1], 10) + parseInt(num[3], 10)));
149 } else if (num[2] === '+') {
150 // Add the remaining of the words
151 compiled = compiled.concat(words.slice(parseInt(num[1], 10)));
152 } else {
153 // Add a single word
154 compiled.push(words[parseInt(num[1], 10)]);
155 }
156
157 continue;
158 }
159
160
161 // Refering to a variable
162 if (typeof this.vars[current_alias_word.substr(1)] !== 'undefined') {
163
164 // Get the variable
165 compiled.push(this.vars[current_alias_word.substr(1)]);
166
167 continue;
168 }
169
170 }
171
172 return compiled;
173 };
174
175
176 this.process = function (input) {
177 input = input || '';
178
179 var words = input.split(' ');
180
181 depth++;
182 if (depth >= this.recursive_depth) {
183 depth--;
184 return input;
185 }
186
187 if (this.aliases[words[0]]) {
188 words = this.processInput(words);
189
190 if (this.aliases[words[0]]) {
191 words = this.process(words.join(' ')).split(' ');
192 }
193
194 }
195
196 depth--;
197 return words.join(' ');
198 };
199}
200
201
202
203
204
205
206
21536e7b
D
207
208
209
210
1167a85d
D
211/**
212 * Convert HSL to RGB formatted colour
213 */
214function hsl2rgb(h, s, l) {
215 var m1, m2, hue;
216 var r, g, b
217 s /=100;
218 l /= 100;
219 if (s == 0)
220 r = g = b = (l * 255);
221 else {
222 function HueToRgb(m1, m2, hue) {
223 var v;
224 if (hue < 0)
225 hue += 1;
226 else if (hue > 1)
227 hue -= 1;
228
229 if (6 * hue < 1)
230 v = m1 + (m2 - m1) * hue * 6;
231 else if (2 * hue < 1)
232 v = m2;
233 else if (3 * hue < 2)
234 v = m1 + (m2 - m1) * (2/3 - hue) * 6;
235 else
236 v = m1;
237
238 return 255 * v;
239 }
240 if (l <= 0.5)
241 m2 = l * (s + 1);
242 else
243 m2 = l + s - l * s;
244 m1 = l * 2 - m2;
245 hue = h / 360;
246 r = HueToRgb(m1, m2, hue + 1/3);
247 g = HueToRgb(m1, m2, hue);
248 b = HueToRgb(m1, m2, hue - 1/3);
249 }
250 return [r,g,b];
251}
252
253
254
255
2dd6a025 256
2dd6a025
D
257/**
258* Formats a message. Adds bold, underline and colouring
259* @param {String} msg The message to format
260* @returns {String} The HTML formatted message
261*/
262function formatIRCMsg (msg) {
f47955d3
JA
263 "use strict";
264 var out = '',
265 currentTag = '',
266 openTags = {
267 bold: false,
268 italic: false,
269 underline: false,
270 colour: false
271 },
f47955d3
JA
272 spanFromOpen = function () {
273 var style = '',
274 colours;
643ca730 275 if (!(openTags.bold || openTags.italic || openTags.underline || openTags.colour)) {
f47955d3
JA
276 return '';
277 } else {
278 style += (openTags.bold) ? 'font-weight: bold; ' : '';
279 style += (openTags.italic) ? 'font-style: italic; ' : '';
280 style += (openTags.underline) ? 'text-decoration: underline; ' : '';
281 if (openTags.colour) {
282 colours = openTags.colour.split(',');
283 style += 'color: ' + colours[0] + ((colours[1]) ? '; background-color: ' + colours[1] + ';' : '');
284 }
9d8f8dfe 285 return '<span class="format_span" style="' + style + '">';
f47955d3
JA
286 }
287 },
2dd6a025 288 colourMatch = function (str) {
f47955d3 289 var re = /^\x03(([0-9][0-9]?)(,([0-9][0-9]?))?)/;
2dd6a025 290 return re.exec(str);
f47955d3
JA
291 },
292 hexFromNum = function (num) {
2dd6a025
D
293 switch (parseInt(num, 10)) {
294 case 0:
295 return '#FFFFFF';
296 case 1:
297 return '#000000';
298 case 2:
299 return '#000080';
300 case 3:
301 return '#008000';
302 case 4:
303 return '#FF0000';
304 case 5:
305 return '#800040';
306 case 6:
307 return '#800080';
308 case 7:
309 return '#FF8040';
310 case 8:
311 return '#FFFF00';
312 case 9:
313 return '#80FF00';
314 case 10:
315 return '#008080';
316 case 11:
317 return '#00FFFF';
318 case 12:
319 return '#0000FF';
320 case 13:
321 return '#FF55FF';
322 case 14:
323 return '#808080';
324 case 15:
325 return '#C0C0C0';
326 default:
327 return null;
328 }
f47955d3
JA
329 },
330 i = 0,
331 colours = [],
332 match;
333
334 for (i = 0; i < msg.length; i++) {
335 switch (msg[i]) {
336 case '\x02':
643ca730 337 if ((openTags.bold || openTags.italic || openTags.underline || openTags.colour)) {
f47955d3
JA
338 out += currentTag + '</span>';
339 }
340 openTags.bold = !openTags.bold;
341 currentTag = spanFromOpen();
342 break;
343 case '\x1D':
643ca730 344 if (an(openTags.bold || openTags.italic || openTags.underline || openTags.colour)) {
f47955d3
JA
345 out += currentTag + '</span>';
346 }
347 openTags.italic = !openTags.italic;
348 currentTag = spanFromOpen();
349 break;
350 case '\x1F':
643ca730 351 if ((openTags.bold || openTags.italic || openTags.underline || openTags.colour)) {
f47955d3
JA
352 out += currentTag + '</span>';
353 }
354 openTags.underline = !openTags.underline;
355 currentTag = spanFromOpen();
356 break;
357 case '\x03':
643ca730 358 if ((openTags.bold || openTags.italic || openTags.underline || openTags.colour)) {
f47955d3
JA
359 out += currentTag + '</span>';
360 }
361 match = colourMatch(msg.substr(i, 6));
362 if (match) {
f47955d3
JA
363 i += match[1].length;
364 // 2 & 4
365 colours[0] = hexFromNum(match[2]);
366 if (match[4]) {
367 colours[1] = hexFromNum(match[4]);
2dd6a025 368 }
f47955d3
JA
369 openTags.colour = colours.join(',');
370 } else {
371 openTags.colour = false;
372 }
373 currentTag = spanFromOpen();
374 break;
375 case '\x0F':
643ca730 376 if ((openTags.bold || openTags.italic || openTags.underline || openTags.colour)) {
f47955d3 377 out += currentTag + '</span>';
2dd6a025 378 }
f47955d3
JA
379 openTags.bold = openTags.italic = openTags.underline = openTags.colour = false;
380 break;
381 default:
643ca730 382 if ((openTags.bold || openTags.italic || openTags.underline || openTags.colour)) {
f47955d3
JA
383 currentTag += msg[i];
384 } else {
385 out += msg[i];
386 }
387 break;
2dd6a025 388 }
f47955d3 389 }
643ca730 390 if ((openTags.bold || openTags.italic || openTags.underline || openTags.colour)) {
f47955d3
JA
391 out += currentTag + '</span>';
392 }
393 return out;
2dd6a025
D
394}
395
396
397
398
51a4c383
D
399function formatDate (d) {
400 d = d || new Date();
401 return d.toLocaleDateString() + ', ' + d.getHours().toString() + ':' + d.getMinutes().toString() + ':' + d.getSeconds().toString();
6451c1bd 402}