Browse Source

refactored Javascript and make related changes in other files

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

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

@@ -1,7 +1,6 @@
1 1
 move to JSON data format -- done
2 2
 XBL -- done
3
-externalize strings
4
-format JS/refactor JS/Use FUEL
3
+format JS/refactor JS/Use FUEL -- done
5 4
 put license
6 5
 caching/db, threading, XUL templates
7 6
 new UI

+ 274
- 286
chrome/content/diggsidebar.js View File

@@ -1,307 +1,295 @@
1
-var gsti;
2
-var currentStories = new Array();
3
-var playing = true;
4
-
5
-//getElementById
6
-function $ei(id, parentNode) {
7
-	parentNode = parentNode || document;
8
-	return parentNode.getElementById(id);
9
-}
10
-
11
-function fetchData(url, handler) {
12
-	//alert("inside fetchData");
13
-        $ei('diggIndicator').style.display = '';
14
-        $ei("diggIndicator").setAttribute('mode', 'undetermined');
15
-	var request = Components.
16
-              classes["@mozilla.org/xmlextras/xmlhttprequest;1"].
17
-              createInstance();
18
-
19
-	// QI the object to nsIDOMEventTarget to set event handlers on it:
20
-
21
-	request.QueryInterface(Components.interfaces.nsIDOMEventTarget);
22
-	request.addEventListener("load", handler, false);
23
-        request.addEventListener("error", handleError, false);
24
-
25
-	// QI it to nsIXMLHttpRequest to open and send the request:
26
-
27
-	request.QueryInterface(Components.interfaces.nsIXMLHttpRequest);
28
-	request.open("GET", url, true);
29
-	request.send(null);
30
-        //$ei('diggIndicator').style.display = '';
31
-
32
-}
33
-
34
-function handleError(e) {
35
-        $ei('storyList').appendItem("!! Error accessing Digg !!");
36
-        //$ei('diggIndicator').style.display = 'none';
37
-}
38
-
39
-function populateMenu(e) {
40
-    XHR = e.target;
41
-	data = decodeJson(XHR.responseText);
42
-    topics = data.topics;
43
-    containers = new Array();
1
+var DiggSidebar = {
2
+    prefs: Application.extensions.get("diggsidebar@abhinavsarkar.net").prefs,
3
+    categories: ['All', 'Popular', 'Upcoming', 'Hot', 'Top'],
4
+    timerId : null,
5
+    currentStories : new Array(),
6
+    playing : true,
7
+
8
+    fetchData: function(url, handler) {
9
+        DiggSidebar.diggIndicator.style.display = '';
10
+
11
+        var request = new XMLHttpRequest();
12
+        request.onerror = DiggSidebar.errorHandler;
13
+        request.onload = handler;
14
+        request.open("GET", url, true);
15
+        request.send(null);
16
+    },
17
+
18
+    errorHandler: function(e) {
19
+        window.alert("Error in accessing data from Digg");
20
+    },
21
+
22
+    populateMenu: function(e) {
23
+        var XHR = e.target;
24
+        var data = DiggSidebar.Utils.decodeJson(XHR.responseText);
25
+        var topics = data.topics;
26
+        var containers = new Array();
27
+
28
+        while (DiggSidebar.topicPopup.firstChild) {
29
+                DiggSidebar.topicPopup.removeChild(DiggSidebar.topicPopup.firstChild);
30
+        }
44 31
 
45
-    while ($ei('topicPopup').firstChild) {
46
-            $ei('topicPopup').removeChild($ei('topicPopup').firstChild);
47
-    }
32
+        topics.forEach(function (topic) {
33
+            var menu = document.createElement('menu');
34
+            menu.setAttribute('id', topic.short_name + 'Menu');
35
+            menu.setAttribute('label', topic.name);
36
+            menu.setAttribute('accesskey', topic.name.charAt(0));
37
+
38
+            var menupopup = document.createElement('menupopup');
39
+            menupopup.setAttribute('id', topic.short_name + 'Popup');
40
+
41
+            DiggSidebar.categories.forEach(
42
+                function (label) {
43
+                    var menuitem = document.createElement('menuitem');
44
+                    menuitem.setAttribute('label', label);
45
+                    menuitem.setAttribute('value', '/topic/' + topic.short_name + '/' + label.toLowerCase());
46
+                    menuitem.setAttribute('accesskey', label.charAt(0));
47
+                    menuitem.setAttribute('oncommand', "DiggSidebar.setEndPoint(this.value)");
48
+                    menupopup.appendChild(menuitem);
49
+                }
50
+            );
51
+            menu.appendChild(menupopup);
52
+            DiggSidebar.topicPopup.appendChild(menu);
53
+
54
+            containers.push(topic.container);
55
+        });
56
+
57
+        while (DiggSidebar.containerPopup.firstChild) {
58
+                DiggSidebar.containerPopup.removeChild(DiggSidebar.containerPopup.firstChild);
59
+        }
48 60
 
49
-    topics.forEach(function (topic) {
50
-        menu = document.createElement('menu');
51
-        menu.setAttribute('id', topic.short_name + 'Menu');
52
-        menu.setAttribute('label', topic.name);
53
-        menu.setAttribute('accesskey', topic.name.charAt(0));
54
-
55
-        menupopup = document.createElement('menupopup');
56
-        menupopup.setAttribute('id', topic.short_name + 'Popup');
57
-
58
-        ['All', 'Popular', 'Upcoming', 'Hot', 'Top'].forEach(
59
-            function (label) {
60
-                menuitem = document.createElement('menuitem');
61
-                menuitem.setAttribute('label', label);
62
-                menuitem.setAttribute('value', '/topic/' + topic.short_name + '/' + label.toLowerCase());
63
-                menuitem.setAttribute('accesskey', label.charAt(0));
64
-                menuitem.setAttribute('oncommand', "setEndPoint(this.value)");
65
-                menupopup.appendChild(menuitem);
61
+        var filteredContainers = {name: [], short_name: []};
62
+        containers.forEach(function (container) {
63
+            var name = container.name;
64
+            var short_name = container.short_name;
65
+            if (filteredContainers.name.indexOf(name) == -1) {
66
+                filteredContainers.name.push(name);
67
+                filteredContainers.short_name.push(short_name);
66 68
             }
67
-        );
68
-        menu.appendChild(menupopup);
69
-        $ei('topicPopup').appendChild(menu);
70
-
71
-        containers.push(topic.container);
72
-    });
73
-
74
-    while ($ei('containerPopup').firstChild) {
75
-            $ei('containerPopup').removeChild($ei('containerPopup').firstChild);
76
-    }
77
-
78
-    filteredContainers = {name: [], short_name: []};
79
-    containers.forEach(function (container) {
80
-        name = container.name;
81
-        short_name = container.short_name;
82
-        if (filteredContainers.name.indexOf(name) == -1) {
83
-            filteredContainers.name.push(name);
84
-            filteredContainers.short_name.push(short_name);
69
+        });
70
+
71
+        for (var i=0; i<filteredContainers.name.length; i++) {
72
+            var menu = document.createElement('menu');
73
+            menu.setAttribute('id', filteredContainers.short_name[i] + 'Menu');
74
+            menu.setAttribute('label', filteredContainers.name[i]);
75
+            menu.setAttribute('accesskey', filteredContainers.name[i].charAt(0));
76
+
77
+            var menupopup = document.createElement('menupopup');
78
+            menupopup.setAttribute('id', filteredContainers.short_name[i] + 'Popup');
79
+
80
+            DiggSidebar.categories.forEach(
81
+                function (label) {
82
+                    var menuitem = document.createElement('menuitem');
83
+                    menuitem.setAttribute('label', label);
84
+                    menuitem.setAttribute('value', '/container/' +
85
+                        filteredContainers.short_name[i] + '/' + label.toLowerCase());
86
+                    menuitem.setAttribute('accesskey', label.charAt(0));
87
+                    menuitem.setAttribute('oncommand', "DiggSidebar.setEndPoint(this.value)");
88
+                    menupopup.appendChild(menuitem);
89
+                }
90
+            );
91
+
92
+            menu.appendChild(menupopup);
93
+            DiggSidebar.containerPopup.appendChild(menu);
85 94
         }
86
-    });
87
-
88
-    for (i=0; i<filteredContainers.name.length; i++) {
89
-        menu = document.createElement('menu');
90
-        menu.setAttribute('id', filteredContainers.short_name[i] + 'Menu');
91
-        menu.setAttribute('label', filteredContainers.name[i]);
92
-        menu.setAttribute('accesskey', filteredContainers.name[i].charAt(0));
93
-
94
-        menupopup = document.createElement('menupopup');
95
-        menupopup.setAttribute('id', filteredContainers.short_name[i] + 'Popup');
96
-
97
-        ['All', 'Popular', 'Upcoming', 'Hot', 'Top'].forEach(
98
-            function (label) {
99
-                menuitem = document.createElement('menuitem');
100
-                menuitem.setAttribute('label', label);
101
-                menuitem.setAttribute('value', '/container/' +
102
-                    filteredContainers.short_name[i] + '/' + label.toLowerCase());
103
-                menuitem.setAttribute('accesskey', label.charAt(0));
104
-                menuitem.setAttribute('oncommand', "setEndPoint(this.value)");
105
-                menupopup.appendChild(menuitem);
106
-            }
107
-        );
95
+        DiggSidebar.diggIndicator.style.display = 'none';
96
+    },
108 97
 
109
-        menu.appendChild(menupopup);
110
-        $ei('containerPopup').appendChild(menu);
111
-    }
112
-    $ei('diggIndicator').style.display = 'none';
113
-}
114
-
115
-function populateStoryList(e) {
116
-	XHR = e.target;
117
-	//Application.console.log(XHR.responseText);
118
-	data = decodeJson(XHR.responseText);
119
-	stories = data.stories;
120
-
121
-    while ($ei('storyList').firstChild) {
122
-        $ei('storyList').removeChild($ei('storyList').firstChild);
123
-    }
124
-
125
-    var newStories = new Array();
126
-    //Application.console.log(stories.length);
127
-
128
-	stories.forEach(function (story) {
129
-        newStories.push(story.id);
130
-		//Application.console.log(i + " " + story.title + " " + story.id);
131
-
132
-        listitem = $ei('storyList').appendChild(document.createElement('richlistitem'));
133
-        listitem.id = "story_" + story.id;
134
-        listitem.setAttribute('title', story.title);
135
-
136
-        if (currentStories.indexOf(story.id) != -1) listitem.new = false;
137
-    });
138
-    currentStories = newStories;
139
-
140
-    //Adaptive update interval code START
141
-    sum = 0;
142
-    weights = [4, 3, 2, 1]
143
-    date = (stories[0].promote_date != null) ? 'promote_date' : 'submit_date';
144
-    stories.sort(function (a, b) {
145
-        if (a[date] > b[date]) return -1;
146
-        else if (a[date] < b[date]) return 1;
147
-        return 0;
148
-    });
149
-    for (i=0; i<Math.min(4, stories.length-1); i++) {
150
-        diff = stories[i][date] - stories[i+1][date];
151
-        sum += weights[i]*diff;
152
-    }
153
-    updateInterval = Math.round(
154
-        sum*1000/weights
155
-        .splice(0,Math.min(4, stories.length-1))
156
-        .reduce(function(a, b){return a + b;})
157
-    );
158
-    //Application.console.log(updateInterval);
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;
159 103
 
160
-    var prefs = Application.extensions.get("diggsidebar@abhinavsarkar.net").prefs;
161
-
162
-    if (updateInterval > 0) {
163
-        previousUpdateInterval = prefs.get("updateinterval").value;
164
-        prefs.get("updateinterval").value = updateInterval;
165
-
166
-        updateIntervalDecay = prefs.get("updateintervaldecay").value;
104
+        while (DiggSidebar.storyList.firstChild) {
105
+            DiggSidebar.storyList.removeChild(DiggSidebar.storyList.firstChild);
106
+        }
167 107
 
168
-        if (previousUpdateInterval == updateInterval)
169
-            prefs.get("updateintervaldecay").value = updateIntervalDecay + 1;
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;
120
+        });
121
+        DiggSidebar.currentStories = newStories;
122
+        DiggSidebar.setUpdateInterval(stories);
123
+        DiggSidebar.diggIndicator.style.display = 'none';
124
+    },
125
+
126
+    populateDescription: function(e) {
127
+        var XHR = e.target;
128
+        //Application.console.log(XHR.responseText);
129
+        var data = DiggSidebar.Utils.decodeJson(XHR.responseText);
130
+        var story = data.stories[0];
131
+
132
+        var now = new Date();
133
+        if (story.promote_date != null)
134
+            var then = new Date(story.promote_date*1000);
170 135
         else
171
-            prefs.get("updateintervaldecay").value = 0;
172
-    }
173
-    window.clearTimeout(gsti);
174
-
175
-    updateInterval = prefs.get("updateinterval").value;
176
-    updateIntervalDecay = prefs.get("updateintervaldecay").value;
177
-
178
-    timeout = Math.round(updateInterval*(Math.pow(1.5, updateIntervalDecay)));
179
-    gsti = window.setTimeout(getStories, timeout);
180
-    //Adaptive update interval code END
181
-
182
-    $ei('diggIndicator').style.display = 'none';
183
-}
184
-
185
-function populateDescription(e) {
186
-	XHR = e.target;
187
-    //Application.console.log(XHR.responseText);
188
-	data = decodeJson(XHR.responseText);
189
-	story = data.stories[0];
190
-
191
-    now = new Date();
192
-    if (story.promote_date != null)
193
-        then = new Date(story.promote_date*1000);
194
-    else
195
-        then = new Date(story.submit_date*1000);
196
-    diff = Math.max(now - then, 0)
197
-
198
-    hr = Math.floor(diff/(1000*3600));
199
-    min = Math.floor(diff/(1000*60)) - 60*hr;
200
-
201
-    listitems = document.getElementsByTagName('richlistitem');
202
-    for (i=0; i<listitems.length; i++) {
203
-            listitems[i].hideDesc();
204
-    }
136
+            var then = new Date(story.submit_date*1000);
137
+        var diff = Math.max(now - then, 0)
205 138
 
206
-    li = $ei('story_'+ story.id)
207
-
208
-	storyDate = ((hr > 0) && (min > 0)) ?
209
-        (hr + " hr " + min + " mins ago") :
210
-        ((hr == 0) && (min > 0)) ?
211
-        (min + " mins ago") :
212
-        ((hr == 0) && (min == 0)) ?
213
-        "just now" : "";
214
-    li.setAttribute('date', storyDate);
215
-	li.setAttribute('status', story.status);
216
-	li.setAttribute('container', story.container.name);
217
-	li.setAttribute('topic', story.topic.name);
218
-	li.setAttribute('username', story.user.name);
219
-    li.setAttribute('userlink', "http://digg.com/users/" + story.user.name);
220
-	li.setAttribute('diggs', story.diggs);
221
-	li.setAttribute('comments', story.comments);
222
-	li.setAttribute('desc', story.description);
223
-	li.setAttribute('link', story.link);
224
-	li.setAttribute('href',  story.href);
225
-
226
-    li.read = true;
227
-    li.new = false;
228
-    li.showDesc()
229
-
230
-    $ei('diggIndicator').style.display = 'none';
231
-}
232
-
233
-function getStories() {
234
-    var prefs = Application.extensions.get("diggsidebar@abhinavsarkar.net").prefs;
235
-    ep = prefs.get("endpoint").value || '';
236
-    $ei('diggEndPoint').value = "digg" + ep;
237
-
238
-    fetchData("http://services.digg.com/stories" + ep.replace(/\/all/g, '') +
239
-        "?count=30" + "&type=json" +
240
-		"&appkey=" + encodeURIComponent("http://diggsidebar.googlepages.com"),
241
-		populateStoryList);
242
-    //gsti = window.setTimeout(getStories,          prefs.getIntPref("extensions.diggsidebar.updateinterval"));
243
-}
139
+        var hr = Math.floor(diff/(1000*3600));
140
+        var min = Math.floor(diff/(1000*60)) - 60*hr;
244 141
 
245
-function getDescription(storyId) {
246
-    if (storyId == null) return;
247
-	fetchData("http://services.digg.com/story/" + storyId + "?type=json" +
248
-		"&appkey=" + encodeURIComponent("http://diggsidebar.googlepages.com"),
249
-		populateDescription);
250
-}
142
+        var listitems = document.getElementsByTagName('richlistitem');
143
+        for (var i=0; i<listitems.length; i++) {
144
+                listitems[i].hideDesc();
145
+        }
251 146
 
252
-function createMenu() {
253
-        fetchData("http://services.digg.com/topics" + "?type=json" +
254
-		"&appkey=" + encodeURIComponent("http://diggsidebar.googlepages.com"),
255
-                populateMenu);
256
-}
147
+        var li = document.getElementById('story_'+ story.id)
148
+
149
+        var relativeTime = ((hr > 0) && (min > 0)) ?
150
+            (hr + " hr " + min + " mins ago") :
151
+            ((hr == 0) && (min > 0)) ?
152
+            (min + " mins ago") :
153
+            ((hr == 0) && (min == 0)) ?
154
+            "just now" : "";
155
+        li.setAttribute('date', relativeTime);
156
+        li.setAttribute('status', story.status);
157
+        li.setAttribute('container', story.container.name);
158
+        li.setAttribute('topic', story.topic.name);
159
+        li.setAttribute('username', story.user.name);
160
+        li.setAttribute('userlink', "http://digg.com/users/" + story.user.name);
161
+        li.setAttribute('diggs', story.diggs);
162
+        li.setAttribute('comments', story.comments);
163
+        li.setAttribute('desc', story.description);
164
+        li.setAttribute('link', story.link);
165
+        li.setAttribute('href',  story.href);
166
+
167
+        li.read = true;
168
+        li.new = false;
169
+        li.showDesc()
170
+
171
+        DiggSidebar.diggIndicator.style.display = 'none';
172
+    },
173
+
174
+    getStories: function() {
175
+        var ep = DiggSidebar.prefs.get("endpoint").value || '';
176
+        DiggSidebar.diggEndPoint.value = "digg" + ep;
177
+
178
+        DiggSidebar.fetchData("http://services.digg.com/stories" + ep.replace(/\/all/g, '') +
179
+            "?count=30" + "&type=json" +
180
+            "&appkey=" + encodeURIComponent("http://diggsidebar.googlepages.com"),
181
+            DiggSidebar.populateStoryList);
182
+    },
183
+
184
+    getDescription: function(storyId) {
185
+        if (storyId == null) return;
186
+        DiggSidebar.fetchData("http://services.digg.com/story/" + storyId + "?type=json" +
187
+            "&appkey=" + encodeURIComponent("http://diggsidebar.googlepages.com"),
188
+            DiggSidebar.populateDescription);
189
+    },
190
+
191
+    createMenu: function() {
192
+        DiggSidebar.fetchData("http://services.digg.com/topics" + "?type=json" +
193
+            "&appkey=" + encodeURIComponent("http://diggsidebar.googlepages.com"),
194
+            DiggSidebar.populateMenu);
195
+    },
196
+
197
+    setUpdateInterval: function(stories) {
198
+        //Adaptive update interval code START
199
+        var sum = 0;
200
+        var weights = [4, 3, 2, 1]
201
+        var date = (stories[0].promote_date != null) ? 'promote_date' : 'submit_date';
202
+        stories.sort(function (a, b) {
203
+            if (a[date] > b[date]) return -1;
204
+            else if (a[date] < b[date]) return 1;
205
+            return 0;
206
+        });
207
+        for (var i=0; i<Math.min(4, stories.length-1); i++) {
208
+            var diff = stories[i][date] - stories[i+1][date];
209
+            sum += weights[i]*diff;
210
+        }
211
+        var updateInterval = Math.round(
212
+            sum*1000/weights
213
+            .splice(0,Math.min(4, stories.length-1))
214
+            .reduce(function(a, b){return a + b;})
215
+        );
216
+        //Application.console.log(updateInterval);
257 217
 
258
-function openInTab(href) {
259
-        var mainWindow = window.QueryInterface(Components.interfaces.nsIInterfaceRequestor)
260
-                   .getInterface(Components.interfaces.nsIWebNavigation)
261
-                   .QueryInterface(Components.interfaces.nsIDocShellTreeItem)
262
-                   .rootTreeItem
263
-                   .QueryInterface(Components.interfaces.nsIInterfaceRequestor)
264
-                   .getInterface(Components.interfaces.nsIDOMWindow);
265
-        mainWindow.getBrowser().selectedTab = mainWindow.getBrowser().addTab(href);
266
-}
218
+        if (updateInterval > 0) {
219
+            var previousUpdateInterval = DiggSidebar.prefs.get("updateinterval").value;
220
+            DiggSidebar.prefs.get("updateinterval").value = updateInterval;
267 221
 
268
-function setEndPoint(ep) {
269
-        var prefs = Application.extensions.get("diggsidebar@abhinavsarkar.net").prefs;
270
-        prefs.get("endpoint").value = ep;
271
-        window.clearTimeout(gsti);
272
-        //window.clearInterval(psi);
273
-        getStories();
274
-}
222
+            var updateIntervalDecay = DiggSidebar.prefs.get("updateintervaldecay").value;
275 223
 
276
-function togglePlayPause() {
277
-        if (playing) {
278
-               window.clearTimeout(gsti);
279
-               //window.clearInterval(psi);
280
-               $ei('diggPlayPause').image = "chrome://diggsidebar/content/image/Play.png";
281
-               $ei('diggPlayPause').setAttribute("tooltiptext", "Click to Start autoupdate");
282
-               playing = false;
224
+            if (previousUpdateInterval == updateInterval)
225
+                DiggSidebar.prefs.get("updateintervaldecay").value = updateIntervalDecay + 1;
226
+            else
227
+                DiggSidebar.prefs.get("updateintervaldecay").value = 0;
228
+        }
229
+        window.clearTimeout(DiggSidebar.timerId);
230
+
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)));
235
+        DiggSidebar.timerId = window.setTimeout(DiggSidebar.getStories, timeout);
236
+        //Adaptive update interval code END
237
+    },
238
+
239
+    setEndPoint: function(ep) {
240
+        DiggSidebar.prefs.get("endpoint").value = ep;
241
+        window.clearTimeout(DiggSidebar.timerId);
242
+        DiggSidebar.getStories();
243
+    },
244
+
245
+    togglePlayPause: function() {
246
+        if (DiggSidebar.playing) {
247
+            window.clearTimeout(DiggSidebar.timerId);
248
+            DiggSidebar.diggPlayPause.image = "chrome://diggsidebar/content/image/Play.png";
249
+            DiggSidebar.diggPlayPause.setAttribute("tooltiptext", "Click to Start autoupdate");
250
+            DiggSidebar.playing = false;
283 251
         } else {
284
-                var prefs = Application.extensions.get("diggsidebar@abhinavsarkar.net").prefs;
285
-                updateInterval = prefs.get("extensions.diggsidebar.updateinterval").value;
286
-                updateIntervalDecay = prefs.get("extensions.diggsidebar.updateintervaldecay").value;
287
-
288
-                timeout = Math.round(updateInterval*(Math.pow(1.5, updateIntervalDecay)));
289
-                //newTimeout = Math.round(timeout*parseInt($ei("diggIndicator").getAttribute('value'))/100);
290
-                gsti = window.setTimeout(getStories, timeout);
291
-                //psi = window.setInterval(showProgress, Math.round(timeout/100));
292
-                //window.setTimeout("window.clearInterval(psi)", newTimeout);
293
-                $ei('diggPlayPause').image = "chrome://diggsidebar/content/image/Pause.png";
294
-                $ei('diggPlayPause').setAttribute("tooltiptext", "Click to Pause autoupdate");
295
-                playing = true;
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)));
256
+            DiggSidebar.timerId = window.setTimeout(DiggSidebar.getStories, timeout);
257
+            DiggSidebar.diggPlayPause.image = "chrome://diggsidebar/content/image/Pause.png";
258
+            DiggSidebar.diggPlayPause.setAttribute("tooltiptext", "Click to Pause autoupdate");
259
+            DiggSidebar.playing = true;
296 260
         }
