util.js
Summary
No overview generated for 'util.js'
"use strict";
var xhrMod = require("xhr");
var urlMod = require("url");
exports.heir = function heir(p) {
function f() {};
f.prototype = p.prototype;
return new f();
};
exports.getBugNo = function getBugNo(url) {
var re = new RegExp(".*id=([0-9]+).*$");
var bugNo = null;
if (!url) {
throw new Error("Missing URL value!");
}
var reResult = re.exec(url);
if (reResult[1]) {
bugNo = reResult[1];
}
return bugNo;
};
exports.notification = function notification(msg) {
var body = msg;
var title = "Bugzilla Notification";
var icon = null;
if (typeof(msg) === "object") {
body = msg.body;
if ("title" in msg) {
title = msg.title;
}
if ("icon" in msg) {
icon = msg.icon;
}
}
try {
var classObj = Cc["@mozilla.org/alerts-service;1"];
var alertService = classObj.getService(Ci.nsIAlertsService);
alertService.showAlertNotification(icon, title, body);
return true;
} catch (e) {
console.error("Unable to display notification:", msg);
return false;
}
};
exports.getISODate = function getISODate(dateStr) {
function pad(n) {
return n < 10 ? '0' + n : n;
}
var date = new Date(dateStr);
return date.getFullYear() + '-' + pad(date.getMonth() + 1) + '-' +
pad(date.getDate());
};
var isInList = exports.isInList = function isInList(mbr, list) {
if (!list) {
return false;
}
return (list.indexOf(mbr) !== -1);
};
var valToArray = exports.valToArray = function valToArray(val) {
var isArr = val &&
val.constructor &&
val.constructor.name === "Array";
return isArr ? val : [val];
};
exports.addCSVValue = function addCSVValue(str, value) {
var parts = (str.trim().length > 0 ? str.split(/,\s*/) : []);
if (!value) {
return str;
}
if (!isInList(value, parts)) {
var newValue = valToArray(value);
parts = parts.concat(newValue);
}
parts = parts.join(",").split(",");
return parts.join(", ");
};
exports.removeCSVValue = function removeCSVValue(str, value) {
str = str.trim();
var parts = str ? str.split(/,\s*/) : [];
var valueArr = value instanceof Array ? value : value.split(/,\s*/);
parts = parts.filter(function (e, i, a) {
return (!isInList(e, valueArr));
});
return parts.join(", ");
};
var filterByRegexp = exports.filterByRegexp =
function filterByRegexp(list, chosingMark) {
var chosenPair = [];
if (list.length > 0) {
chosenPair = list.filter(function (pair) {
return new RegExp(pair.regexp, "i").test(chosingMark);
});
}
if (chosenPair.length > 0) {
return chosenPair[0].addr;
} else {
return "";
}
};
var getPassword = exports.getPassword = function getPassword() {
var prompts = Cc["@mozilla.org/embedcomp/prompt-service;1"]
.getService(Ci.nsIPromptService);
var password = {
value : ""
};
var check = {
value : true
};
var result = prompts.promptPassword(null, "Title", "Enter password:",
password, null, check);
if (result) {
return password.value ? password.value : null;
} else {
return undefined;
}
};
var loadText = exports.loadText = function loadText(URL, cb_function, what) {
if (what === undefined) {
what = this;
}
var req = new xhrMod.XMLHttpRequest();
req.open("GET", URL, true);
req.onreadystatechange = function (aEvt) {
if (req.readyState === 4) {
if (req.status === 200) {
cb_function.call(what, req.responseText);
} else {
throw "Getting " + URL + "failed!";
}
}
};
req.send("");
};
exports.loadJSON = function loadJSON(URL, cb_function, what) {
if (what === undefined) {
what = this;
}
loadText(URL, function (text) {
var data = JSON.parse(text);
cb_function.call(what, data);
}, what);
};
exports.httpPOST = function httpPOST(URL, data, cb_function, what, mimeData, mimeGet) {
what = what === undefined ? this : what;
mimeData = mimeData === undefined ? "application/x-www-form-urlencoded" : mimeData;
mimeGet = mimeGet === undefined ? "text/plain" : mimeGet;
var req = new xhrMod.XMLHttpRequest();
console.log("req = " + req.toSource());
req.open("POST", URL, true);
req.overrideMimeType(mimeGet);
req.setRequestHeader("Content-type", mimeData);
req.onreadystatechange = function(aEvt) {
if (req.readyState === 4) {
if (req.status === 200) {
console.log("POST success!");
cb_function.call(what, req);
} else {
console.error("POST failed!");
}
}
};
req.send(data);
};
Documentation generated by
JSDoc on Wed Jun 23 09:33:14 2010