aboutsummaryrefslogblamecommitdiffstats
path: root/docs/html/overview-summary-util.js.html
blob: 6be01b54e7d9bcd2d8ef4d141b4e70e80a6e07a0 (plain) (tree)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453




































































































































































































































































































































































































































































                                                                                                                                                              
<!doctype html public "-//W3C//DTD HTML 4.0 Frameset//EN""http://www.w3.org/TR/REC-html40/frameset.dtd">
<html>
<head>
<title>
 Overview
</title>
<link rel ="stylesheet" type="text/css" href="stylesheet.css" title="Style">
<script>
function asd() {
	
		parent.document.title="util.js Overview";
	
}
</script>
</head>
<body bgcolor="white" onload="asd();">

<!-- ========== START OF NAVBAR ========== -->
<a name="navbar_top"><!-- --></a>
<table border="0" width="100%" cellpadding="1" cellspacing="0">
<tr>
<td colspan=2 bgcolor="#EEEEFF" class="NavBarCell1">
<a name="navbar_top_firstrow"><!-- --></a>
<table border="0" cellpadding="0" cellspacing="3">
  <tr align="center" valign="top">
  
  
  <td bgcolor="#EEEEFF" class="NavBarCell1">    <a href="overview-summary.html"><font class="NavBarFont1"><b>Overview</b></font></a>&nbsp;</td>
  <td bgcolor="#FFFFFF" class="NavBarCell1Rev">	&nbsp;<font class="NavBarFont1Rev"><b>File</b></font>&nbsp;</td>
  

  <td bgcolor="#FFFFFF" class="NavBarCell1"> 	<font class="NavBarFont1">Class</font>&nbsp;</td>
  <td bgcolor="#EEEEFF" class="NavBarCell1">    <a href="overview-tree.html"><font class="NavBarFont1"><b>Tree</b></font></a>&nbsp;</td>
  <td bgcolor="#EEEEFF" class="NavBarCell1">    <a href="index-all.html"--><font class="NavBarFont1"><b>Index</b></font></a>&nbsp;</td>
  <td bgcolor="#EEEEFF" class="NavBarCell1">    <a href="help-doc.html"><font class="NavBarFont1"><b>Help</b></font></a>&nbsp;</td>
  </tr>
</table>
</td>
<td bgcolor="#EEEEFF" align="right" valign="top">
<em>
<b></b></em>
</td>
</tr>

<tr>
<td bgcolor="white" class="NavBarCell2"><font size="-2">
&nbsp;PREV&nbsp;
&nbsp;NEXT</font></td>
<td bgcolor="white" class="NavBarCell2"><font size="-2">
  <a href="index.html" target="_top"><b>FRAMES</b></a>  &nbsp;
&nbsp;<a href="overview-summary.html" target="_top"><b>NO FRAMES</b></a>
&nbsp;&nbsp;
<script>
  <!--
  if(window==top) {
    document.writeln('<A HREF="allclasses-noframe.html" TARGET=""><B>All Classes</B></A>');
  }
  //-->
</script>
<noscript>
<a href="allclasses-noframe.html" target=""><b>All Classes</b></a>
</noscript>
</font></td>
</tr>
</table>
<!-- =========== END OF NAVBAR =========== -->

<hr>
<center>
	
	   <h2>util.js</h2>
	
</center>

	


<h4>Summary</h4>
<p>
	
		No overview generated for 'util.js'<BR/><BR/>
	
</p>

<hr>



<!-- ========== METHOD SUMMARY =========== -->

<!-- ========== END METHOD SUMMARY =========== -->


        <pre class="sourceview"><span class="comment">/*global exports: false, require: false, Cc: false, Ci: false, console: false */</span>
<span class="comment">/*jslint onevar: false */</span>
<span class="comment">// Released under the MIT/X11 license</span>
<span class="comment">// http://www.opensource.org/licenses/mit-license.php</span>
<span class="literal">"use strict"</span>;
<span class="comment">// ==============================================================</span>
var xhrMod = require(<span class="literal">"xhr"</span>);
var urlMod = require(<span class="literal">"url"</span>);

<span class="comment">/**
 * Function for the management of the prototypal inheritace
 * David Flanagan, Javascript: The Definitve Guide,
 * IV. edition, O'Reilly, 2006, p. 168
 *
 * <span class="attrib">@param</span> superobject
 * <span class="attrib">@return</span> new object, it needs new prototype.constructor
 *
 * &lt;pre&gt;
 * function Father(x) {
 *    this.family = x;
 * }
 *
 * function Son(x,w) {
 *    Father.call(this,x);
 *    this.wife = w;
 * }
 * Son.prototype = heir(Father);
 * Son.prototype.constructor = Son;
 * &lt;/pre&gt;
 */</span>
