logger.js

Summary

No overview generated for 'logger.js'


Class Summary
Logger  

// Released under the MIT/X11 license
// http://www.opensource.org/licenses/mit-license.php
"use strict";
var urlMod = require("url");
var utilMod = require("util");
var Color = require("color").Color;
var tabs = require("tabs");
var apiUtils = require("api-utils");

function Logger(store, abbsMap) {
    this.EmptyLogsColor = new Color(0, 255, 0);
    this.FullLogsColor = new Color(0, 40, 103);

    this.store = store;
    this.abbsMap = abbsMap;
};

exports.Logger = Logger;

Logger.prototype.addLogRecord = function(that) {
    console.log("Adding log record!");
    var rec = {};
    rec.date = new Date();
    rec.url = that.doc.location.toString();
    rec.title = that.title;
    var comment = tabs.activeTab.contentWindow.prompt(
            "Enter comments for this comment");
    console.log("comment = " + comment);
    if (comment && comment.length > 0) {
        console.log("I am in!");
        comment = comment.trim();
        rec.comment = comment;
        console.log("rec.comment = " + rec.comment);
        // FIXME We break on the following line. Not sure what's going on!
        var dateStr = utilMod.getISODate(rec.date);
        console.log("rec.date = " + rec.date + ", dateStr = " + dateStr);
        var urlStr = urlMod.URL(rec.url).host;
        console.log("rec.url = " + rec.url + ", urlStr = " + urlStr);
        var recKey = dateStr + "+"
            + urlStr
            + "+" + that.bugNo;
        console.log("recKey = " + recKey);
        console.log("rec = " + rec.toSource());

        if (this.store[recKey]) {
            this.store[recKey].comment += "<br/>\n" + comment;
        } else {
            this.store[recKey] = rec;
        }
    }
    return comment;
};

Logger.prototype.getLength = function () {
    var counter = 0;
    for (var key in this.store) {
        counter += 1;
    }
    return counter;
};

Logger.prototype.getBugzillaAbbr = function(url) {
    // for https://bugzilla.redhat.com/show_bug.cgi?id=579123 get RH
    // for https://bugzilla.mozilla.org/show_bug.cgi?id=579123 get MoFo
    var abbr = this.abbsMap[urlMod.URL(url).host];
    return abbr;
};

Logger.prototype.timeSheetRecordsPrinter = function(body, records) {
    var that = this;
    var commentBugRE = new RegExp("[bB]ug\\s+([0-9]+)","g");
    // sort the records into temporary array
    var tmpArr = [];

    for ( var i in records) {
        if (records.hasOwnProperty(i)) {
            tmpArr.push( [ i, records[i] ]);
        }
    }
    tmpArr.sort(function(a, b) {
        return a[0] > b[0] ? 1 : -1;
    });

    var currentDay = "";
    // now print the array
    tmpArr.forEach(function(rec) {
                var x = rec[1];
                var dayStr = utilMod.getISODate(x.date);
                var host = urlMod.URL(x.url).host;
                var BZName = that.getBugzillaAbbr(x.url);
                var bugNo = utilMod.getBugNo(x.url);
                if (dayStr != currentDay) {
                    currentDay = dayStr;
                    body.innerHTML += "<hr/><p><strong>" + currentDay
                            + "</strong></p>";
                }
                // replace "bug ####" with a hyperlink to the current bugzilla
                var comment = x.comment.replace(commentBugRE,
                    "<a href='http://"+host+"/show_bug.cgi?id=$1'>$&</a>");
                body.innerHTML += "<p><em><a href='"
                        + x.url
                        + "'>Bug "
                        + BZName + "/" + bugNo + ": "
                        + x.title
                        + "</a>"
                        + " </em>\n<br/>" + comment + "</p>";
            });
};

/**
 *
 */
Logger.prototype.createBlankPage = function (ttl, bodyBuildCB) {
    var title = ttl || "Yet another untitled page";
    var that = this;

    var logTab = tabs.open({
        url: "about:blank",
        onOpen: function (tab) {
            var otherDoc = tab.contentDocument;
            otherDoc.title = title;
            otherDoc.body.innerHTML = "<h1>" + title + "</h1>";
            bodyBuildCB.call(that, otherDoc.body);
        }
    });
};

Logger.prototype.generateTimeSheet = function(body) {
    var doc = body.ownerDocument;
    this.timeSheetRecordsPrinter(body, this.store);
};


Documentation generated by JSDoc on Wed Jun 23 09:33:14 2010