<!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> </td>
<td bgcolor="#FFFFFF" class="NavBarCell1Rev"> <font class="NavBarFont1Rev"><b>File</b></font> </td>
<td bgcolor="#FFFFFF" class="NavBarCell1"> <font class="NavBarFont1">Class</font> </td>
<td bgcolor="#EEEEFF" class="NavBarCell1"> <a href="overview-tree.html"><font class="NavBarFont1"><b>Tree</b></font></a> </td>
<td bgcolor="#EEEEFF" class="NavBarCell1"> <a href="index-all.html"--><font class="NavBarFont1"><b>Index</b></font></a> </td>
<td bgcolor="#EEEEFF" class="NavBarCell1"> <a href="help-doc.html"><font class="NavBarFont1"><b>Help</b></font></a> </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">
PREV
NEXT</font></td>
<td bgcolor="white" class="NavBarCell2"><font size="-2">
<a href="index.html" target="_top"><b>FRAMES</b></a>
<a href="overview-summary.html" target="_top"><b>NO FRAMES</b></a>
<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
*
* <pre>
* 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;
* </pre>
*/</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 < 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 &&
val.constructor &&
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 > 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 > 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 > 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> </td>
<td bgcolor="#FFFFFF" class="NavBarCell1Rev"> <font class="NavBarFont1Rev"><b>File</b></font> </td>
<td bgcolor="#FFFFFF" class="NavBarCell1"> <font class="NavBarFont1">Class</font> </td>
<td bgcolor="#EEEEFF" class="NavBarCell1"> <a href="overview-tree.html"><font class="NavBarFont1"><b>Tree</b></font></a> </td>
<td bgcolor="#EEEEFF" class="NavBarCell1"> <a href="index-all.html"--><font class="NavBarFont1"><b>Index</b></font></a> </td>
<td bgcolor="#EEEEFF" class="NavBarCell1"> <a href="help-doc.html"><font class="NavBarFont1"><b>Help</b></font></a> </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">
PREV
NEXT</font></td>
<td bgcolor="white" class="NavBarCell2"><font size="-2">
<a href="index.html" target="_top"><b>FRAMES</b></a>
<a href="overview-summary.html" target="_top"><b>NO FRAMES</b></a>
<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>