exports.heir = <span class="reserved">function</span> heir(p) {
    <span class="reserved">function</span> f() {};
    f.<span class="reserved">prototype</span> = p.<span class="reserved">prototype</span>;
    <span class="reserved">return</span> new f();
};


exports.getBugNo = <span class="reserved">function</span> getBugNo(url) {
    var re = new RegExp(<span class="literal">".*id=([0-9]+).*$"</span>);
    var bugNo = null;
    <span class="reserved">if</span> (!url) {
        throw new Error(<span class="literal">"Missing URL value!"</span>);
    }
    var reResult = re.exec(url);
    <span class="reserved">if</span> (reResult[1]) {
        bugNo = reResult[1];
    }
    <span class="reserved">return</span> bugNo;
};

<span class="comment">/**
 * Show a system notification with the given message
 *
 * <span class="attrib">@param</span> String or Object with a message to be shown in a default
 * notification or object with properties title, icon, and body
 * <span class="attrib">@return</span> None
 */</span>
exports.notification = <span class="reserved">function</span> notification(msg) {
    var body = msg;
    var title = <span class="literal">"Bugzilla Notification"</span>;
    var icon = null;

    <span class="reserved">if</span> (typeof(msg) === <span class="literal">"object"</span>) {
        body = msg.body;
        <span class="reserved">if</span> (<span class="literal">"title"</span> in msg) {
            title = msg.title;
        }
        <span class="reserved">if</span> (<span class="literal">"icon"</span> in msg) {
            icon = msg.icon;
        }
    }
    try {
        var classObj = Cc[<span class="literal">"@mozilla.org/alerts-service;1"</span>];
        var alertService = classObj.getService(Ci.nsIAlertsService);

        alertService.showAlertNotification(icon, title, body);
        <span class="reserved">return</span> true;
    } catch (e) {
        console.error(<span class="literal">"Unable to display notification:"</span>, msg);
        <span class="reserved">return</span> false;
    }
};

<span class="comment">/**
 * format date to be in ISO format (just day part)
 *
 * <span class="attrib">@param</span> date
 * <span class="attrib">@return</span> string with the formatted date
 */</span>
exports.getISODate = <span class="reserved">function</span> getISODate(dateStr) {
    <span class="reserved">function</span> pad(n) {
        <span class="reserved">return</span> n &lt; 10 ? <span class="literal">'0'</span> + n : n;
    }
    var date = new Date(dateStr);
    <span class="reserved">return</span> date.getFullYear() + <span class="literal">'-'</span> + pad(date.getMonth() + 1) + <span class="literal">'-'</span> +
            pad(date.getDate());
};

<span class="comment">/**
 * Check whether an item is member of the list. Idea is just to make long if
 * commands slightly more readable.
 *
 * <span class="attrib">@param</span> mbr string to be searched in the list
 * <span class="attrib">@param</span> list list
 * <span class="attrib">@return</span> position of the string in the list, or -1 if none found.
 */</span>
var isInList = exports.isInList = <span class="reserved">function</span> isInList(mbr, list) {
    <span class="reserved">if</span> (!list) {
        <span class="reserved">return</span> false;
    }
    <span class="reserved">return</span> (list.indexOf(mbr) !== -1);
};

<span class="comment">/**
 * Make sure value returned is Array
 *
 * <span class="attrib">@param</span> Array/String
 * <span class="attrib">@return</span> Array
 *
 * If something else than Array or String is passed to the function
 * the result will be untouched actual argument of the call.
 */</span>
var valToArray = exports.valToArray = <span class="reserved">function</span> valToArray(val) {
    var isArr = val &amp;&amp;
        val.constructor &amp;&amp;
        val.constructor.name === <span class="literal">"Array"</span>;
    <span class="reserved">return</span> isArr ? val : [val];
};

<span class="comment">/**
 * Merges two comma separated string as a list and returns new string
 *
 * <span class="attrib">@param</span> str String with old values
 * <span class="attrib">@param</span> value String/Array with other values
 * <span class="attrib">@return</span> String with merged lists
 */</span>
exports.addCSVValue = <span class="reserved">function</span> addCSVValue(str, value) {
    var parts = (str.trim().length &gt; 0 ? str.split(/,\s*/) : []);
    <span class="reserved">if</span> (!value) {
        <span class="reserved">return</span> str;
    }
    <span class="reserved">if</span> (!isInList(value, parts)) {
        var newValue = valToArray(value);
        parts = parts.concat(newValue);
    }
    <span class="comment">// this is necessary to get comma-space separated string even when</span>
    <span class="comment">// value is an array already</span>
    parts = parts.join(<span class="literal">","</span>).split(<span class="literal">","</span>);
    <span class="reserved">return</span> parts.join(<span class="literal">", "</span>);
};

