From 4ffd6b9e6b61c9eaa323e4f4d537db5d1a36872d Mon Sep 17 00:00:00 2001 From: Matěj Cepl Date: Fri, 24 Sep 2010 12:28:11 +0200 Subject: Fix Logger object to be self contained and use storage properly * there is really no other way than using module.storage directly Sucks! * all logger-related logic should go to the module and not be flying around (import TS, clear, isEmpty) --- lib/bzpage.js | 50 ++++++++++++++--------------------------- lib/logger.js | 72 +++++++++++++++++++++++++++++++++++++++++++---------------- lib/main.js | 9 +------- 3 files changed, 71 insertions(+), 60 deletions(-) diff --git a/lib/bzpage.js b/lib/bzpage.js index 269f4fa..2aeaf51 100644 --- a/lib/bzpage.js +++ b/lib/bzpage.js @@ -7,7 +7,6 @@ var passUtils = require("passwords"); var apiUtils = require("api-utils"); var selfMod = require("self"); var clip = require("clipboard"); -var fileMod = require("file"); var simpleStorage = require("simple-storage"); var preferences = require("preferences-service"); var selection = require("selection"); @@ -945,6 +944,18 @@ BZPage.prototype.setUpLogging = function setUpLogging () { evt.preventDefault(); }, false); + var clearLogsUI = this.doc.createElement("li"); + clearLogsUI.innerHTML = "\u00A0-\u00A0" + + "Clear TS"; + additionalButtons.appendChild(clearLogsUI); + var clearLogAElem = this.doc.getElementById("clearLogs"); + clearLogAElem.addEventListener("click", function(evt) { + that.log.clearStore(this); + console.log("this.store wiped out!"); + evt.stopPropagation(); + evt.preventDefault(); + }, false); + var ImportTimeSheetUI = this.doc.createElement("li"); ImportTimeSheetUI.innerHTML = "\u00A0-\u00A0" + "Import TS"; @@ -952,45 +963,18 @@ BZPage.prototype.setUpLogging = function setUpLogging () { this.doc.getElementById("importTSButton").addEventListener( "click", function(evt) { - var otherTS = {}, thisTS = that.log.store; - jsonPaths = prompts.promptFileOpenPicker(that.win); - if (fileMod.exists(jsonPaths)) { - otherTS = JSON.parse(fileMod.read(jsonPaths)); - if (otherTS.logs) { - for (var rec in otherTS.logs) { - thisTS[rec] = otherTS.logs[rec]; - } - } else { - console.error("This is not a log file!"); - } - } else { - console.error("File " + jsonPaths + " doesn't exist!"); - } + that.log.importOtherStore(jsonPaths, clearLogAElem); evt.stopPropagation(); evt.preventDefault(); }, false); - var clearLogsUI = this.doc.createElement("li"); - clearLogsUI.innerHTML = "\u00A0-\u00A0" - + "Clear TS"; - additionalButtons.appendChild(clearLogsUI); - var clearLogAElem = this.doc.getElementById("clearLogs"); - clearLogAElem.addEventListener("click", function() { - that.log.store = {}; - this.style.color = that.log.EmptyLogsColor; - this.style.fontWeight = "normal"; - console.log("this.store wiped out!"); - evt.stopPropagation(); - evt.preventDefault(); - }, false); - - if (this.log.store.length > 0) { - clearLogAElem.style.color = this.log.FullLogsColor; - clearLogAElem.style.fontWeight = "bolder"; - } else { + if (this.log.isEmpty()) { clearLogAElem.style.color = this.log.EmptyLogsColor; clearLogAElem.style.fontWeight = "normal"; + } else { + clearLogAElem.style.color = this.log.FullLogsColor; + clearLogAElem.style.fontWeight = "bolder"; } }; diff --git a/lib/logger.js b/lib/logger.js index 1936ad5..052364d 100644 --- a/lib/logger.js +++ b/lib/logger.js @@ -3,27 +3,69 @@ "use strict"; var urlMod = require("url"); var utilMod = require("util"); +var fileMod = require("file"); var Color = require("color").Color; var tabs = require("tabs"); var prompts = require("prompts"); var apiUtils = require("api-utils"); var xrpc = require("xmlrpc"); +var myStorage = require("simple-storage"); -function Logger(store, abbsMap) { +var Logger = exports.Logger = function Logger(abbsMap) { this.EmptyLogsColor = new Color(0, 255, 0); this.FullLogsColor = new Color(0, 40, 103); - this.store = store; + if (!myStorage.storage.logs) { + myStorage.storage.logs = {}; + } + this.abbsMap = abbsMap; }; -exports.Logger = Logger; +Logger.prototype.size = function size() { + var size = 0, key; + for (key in myStorage.storage.logs) { + size++; + } + console.log("logStore.size = " + size); + return size; +}; + +Logger.prototype.isEmpty = function isEmpty() { + return (this.size() === 0); +}; + +Logger.prototype.clearStore = function clearStore(clearLink) { + myStorage.storage.logs = {}; + var size = this.size(); + clearLink.style.color = this.EmptyLogsColor; + clearLink.style.fontWeight = "normal"; +}; + +Logger.prototype.importOtherStore = function importOtherStore (filename, clearLink) { + 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!"); + } + if (this.size() > 0) { + clearLink.style.color = this.FullLogsColor; + clearLink.style.fontWeight = "bolder"; + } +}; -Logger.prototype.addLogRecord = function(that) { +Logger.prototype.addLogRecord = function addLogRecord(page) { var rec = {}; rec.date = new Date(); - rec.url = that.doc.location.toString(); - rec.title = that.title; + rec.url = page.doc.location.toString(); + rec.title = page.title; var comment = prompts.prompt( "Enter comments for this comment"); if (comment && comment.length > 0) { @@ -33,25 +75,17 @@ Logger.prototype.addLogRecord = function(that) { var urlStr = urlMod.URL(rec.url).host; var recKey = dateStr + "+" + urlStr - + "+" + that.bugNo; + + "+" + page.bugNo; - if (this.store[recKey]) { - this.store[recKey].comment += "
\n" + comment; + if (myStorage.storage.logs[recKey]) { + myStorage.storage.logs[recKey].comment += "
\n" + comment; } else { - this.store[recKey] = rec; + myStorage.storage.logs[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 @@ -122,5 +156,5 @@ Logger.prototype.createBlankPage = function (ttl, bodyBuildCB) { Logger.prototype.generateTimeSheet = function(body) { var doc = body.ownerDocument; - this.timeSheetRecordsPrinter(body, this.store); + this.timeSheetRecordsPrinter(body, myStorage.storage.logs); }; diff --git a/lib/main.js b/lib/main.js index a5700d6..9eb62f0 100644 --- a/lib/main.js +++ b/lib/main.js @@ -13,7 +13,6 @@ "use strict"; var prompts = require("prompts"); var logger = require("logger"); -var myStorage = require("simple-storage").storage; var browser = require("tab-browser"); var selfMod = require("self"); var Request = require("request").Request; @@ -89,13 +88,7 @@ function initialize(callback) { }, this); } - if (!myStorage.logs) { - myStorage.logs = {}; - } - - var logConstructor = logger.Logger; - config.logger = new logConstructor(myStorage.logs, - JSON.parse(selfMod.data.load("bugzillalabelAbbreviations.json"))); + config.logger = new logger.Logger(JSON.parse(selfMod.data.load("bugzillalabelAbbreviations.json"))); config.matches = config.gJSONData.configData.matches; config.skipMatches = config.matches.map(function(x) { -- cgit