util.js

Summary

No overview generated for 'util.js'


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

/**
 * Function for the management of the prototypal inheritace
 * David Flanagan, Javascript: The Definitve Guide,
 * IV. edition, O'Reilly, 2006, p. 168
 *
 * @param superobject
 * @return 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>
 */
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;
};

/**
 * Show a system notification with the given message
 *
 * @param String or Object with a message to be shown in a default
 * notification or object with properties title, icon, and body
 * @return None
 */
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;
    }
};

/**
 * format date to be in ISO format (just day part)
 *
 * @param date
 * @return string with the formatted date
 */
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());
};

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

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

/**
 * Merges two comma separated string as a list and returns new string
 *
 * @param str String with old values
 * @param value String/Array with other values
 * @return String with merged lists
 */
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);
    }
    // this is necessary to get comma-space separated string even when
    // value is an array already
    parts = parts.join(",").split(",");
    return parts.join(", ");
};

/**
 * Treats comma separated string as a list and removes one item from it
 *
 * @param str String treated as a list
 * @param value String with the value to be removed from str
 * @return String with the resulting list comma separated
 */
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(", ");
};

/**
 * select element of the array where regexp in the first element matches second
 * parameter of this function
 *
 * @param list Array with regexps and return values
 * @param chosingMark String by which the element of array is to be matched
 * @return Object chosen element
 */
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 "";
    }
};

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

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

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

    loadText(URL, function (text) {
        var data = JSON.parse(text);
        cb_function.call(what, data);
    }, what);
};

/**
 * run HTTP POST request
 *
 * @param URL String with URL; required
 * @param data Object/String with data ; required
 * @param cb_function Function called when the request succeeds, with
 *        the only parameter being request object ; required
 * @param what Object which will represent this for the cb_function ; optional
 * @param  mimeData String with MIME type of data
 * @param mimeGet String with MIME type expected on return
 */
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