Browse Source

refactoring and reformatting.

git-svn-id: file:///tmp/snv/trunk@22 12951d8a-c33d-4b7c-b961-822215c816
Abhinav Sarkar 10 years ago
parent
commit
8aff3915e8
4 changed files with 72 additions and 52 deletions
  1. 69
    49
      chrome/content/diggsidebar.js
  2. 1
    1
      chrome/content/diggstory.xbl
  3. 1
    1
      chrome/skin/diggsidebar.css
  4. 1
    1
      install.rdf

+ 69
- 49
chrome/content/diggsidebar.js View File

@@ -41,13 +41,15 @@ var DiggSidebar = {
41 41
     observerService: Components.classes["@mozilla.org/observer-service;1"]
42 42
         .getService(Components.interfaces.nsIObserverService),
43 43
 
44
+    $ce: function (tagName) {
45
+        return document.createElement(tagName);
46
+    },
44 47
 
45
-    fetchData: function (url, handler) {
46
-        DiggSidebar.indicateActivity();
47
-
48
+    fetchData: function (url, loadHandler, errorHandler) {
48 49
         var request = new XMLHttpRequest();
49 50
         request.onerror = DiggSidebar.errorHandler;
50
-        request.onload = handler;
51
+        request.onload = loadHandler;
52
+        request.onerror = errorHandler;
51 53
         request.open("GET", url, true);
52 54
         request.send(null);
53 55
     },
@@ -72,53 +74,56 @@ var DiggSidebar = {
72 74
         var containers = data.containers;
73 75
 
74 76
         containers.forEach(function (container) {
75
-            var cmenu = document.createElement("menu");
77
+            var cmenu = DiggSidebar.$ce("menu");
76 78
             with (cmenu) {
77 79
                 setAttribute("id", container.short_name + "Menu");
78 80
                 setAttribute("label", container.name);
79 81
                 setAttribute("accesskey", container.name.charAt(0));
80 82
             }
81 83
 
82
-            var cmenupopup = document.createElement("menupopup");
84
+            var cmenupopup = DiggSidebar.$ce("menupopup");
83 85
             cmenupopup.setAttribute("id", container.short_name + "Popup");
84 86
 
85 87
             DiggSidebar.categories.forEach(
86 88
                 function (category) {
87
-                    var cmenuitem = document.createElement("menuitem");
89
+                    var cmenuitem = DiggSidebar.$ce("menuitem");
88 90
                     with (cmenuitem) {
89 91
                         setAttribute("label", category);
90
-                        setAttribute("value", "/container/" + container.short_name + "/" + category.toLowerCase());
92
+                        setAttribute("value",
93
+                            "/container/" + container.short_name +
94
+                            "/" + category.toLowerCase());
91 95
                         setAttribute("accesskey", category.charAt(0));
92
-                        setAttribute("oncommand", "DiggSidebar.setEndPoint(this.value)");
96
+                        setAttribute("oncommand",
97
+                            "DiggSidebar.setEndPoint(this.value)");
93 98
                     }
94 99
                     cmenupopup.appendChild(cmenuitem);
95 100
                 }
96 101
             );
97
-            cmenupopup.appendChild(document.createElement("menuseparator"));
102
+            cmenupopup.appendChild(DiggSidebar.$ce("menuseparator"));
98 103
             var topics = container.topics;
99 104
             topics.forEach(function (topic) {
100
-                var tmenu = document.createElement("menu");
105
+                var tmenu = DiggSidebar.$ce("menu");
101 106
                 with (tmenu) {
102 107
                     setAttribute("id", topic.short_name + "Menu");
103 108
                     setAttribute("label", topic.name);
104 109
                     setAttribute("accesskey", topic.name.charAt(0));
105 110
                 }
106 111
 
107
-                var tmenupopup = document.createElement("menupopup");
112
+                var tmenupopup = DiggSidebar.$ce("menupopup");
108 113
                 tmenupopup.setAttribute("id", topic.short_name + "Popup");
109 114
 
110
-                DiggSidebar.categories.forEach(
111
-                    function (category) {
112
-                        var tmenuitem = document.createElement("menuitem");
113
-                        with (tmenuitem) {
114
-                            setAttribute("label", category);
115
-                            setAttribute("value", "/topic/" + topic.short_name + "/" + category.toLowerCase());
116
-                            setAttribute("accesskey", category.charAt(0));
117
-                            setAttribute("oncommand", "DiggSidebar.setEndPoint(this.value)");
118
-                        }
119
-                        tmenupopup.appendChild(tmenuitem);
115
+                DiggSidebar.categories.forEach(function (category) {
116
+                    var tmenuitem = DiggSidebar.$ce("menuitem");
117
+                    with (tmenuitem) {
118
+                        setAttribute("label", category);
119
+                        setAttribute("value", "/topic/" + topic.short_name +
120
+                            "/" + category.toLowerCase());
121
+                        setAttribute("accesskey", category.charAt(0));
122
+                        setAttribute("oncommand",
123
+                            "DiggSidebar.setEndPoint(this.value)");
120 124
                     }
121
-                );
125
+                    tmenupopup.appendChild(tmenuitem);
126
+                });
122 127
                 tmenu.appendChild(tmenupopup);
123 128
                 cmenupopup.appendChild(tmenu);
124 129
             });
@@ -128,16 +133,20 @@ var DiggSidebar = {
128 133
     },
129 134
 
130 135
     populateStoryList: function () {
136
+        var ep = DiggSidebar.prefs.get("endpoint").value || "";
137
+        DiggSidebar.UI.endPointDesc.value = "digg" + ep;
138
+
131 139
         var newStoryIds = [];
132 140
 
133 141
         var jp = new JPath(DiggSidebar.stories);
134 142
         var filteredStories = jp.$(function (story) {
135 143
             return (story.$("category").json == DiggSidebar.endpoint.category);
136 144
         }).json;
145
+
137 146
         jp = new JPath(filteredStories);
138 147
         if (DiggSidebar.endpoint.topic) {
139 148
             filteredStories = jp.$(function (story) {
140
-                return (story.$("topic/short_name").json == DiggSidebar.endpoint.topic);
149
+                return story.$("topic/short_name").json == DiggSidebar.endpoint.topic;
141 150
             }).json;
142 151
         }
143 152
         if (DiggSidebar.endpoint.container) {
@@ -170,7 +179,8 @@ var DiggSidebar = {
170 179
                     ((hr == 0) && (min == 0)) ?
171 180
                     "just now" : "";
172 181
 
173
-                var li = DiggSidebar.UI.storyListBox.appendChild(document.createElement("richlistitem"));
182
+                var li = DiggSidebar.UI.storyListBox.appendChild(
183
+                    DiggSidebar.$ce("richlistitem"));
174 184
                 li.id = "story_" + story.id;
175 185
                 var attributes = {
176 186
                     title: story.title,
@@ -221,23 +231,25 @@ var DiggSidebar = {
221 231
             DiggSidebar.refreshing = true;
222 232
             var ep = DiggSidebar.prefs.get("endpoint").value || "";
223 233
 
224
-            DiggSidebar.fetchData("http://services.digg.com/stories" + ep.replace(/\/all/g, "") +
225
-                "?count=30" + "&type=json" +
234
+            DiggSidebar.indicateActivity();
235
+            DiggSidebar.fetchData("http://services.digg.com/stories" +
236
+                ep.replace(/\/all/g, "") + "?count=30" + "&type=json" +
226 237
                 "&appkey=" + encodeURIComponent(DiggSidebar.homepageURL),
227
-                function (e) {
228
-                    DiggSidebar.UI.endPointDesc.value = "digg" + ep;
229 238
 
239
+                function (e) {
230 240
                     var stories = DiggSidebar.Utils.decodeJson(e.target.responseText).stories;
231
-                    var newStories = stories.filter(
232
-                        function (story) {
233
-                            return !(DiggSidebar.storyIds.indexOf(story.id) != -1);
234
-                        });
241
+                    stories.forEach(function (story) { story.id = parseInt(story.id); });
242
+                    
243
+                    var newStories = stories.filter(function (story) {
244
+                        return DiggSidebar.storyIds.indexOf(story.id) == -1;
245
+                    });
235 246
                     newStories.forEach(function (story) {
236 247
                         story.category = DiggSidebar.endpoint.category;
237 248
                         story.read = false;
238 249
                     });
239 250
                     DiggSidebar.stories = newStories.concat(DiggSidebar.stories);
240
-                    DiggSidebar.observerService.notifyObservers(null, DiggSidebar.storyListRefreshEventTopic, null);
251
+                    DiggSidebar.observerService.notifyObservers(
252
+                        null, DiggSidebar.storyListRefreshEventTopic, null);
241 253
 
242 254
                     DiggSidebar.refreshing = false;
243 255
                     DiggSidebar.setUpdateInterval(stories);
@@ -247,16 +259,18 @@ var DiggSidebar = {
247 259
                     DiggSidebar.timerId = window.setTimeout(DiggSidebar.getStories, timeout);
248 260
 
249 261
                     DiggSidebar.indicateInactivity();
250
-                });
262
+                },
263
+                DiggSidebar.errorHandler);
251 264
         } else {
252 265
             window.setTimeout(DiggSidebar.getStories, 1000);
253 266
         }
254 267
     },
255 268
 
256 269
     createMenu: function () {
270
+        DiggSidebar.indicateActivity();
257 271
         DiggSidebar.fetchData("http://services.digg.com/containers" + "?type=json" +
258 272
             "&appkey=" + encodeURIComponent(DiggSidebar.homepageURL),
259
-            DiggSidebar.populateMenu);
273
+            DiggSidebar.populateMenu, DiggSidebar.errorHandler);
260 274
     },
261 275
 
262 276
     setUpdateInterval: function (stories) {
@@ -302,7 +316,8 @@ var DiggSidebar = {
302 316
     },
303 317
 
304 318
     calculateTimeout: function () {
305
-        return Math.round(DiggSidebar.updateInterval * (Math.pow(1.5, DiggSidebar.updateIntervalDecay)));
319
+        return Math.round(
320
+            DiggSidebar.updateInterval * (Math.pow(1.5, DiggSidebar.updateIntervalDecay)));
306 321
     },
307 322
 
308 323
     setEndPoint: function (ep) {
@@ -343,13 +358,14 @@ var DiggSidebar = {
343 358
             disabled = false;
344 359
             onmouseover = function () {
345 360
                 var autoRefreshTime = Math.round(
346
-                    (DiggSidebar.lastUpdateAt + DiggSidebar.calculateTimeout() - new Date().getTime()) / 1000
347
-                );
361
+                    (DiggSidebar.lastUpdateAt +
362
+                     DiggSidebar.calculateTimeout() - new Date().getTime()) / 1000);
363
+
348 364
                 autoRefreshTime = (autoRefreshTime > 3600) ?
349
-                                        Math.round(autoRefreshTime / 3600) + " hours":
350
-                                        (autoRefreshTime > 60) ?
351
-                                            Math.round(autoRefreshTime / 60) + " minutes":
352
-                                            autoRefreshTime + " seconds";
365
+                    Math.round(autoRefreshTime / 3600) + " hours":
366
+                    (autoRefreshTime > 60) ?
367
+                        Math.round(autoRefreshTime / 60) + " minutes":
368
+                        autoRefreshTime + " seconds";
353 369
                 setAttribute("tooltiptext", "Click to refresh now.\n" +
354 370
                     "Autorefreshing in " + autoRefreshTime);
355 371
             };
@@ -358,7 +374,8 @@ var DiggSidebar = {
358 374
     },
359 375
 
360 376
     initialize: function () {
361
-        DiggSidebar.endpoint = DiggSidebar.getEndpointParts(DiggSidebar.prefs.get("endpoint").value);
377
+        DiggSidebar.endpoint = DiggSidebar.getEndpointParts(
378
+            DiggSidebar.prefs.get("endpoint").value);
362 379
         var $ = function (id) {
363 380
             return document.getElementById(id);
364 381
         };
@@ -371,12 +388,14 @@ var DiggSidebar = {
371 388
         DiggSidebar.UI.endPointDesc = $("dsEndPointDesc");
372 389
 
373 390
         DiggSidebar.storyListObserver = new DiggSidebar.StoryListObserver();
391
+        DiggSidebar.storyListObserver.register();
374 392
         DiggSidebar.createMenu();
375 393
         DiggSidebar.getStories();
376 394
     },
377 395
 
378 396
     destroy: function () {
379 397
         DiggSidebar.storyListObserver.unregister();
398
+        DiggSidebar.stories = [];
380 399
     }
381 400
 };
382 401
 
@@ -406,26 +425,27 @@ DiggSidebar.Utils = {
406 425
 
407 426
 DiggSidebar.StoryListObserver = function () {
408 427
     this.registered = false;
409
-    this.register();
410 428
 };
411 429
 
412 430
 DiggSidebar.StoryListObserver.prototype = {
413 431
     observe: function (subject, topic, data) {
414
-        if (topic == window.DiggSidebar.storyListRefreshEventTopic) {
415
-            window.DiggSidebar.populateStoryList();
432
+        if (topic == DiggSidebar.storyListRefreshEventTopic) {
433
+            DiggSidebar.populateStoryList();
416 434
         }
417 435
     },
418 436
 
419 437
     register: function () {
420 438
         if (!this.registered) {
421
-            DiggSidebar.observerService.addObserver(this, DiggSidebar.storyListRefreshEventTopic, false);
439
+            DiggSidebar.observerService.addObserver(
440
+                this, DiggSidebar.storyListRefreshEventTopic, false);
422 441
             this.registered = true;
423 442
         }
424 443
     },
425 444
 
426 445
     unregister: function () {
427 446
         if (this.registered) {
428
-            DiggSidebar.observerService.removeObserver(this, DiggSidebar.storyListRefreshEventTopic);
447
+            DiggSidebar.observerService.removeObserver(
448
+                this, DiggSidebar.storyListRefreshEventTopic);
429 449
             this.registered = false;
430 450
         }
431 451
     }

chrome/content/diggstory.xml → chrome/content/diggstory.xbl View File

@@ -76,7 +76,7 @@
76 76
             </constructor>
77 77
             <property name="read"
78 78
                 onget="return this.d.read;"
79
-                onset="this.d.read = val; if(val==true) this.markAsRead();"/>
79
+                onset="this.d.read = val; if(val) this.markAsRead();"/>
80 80
             <method name="markAsRead">
81 81
                 <body>
82 82
                 <![CDATA[

+ 1
- 1
chrome/skin/diggsidebar.css View File

@@ -73,7 +73,7 @@ a img {
73 73
     border: 2px solid #E5ECF3;
74 74
     border-top: none;
75 75
     color: #000000!important;
76
-    -moz-binding: url("chrome://diggsidebar/content/diggstory.xml#diggstoryitem");
76
+    -moz-binding: url("chrome://diggsidebar/content/diggstory.xbl#diggstoryitem");
77 77
 }
78 78
 
79 79
 richlistitem[selected="true"] {

+ 1
- 1
install.rdf View File

@@ -24,7 +24,7 @@
24 24
   <RDF:Description RDF:about="rdf:#$55wJ23"
25 25
                    em:id="{ec8030f7-c20a-464f-9b0e-13a3a9e97384}"
26 26
                    em:minVersion="3.0"
27
-                   em:maxVersion="3.5.*" />
27
+                   em:maxVersion="3.6alpha1" />
28 28
   <RDF:Description RDF:about="urn:mozilla:install-manifest"
29 29
                    em:id="diggsidebar@abhinavsarkar.net"
30 30
                    em:name="Digg Sidebar"

Loading…
Cancel
Save