<span class="comment">/**
 * Treats comma separated string as a list and removes one item from it
 *
 * <span class="attrib">@param</span> str String treated as a list
 * <span class="attrib">@param</span> value String with the value to be removed from str
 * <span class="attrib">@return</span> String with the resulting list comma separated
 */</span>
exports.removeCSVValue = <span class="reserved">function</span> removeCSVValue(str, value) {
    str = str.trim();
    var parts = str ? str.split(/,\s*/) : [];
    var valueArr = value instanceof Array ? value : value.split(/,\s*/);
    parts = parts.filter(<span class="reserved">function</span> (e, i, a) {
        <span class="reserved">return</span> (!isInList(e, valueArr));
    });
    <span class="reserved">return</span> parts.join(<span class="literal">", "</span>);
};

<span class="comment">/**
 * select element of the array where regexp in the first element matches second
 * parameter of this function
 *
 * <span class="attrib">@param</span> list Array with regexps and return values
 * <span class="attrib">@param</span> chosingMark String by which the element of array is to be matched
 * <span class="attrib">@return</span> Object chosen element
 */</span>
var filterByRegexp = exports.filterByRegexp =
        <span class="reserved">function</span> filterByRegexp(list, chosingMark) {
    var chosenPair = [];
    <span class="reserved">if</span> (list.length &gt; 0) {
        chosenPair = list.filter(<span class="reserved">function</span> (pair) {
            <span class="reserved">return</span> new RegExp(pair.regexp, <span class="literal">"i"</span>).test(chosingMark);
        });
    }
    <span class="reserved">if</span> (chosenPair.length &gt; 0) {
        <span class="reserved">return</span> chosenPair[0].addr;
    } <span class="reserved">else</span> {
        <span class="reserved">return</span> <span class="literal">""</span>;
    }
};

<span class="comment">/**
 * returns password with a special password
 *
 * <span class="attrib">@return</span> String with the password
 */</span>
var getPassword = exports.getPassword = <span class="reserved">function</span> getPassword() {
    var prompts = Cc[<span class="literal">"@mozilla.org/embedcomp/prompt-service;1"</span>]
            .getService(Ci.nsIPromptService);
    var password = {
        value : <span class="literal">""</span>
    }; <span class="comment">// default the password to pass</span>
    var check = {
        value : true
    }; <span class="comment">// default the checkbox to true</span>
    var result = prompts.promptPassword(null, <span class="literal">"Title"</span>, <span class="literal">"Enter password:"</span>,
            password, null, check);
    <span class="comment">// result is true if OK was pressed, false if cancel was pressed.</span>
    <span class="comment">// password.value is set if OK was pressed.</span>
    <span class="comment">// The checkbox is not displayed.</span>
    <span class="reserved">if</span> (result) {
        <span class="reserved">return</span> password.value ? password.value : null;
    } <span class="reserved">else</span> {
        <span class="reserved">return</span> undefined;
    }
};

<span class="comment">/**
 * Load text from URL
 *
 * <span class="attrib">@param</span> URL String
 * <span class="attrib">@param</span> cb_function Function to be called when the download happens with
 * the downloaded body of the HTTP message as the only parameter
 * <span class="attrib">@param</span> what optional Object argument representing this for this call
 * <span class="attrib">@return</span> none
 */</span>
var loadText = exports.loadText = <span class="reserved">function</span> loadText(URL, cb_function, what) {
    <span class="reserved">if</span> (what === undefined) { <span class="comment">// missing optional argument</span>
        what = <span class="reserved">this</span>;
    }

    var req = new xhrMod.XMLHttpRequest();
    req.open(<span class="literal">"GET"</span>, URL, true);
    req.onreadystatechange = <span class="reserved">function</span> (aEvt) {
        <span class="reserved">if</span> (req.readyState === 4) {
            <span class="reserved">if</span> (req.status === 200) {
                cb_function.call(what, req.responseText);
            } <span class="reserved">else</span> {
                throw <span class="literal">"Getting "</span> + URL + <span class="literal">"failed!"</span>;
            }
        }
    };
    req.send(<span class="literal">""</span>);
};

<span class="comment">/**
 * Load JSON from URL
 *
 * <span class="attrib">@param</span> URL String
 * <span class="attrib">@param</span> cb_function Function to be called when the download happens with
 * the downloaded JSON as the only parameter
 * <span class="attrib">@param</span> what optional Object argument representing this for this call
 * <span class="attrib">@return</span> none
 */</span>
