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