refactoring and reformatting.
git-svn-id: file:///tmp/snv/trunk@22 12951d8a-c33d-4b7c-b961-822215c816e1
This commit is contained in:
parent
629914c24b
commit
8aff3915e8
@ -41,13 +41,15 @@ var DiggSidebar = {
|
||||
observerService: Components.classes["@mozilla.org/observer-service;1"]
|
||||
.getService(Components.interfaces.nsIObserverService),
|
||||
|
||||
$ce: function (tagName) {
|
||||
return document.createElement(tagName);
|
||||
},
|
||||
|
||||
fetchData: function (url, handler) {
|
||||
DiggSidebar.indicateActivity();
|
||||
|
||||
fetchData: function (url, loadHandler, errorHandler) {
|
||||
var request = new XMLHttpRequest();
|
||||
request.onerror = DiggSidebar.errorHandler;
|
||||
request.onload = handler;
|
||||
request.onload = loadHandler;
|
||||
request.onerror = errorHandler;
|
||||
request.open("GET", url, true);
|
||||
request.send(null);
|
||||
},
|
||||
@ -72,53 +74,56 @@ var DiggSidebar = {
|
||||
var containers = data.containers;
|
||||
|
||||
containers.forEach(function (container) {
|
||||
var cmenu = document.createElement("menu");
|
||||
var cmenu = DiggSidebar.$ce("menu");
|
||||
with (cmenu) {
|
||||
setAttribute("id", container.short_name + "Menu");
|
||||
setAttribute("label", container.name);
|
||||
setAttribute("accesskey", container.name.charAt(0));
|
||||
}
|
||||
|
||||
var cmenupopup = document.createElement("menupopup");
|
||||
var cmenupopup = DiggSidebar.$ce("menupopup");
|
||||
cmenupopup.setAttribute("id", container.short_name + "Popup");
|
||||
|
||||
DiggSidebar.categories.forEach(
|
||||
function (category) {
|
||||
var cmenuitem = document.createElement("menuitem");
|
||||
var cmenuitem = DiggSidebar.$ce("menuitem");
|
||||
with (cmenuitem) {
|
||||
setAttribute("label", category);
|
||||
setAttribute("value", "/container/" + container.short_name + "/" + category.toLowerCase());
|
||||
setAttribute("value",
|
||||
"/container/" + container.short_name +
|
||||
"/" + category.toLowerCase());
|
||||
setAttribute("accesskey", category.charAt(0));
|
||||
setAttribute("oncommand", "DiggSidebar.setEndPoint(this.value)");
|
||||
setAttribute("oncommand",
|
||||
"DiggSidebar.setEndPoint(this.value)");
|
||||
}
|
||||
cmenupopup.appendChild(cmenuitem);
|
||||
}
|
||||
);
|
||||
cmenupopup.appendChild(document.createElement("menuseparator"));
|
||||
cmenupopup.appendChild(DiggSidebar.$ce("menuseparator"));
|
||||
var topics = container.topics;
|
||||
topics.forEach(function (topic) {
|
||||
var tmenu = document.createElement("menu");
|
||||
var tmenu = DiggSidebar.$ce("menu");
|
||||
with (tmenu) {
|
||||
setAttribute("id", topic.short_name + "Menu");
|
||||
setAttribute("label", topic.name);
|
||||
setAttribute("accesskey", topic.name.charAt(0));
|
||||
}
|
||||
|
||||
var tmenupopup = document.createElement("menupopup");
|
||||
var tmenupopup = DiggSidebar.$ce("menupopup");
|
||||
tmenupopup.setAttribute("id", topic.short_name + "Popup");
|
||||
|
||||
DiggSidebar.categories.forEach(
|
||||
function (category) {
|
||||
var tmenuitem = document.createElement("menuitem");
|
||||
DiggSidebar.categories.forEach(function (category) {
|
||||
var tmenuitem = DiggSidebar.$ce("menuitem");
|
||||
with (tmenuitem) {
|
||||
setAttribute("label", category);
|
||||
setAttribute("value", "/topic/" + topic.short_name + "/" + category.toLowerCase());
|
||||
setAttribute("value", "/topic/" + topic.short_name +
|
||||
"/" + category.toLowerCase());
|
||||
setAttribute("accesskey", category.charAt(0));
|
||||
setAttribute("oncommand", "DiggSidebar.setEndPoint(this.value)");
|
||||
setAttribute("oncommand",
|
||||
"DiggSidebar.setEndPoint(this.value)");
|
||||
}
|
||||
tmenupopup.appendChild(tmenuitem);
|
||||
}
|
||||
);
|
||||
});
|
||||
tmenu.appendChild(tmenupopup);
|
||||
cmenupopup.appendChild(tmenu);
|
||||
});
|
||||
@ -128,16 +133,20 @@ var DiggSidebar = {
|
||||
},
|
||||
|
||||
populateStoryList: function () {
|
||||
var ep = DiggSidebar.prefs.get("endpoint").value || "";
|
||||
DiggSidebar.UI.endPointDesc.value = "digg" + ep;
|
||||
|
||||
var newStoryIds = [];
|
||||
|
||||
var jp = new JPath(DiggSidebar.stories);
|
||||
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) {
|
||||
return (story.$("topic/short_name").json == DiggSidebar.endpoint.topic);
|
||||
return story.$("topic/short_name").json == DiggSidebar.endpoint.topic;
|
||||
}).json;
|
||||
}
|
||||
if (DiggSidebar.endpoint.container) {
|
||||
@ -170,7 +179,8 @@ var DiggSidebar = {
|
||||
((hr == 0) && (min == 0)) ?
|
||||
"just now" : "";
|
||||
|
||||
var li = DiggSidebar.UI.storyListBox.appendChild(document.createElement("richlistitem"));
|
||||
var li = DiggSidebar.UI.storyListBox.appendChild(
|
||||
DiggSidebar.$ce("richlistitem"));
|
||||
li.id = "story_" + story.id;
|
||||
var attributes = {
|
||||
title: story.title,
|
||||
@ -221,23 +231,25 @@ var DiggSidebar = {
|
||||
DiggSidebar.refreshing = true;
|
||||
var ep = DiggSidebar.prefs.get("endpoint").value || "";
|
||||
|
||||
DiggSidebar.fetchData("http://services.digg.com/stories" + ep.replace(/\/all/g, "") +
|
||||
"?count=30" + "&type=json" +
|
||||
DiggSidebar.indicateActivity();
|
||||
DiggSidebar.fetchData("http://services.digg.com/stories" +
|
||||
ep.replace(/\/all/g, "") + "?count=30" + "&type=json" +
|
||||
"&appkey=" + encodeURIComponent(DiggSidebar.homepageURL),
|
||||
function (e) {
|
||||
DiggSidebar.UI.endPointDesc.value = "digg" + ep;
|
||||
|
||||
function (e) {
|
||||
var stories = DiggSidebar.Utils.decodeJson(e.target.responseText).stories;
|
||||
var newStories = stories.filter(
|
||||
function (story) {
|
||||
return !(DiggSidebar.storyIds.indexOf(story.id) != -1);
|
||||
stories.forEach(function (story) { story.id = parseInt(story.id); });
|
||||
|
||||
var newStories = stories.filter(function (story) {
|
||||
return DiggSidebar.storyIds.indexOf(story.id) == -1;
|
||||
});
|
||||
newStories.forEach(function (story) {
|
||||
story.category = DiggSidebar.endpoint.category;
|
||||
story.read = false;
|
||||
});
|
||||
DiggSidebar.stories = newStories.concat(DiggSidebar.stories);
|
||||
DiggSidebar.observerService.notifyObservers(null, DiggSidebar.storyListRefreshEventTopic, null);
|
||||
DiggSidebar.observerService.notifyObservers(
|
||||
null, DiggSidebar.storyListRefreshEventTopic, null);
|
||||
|
||||
DiggSidebar.refreshing = false;
|
||||
DiggSidebar.setUpdateInterval(stories);
|
||||
@ -247,16 +259,18 @@ var DiggSidebar = {
|
||||
DiggSidebar.timerId = window.setTimeout(DiggSidebar.getStories, timeout);
|
||||
|
||||
DiggSidebar.indicateInactivity();
|
||||
});
|
||||
},
|
||||
DiggSidebar.errorHandler);
|
||||
} else {
|
||||
window.setTimeout(DiggSidebar.getStories, 1000);
|
||||
}
|
||||
},
|
||||
|
||||
createMenu: function () {
|
||||
DiggSidebar.indicateActivity();
|
||||
DiggSidebar.fetchData("http://services.digg.com/containers" + "?type=json" +
|
||||
"&appkey=" + encodeURIComponent(DiggSidebar.homepageURL),
|
||||
DiggSidebar.populateMenu);
|
||||
DiggSidebar.populateMenu, DiggSidebar.errorHandler);
|
||||
},
|
||||
|
||||
setUpdateInterval: function (stories) {
|
||||
@ -302,7 +316,8 @@ var DiggSidebar = {
|
||||
},
|
||||
|
||||
calculateTimeout: function () {
|
||||
return Math.round(DiggSidebar.updateInterval * (Math.pow(1.5, DiggSidebar.updateIntervalDecay)));
|
||||
return Math.round(
|
||||
DiggSidebar.updateInterval * (Math.pow(1.5, DiggSidebar.updateIntervalDecay)));
|
||||
},
|
||||
|
||||
setEndPoint: function (ep) {
|
||||
@ -343,8 +358,9 @@ var DiggSidebar = {
|
||||
disabled = false;
|
||||
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":
|
||||
(autoRefreshTime > 60) ?
|
||||
@ -358,7 +374,8 @@ var DiggSidebar = {
|
||||
},
|
||||
|
||||
initialize: function () {
|
||||
DiggSidebar.endpoint = DiggSidebar.getEndpointParts(DiggSidebar.prefs.get("endpoint").value);
|
||||
DiggSidebar.endpoint = DiggSidebar.getEndpointParts(
|
||||
DiggSidebar.prefs.get("endpoint").value);
|
||||
var $ = function (id) {
|
||||
return document.getElementById(id);
|
||||
};
|
||||
@ -371,12 +388,14 @@ var DiggSidebar = {
|
||||
DiggSidebar.UI.endPointDesc = $("dsEndPointDesc");
|
||||
|
||||
DiggSidebar.storyListObserver = new DiggSidebar.StoryListObserver();
|
||||
DiggSidebar.storyListObserver.register();
|
||||
DiggSidebar.createMenu();
|
||||
DiggSidebar.getStories();
|
||||
},
|
||||
|
||||
destroy: function () {
|
||||
DiggSidebar.storyListObserver.unregister();
|
||||
DiggSidebar.stories = [];
|
||||
}
|
||||
};
|
||||
|
||||
@ -406,26 +425,27 @@ DiggSidebar.Utils = {
|
||||
|
||||
DiggSidebar.StoryListObserver = function () {
|
||||
this.registered = false;
|
||||
this.register();
|
||||
};
|
||||
|
||||
DiggSidebar.StoryListObserver.prototype = {
|
||||
observe: function (subject, topic, data) {
|
||||
if (topic == window.DiggSidebar.storyListRefreshEventTopic) {
|
||||
window.DiggSidebar.populateStoryList();
|
||||
if (topic == DiggSidebar.storyListRefreshEventTopic) {
|
||||
DiggSidebar.populateStoryList();
|
||||
}
|
||||
},
|
||||
|
||||
register: function () {
|
||||
if (!this.registered) {
|
||||
DiggSidebar.observerService.addObserver(this, DiggSidebar.storyListRefreshEventTopic, false);
|
||||
DiggSidebar.observerService.addObserver(
|
||||
this, DiggSidebar.storyListRefreshEventTopic, false);
|
||||
this.registered = true;
|
||||
}
|
||||
},
|
||||
|
||||
unregister: function () {
|
||||
if (this.registered) {
|
||||
DiggSidebar.observerService.removeObserver(this, DiggSidebar.storyListRefreshEventTopic);
|
||||
DiggSidebar.observerService.removeObserver(
|
||||
this, DiggSidebar.storyListRefreshEventTopic);
|
||||
this.registered = false;
|
||||
}
|
||||
}
|
||||
|
@ -76,7 +76,7 @@
|
||||
</constructor>
|
||||
<property name="read"
|
||||
onget="return this.d.read;"
|
||||
onset="this.d.read = val; if(val==true) this.markAsRead();"/>
|
||||
onset="this.d.read = val; if(val) this.markAsRead();"/>
|
||||
<method name="markAsRead">
|
||||
<body>
|
||||
<![CDATA[
|
@ -73,7 +73,7 @@ a img {
|
||||
border: 2px solid #E5ECF3;
|
||||
border-top: none;
|
||||
color: #000000!important;
|
||||
-moz-binding: url("chrome://diggsidebar/content/diggstory.xml#diggstoryitem");
|
||||
-moz-binding: url("chrome://diggsidebar/content/diggstory.xbl#diggstoryitem");
|
||||
}
|
||||
|
||||
richlistitem[selected="true"] {
|
||||
|
@ -24,7 +24,7 @@
|
||||
<RDF:Description RDF:about="rdf:#$55wJ23"
|
||||
em:id="{ec8030f7-c20a-464f-9b0e-13a3a9e97384}"
|
||||
em:minVersion="3.0"
|
||||
em:maxVersion="3.5.*" />
|
||||
em:maxVersion="3.6alpha1" />
|
||||
<RDF:Description RDF:about="urn:mozilla:install-manifest"
|
||||
em:id="diggsidebar@abhinavsarkar.net"
|
||||
em:name="Digg Sidebar"
|
||||
|
Loading…
Reference in New Issue
Block a user