|
@@ -1,9 +1,38 @@
|
1
|
|
-var DiggSidebar = {
|
|
1
|
+/*
|
|
2
|
+ Digg Sidebar - Shows Digg stories in the Firefox sidebar in real time.
|
|
3
|
+ Copyright (C) 2008 Abhinav Sarkar <abhinav dot sarkar at gmail dot com>
|
|
4
|
+
|
|
5
|
+ This program is free software: you can redistribute it and/or modify
|
|
6
|
+ it under the terms of the GNU General Public License as published by
|
|
7
|
+ the Free Software Foundation, either version 3 of the License, or
|
|
8
|
+ (at your option) any later version.
|
|
9
|
+
|
|
10
|
+ This program is distributed in the hope that it will be useful,
|
|
11
|
+ but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
12
|
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
13
|
+ GNU General Public License for more details.
|
|
14
|
+
|
|
15
|
+ You should have received a copy of the GNU General Public License
|
|
16
|
+ along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
17
|
+
|
|
18
|
+*/
|
|
19
|
+
|
|
20
|
+DiggSidebar = {
|
2
|
21
|
prefs: Application.extensions.get("diggsidebar@abhinavsarkar.net").prefs,
|
|
22
|
+ storyListRefreshEventTopic: "extension-diggsidebar-abhinavsarkar-net-storylist-refresh",
|
|
23
|
+ endpoint: {
|
|
24
|
+ container: null,
|
|
25
|
+ topic: null,
|
|
26
|
+ category: "all"
|
|
27
|
+ },
|
|
28
|
+ updateInterval: 10,
|
|
29
|
+ updateIntervalDecay: 0,
|
|
30
|
+ shownStoriesCount: 30,
|
3
|
31
|
categories: ['All', 'Popular', 'Upcoming', 'Hot', 'Top'],
|
4
|
|
- timerId : null,
|
5
|
|
- currentStories : new Array(),
|
6
|
|
- playing : true,
|
|
32
|
+ timerId: null,
|
|
33
|
+ storyIds: [],
|
|
34
|
+ playing: true,
|
|
35
|
+ stories: [],
|
7
|
36
|
|
8
|
37
|
fetchData: function(url, handler) {
|
9
|
38
|
DiggSidebar.diggIndicator.style.display = '';
|
|
@@ -95,31 +124,36 @@ var DiggSidebar = {
|
95
|
124
|
DiggSidebar.diggIndicator.style.display = 'none';
|
96
|
125
|
},
|
97
|
126
|
|
98
|
|
- populateStoryList: function(e) {
|
99
|
|
- var XHR = e.target;
|
100
|
|
- //Application.console.log(XHR.responseText);
|
101
|
|
- var data = DiggSidebar.Utils.decodeJson(XHR.responseText);
|
102
|
|
- var stories = data.stories;
|
103
|
|
-
|
|
127
|
+ populateStoryList: function() {
|
104
|
128
|
while (DiggSidebar.storyList.firstChild) {
|
105
|
129
|
DiggSidebar.storyList.removeChild(DiggSidebar.storyList.firstChild);
|
106
|
130
|
}
|
107
|
131
|
|
108
|
|
- var newStories = new Array();
|
109
|
|
- //Application.console.log(stories.length);
|
110
|
|
-
|
111
|
|
- stories.forEach(function (story) {
|
112
|
|
- newStories.push(story.id);
|
113
|
|
- //Application.console.log(i + " " + story.title + " " + story.id);
|
114
|
|
-
|
115
|
|
- var listitem = DiggSidebar.storyList.appendChild(document.createElement('richlistitem'));
|
116
|
|
- listitem.id = "story_" + story.id;
|
117
|
|
- listitem.setAttribute('title', story.title);
|
118
|
|
-
|
119
|
|
- if (DiggSidebar.currentStories.indexOf(story.id) != -1) listitem.new = false;
|
|
132
|
+ var newStoryIds = new Array();
|
|
133
|
+ Application.console.log(DiggSidebar.stories.length);
|
|
134
|
+
|
|
135
|
+ var jp = new JPath(DiggSidebar.stories);
|
|
136
|
+ var filtedStories = DiggSidebar.stories;
|
|
137
|
+ if (DiggSidebar.endpoint.topic)
|
|
138
|
+ filtedStories = jp.$(function(story){
|
|
139
|
+ return story.$("topic/short_name").json == DiggSidebar.endpoint.topic;
|
|
140
|
+ }).json;
|
|
141
|
+ if (DiggSidebar.endpoint.container)
|
|
142
|
+ filtedStories = jp.$(function(story){
|
|
143
|
+ return story.$("container/short_name").json == DiggSidebar.endpoint.container;
|
|
144
|
+ }).json;
|
|
145
|
+
|
|
146
|
+ filtedStories.forEach(function (story, index) {
|
|
147
|
+ if (index < DiggSidebar.shownStoriesCount) {
|
|
148
|
+ var listitem = DiggSidebar.storyList.appendChild(document.createElement('richlistitem'));
|
|
149
|
+ listitem.id = "story_" + story.id;
|
|
150
|
+ listitem.setAttribute('title', story.title);
|
|
151
|
+ if (DiggSidebar.storyIds.indexOf(story.id) != -1) listitem.new = false;
|
|
152
|
+ }
|
|
153
|
+ if (DiggSidebar.storyIds.indexOf(story.id) == -1) newStoryIds.push(story.id);
|
120
|
154
|
});
|
121
|
|
- DiggSidebar.currentStories = newStories;
|
122
|
|
- DiggSidebar.setUpdateInterval(stories);
|
|
155
|
+ DiggSidebar.storyIds = DiggSidebar.storyIds.concat(newStoryIds);
|
|
156
|
+ DiggSidebar.setUpdateInterval(DiggSidebar.stories.slice(-5));
|
123
|
157
|
DiggSidebar.diggIndicator.style.display = 'none';
|
124
|
158
|
},
|
125
|
159
|
|
|
@@ -178,7 +212,20 @@ var DiggSidebar = {
|
178
|
212
|
DiggSidebar.fetchData("http://services.digg.com/stories" + ep.replace(/\/all/g, '') +
|
179
|
213
|
"?count=30" + "&type=json" +
|
180
|
214
|
"&appkey=" + encodeURIComponent("http://diggsidebar.googlepages.com"),
|
181
|
|
- DiggSidebar.populateStoryList);
|
|
215
|
+ function(e) {
|
|
216
|
+ var newStories = DiggSidebar.Utils.decodeJson(e.target.responseText).stories.filter(
|
|
217
|
+ function(story) {
|
|
218
|
+ return !(DiggSidebar.storyIds.indexOf(story.id) != -1);
|
|
219
|
+ });
|
|
220
|
+ newStories.forEach(function(story) {
|
|
221
|
+ story.category = DiggSidebar.endpoint.category;
|
|
222
|
+ story.read = false;
|
|
223
|
+ });
|
|
224
|
+ DiggSidebar.stories = newStories.concat(DiggSidebar.stories);
|
|
225
|
+ var observerService = Components.classes["@mozilla.org/observer-service;1"]
|
|
226
|
+ .getService(Components.interfaces.nsIObserverService);
|
|
227
|
+ observerService.notifyObservers(null, DiggSidebar.storyListRefreshEventTopic, null);
|
|
228
|
+ });
|
182
|
229
|
},
|
183
|
230
|
|
184
|
231
|
getDescription: function(storyId) {
|
|
@@ -196,6 +243,12 @@ var DiggSidebar = {
|
196
|
243
|
|
197
|
244
|
setUpdateInterval: function(stories) {
|
198
|
245
|
//Adaptive update interval code START
|
|
246
|
+ if (stories.length == 0) {
|
|
247
|
+ window.clearTimeout(DiggSidebar.timerId);
|
|
248
|
+ var timeout = Math.round(DiggSidebar.updateInterval*(Math.pow(1.5, DiggSidebar.updateIntervalDecay)));
|
|
249
|
+ DiggSidebar.timerId = window.setTimeout(DiggSidebar.getStories, timeout);
|
|
250
|
+ return;
|
|
251
|
+ }
|
199
|
252
|
var sum = 0;
|
200
|
253
|
var weights = [4, 3, 2, 1]
|
201
|
254
|
var date = (stories[0].promote_date != null) ? 'promote_date' : 'submit_date';
|
|
@@ -216,32 +269,46 @@ var DiggSidebar = {
|
216
|
269
|
//Application.console.log(updateInterval);
|
217
|
270
|
|
218
|
271
|
if (updateInterval > 0) {
|
219
|
|
- var previousUpdateInterval = DiggSidebar.prefs.get("updateinterval").value;
|
220
|
|
- DiggSidebar.prefs.get("updateinterval").value = updateInterval;
|
|
272
|
+ var previousUpdateInterval = DiggSidebar.updateInterval;
|
|
273
|
+ DiggSidebar.updateInterval = updateInterval;
|
221
|
274
|
|
222
|
|
- var updateIntervalDecay = DiggSidebar.prefs.get("updateintervaldecay").value;
|
|
275
|
+ var updateIntervalDecay = DiggSidebar.updateIntervalDecay;
|
223
|
276
|
|
224
|
277
|
if (previousUpdateInterval == updateInterval)
|
225
|
|
- DiggSidebar.prefs.get("updateintervaldecay").value = updateIntervalDecay + 1;
|
|
278
|
+ DiggSidebar.updateIntervalDecay = updateIntervalDecay + 1;
|
226
|
279
|
else
|
227
|
|
- DiggSidebar.prefs.get("updateintervaldecay").value = 0;
|
|
280
|
+ DiggSidebar.updateIntervalDecay = 0;
|
228
|
281
|
}
|
229
|
282
|
window.clearTimeout(DiggSidebar.timerId);
|
230
|
283
|
|
231
|
|
- var updateInterval = DiggSidebar.prefs.get("updateinterval").value;
|
232
|
|
- var updateIntervalDecay = DiggSidebar.prefs.get("updateintervaldecay").value;
|
233
|
|
-
|
234
|
|
- var timeout = Math.round(updateInterval*(Math.pow(1.5, updateIntervalDecay)));
|
|
284
|
+ var timeout = Math.round(DiggSidebar.updateInterval*(Math.pow(1.5, DiggSidebar.updateIntervalDecay)));
|
235
|
285
|
DiggSidebar.timerId = window.setTimeout(DiggSidebar.getStories, timeout);
|
236
|
286
|
//Adaptive update interval code END
|
237
|
287
|
},
|
238
|
288
|
|
239
|
289
|
setEndPoint: function(ep) {
|
240
|
290
|
DiggSidebar.prefs.get("endpoint").value = ep;
|
|
291
|
+ DiggSidebar.endpoint = DiggSidebar.getEndpointParts(ep);
|
241
|
292
|
window.clearTimeout(DiggSidebar.timerId);
|
242
|
293
|
DiggSidebar.getStories();
|
243
|
294
|
},
|
244
|
295
|
|
|
296
|
+ getEndpointParts: function(ep) {
|
|
297
|
+ var tmp = {
|
|
298
|
+ topic: null,
|
|
299
|
+ container: null,
|
|
300
|
+ category: "all"
|
|
301
|
+ };
|
|
302
|
+ parts = ep.split("/");
|
|
303
|
+ if (parts.length == 2)
|
|
304
|
+ tmp.category = parts[1];
|
|
305
|
+ else if (parts.length == 4) {
|
|
306
|
+ tmp[parts[1]] = parts[2];
|
|
307
|
+ tmp.category = parts[3];
|
|
308
|
+ }
|
|
309
|
+ return tmp;
|
|
310
|
+ },
|
|
311
|
+
|
245
|
312
|
togglePlayPause: function() {
|
246
|
313
|
if (DiggSidebar.playing) {
|
247
|
314
|
window.clearTimeout(DiggSidebar.timerId);
|
|
@@ -249,10 +316,7 @@ var DiggSidebar = {
|
249
|
316
|
DiggSidebar.diggPlayPause.setAttribute("tooltiptext", "Click to Start autoupdate");
|
250
|
317
|
DiggSidebar.playing = false;
|
251
|
318
|
} else {
|
252
|
|
- var updateInterval = DiggSidebar.prefs.get("updateinterval").value;
|
253
|
|
- var updateIntervalDecay = DiggSidebar.prefs.get("updateintervaldecay").value;
|
254
|
|
-
|
255
|
|
- var timeout = Math.round(updateInterval*(Math.pow(1.5, updateIntervalDecay)));
|
|
319
|
+ var timeout = Math.round(DiggSidebar.updateInterval*(Math.pow(1.5, DiggSidebar.updateIntervalDecay)));
|
256
|
320
|
DiggSidebar.timerId = window.setTimeout(DiggSidebar.getStories, timeout);
|
257
|
321
|
DiggSidebar.diggPlayPause.image = "chrome://diggsidebar/content/image/Pause.png";
|
258
|
322
|
DiggSidebar.diggPlayPause.setAttribute("tooltiptext", "Click to Pause autoupdate");
|
|
@@ -261,6 +325,7 @@ var DiggSidebar = {
|
261
|
325
|
},
|
262
|
326
|
|
263
|
327
|
initialize: function(){
|
|
328
|
+ DiggSidebar.endpoint = DiggSidebar.getEndpointParts(DiggSidebar.prefs.get("endpoint").value);
|
264
|
329
|
var $ = function(id) {return document.getElementById(id)};
|
265
|
330
|
DiggSidebar.diggIndicator = $('diggIndicator');
|
266
|
331
|
DiggSidebar.topicPopup = $('topicPopup');
|
|
@@ -268,8 +333,14 @@ var DiggSidebar = {
|
268
|
333
|
DiggSidebar.storyList = $('storyList');
|
269
|
334
|
DiggSidebar.diggEndPoint = $('diggEndPoint');
|
270
|
335
|
DiggSidebar.diggPlayPause = $('diggPlayPause');
|
|
336
|
+
|
|
337
|
+ DiggSidebar.storyListObserver = new StoryListObserver();
|
271
|
338
|
DiggSidebar.createMenu();
|
272
|
339
|
DiggSidebar.getStories();
|
|
340
|
+ },
|
|
341
|
+
|
|
342
|
+ destroy: function(){
|
|
343
|
+ DiggSidebar.storyListObserver.unregister();
|
273
|
344
|
}
|
274
|
345
|
};
|
275
|
346
|
|
|
@@ -290,6 +361,38 @@ DiggSidebar.Utils = {
|
290
|
361
|
return json.decode(string);
|
291
|
362
|
}
|
292
|
363
|
}
|
|
364
|
+
|
|
365
|
+var StoryListObserver = function() {
|
|
366
|
+ this.registered = false;
|
|
367
|
+ this.register();
|
|
368
|
+}
|
|
369
|
+
|
|
370
|
+StoryListObserver.prototype = {
|
|
371
|
+ observe: function(subject, topic, data) {
|
|
372
|
+ if (topic == window.DiggSidebar.storyListRefreshEventTopic) {
|
|
373
|
+ window.DiggSidebar.populateStoryList();
|
|
374
|
+ }
|
|
375
|
+ },
|
|
376
|
+
|
|
377
|
+ register: function() {
|
|
378
|
+ if (this.registered == false) {
|
|
379
|
+ var observerService = Components.classes["@mozilla.org/observer-service;1"]
|
|
380
|
+ .getService(Components.interfaces.nsIObserverService);
|
|
381
|
+ observerService.addObserver(this, DiggSidebar.storyListRefreshEventTopic, false);
|
|
382
|
+ this.registered = true;
|
|
383
|
+ }
|
|
384
|
+ },
|
|
385
|
+
|
|
386
|
+ unregister: function() {
|
|
387
|
+ if (this.registered == true) {
|
|
388
|
+ var observerService = Components.classes["@mozilla.org/observer-service;1"]
|
|
389
|
+ .getService(Components.interfaces.nsIObserverService);
|
|
390
|
+ observerService.removeObserver(this, DiggSidebar.storyListRefreshEventTopic);
|
|
391
|
+ this.registered = false;
|
|
392
|
+ }
|
|
393
|
+ }
|
|
394
|
+}
|
|
395
|
+
|
293
|
396
|
/*TODO
|
294
|
397
|
preferences
|
295
|
398
|
toolbar button
|