From 629914c24b97d5a1afb216fb925dbb12376e2197 Mon Sep 17 00:00:00 2001 From: abhin4v Date: Tue, 1 Sep 2009 18:21:42 +0000 Subject: [PATCH] Did some code cleanup and refactoring. git-svn-id: file:///tmp/snv/trunk@21 12951d8a-c33d-4b7c-b961-822215c816e1 --- DiggSidebar.kpf | 7 + chrome/content/diggsidebar.js | 270 +++++++++++++++++++--------------- install.rdf | 4 +- 3 files changed, 157 insertions(+), 124 deletions(-) create mode 100644 DiggSidebar.kpf diff --git a/DiggSidebar.kpf b/DiggSidebar.kpf new file mode 100644 index 0000000..240d69a --- /dev/null +++ b/DiggSidebar.kpf @@ -0,0 +1,7 @@ + + + + + 1 + + diff --git a/chrome/content/diggsidebar.js b/chrome/content/diggsidebar.js index 0114343..1ea6554 100644 --- a/chrome/content/diggsidebar.js +++ b/chrome/content/diggsidebar.js @@ -32,14 +32,17 @@ var DiggSidebar = { updateIntervalDecay: 0, lastUpdateAt: new Date().getTime(), shownStoriesCount: 30, - categories: ['All', 'Popular', 'Upcoming', 'Hot', 'Top'], + categories: ["All", "Popular", "Upcoming", "Hot", "Top"], timerId: null, storyIds: [], refreshing: false, stories: [], expandedStory: null, + observerService: Components.classes["@mozilla.org/observer-service;1"] + .getService(Components.interfaces.nsIObserverService), - fetchData: function(url, handler) { + + fetchData: function (url, handler) { DiggSidebar.indicateActivity(); var request = new XMLHttpRequest(); @@ -49,7 +52,7 @@ var DiggSidebar = { request.send(null); }, - errorHandler: function(e) { + errorHandler: function (e) { /*if (DiggSidebar.UI.notificationBox.allNotifications.length > 0) DiggSidebar.UI.notificationBox.removeAllNotifications(true);*/ DiggSidebar.UI.notificationBox.appendNotification( @@ -57,54 +60,62 @@ var DiggSidebar = { "dsAccessFailNotification", null, "PRIORITY_CRITICAL_HIGH", - [{callback:DiggSidebar.getStories, label: "Retry", accessKey: "R"}] + [{callback: DiggSidebar.getStories, label: "Retry", accessKey: "R"}] ); DiggSidebar.refreshing = false; DiggSidebar.indicateInactivity(); }, - populateMenu: function(e) { + populateMenu: function (e) { var XHR = e.target; var data = DiggSidebar.Utils.decodeJson(XHR.responseText); var containers = data.containers; containers.forEach(function (container) { - var cmenu = document.createElement('menu'); - cmenu.setAttribute('id', container.short_name + 'Menu'); - cmenu.setAttribute('label', container.name); - cmenu.setAttribute('accesskey', container.name.charAt(0)); + var cmenu = document.createElement("menu"); + with (cmenu) { + setAttribute("id", container.short_name + "Menu"); + setAttribute("label", container.name); + setAttribute("accesskey", container.name.charAt(0)); + } - var cmenupopup = document.createElement('menupopup'); - cmenupopup.setAttribute('id', container.short_name + 'Popup'); + var cmenupopup = document.createElement("menupopup"); + cmenupopup.setAttribute("id", container.short_name + "Popup"); DiggSidebar.categories.forEach( function (category) { - var cmenuitem = document.createElement('menuitem'); - cmenuitem.setAttribute('label', category); - cmenuitem.setAttribute('value', '/container/' + container.short_name + '/' + category.toLowerCase()); - cmenuitem.setAttribute('accesskey', category.charAt(0)); - cmenuitem.setAttribute('oncommand', "DiggSidebar.setEndPoint(this.value)"); + var cmenuitem = document.createElement("menuitem"); + with (cmenuitem) { + setAttribute("label", category); + setAttribute("value", "/container/" + container.short_name + "/" + category.toLowerCase()); + setAttribute("accesskey", category.charAt(0)); + setAttribute("oncommand", "DiggSidebar.setEndPoint(this.value)"); + } cmenupopup.appendChild(cmenuitem); } ); - cmenupopup.appendChild(document.createElement('menuseparator')); + cmenupopup.appendChild(document.createElement("menuseparator")); var topics = container.topics; topics.forEach(function (topic) { - var tmenu = document.createElement('menu'); - tmenu.setAttribute('id', topic.short_name + 'Menu'); - tmenu.setAttribute('label', topic.name); - tmenu.setAttribute('accesskey', topic.name.charAt(0)); + var tmenu = document.createElement("menu"); + with (tmenu) { + setAttribute("id", topic.short_name + "Menu"); + setAttribute("label", topic.name); + setAttribute("accesskey", topic.name.charAt(0)); + } - var tmenupopup = document.createElement('menupopup'); - tmenupopup.setAttribute('id', topic.short_name + 'Popup'); + var tmenupopup = document.createElement("menupopup"); + tmenupopup.setAttribute("id", topic.short_name + "Popup"); DiggSidebar.categories.forEach( function (category) { - var tmenuitem = document.createElement('menuitem'); - tmenuitem.setAttribute('label', category); - tmenuitem.setAttribute('value', '/topic/' + topic.short_name + '/' + category.toLowerCase()); - tmenuitem.setAttribute('accesskey', category.charAt(0)); - tmenuitem.setAttribute('oncommand', "DiggSidebar.setEndPoint(this.value)"); + var tmenuitem = document.createElement("menuitem"); + with (tmenuitem) { + setAttribute("label", category); + setAttribute("value", "/topic/" + topic.short_name + "/" + category.toLowerCase()); + setAttribute("accesskey", category.charAt(0)); + setAttribute("oncommand", "DiggSidebar.setEndPoint(this.value)"); + } tmenupopup.appendChild(tmenuitem); } ); @@ -116,37 +127,41 @@ var DiggSidebar = { }); }, - populateStoryList: function() { - var newStoryIds = new Array(); + populateStoryList: function () { + var newStoryIds = []; var jp = new JPath(DiggSidebar.stories); - var filteredStories = jp.$(function(story){ - return (story.$("category").json == DiggSidebar.endpoint.category) ; + var filteredStories = jp.$(function (story) { + return (story.$("category").json == DiggSidebar.endpoint.category); }).json; jp = new JPath(filteredStories); - if (DiggSidebar.endpoint.topic) - filteredStories = jp.$(function(story){ + if (DiggSidebar.endpoint.topic) { + filteredStories = jp.$(function (story) { return (story.$("topic/short_name").json == DiggSidebar.endpoint.topic); }).json; - if (DiggSidebar.endpoint.container) - filteredStories = jp.$(function(story){ + } + if (DiggSidebar.endpoint.container) { + filteredStories = jp.$(function (story) { return story.$("container/short_name").json == DiggSidebar.endpoint.container; }).json; + } - if (filteredStories.length > 0) + if (filteredStories.length > 0) { DiggSidebar.Utils.removeAllChildren(DiggSidebar.UI.storyListBox); + } filteredStories.forEach(function (story, index) { if (index < DiggSidebar.shownStoriesCount) { var now = new Date(); - if (story.promote_date != null) - var then = new Date(story.promote_date*1000); - else - var then = new Date(story.submit_date*1000); - var diff = Math.max(now - then, 0) + if (story.promote_date != null) { + var then = new Date(story.promote_date * 1000); + } else { + var then = new Date(story.submit_date * 1000); + } + var diff = Math.max(now - then, 0); - var hr = Math.floor(diff/(1000*3600)); - var min = Math.floor(diff/(1000*60)) - 60*hr; + var hr = Math.floor(diff / (1000 * 3600)); + var min = Math.floor(diff / (1000 * 60)) - 60 * hr; var relativeTime = ((hr > 0) && (min > 0)) ? (hr + " hr " + min + " mins ago") : @@ -155,7 +170,7 @@ var DiggSidebar = { ((hr == 0) && (min == 0)) ? "just now" : ""; - var li = DiggSidebar.UI.storyListBox.appendChild(document.createElement('richlistitem')); + var li = DiggSidebar.UI.storyListBox.appendChild(document.createElement("richlistitem")); li.id = "story_" + story.id; var attributes = { title: story.title, @@ -170,56 +185,59 @@ var DiggSidebar = { desc: story.description, link: story.link, href: story.href - } - for (var attr in attributes) + }; + for (var attr in attributes) { li.setAttribute(attr, attributes[attr]); + } li.read = story.read; - if (story.id == DiggSidebar.expandedStory) + if (story.id == DiggSidebar.expandedStory) { li.showDescription(); + } + } + if (DiggSidebar.storyIds.indexOf(story.id) == -1) { + newStoryIds.push(story.id); } - if (DiggSidebar.storyIds.indexOf(story.id) == -1) newStoryIds.push(story.id); }); DiggSidebar.storyIds = DiggSidebar.storyIds.concat(newStoryIds); }, - showDescription: function(storyId) { + showDescription: function (storyId) { var jp = new JPath(DiggSidebar.stories); - var story = jp.query('//[id == ' + storyId + ']')[0]; + var story = jp.query("//[id == " + storyId + "]")[0]; story.read = true; DiggSidebar.expandedStory = parseInt(storyId); var listitems = DiggSidebar.UI.storyListBox.children; - for (var i=0; i b[date]) return -1; - else if (a[date] < b[date]) return 1; - return 0; + if (a[date] > b[date]) { + return -1; + } else if (a[date] < b[date]) { + return 1; + } else { + return 0; + } }); - for (var i=0; i 0) { var previousUpdateInterval = DiggSidebar.updateInterval; DiggSidebar.updateInterval = newUpdateInterval; - if (previousUpdateInterval == newUpdateInterval) + if (previousUpdateInterval == newUpdateInterval) { DiggSidebar.updateIntervalDecay += 1; - else + } else { DiggSidebar.updateIntervalDecay = 0; + } } //Adaptive update interval code END }, - calculateTimeout: function() { - return Math.round(DiggSidebar.updateInterval*(Math.pow(1.5, DiggSidebar.updateIntervalDecay))) + calculateTimeout: function () { + return Math.round(DiggSidebar.updateInterval * (Math.pow(1.5, DiggSidebar.updateIntervalDecay))); }, - setEndPoint: function(ep) { + setEndPoint: function (ep) { DiggSidebar.prefs.get("endpoint").value = ep; DiggSidebar.endpoint = DiggSidebar.getEndpointParts(ep); window.clearTimeout(DiggSidebar.timerId); DiggSidebar.getStories(); }, - getEndpointParts: function(ep) { + getEndpointParts: function (ep) { var tmp = { topic: null, container: null, category: "all" }; var parts = ep.split("/"); - if (parts.length == 2) + if (parts.length == 2) { tmp.category = parts[1]; - else if (parts.length == 4) { + } else if (parts.length == 4) { tmp[parts[1]] = parts[2]; tmp.category = parts[3]; } return tmp; }, - indicateActivity: function() { + indicateActivity: function () { DiggSidebar.UI.indicator.src = "chrome://global/skin/icons/loading_16.png"; with (DiggSidebar.UI.indicator.parentNode) { disabled = true; - setAttribute('tooltiptext', "Refreshing"); + setAttribute("tooltiptext", "Refreshing"); onmouseover = null; } }, - indicateInactivity: function() { + indicateInactivity: function () { DiggSidebar.UI.indicator.src = "chrome://global/skin/icons/notloading_16.png"; with (DiggSidebar.UI.indicator.parentNode) { disabled = false; - onmouseover = function() { + onmouseover = function () { var autoRefreshTime = Math.round( - (DiggSidebar.lastUpdateAt + DiggSidebar.calculateTimeout() - new Date().getTime())/1000 + (DiggSidebar.lastUpdateAt + DiggSidebar.calculateTimeout() - new Date().getTime()) / 1000 ); autoRefreshTime = (autoRefreshTime > 3600) ? - Math.round(autoRefreshTime/3600) + " hours": + Math.round(autoRefreshTime / 3600) + " hours": (autoRefreshTime > 60) ? - Math.round(autoRefreshTime/60) + " minutes": + Math.round(autoRefreshTime / 60) + " minutes": autoRefreshTime + " seconds"; - setAttribute('tooltiptext', "Click to refresh now.\n" + + setAttribute("tooltiptext", "Click to refresh now.\n" + "Autorefreshing in " + autoRefreshTime); - } + }; } DiggSidebar.lastUpdateAt = new Date().getTime(); }, - initialize: function(){ + initialize: function () { DiggSidebar.endpoint = DiggSidebar.getEndpointParts(DiggSidebar.prefs.get("endpoint").value); - var $ = function(id) {return document.getElementById(id)}; + var $ = function (id) { + return document.getElementById(id); + }; DiggSidebar.UI = {}; - DiggSidebar.UI.notificationBox = $('dsNotificationBox'); - DiggSidebar.UI.indicator = $('dsBusyIndicator'); - DiggSidebar.UI.storiesPopup = $('dsStoriesPopup'); - DiggSidebar.UI.storyListBox = $('dsStoryListBox'); - DiggSidebar.UI.endPointDesc = $('dsEndPointDesc'); + DiggSidebar.UI.notificationBox = $("dsNotificationBox"); + DiggSidebar.UI.indicator = $("dsBusyIndicator"); + DiggSidebar.UI.storiesPopup = $("dsStoriesPopup"); + DiggSidebar.UI.storyListBox = $("dsStoryListBox"); + DiggSidebar.UI.endPointDesc = $("dsEndPointDesc"); DiggSidebar.storyListObserver = new DiggSidebar.StoryListObserver(); DiggSidebar.createMenu(); DiggSidebar.getStories(); }, - destroy: function(){ + destroy: function () { DiggSidebar.storyListObserver.unregister(); } }; DiggSidebar.Utils = { - url: function(spec) { + url: function (spec) { var ios = Components.classes["@mozilla.org/network/io-service;1"] .getService(Components.interfaces.nsIIOService); return ios.newURI(spec, null, null); }, - openUrlInTab: function(url) { + openUrlInTab: function (url) { Application.activeWindow.open(DiggSidebar.Utils.url(url)); }, - decodeJson: function(string) { + decodeJson: function (string) { var json = Components.classes["@mozilla.org/dom/json;1"] .createInstance(Components.interfaces.nsIJSON); return json.decode(string); }, - removeAllChildren: function(node) { - while (node.firstChild) + removeAllChildren: function (node) { + while (node.firstChild) { node.removeChild(node.firstChild); + } } -} +}; -DiggSidebar.StoryListObserver = function() { - this.registered = false; - this.register(); -} +DiggSidebar.StoryListObserver = function () { + this.registered = false; + this.register(); +}; DiggSidebar.StoryListObserver.prototype = { - observe: function(subject, topic, data) { + observe: function (subject, topic, data) { if (topic == window.DiggSidebar.storyListRefreshEventTopic) { window.DiggSidebar.populateStoryList(); } }, - register: function() { - if (this.registered == false) { - var observerService = Components.classes["@mozilla.org/observer-service;1"] - .getService(Components.interfaces.nsIObserverService); - observerService.addObserver(this, DiggSidebar.storyListRefreshEventTopic, false); + register: function () { + if (!this.registered) { + DiggSidebar.observerService.addObserver(this, DiggSidebar.storyListRefreshEventTopic, false); this.registered = true; } }, - unregister: function() { - if (this.registered == true) { - var observerService = Components.classes["@mozilla.org/observer-service;1"] - .getService(Components.interfaces.nsIObserverService); - observerService.removeObserver(this, DiggSidebar.storyListRefreshEventTopic); + unregister: function () { + if (this.registered) { + DiggSidebar.observerService.removeObserver(this, DiggSidebar.storyListRefreshEventTopic); this.registered = false; } } diff --git a/install.rdf b/install.rdf index ac000c3..03eb736 100644 --- a/install.rdf +++ b/install.rdf @@ -24,9 +24,9 @@ + em:maxVersion="3.5.*" />