exports.loadJSON = <span class="reserved">function</span> loadJSON(URL, cb_function, what) {
    <span class="reserved">if</span> (what === undefined) { <span class="comment">// missing optional argument</span>
        what = <span class="reserved">this</span>;
    }

    loadText(URL, <span class="reserved">function</span> (text) {
        var data = JSON.parse(text);
        cb_function.call(what, data);
    }, what);
};

<span class="comment">/**
 * run HTTP POST request
 *
 * <span class="attrib">@param</span> URL String with URL; required
 * <span class="attrib">@param</span> data Object/String with data ; required
 * <span class="attrib">@param</span> cb_function Function called when the request succeeds, with
 *        the only parameter being request object ; required
 * <span class="attrib">@param</span> what Object which will represent this for the cb_function ; optional
 * <span class="attrib">@param</span>  mimeData String with MIME type of data
 * <span class="attrib">@param</span> mimeGet String with MIME type expected on return
 */</span>
exports.httpPOST = <span class="reserved">function</span> httpPOST(URL, data, cb_function, what, mimeData, mimeGet) {
	what = what === undefined ? <span class="reserved">this</span> : what;
	mimeData = mimeData === undefined ? <span class="literal">"application/x-www-form-urlencoded"</span> : mimeData;
	mimeGet = mimeGet === undefined ? <span class="literal">"text/plain"</span> : mimeGet;

    var req = new xhrMod.XMLHttpRequest();
    console.log(<span class="literal">"req = "</span> + req.toSource());
    req.open(<span class="literal">"POST"</span>, URL, true);

    req.overrideMimeType(mimeGet);
    req.setRequestHeader(<span class="literal">"Content-type"</span>, mimeData);
    req.onreadystatechange = <span class="reserved">function</span>(aEvt) {
        <span class="reserved">if</span> (req.readyState === 4) {
            <span class="reserved">if</span> (req.status === 200) {
                console.log(<span class="literal">"POST success!"</span>);
                cb_function.call(what, req);
            } <span class="reserved">else</span> {
                console.error(<span class="literal">"POST failed!"</span>);
            }
        }
    };
    req.send(data);
};
</pre>
	<hr>



<!-- ========== START OF NAVBAR ========== -->
<a name="navbar_top"><!-- --></a>
<table border="0" width="100%" cellpadding="1" cellspacing="0">
<tr>
<td colspan=2 bgcolor="#EEEEFF" class="NavBarCell1">
<a name="navbar_top_firstrow"><!-- --></a>
<table border="0" cellpadding="0" cellspacing="3">
  <tr align="center" valign="top">
  
  
  <td bgcolor="#EEEEFF" class="NavBarCell1">    <a href="overview-summary.html"><font class="NavBarFont1"><b>Overview</b></font></a>&nbsp;</td>
  <td bgcolor="#FFFFFF" class="NavBarCell1Rev">	&nbsp;<font class="NavBarFont1Rev"><b>File</b></font>&nbsp;</td>
  

  <td bgcolor="#FFFFFF" class="NavBarCell1"> <font class="NavBarFont1">Class</font>&nbsp;</td>
  <td bgcolor="#EEEEFF" class="NavBarCell1">    <a href="overview-tree.html"><font class="NavBarFont1"><b>Tree</b></font></a>&nbsp;</td>
  <td bgcolor="#EEEEFF" class="NavBarCell1">    <a href="index-all.html"--><font class="NavBarFont1"><b>Index</b></font></a>&nbsp;</td>
  <td bgcolor="#EEEEFF" class="NavBarCell1">    <a href="help-doc.html"><font class="NavBarFont1"><b>Help</b></font></a>&nbsp;</td>
  </tr>
</table>
</td>
<td bgcolor="#EEEEFF" align="right" valign="top"><em>
<b></b></em>
</td>
</tr>

<tr>
<td bgcolor="white" class="NavBarCell2"><font size="-2">
&nbsp;PREV&nbsp;
&nbsp;NEXT</font></td>
<td bgcolor="white" class="NavBarCell2"><font size="-2">
  <a href="index.html" target="_top"><b>FRAMES</b></a>  &nbsp;
&nbsp;<a href="overview-summary.html" target="_top"><b>NO FRAMES</b></a>
&nbsp;&nbsp;
<script>
  <!--
  if(window==top) {
    document.writeln('<A HREF="allclasses-noframe.html" TARGET=""><B>All Classes</B></A>');
  }
  //-->
</script>
<noscript>
<a href="allclasses-noframe.html" target=""><b>All Classes</b></a>
</noscript>
</font></td>
</tr>
</table>
<!-- =========== END OF NAVBAR =========== -->

<hr>
<font size="-1">

</font>
<div class="jsdoc_ctime">Documentation generated by <a href="http://jsdoc.sourceforge.net/" target="_parent">JSDoc</a> on Wed Jun 23 09:33:14 2010</div>
</body>
</html>