// 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 fileMod = require("file");
var tabs = require("tabs");
var prompts = require("prompts");
var apiUtils = require("api-utils");
var xrpc = require("xmlrpc");
var myStorage = require("simple-storage");
var libbz = require("libbugzilla");
var EmptyLogsColor = "rgb(0, 255, 0)";
var FullLogsColor = "rgb(0, 40, 103)";
var abbsMap = {};
exports.initialize = function initialize(aMap) {
if (!myStorage.storage.logs) {
myStorage.storage.logs = {};
}
abbsMap = aMap;
};
exports.addLogRecord = function addLogRecord(rec) {
if (myStorage.storage.logs[rec.key]) {
myStorage.storage.logs[rec.key].comment += "
\n" + comment;
}
else {
myStorage.storage.logs[rec.key] = rec;
}
};
function storeSize() {
var size = 0, key;
for (key in myStorage.storage.logs) {
size++;
}
return size;
}
function isEmpty() {
return (storeSize() === 0);
}
exports.clearTimeSheet = function clearTimeSheet() {
myStorage.storage.logs = {};
var size = storeSize();
};
exports.importTimeSheet = function importTimeSheet() {
var filename = prompts.promptFileOpenPicker();
if (fileMod.exists(filename)) {
var otherTS = JSON.parse(fileMod.read(filename));
if (otherTS.logs) {
for (var rec in otherTS.logs) {
myStorage.storage.logs[rec] = otherTS.logs[rec];
}
}
else {
console.error("This is not a log file!");
}
}
else {
console.error("File " + filename + " doesn't exist!");
}
};
function getBugzillaAbbr(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
return abbsMap[urlMod.URL(url).host];
}
exports.generateTimeSheet = function generateTimeSheet() {
var docHTML = timeSheetRecordsPrinter(myStorage.storage.logs, new Date());
libbz.openURLInNewTab("data:text/html;charset=utf-8," + docHTML);
};
var timeSheetRecordsPrinter = exports.timeSheetRecordsPrinter =
function timeSheetRecordsPrinter(records, date) {
var commentBugRE = new RegExp("[bB]ug\\s+([0-9]+)","g");
// sort the records into temporary array
var tmpArr = [];
var dateStr = date.getFullYear() + "-" +
xrpc.leadingZero(date.getMonth()+1) + "-" +
xrpc.leadingZero(date.getDate());
var outStr = '' +
"
" + currentDay + "
\n"; } // replace "bug ####" with a hyperlink to the current bugzilla var comment = x.comment.replace(commentBugRE, "$&"); outStr += "Bug "
+ BZName + "/" + bugNo + ": "
+ x.title
+ ""
+ " \n
" + comment + "