Browse Source

changed the method to show the description of stories

git-svn-id: file:///tmp/snv/trunk@10 12951d8a-c33d-4b7c-b961-822215c816
Abhinav Sarkar 13 years ago
parent
commit
04a1088a4a

+ 2
- 2
chrome/content/TODO.txt View File

@@ -1,7 +1,7 @@
1 1
 move to JSON data format -- done
2 2
 XBL -- done
3 3
 format JS/refactor JS/Use FUEL -- done
4
-caching/db, threading
4
+caching/db, notifier-observer -- done
5
+search
5 6
 new UI, icons, thumbnails
6
-JQuery
7 7
 put license

+ 59
- 77
chrome/content/diggsidebar.js View File

@@ -17,7 +17,7 @@
17 17
 
18 18
 */
19 19
 
20
-DiggSidebar = {
20
+var DiggSidebar = {
21 21
     prefs: Application.extensions.get("diggsidebar@abhinavsarkar.net").prefs,
22 22
     storyListRefreshEventTopic: "extension-diggsidebar-abhinavsarkar-net-storylist-refresh",
23 23
     endpoint: {
@@ -130,7 +130,7 @@ DiggSidebar = {
130 130
         }
131 131
 
132 132
         var newStoryIds = new Array();
133
-        Application.console.log(DiggSidebar.stories.length);
133
+        Application.console.log("DiggSidebar.stories.length = " + DiggSidebar.stories.length);
134 134
 
135 135
         var jp = new JPath(DiggSidebar.stories);
136 136
         var filtedStories = DiggSidebar.stories;
@@ -145,64 +145,65 @@ DiggSidebar = {
145 145
 
146 146
         filtedStories.forEach(function (story, index) {
147 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;
148
+                var now = new Date();
149
+                if (story.promote_date != null)
150
+                    var then = new Date(story.promote_date*1000);
151
+                else
152
+                    var then = new Date(story.submit_date*1000);
153
+                var diff = Math.max(now - then, 0)
154
+
155
+                var hr = Math.floor(diff/(1000*3600));
156
+                var min = Math.floor(diff/(1000*60)) - 60*hr;
157
+
158
+                var relativeTime = ((hr > 0) && (min > 0)) ?
159
+                    (hr + " hr " + min + " mins ago") :
160
+                    ((hr == 0) && (min > 0)) ?
161
+                    (min + " mins ago") :
162
+                    ((hr == 0) && (min == 0)) ?
163
+                    "just now" : "";
164
+
165
+                var li = DiggSidebar.storyList.appendChild(document.createElement('richlistitem'));
166
+                li.id = "story_" + story.id;
167
+                li.setAttribute('title', story.title);
168
+                li.setAttribute('date', relativeTime);
169
+                li.setAttribute('status', story.status);
170
+                li.setAttribute('container', story.container.name);
171
+                li.setAttribute('topic', story.topic.name);
172
+                li.setAttribute('username', story.user.name);
173
+                li.setAttribute('userlink', "http://digg.com/users/" + story.user.name);
174
+                li.setAttribute('diggs', story.diggs);
175
+                li.setAttribute('comments', story.comments);
176
+                li.setAttribute('desc', story.description);
177
+                li.setAttribute('link', story.link);
178
+                li.setAttribute('href',  story.href);
179
+
180
+                if (DiggSidebar.storyIds.indexOf(story.id) != -1) li.new = false;
181
+                li.read = story.read;
152 182
             }
153 183
             if (DiggSidebar.storyIds.indexOf(story.id) == -1) newStoryIds.push(story.id);
154 184
         });
155 185
         DiggSidebar.storyIds = DiggSidebar.storyIds.concat(newStoryIds);
156
-        DiggSidebar.setUpdateInterval(DiggSidebar.stories.slice(-5));
157
-        DiggSidebar.diggIndicator.style.display = 'none';
158
-    },
186
+        DiggSidebar.setUpdateInterval(DiggSidebar.stories.slice(0,5));
159 187
 
160
-    populateDescription: function(e) {
161
-        var XHR = e.target;
162
-        //Application.console.log(XHR.responseText);
163
-        var data = DiggSidebar.Utils.decodeJson(XHR.responseText);
164
-        var story = data.stories[0];
188
+        window.clearTimeout(DiggSidebar.timerId);
189
+        var timeout = Math.round(DiggSidebar.updateInterval*(Math.pow(1.5, DiggSidebar.updateIntervalDecay)));
190
+        DiggSidebar.timerId = window.setTimeout(DiggSidebar.getStories, timeout);
165 191
 
166
-        var now = new Date();
167
-        if (story.promote_date != null)
168
-            var then = new Date(story.promote_date*1000);
169
-        else
170
-            var then = new Date(story.submit_date*1000);
171
-        var diff = Math.max(now - then, 0)
192
+        DiggSidebar.diggIndicator.style.display = 'none';
193
+    },
172 194
 
173
-        var hr = Math.floor(diff/(1000*3600));
174
-        var min = Math.floor(diff/(1000*60)) - 60*hr;
195
+    showDescription: function(storyId) {
196
+        var jp = new JPath(DiggSidebar.stories);
197
+        var story = jp.query('//[id == ' + storyId + ']')[0];
198
+        story.read = true;
175 199
 
176 200
         var listitems = document.getElementsByTagName('richlistitem');
177
-        for (var i=0; i<listitems.length; i++) {
178
-                listitems[i].hideDesc();
179
-        }
201
+        for (var i=0; i<listitems.length; i++)
202
+            listitems[i].hideDescription();
180 203
 
181 204
         var li = document.getElementById('story_'+ story.id)
182
-
183
-        var relativeTime = ((hr > 0) && (min > 0)) ?
184
-            (hr + " hr " + min + " mins ago") :
185
-            ((hr == 0) && (min > 0)) ?
186
-            (min + " mins ago") :
187
-            ((hr == 0) && (min == 0)) ?
188
-            "just now" : "";
189
-        li.setAttribute('date', relativeTime);
190
-        li.setAttribute('status', story.status);
191
-        li.setAttribute('container', story.container.name);
192
-        li.setAttribute('topic', story.topic.name);
193
-        li.setAttribute('username', story.user.name);
194
-        li.setAttribute('userlink', "http://digg.com/users/" + story.user.name);
195
-        li.setAttribute('diggs', story.diggs);
196
-        li.setAttribute('comments', story.comments);
197
-        li.setAttribute('desc', story.description);
198
-        li.setAttribute('link', story.link);
199
-        li.setAttribute('href',  story.href);
200
-
201 205
         li.read = true;
202
-        li.new = false;
203
-        li.showDesc()
204
-
205
-        DiggSidebar.diggIndicator.style.display = 'none';
206
+        li.showDescription();
206 207
     },
207 208
 
208 209
     getStories: function() {
@@ -228,13 +229,6 @@ DiggSidebar = {
228 229
             });
229 230
     },
230 231
 
231
-    getDescription: function(storyId) {
232
-        if (storyId == null) return;
233
-        DiggSidebar.fetchData("http://services.digg.com/story/" + storyId + "?type=json" +
234
-            "&appkey=" + encodeURIComponent("http://diggsidebar.googlepages.com"),
235
-            DiggSidebar.populateDescription);
236
-    },
237
-
238 232
     createMenu: function() {
239 233
         DiggSidebar.fetchData("http://services.digg.com/topics" + "?type=json" +
240 234
             "&appkey=" + encodeURIComponent("http://diggsidebar.googlepages.com"),
@@ -243,12 +237,7 @@ DiggSidebar = {
243 237
 
244 238
     setUpdateInterval: function(stories) {
245 239
         //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
-        }
240
+        if (stories.length == 0) return;
252 241
         var sum = 0;
253 242
         var weights = [4, 3, 2, 1]
254 243
         var date = (stories[0].promote_date != null) ? 'promote_date' : 'submit_date';
@@ -261,28 +250,21 @@ DiggSidebar = {
261 250
             var diff = stories[i][date] - stories[i+1][date];
262 251
             sum += weights[i]*diff;
263 252
         }
264
-        var updateInterval = Math.round(
253
+        var newUpdateInterval = Math.round(
265 254
             sum*1000/weights
266 255
             .splice(0,Math.min(4, stories.length-1))
267 256
             .reduce(function(a, b){return a + b;})
268 257
         );
269
-        //Application.console.log(updateInterval);
270 258
 
271
-        if (updateInterval > 0) {
259
+        if (newUpdateInterval > 0) {
272 260
             var previousUpdateInterval = DiggSidebar.updateInterval;
273
-            DiggSidebar.updateInterval = updateInterval;
274
-
275
-            var updateIntervalDecay = DiggSidebar.updateIntervalDecay;
261
+            DiggSidebar.updateInterval = newUpdateInterval;
276 262
 
277
-            if (previousUpdateInterval == updateInterval)
278
-                DiggSidebar.updateIntervalDecay = updateIntervalDecay + 1;
263
+            if (previousUpdateInterval == newUpdateInterval)
264
+                DiggSidebar.updateIntervalDecay += 1;
279 265
             else
280 266
                 DiggSidebar.updateIntervalDecay = 0;
281 267
         }
282
-        window.clearTimeout(DiggSidebar.timerId);
283
-
284
-        var timeout = Math.round(DiggSidebar.updateInterval*(Math.pow(1.5, DiggSidebar.updateIntervalDecay)));
285
-        DiggSidebar.timerId = window.setTimeout(DiggSidebar.getStories, timeout);
286 268
         //Adaptive update interval code END
287 269
     },
288 270
 
@@ -299,7 +281,7 @@ DiggSidebar = {
299 281
             container: null,
300 282
             category: "all"
301 283
         };
302
-        parts = ep.split("/");
284
+        var parts = ep.split("/");
303 285
         if (parts.length == 2)
304 286
             tmp.category = parts[1];
305 287
         else if (parts.length == 4) {
@@ -334,7 +316,7 @@ DiggSidebar = {
334 316
         DiggSidebar.diggEndPoint = $('diggEndPoint');
335 317
         DiggSidebar.diggPlayPause = $('diggPlayPause');
336 318
 
337
-        DiggSidebar.storyListObserver = new StoryListObserver();
319
+        DiggSidebar.storyListObserver = new DiggSidebar.StoryListObserver();
338 320
         DiggSidebar.createMenu();
339 321
         DiggSidebar.getStories();
340 322
     },
@@ -362,12 +344,12 @@ DiggSidebar.Utils = {
362 344
     }
363 345
 }
364 346
 
365
-var StoryListObserver = function() {
347
+DiggSidebar.StoryListObserver = function() {
366 348
    this.registered = false;
367 349
    this.register();
368 350
 }
369 351
 
370
-StoryListObserver.prototype = {
352
+DiggSidebar.StoryListObserver.prototype = {
371 353
     observe: function(subject, topic, data) {
372 354
         if (topic == window.DiggSidebar.storyListRefreshEventTopic) {
373 355
             window.DiggSidebar.populateStoryList();

+ 1
- 1
chrome/content/diggsidebar.xul View File

@@ -58,5 +58,5 @@
58 58
     <description id="diggEndPoint">digg</description>
59 59
     <richlistbox flex="1"
60 60
                  id="storyList"
61
-                 onselect="DiggSidebar.getDescription(this.selectedItem.id.substr(6))" />
61
+                 onselect="DiggSidebar.showDescription(this.selectedItem.id.substr(6))" />
62 62
 </page>

+ 3
- 2
chrome/content/diggstory.xml View File

@@ -87,16 +87,17 @@
87 87
                 ]]>
88 88
                 </body>
89 89
             </method>
90
-            <method name="showDesc">
90
+            <method name="showDescription">
91 91
                 <body>
92 92
                 <![CDATA[
93 93
                     document.getAnonymousElementByAttribute(this, "class", "storyCEIcon").src =
94 94
                         "chrome://diggsidebar/content/image/down.jpg";
95 95
                     document.getAnonymousElementByAttribute(this, "class", "storyDetails").style.display = 'block';
96
+                    this.read = true;
96 97
                 ]]>
97 98
                 </body>
98 99
             </method>
99
-            <method name="hideDesc">
100
+            <method name="hideDescription">
100 101
                 <body>
101 102
                 <![CDATA[
102 103
                     document.getAnonymousElementByAttribute(this, "class", "storyCEIcon").src =

Loading…
Cancel
Save