From 3d3340679666aa4a26c94d914ffee5d34b7db940 Mon Sep 17 00:00:00 2001 From: abhin4v Date: Tue, 2 Dec 2008 17:41:31 +0000 Subject: [PATCH] refactored Javascript and make related changes in other files git-svn-id: file:///tmp/snv/trunk@8 12951d8a-c33d-4b7c-b961-822215c816e1 --- chrome/content/TODO.txt | 3 +- chrome/content/diggsidebar.js | 538 ++++++++++++++++----------------- chrome/content/diggsidebar.xul | 32 +- chrome/content/diggstory.xml | 10 +- chrome/skin/diggsidebar.css | 12 +- 5 files changed, 300 insertions(+), 295 deletions(-) diff --git a/chrome/content/TODO.txt b/chrome/content/TODO.txt index aba38dc..66e1ab2 100644 --- a/chrome/content/TODO.txt +++ b/chrome/content/TODO.txt @@ -1,7 +1,6 @@ move to JSON data format -- done XBL -- done -externalize strings -format JS/refactor JS/Use FUEL +format JS/refactor JS/Use FUEL -- done put license caching/db, threading, XUL templates new UI diff --git a/chrome/content/diggsidebar.js b/chrome/content/diggsidebar.js index 3ecf037..bf503fd 100644 --- a/chrome/content/diggsidebar.js +++ b/chrome/content/diggsidebar.js @@ -1,307 +1,295 @@ -var gsti; -var currentStories = new Array(); -var playing = true; +var DiggSidebar = { + prefs: Application.extensions.get("diggsidebar@abhinavsarkar.net").prefs, + categories: ['All', 'Popular', 'Upcoming', 'Hot', 'Top'], + timerId : null, + currentStories : new Array(), + playing : true, -//getElementById -function $ei(id, parentNode) { - parentNode = parentNode || document; - return parentNode.getElementById(id); -} + fetchData: function(url, handler) { + DiggSidebar.diggIndicator.style.display = ''; -function fetchData(url, handler) { - //alert("inside fetchData"); - $ei('diggIndicator').style.display = ''; - $ei("diggIndicator").setAttribute('mode', 'undetermined'); - var request = Components. - classes["@mozilla.org/xmlextras/xmlhttprequest;1"]. - createInstance(); + var request = new XMLHttpRequest(); + request.onerror = DiggSidebar.errorHandler; + request.onload = handler; + request.open("GET", url, true); + request.send(null); + }, - // QI the object to nsIDOMEventTarget to set event handlers on it: + errorHandler: function(e) { + window.alert("Error in accessing data from Digg"); + }, - request.QueryInterface(Components.interfaces.nsIDOMEventTarget); - request.addEventListener("load", handler, false); - request.addEventListener("error", handleError, false); + populateMenu: function(e) { + var XHR = e.target; + var data = DiggSidebar.Utils.decodeJson(XHR.responseText); + var topics = data.topics; + var containers = new Array(); - // QI it to nsIXMLHttpRequest to open and send the request: - - request.QueryInterface(Components.interfaces.nsIXMLHttpRequest); - request.open("GET", url, true); - request.send(null); - //$ei('diggIndicator').style.display = ''; - -} - -function handleError(e) { - $ei('storyList').appendItem("!! Error accessing Digg !!"); - //$ei('diggIndicator').style.display = 'none'; -} - -function populateMenu(e) { - XHR = e.target; - data = decodeJson(XHR.responseText); - topics = data.topics; - containers = new Array(); - - while ($ei('topicPopup').firstChild) { - $ei('topicPopup').removeChild($ei('topicPopup').firstChild); - } - - topics.forEach(function (topic) { - menu = document.createElement('menu'); - menu.setAttribute('id', topic.short_name + 'Menu'); - menu.setAttribute('label', topic.name); - menu.setAttribute('accesskey', topic.name.charAt(0)); - - menupopup = document.createElement('menupopup'); - menupopup.setAttribute('id', topic.short_name + 'Popup'); - - ['All', 'Popular', 'Upcoming', 'Hot', 'Top'].forEach( - function (label) { - menuitem = document.createElement('menuitem'); - menuitem.setAttribute('label', label); - menuitem.setAttribute('value', '/topic/' + topic.short_name + '/' + label.toLowerCase()); - menuitem.setAttribute('accesskey', label.charAt(0)); - menuitem.setAttribute('oncommand', "setEndPoint(this.value)"); - menupopup.appendChild(menuitem); - } - ); - menu.appendChild(menupopup); - $ei('topicPopup').appendChild(menu); - - containers.push(topic.container); - }); - - while ($ei('containerPopup').firstChild) { - $ei('containerPopup').removeChild($ei('containerPopup').firstChild); - } - - filteredContainers = {name: [], short_name: []}; - containers.forEach(function (container) { - name = container.name; - short_name = container.short_name; - if (filteredContainers.name.indexOf(name) == -1) { - filteredContainers.name.push(name); - filteredContainers.short_name.push(short_name); + while (DiggSidebar.topicPopup.firstChild) { + DiggSidebar.topicPopup.removeChild(DiggSidebar.topicPopup.firstChild); } - }); - for (i=0; i b[date]) return -1; - else if (a[date] < b[date]) return 1; - return 0; - }); - for (i=0; i 0) { - previousUpdateInterval = prefs.get("updateinterval").value; - prefs.get("updateinterval").value = updateInterval; + if (DiggSidebar.currentStories.indexOf(story.id) != -1) listitem.new = false; + }); + DiggSidebar.currentStories = newStories; + DiggSidebar.setUpdateInterval(stories); + DiggSidebar.diggIndicator.style.display = 'none'; + }, - updateIntervalDecay = prefs.get("updateintervaldecay").value; + populateDescription: function(e) { + var XHR = e.target; + //Application.console.log(XHR.responseText); + var data = DiggSidebar.Utils.decodeJson(XHR.responseText); + var story = data.stories[0]; - if (previousUpdateInterval == updateInterval) - prefs.get("updateintervaldecay").value = updateIntervalDecay + 1; + var now = new Date(); + if (story.promote_date != null) + var then = new Date(story.promote_date*1000); else - prefs.get("updateintervaldecay").value = 0; - } - window.clearTimeout(gsti); + var then = new Date(story.submit_date*1000); + var diff = Math.max(now - then, 0) - updateInterval = prefs.get("updateinterval").value; - updateIntervalDecay = prefs.get("updateintervaldecay").value; + var hr = Math.floor(diff/(1000*3600)); + var min = Math.floor(diff/(1000*60)) - 60*hr; - timeout = Math.round(updateInterval*(Math.pow(1.5, updateIntervalDecay))); - gsti = window.setTimeout(getStories, timeout); - //Adaptive update interval code END - - $ei('diggIndicator').style.display = 'none'; -} - -function populateDescription(e) { - XHR = e.target; - //Application.console.log(XHR.responseText); - data = decodeJson(XHR.responseText); - story = data.stories[0]; - - now = new Date(); - if (story.promote_date != null) - then = new Date(story.promote_date*1000); - else - then = new Date(story.submit_date*1000); - diff = Math.max(now - then, 0) - - hr = Math.floor(diff/(1000*3600)); - min = Math.floor(diff/(1000*60)) - 60*hr; - - listitems = document.getElementsByTagName('richlistitem'); - for (i=0; i 0) && (min > 0)) ? - (hr + " hr " + min + " mins ago") : - ((hr == 0) && (min > 0)) ? - (min + " mins ago") : - ((hr == 0) && (min == 0)) ? - "just now" : ""; - li.setAttribute('date', storyDate); - li.setAttribute('status', story.status); - li.setAttribute('container', story.container.name); - li.setAttribute('topic', story.topic.name); - li.setAttribute('username', story.user.name); - li.setAttribute('userlink', "http://digg.com/users/" + story.user.name); - li.setAttribute('diggs', story.diggs); - li.setAttribute('comments', story.comments); - li.setAttribute('desc', story.description); - li.setAttribute('link', story.link); - li.setAttribute('href', story.href); - - li.read = true; - li.new = false; - li.showDesc() - - $ei('diggIndicator').style.display = 'none'; -} - -function getStories() { - var prefs = Application.extensions.get("diggsidebar@abhinavsarkar.net").prefs; - ep = prefs.get("endpoint").value || ''; - $ei('diggEndPoint').value = "digg" + ep; - - fetchData("http://services.digg.com/stories" + ep.replace(/\/all/g, '') + - "?count=30" + "&type=json" + - "&appkey=" + encodeURIComponent("http://diggsidebar.googlepages.com"), - populateStoryList); - //gsti = window.setTimeout(getStories, prefs.getIntPref("extensions.diggsidebar.updateinterval")); -} - -function getDescription(storyId) { - if (storyId == null) return; - fetchData("http://services.digg.com/story/" + storyId + "?type=json" + - "&appkey=" + encodeURIComponent("http://diggsidebar.googlepages.com"), - populateDescription); -} - -function createMenu() { - fetchData("http://services.digg.com/topics" + "?type=json" + - "&appkey=" + encodeURIComponent("http://diggsidebar.googlepages.com"), - populateMenu); -} - -function openInTab(href) { - var mainWindow = window.QueryInterface(Components.interfaces.nsIInterfaceRequestor) - .getInterface(Components.interfaces.nsIWebNavigation) - .QueryInterface(Components.interfaces.nsIDocShellTreeItem) - .rootTreeItem - .QueryInterface(Components.interfaces.nsIInterfaceRequestor) - .getInterface(Components.interfaces.nsIDOMWindow); - mainWindow.getBrowser().selectedTab = mainWindow.getBrowser().addTab(href); -} - -function setEndPoint(ep) { - var prefs = Application.extensions.get("diggsidebar@abhinavsarkar.net").prefs; - prefs.get("endpoint").value = ep; - window.clearTimeout(gsti); - //window.clearInterval(psi); - getStories(); -} - -function togglePlayPause() { - if (playing) { - window.clearTimeout(gsti); - //window.clearInterval(psi); - $ei('diggPlayPause').image = "chrome://diggsidebar/content/image/Play.png"; - $ei('diggPlayPause').setAttribute("tooltiptext", "Click to Start autoupdate"); - playing = false; - } else { - var prefs = Application.extensions.get("diggsidebar@abhinavsarkar.net").prefs; - updateInterval = prefs.get("extensions.diggsidebar.updateinterval").value; - updateIntervalDecay = prefs.get("extensions.diggsidebar.updateintervaldecay").value; - - timeout = Math.round(updateInterval*(Math.pow(1.5, updateIntervalDecay))); - //newTimeout = Math.round(timeout*parseInt($ei("diggIndicator").getAttribute('value'))/100); - gsti = window.setTimeout(getStories, timeout); - //psi = window.setInterval(showProgress, Math.round(timeout/100)); - //window.setTimeout("window.clearInterval(psi)", newTimeout); - $ei('diggPlayPause').image = "chrome://diggsidebar/content/image/Pause.png"; - $ei('diggPlayPause').setAttribute("tooltiptext", "Click to Pause autoupdate"); - playing = true; + var listitems = document.getElementsByTagName('richlistitem'); + for (var i=0; i 0) && (min > 0)) ? + (hr + " hr " + min + " mins ago") : + ((hr == 0) && (min > 0)) ? + (min + " mins ago") : + ((hr == 0) && (min == 0)) ? + "just now" : ""; + li.setAttribute('date', relativeTime); + li.setAttribute('status', story.status); + li.setAttribute('container', story.container.name); + li.setAttribute('topic', story.topic.name); + li.setAttribute('username', story.user.name); + li.setAttribute('userlink', "http://digg.com/users/" + story.user.name); + li.setAttribute('diggs', story.diggs); + li.setAttribute('comments', story.comments); + li.setAttribute('desc', story.description); + li.setAttribute('link', story.link); + li.setAttribute('href', story.href); + + li.read = true; + li.new = false; + li.showDesc() + + DiggSidebar.diggIndicator.style.display = 'none'; + }, + + getStories: function() { + var ep = DiggSidebar.prefs.get("endpoint").value || ''; + DiggSidebar.diggEndPoint.value = "digg" + ep; + + DiggSidebar.fetchData("http://services.digg.com/stories" + ep.replace(/\/all/g, '') + + "?count=30" + "&type=json" + + "&appkey=" + encodeURIComponent("http://diggsidebar.googlepages.com"), + DiggSidebar.populateStoryList); + }, + + getDescription: function(storyId) { + if (storyId == null) return; + DiggSidebar.fetchData("http://services.digg.com/story/" + storyId + "?type=json" + + "&appkey=" + encodeURIComponent("http://diggsidebar.googlepages.com"), + DiggSidebar.populateDescription); + }, + + createMenu: function() { + DiggSidebar.fetchData("http://services.digg.com/topics" + "?type=json" + + "&appkey=" + encodeURIComponent("http://diggsidebar.googlepages.com"), + DiggSidebar.populateMenu); + }, + + setUpdateInterval: function(stories) { + //Adaptive update interval code START + var sum = 0; + var weights = [4, 3, 2, 1] + var date = (stories[0].promote_date != null) ? 'promote_date' : 'submit_date'; + stories.sort(function (a, b) { + if (a[date] > b[date]) return -1; + else if (a[date] < b[date]) return 1; + return 0; + }); + for (var i=0; i 0) { + var previousUpdateInterval = DiggSidebar.prefs.get("updateinterval").value; + DiggSidebar.prefs.get("updateinterval").value = updateInterval; + + var updateIntervalDecay = DiggSidebar.prefs.get("updateintervaldecay").value; + + if (previousUpdateInterval == updateInterval) + DiggSidebar.prefs.get("updateintervaldecay").value = updateIntervalDecay + 1; + else + DiggSidebar.prefs.get("updateintervaldecay").value = 0; + } + window.clearTimeout(DiggSidebar.timerId); + + var updateInterval = DiggSidebar.prefs.get("updateinterval").value; + var updateIntervalDecay = DiggSidebar.prefs.get("updateintervaldecay").value; + + var timeout = Math.round(updateInterval*(Math.pow(1.5, updateIntervalDecay))); + DiggSidebar.timerId = window.setTimeout(DiggSidebar.getStories, timeout); + //Adaptive update interval code END + }, + + setEndPoint: function(ep) { + DiggSidebar.prefs.get("endpoint").value = ep; + window.clearTimeout(DiggSidebar.timerId); + DiggSidebar.getStories(); + }, + + togglePlayPause: function() { + if (DiggSidebar.playing) { + window.clearTimeout(DiggSidebar.timerId); + DiggSidebar.diggPlayPause.image = "chrome://diggsidebar/content/image/Play.png"; + DiggSidebar.diggPlayPause.setAttribute("tooltiptext", "Click to Start autoupdate"); + DiggSidebar.playing = false; + } else { + var updateInterval = DiggSidebar.prefs.get("updateinterval").value; + var updateIntervalDecay = DiggSidebar.prefs.get("updateintervaldecay").value; + + var timeout = Math.round(updateInterval*(Math.pow(1.5, updateIntervalDecay))); + DiggSidebar.timerId = window.setTimeout(DiggSidebar.getStories, timeout); + DiggSidebar.diggPlayPause.image = "chrome://diggsidebar/content/image/Pause.png"; + DiggSidebar.diggPlayPause.setAttribute("tooltiptext", "Click to Pause autoupdate"); + DiggSidebar.playing = true; + } + }, + + initialize: function(){ + var $ = function(id) {return document.getElementById(id)}; + DiggSidebar.diggIndicator = $('diggIndicator'); + DiggSidebar.topicPopup = $('topicPopup'); + DiggSidebar.containerPopup = $('containerPopup'); + DiggSidebar.storyList = $('storyList'); + DiggSidebar.diggEndPoint = $('diggEndPoint'); + DiggSidebar.diggPlayPause = $('diggPlayPause'); + DiggSidebar.createMenu(); + DiggSidebar.getStories(); + } }; +DiggSidebar.Utils = { + 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) { + Application.activeWindow.open(DiggSidebar.Utils.url(url)); + }, + + decodeJson: function(string) { + var json = Components.classes["@mozilla.org/dom/json;1"] + .createInstance(Components.interfaces.nsIJSON); + return json.decode(string); + } +} /*TODO preferences toolbar button diff --git a/chrome/content/diggsidebar.xul b/chrome/content/diggsidebar.xul index 7cc97c8..ac9bf08 100644 --- a/chrome/content/diggsidebar.xul +++ b/chrome/content/diggsidebar.xul @@ -6,7 +6,7 @@ + onload="DiggSidebar.initialize()">