297
-}
298
-
299
-function decodeJson(string) {
300
-  var json = Components.classes["@mozilla.org/dom/json;1"]
301
-             .createInstance(Components.interfaces.nsIJSON);
302
-  return json.decode(string);
261
+    },
262
+
263
+    initialize: function(){
264
+        var $ = function(id) {return document.getElementById(id)};
265
+        DiggSidebar.diggIndicator = $('diggIndicator');
266
+        DiggSidebar.topicPopup = $('topicPopup');
267
+        DiggSidebar.containerPopup = $('containerPopup');
268
+        DiggSidebar.storyList = $('storyList');
269
+        DiggSidebar.diggEndPoint = $('diggEndPoint');
270
+        DiggSidebar.diggPlayPause = $('diggPlayPause');
271
+        DiggSidebar.createMenu();
272
+        DiggSidebar.getStories();
273
+    }
303 274
 };
304 275
 
276
+DiggSidebar.Utils = {
277
+    url: function(spec) {
278
+        var ios = Components.classes["@mozilla.org/network/io-service;1"]
279
+                    .getService(Components.interfaces.nsIIOService);
280
+        return ios.newURI(spec, null, null);
281
+    },
282
+
283
+    openUrlInTab: function(url) {
284
+        Application.activeWindow.open(DiggSidebar.Utils.url(url));
285
+    },
286
+
287
+    decodeJson: function(string) {
288
+        var json = Components.classes["@mozilla.org/dom/json;1"]
289
+            .createInstance(Components.interfaces.nsIJSON);
290
+        return json.decode(string);
291
+    }
292
+}
305 293
 /*TODO
306 294
  preferences
307 295
  toolbar button

+ 23
- 9
chrome/content/diggsidebar.xul View File

@@ -6,7 +6,7 @@
6 6
 
7 7
 <page id="sbDiggSidebar" title="&diggsidebar.title;"
8 8
     xmlns="http://www.mozilla.org/keymaster/gatekeeper/there.is.only.xul"
9
-    onload="createMenu();getStories()">
9
+    onload="DiggSidebar.initialize()">
10 10
     <script src="chrome://diggsidebar/content/diggsidebar.js" type="application/x-javascript" />
11 11
     <hbox>
12 12
         <toolbar flex="1">
@@ -14,11 +14,16 @@
14 14
                 <menubar id="diggsidebarMenubar" flex="1" style="width: 100%">
15 15
                     <menu id="storiesMenu" label="Stories" accesskey="S">
16 16
                         <menupopup id="storiesPopup">
17
-                            <menuitem label="All" value="/all" oncommand="setEndPoint(this.value)" accesskey="A" />
18
-                            <menuitem label="Popular" value="/popular" oncommand="setEndPoint(this.value)" accesskey="P" />
19
-                            <menuitem label="Upcoming" value="/upcoming" oncommand="setEndPoint(this.value)" accesskey="U" />
20
-                            <menuitem label="Hot" value="/hot" oncommand="setEndPoint(this.value)" accesskey="H" />
21
-                            <menuitem label="Top" value="/top" oncommand="setEndPoint(this.value)" accesskey="T" />
17
+                            <menuitem label="All" value="/all"
18
+                                      oncommand="DiggSidebar.setEndPoint(this.value)" accesskey="A" />
19
+                            <menuitem label="Popular" value="/popular"
20
+                                      oncommand="DiggSidebar.setEndPoint(this.value)" accesskey="P" />
21
+                            <menuitem label="Upcoming" value="/upcoming"
22
+                                      oncommand="DiggSidebar.setEndPoint(this.value)" accesskey="U" />
23
+                            <menuitem label="Hot" value="/hot"
24
+                                      oncommand="DiggSidebar.setEndPoint(this.value)" accesskey="H" />
25
+                            <menuitem label="Top" value="/top"
26
+                                      oncommand="DiggSidebar.setEndPoint(this.value)" accesskey="T" />
22 27
                             <menuseparator />
23 28
                             <menu id="containerMenu" label="Containers" accesskey="C">
24 29
                                 <menupopup id="containerPopup">
@@ -34,13 +39,22 @@
34 39
                     </menu>
35 40
                 </menubar>
36 41
             </toolbaritem>
37
-            <toolbarbutton id="diggPlayPause" image="chrome://diggsidebar/content/image/Pause.png" tooltiptext="Click to Pause autoupdate" oncommand="togglePlayPause()" />
42
+            <toolbarbutton id="diggPlayPause"
43
+                           image="chrome://diggsidebar/content/image/Pause.png"
44
+                           tooltiptext="Click to Pause autoupdate"
45
+                           oncommand="DiggSidebar.togglePlayPause()" />
38 46
             <toolbarspacer flex="5" />
39 47
             <toolbaritem>
40
-                <progressmeter flex="1" width="5" mode="undetermined" id="diggIndicator" style="height: 85% !important; width: 30% !important" />
48
+                <progressmeter flex="1"
49
+                               width="5"
50
+                               mode="undetermined"
51
+                               id="diggIndicator"
52
+                               style="height: 85% !important; width: 30% !important" />
41 53
             </toolbaritem>
42 54
         </toolbar>
43 55
     </hbox>
44 56
     <description id="diggEndPoint">digg</description>
45
-    <richlistbox flex="1" id="storyList" onselect="getDescription(this.selectedItem.id.substr(6))"></richlistbox>
57
+    <richlistbox flex="1"
58
+                 id="storyList"
59
+                 onselect="DiggSidebar.getDescription(this.selectedItem.id.substr(6))" />
46 60
 </page>

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

@@ -24,12 +24,14 @@
24 24
                 </html:div>
25 25
                 <html:div>
26 26
                     <html:span xbl:inherits="xbl:text=desc" class="storyDesc"></html:span>
27
-                    <html:a xbl:inherits="href=link" class="storyLink" onclick="openInTab(this.href); return false">
27
+                    <html:a xbl:inherits="href=link" class="storyLink"
28
+                            onclick="DiggSidebar.Utils.openUrlInTab(this.href); return false">
28 29
                         <html:img src="chrome://diggsidebar/content/image/external.png" />
29 30
                     </html:a>
30 31
                 </html:div>
31 32
                 <html:div style="text-align:center;">
32
-                    <html:a xbl:inherits="href=href" class="storyHref" onclick="openInTab(this.href); return false">
33
+                    <html:a xbl:inherits="href=href" class="storyHref"
34
+                            onclick="DiggSidebar.Utils.openUrlInTab(this.href); return false">
33 35
                         Digg This
34 36
                     </html:a>
35 37
                 </html:div>
@@ -41,7 +43,9 @@
41 43
                         <html:span xbl:inherits="xbl:text=comments" class="storyComments"></html:span>
42 44
                     </html:span>
43 45
                     by
44
-                    <html:a xbl:inherits="href=userlink,xbl:text=username" class="storyUserLink" onclick="openInTab(this.href); return false"></html:a>
46
+                    <html:a xbl:inherits="href=userlink,xbl:text=username"
47
+                            class="storyUserLink"
48
+                            onclick="DiggSidebar.Utils.openUrlInTab(this.href); return false"></html:a>
45 49
                     <html:span xbl:inherits="xbl:text=date" class="storyDate"></html:span>
46 50
                 </html:div>
47 51
             </html:div>

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

@@ -17,7 +17,9 @@ a img {
17 17
     display: none;
18 18
     background-color: #ffffff;
19 19
     clear: both;
20
-    border-top: 1px dashed black;
20
+    border: 1px dashed black;
21
+    margin: 0px 8px 8px 8px;
22
+    width: 230px;
21 23
 }
22 24
 .storyDetails > div {
23 25
     clear: both;
@@ -46,22 +48,20 @@ a img {
46 48
 }
47 49
 .storyHeader {
48 50
     cursor: pointer;
51
+    padding: 2px;
52
+    width: 250px;
49 53
 }
50
-.storyHref:hover, .storyUserName:hover {
54
+.storyHref:hover, .storyUserLink:hover {
51 55
     color: white;
52 56
     background-color: blue;
53 57
 }
54 58
 
55 59
 #storyList richlistitem{
56 60
     border-bottom: 1px solid black;
57
-    padding-bottom: 3px;
58 61
     color: #000000!important;
59 62
     -moz-binding: url("chrome://diggsidebar/content/diggstory.xml#diggstoryitem");
60 63
 }
61 64
 
62
-richlistitem > vbox > div{
63
-    width: 250px;
64
-}
65 65
 richlistitem[selected="true"] {
66 66
     background-color: #FFEBF0;
67 67
     color: inherit;

Loading…
Cancel
Save