Browse Source

more refactoring

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

+ 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, notifier-observer -- done
5
-search
4
+caching, notifier-observer -- done
5
+search?
6 6
 new UI, icons, thumbnails
7 7
 put license

+ 45
- 44
chrome/content/diggsidebar.js View File

@@ -35,7 +35,7 @@ var DiggSidebar = {
35 35
     stories: [],
36 36
 
37 37
     fetchData: function(url, handler) {
38
-        DiggSidebar.diggIndicator.style.display = '';
38
+        DiggSidebar.UI.indicator.style.display = '';
39 39
 
40 40
         var request = new XMLHttpRequest();
41 41
         request.onerror = DiggSidebar.errorHandler;
@@ -54,9 +54,7 @@ var DiggSidebar = {
54 54
         var topics = data.topics;
55 55
         var containers = new Array();
56 56
 
57
-        while (DiggSidebar.topicPopup.firstChild) {
58
-                DiggSidebar.topicPopup.removeChild(DiggSidebar.topicPopup.firstChild);
59
-        }
57
+        DiggSidebar.Utils.removeAllChildren(DiggSidebar.UI.topicPopup);
60 58
 
61 59
         topics.forEach(function (topic) {
62 60
             var menu = document.createElement('menu');
@@ -78,14 +76,12 @@ var DiggSidebar = {
78 76
                 }
79 77
             );
80 78
             menu.appendChild(menupopup);
81
-            DiggSidebar.topicPopup.appendChild(menu);
79
+            DiggSidebar.UI.topicPopup.appendChild(menu);
82 80
 
83 81
             containers.push(topic.container);
84 82
         });
85 83
 
86
-        while (DiggSidebar.containerPopup.firstChild) {
87
-                DiggSidebar.containerPopup.removeChild(DiggSidebar.containerPopup.firstChild);
88
-        }
84
+        DiggSidebar.Utils.removeAllChildren(DiggSidebar.UI.containerPopup);
89 85
 
90 86
         var filteredContainers = {name: [], short_name: []};
91 87
         containers.forEach(function (container) {
@@ -119,18 +115,14 @@ var DiggSidebar = {
119 115
             );
120 116
 
121 117
             menu.appendChild(menupopup);
122
-            DiggSidebar.containerPopup.appendChild(menu);
118
+            DiggSidebar.UI.containerPopup.appendChild(menu);
123 119
         }
124
-        DiggSidebar.diggIndicator.style.display = 'none';
120
+        DiggSidebar.UI.indicator.style.display = 'none';
125 121
     },
126 122
 
127 123
     populateStoryList: function() {
128
-        while (DiggSidebar.storyList.firstChild) {
129
-            DiggSidebar.storyList.removeChild(DiggSidebar.storyList.firstChild);
130
-        }
131
-
124
+        DiggSidebar.Utils.removeAllChildren(DiggSidebar.UI.storyListBox);
132 125
         var newStoryIds = new Array();
133
-        Application.console.log("DiggSidebar.stories.length = " + DiggSidebar.stories.length);
134 126
 
135 127
         var jp = new JPath(DiggSidebar.stories);
136 128
         var filteredStories = jp.$(function(story){
@@ -165,20 +157,24 @@ var DiggSidebar = {
165 157
                     ((hr == 0) && (min == 0)) ?
166 158
                     "just now" : "";
167 159
 
168
-                var li = DiggSidebar.storyList.appendChild(document.createElement('richlistitem'));
160
+                var li = DiggSidebar.UI.storyListBox.appendChild(document.createElement('richlistitem'));
169 161
                 li.id = "story_" + story.id;
170
-                li.setAttribute('title', story.title);
171
-                li.setAttribute('date', relativeTime);
172
-                li.setAttribute('status', story.status);
173
-                li.setAttribute('container', story.container.name);
174
-                li.setAttribute('topic', story.topic.name);
175
-                li.setAttribute('username', story.user.name);
176
-                li.setAttribute('userlink', "http://digg.com/users/" + story.user.name);
177
-                li.setAttribute('diggs', story.diggs);
178
-                li.setAttribute('comments', story.comments);
179
-                li.setAttribute('desc', story.description);
180
-                li.setAttribute('link', story.link);
181
-                li.setAttribute('href',  story.href);
162
+                var attributes = {
163
+                    title: story.title,
164
+                    date: relativeTime,
165
+                    status: story.status,
166
+                    container: story.container.name,
167
+                    topic: story.topic.name,
168
+                    username: story.user.name,
169
+                    userlink: "http://digg.com/users/" + story.user.name,
170
+                    diggs: story.diggs,
171
+                    comments: story.comments,
172
+                    desc: story.description,
173
+                    link: story.link,
174
+                    href:  story.href
175
+                }
176
+                for (attr in attributes)
177
+                    li.setAttribute(attr, attributes[attr]);
182 178
 
183 179
                 if (DiggSidebar.storyIds.indexOf(story.id) != -1) li.new = false;
184 180
                 li.read = story.read;
@@ -192,7 +188,7 @@ var DiggSidebar = {
192 188
         var timeout = Math.round(DiggSidebar.updateInterval*(Math.pow(1.5, DiggSidebar.updateIntervalDecay)));
193 189
         DiggSidebar.timerId = window.setTimeout(DiggSidebar.getStories, timeout);
194 190
 
195
-        DiggSidebar.diggIndicator.style.display = 'none';
191
+        DiggSidebar.UI.indicator.style.display = 'none';
196 192
     },
197 193
 
198 194
     showDescription: function(storyId) {
@@ -200,18 +196,16 @@ var DiggSidebar = {
200 196
         var story = jp.query('//[id == ' + storyId + ']')[0];
201 197
         story.read = true;
202 198
 
203
-        var listitems = document.getElementsByTagName('richlistitem');
199
+        var listitems = DiggSidebar.UI.storyListBox.children;
204 200
         for (var i=0; i<listitems.length; i++)
205 201
             listitems[i].hideDescription();
206 202
 
207
-        var li = document.getElementById('story_'+ story.id)
208
-        li.read = true;
209
-        li.showDescription();
203
+        document.getElementById('story_'+ story.id).showDescription();
210 204
     },
211 205
 
212 206
     getStories: function() {
213 207
         var ep = DiggSidebar.prefs.get("endpoint").value || '';
214
-        DiggSidebar.diggEndPoint.value = "digg" + ep;
208
+        DiggSidebar.UI.endPointDesc.value = "digg" + ep;
215 209
 
216 210
         DiggSidebar.fetchData("http://services.digg.com/stories" + ep.replace(/\/all/g, '') +
217 211
             "?count=30" + "&type=json" +
@@ -297,14 +291,14 @@ var DiggSidebar = {
297 291
     togglePlayPause: function() {
298 292
         if (DiggSidebar.playing) {
299 293
             window.clearTimeout(DiggSidebar.timerId);
300
-            DiggSidebar.diggPlayPause.image = "chrome://diggsidebar/content/image/Play.png";
301
-            DiggSidebar.diggPlayPause.setAttribute("tooltiptext", "Click to Start autoupdate");
294
+            DiggSidebar.UI.playPauseButton.image = "chrome://diggsidebar/content/image/Play.png";
295
+            DiggSidebar.UI.playPauseButton.setAttribute("tooltiptext", "Click to Start autoupdate");
302 296
             DiggSidebar.playing = false;
303 297
         } else {
304 298
             var timeout = Math.round(DiggSidebar.updateInterval*(Math.pow(1.5, DiggSidebar.updateIntervalDecay)));
305 299
             DiggSidebar.timerId = window.setTimeout(DiggSidebar.getStories, timeout);
306
-            DiggSidebar.diggPlayPause.image = "chrome://diggsidebar/content/image/Pause.png";
307
-            DiggSidebar.diggPlayPause.setAttribute("tooltiptext", "Click to Pause autoupdate");
300
+            DiggSidebar.UI.playPauseButton.image = "chrome://diggsidebar/content/image/Pause.png";
301
+            DiggSidebar.UI.playPauseButton.setAttribute("tooltiptext", "Click to Pause autoupdate");
308 302
             DiggSidebar.playing = true;
309 303
         }
310 304
     },
@@ -312,12 +306,14 @@ var DiggSidebar = {
312 306
     initialize: function(){
313 307
         DiggSidebar.endpoint = DiggSidebar.getEndpointParts(DiggSidebar.prefs.get("endpoint").value);
314 308
         var $ = function(id) {return document.getElementById(id)};
315
-        DiggSidebar.diggIndicator = $('diggIndicator');
316
-        DiggSidebar.topicPopup = $('topicPopup');
317
-        DiggSidebar.containerPopup = $('containerPopup');
318
-        DiggSidebar.storyList = $('storyList');
319
-        DiggSidebar.diggEndPoint = $('diggEndPoint');
320
-        DiggSidebar.diggPlayPause = $('diggPlayPause');
309
+
310
+        DiggSidebar.UI = {};
311
+        DiggSidebar.UI.indicator = $('dsBusyIndicator');
312
+        DiggSidebar.UI.topicPopup = $('dsTopicPopup');
313
+        DiggSidebar.UI.containerPopup = $('dsContainerPopup');
314
+        DiggSidebar.UI.storyListBox = $('dsStoryListBox');
315
+        DiggSidebar.UI.endPointDesc = $('dsEndPointDesc');
316
+        DiggSidebar.UI.playPauseButton = $('dsPlayPauseButton');
321 317
 
322 318
         DiggSidebar.storyListObserver = new DiggSidebar.StoryListObserver();
323 319
         DiggSidebar.createMenu();
@@ -344,6 +340,11 @@ DiggSidebar.Utils = {
344 340
         var json = Components.classes["@mozilla.org/dom/json;1"]
345 341
             .createInstance(Components.interfaces.nsIJSON);
346 342
         return json.decode(string);
343
+    },
344
+
345
+    removeAllChildren: function(node) {
346
+        while (node.firstChild)
347
+            node.removeChild(node.firstChild);
347 348
     }
348 349
 }
349 350
 

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

@@ -13,9 +13,9 @@
13 13
     <hbox>
14 14
         <toolbar flex="1">
15 15
             <toolbaritem>
16
-                <menubar id="diggsidebarMenubar" flex="1" style="width: 100%">
17
-                    <menu id="storiesMenu" label="Stories" accesskey="S">
18
-                        <menupopup id="storiesPopup">
16
+                <menubar id="dsMenubar" flex="1" style="width: 100%">
17
+                    <menu id="dsStoriesMenu" label="Stories" accesskey="S">
18
+                        <menupopup id="dsStoriesPopup">
19 19
                             <menuitem label="All" value="/all"
20 20
                                       oncommand="DiggSidebar.setEndPoint(this.value)" accesskey="A" />
21 21
                             <menuitem label="Popular" value="/popular"
@@ -27,13 +27,13 @@
27 27
                             <menuitem label="Top" value="/top"
28 28
                                       oncommand="DiggSidebar.setEndPoint(this.value)" accesskey="T" />
29 29
                             <menuseparator />
30
-                            <menu id="containerMenu" label="Containers" accesskey="C">
31
-                                <menupopup id="containerPopup">
30
+                            <menu id="dsContainerMenu" label="Containers" accesskey="C">
31
+                                <menupopup id="dsContainerPopup">
32 32
                                     <menuitem label="Populating..." />
33 33
                                 </menupopup>
34 34
                             </menu>
35
-                            <menu id="topicMenu" label="Topics" accesskey="T">
36
-                                <menupopup id="topicPopup">
35
+                            <menu id="dsTopicMenu" label="Topics" accesskey="T">
36
+                                <menupopup id="dsTopicPopup">
37 37
                                     <menuitem label="Populating..." />
38 38
                                 </menupopup>
39 39
                             </menu>
@@ -41,7 +41,7 @@
41 41
                     </menu>
42 42
                 </menubar>
43 43
             </toolbaritem>
44
-            <toolbarbutton id="diggPlayPause"
44
+            <toolbarbutton id="dsPlayPauseButton"
45 45
                            image="chrome://diggsidebar/content/image/Pause.png"
46 46
                            tooltiptext="Click to Pause autoupdate"
47 47
                            oncommand="DiggSidebar.togglePlayPause()" />
@@ -50,13 +50,13 @@
50 50
                 <progressmeter flex="1"
51 51
                                width="5"
52 52
                                mode="undetermined"
53
-                               id="diggIndicator"
53
+                               id="dsBusyIndicator"
54 54
                                style="height: 85% !important; width: 30% !important" />
55 55
             </toolbaritem>
56 56
         </toolbar>
57 57
     </hbox>
58
-    <description id="diggEndPoint">digg</description>
58
+    <description id="dsEndPointDesc">digg</description>
59 59
     <richlistbox flex="1"
60
-                 id="storyList"
60
+                 id="dsStoryListBox"
61 61
                  onselect="DiggSidebar.showDescription(this.selectedItem.id.substr(6))" />
62 62
 </page>

+ 20
- 12
chrome/content/diggstory.xml View File

@@ -11,8 +11,8 @@
11 11
         <content>
12 12
             <xul:vbox>
13 13
             <html:div class="storyHeader">
14
-                <html:span class="storyRead" title="Unread">R</html:span>
15
-                <html:span class="storyNew" title="New">N</html:span>
14
+                <html:span class="storyNew" title="New">New</html:span>
15
+                <html:span class="storyRead" title="Unread">Unread</html:span>
16 16
                 <html:img src="chrome://diggsidebar/content/image/up.jpg" class="storyCEIcon" />
17 17
                 <html:span xbl:inherits="xbl:text=title" class="storyTitle"></html:span>
18 18
             </html:div>
@@ -57,6 +57,7 @@
57 57
                 this.d = {};
58 58
                 this.d.read = false;
59 59
                 this.d.new = true;
60
+                this.d.collapsed = true;
60 61
                 ]]>
61 62
             </constructor>
62 63
             <property name="read"
@@ -65,25 +66,30 @@
65 66
             <property name="new"
66 67
                 onget="return this.d.new;"
67 68
                 onset="this.d.new = val; if(val==false) this.markAsOld();"/>
69
+            <property name="collapsed"
70
+                onget="return this.d.collapsed;"
71
+                onset="this.d.collapsed = val;"/>
68 72
             <method name="markAsRead">
69 73
                 <body>
70 74
                 <![CDATA[
71
-                    var storyRead = document.getAnonymousElementByAttribute(this, "class", "storyRead");
72
-                    storyRead.style.textDecoration = "line-through";
73
-                    storyRead.style.backgroundColor = "black";
74
-                    storyRead.style.color = "white";
75
-                    storyRead.title = "Read";
75
+                    with (document.getAnonymousElementByAttribute(this, "class", "storyRead")) {
76
+                        style.backgroundColor = "black";
77
+                        style.color = "white";
78
+                        title = "Read";
79
+                        textContent = "Read";
80
+                    }
76 81
                 ]]>
77 82
                 </body>
78 83
             </method>
79 84
             <method name="markAsOld">
80 85
                 <body>
81 86
                 <![CDATA[
82
-                    var storyNew = document.getAnonymousElementByAttribute(this, "class", "storyNew");
83
-                    storyNew.style.textDecoration = "line-through";
84
-                    storyNew.style.backgroundColor = "black";
85
-                    storyNew.style.color = "white";
86
-                    storyNew.title = "Old";
87
+                    with (document.getAnonymousElementByAttribute(this, "class", "storyNew")) {
88
+                        style.backgroundColor = "black";
89
+                        style.color = "white";
90
+                        title = "Old";
91
+                        textContent = "Old";
92
+                    }
87 93
                 ]]>
88 94
                 </body>
89 95
             </method>
@@ -94,6 +100,7 @@
94 100
                         "chrome://diggsidebar/content/image/down.jpg";
95 101
                     document.getAnonymousElementByAttribute(this, "class", "storyDetails").style.display = 'block';
96 102
                     this.read = true;
103
+                    this.collapsed = false;
97 104
                 ]]>
98 105
                 </body>
99 106
             </method>
@@ -103,6 +110,7 @@
103 110
                     document.getAnonymousElementByAttribute(this, "class", "storyCEIcon").src =
104 111
                         "chrome://diggsidebar/content/image/up.jpg";
105 112
                     document.getAnonymousElementByAttribute(this, "class", "storyDetails").style.display = 'none';
113
+                    this.collapsed = true;
106 114
                 ]]>
107 115
                 </body>
108 116
             </method>

+ 1
- 1
chrome/locale/en-US/diggsidebar.dtd View File

@@ -1,3 +1,3 @@
1 1
 <!ENTITY diggsidebar.title "Digg Sidebar">
2 2
 <!ENTITY openDiggSidebar.commandkey "D">
3
-<!ENTITY openDiggSidebar.modifierskey "accel shift">
3
+<!ENTITY openDiggSidebar.modifierskey "alt shift">

+ 4
- 2
chrome/skin/diggsidebar.css View File

@@ -6,7 +6,7 @@ a {
6 6
 a img {
7 7
     border: none;
8 8
 }
9
-#storyList {
9
+#dsStoryListBox {
10 10
     margin: 1px;
11 11
     border-style: solid !important;
12 12
     border-color: #000;
@@ -37,6 +37,8 @@ a img {
37 37
 }
38 38
 .storyNew, .storyRead {
39 39
     padding: 0px 2px;
40
+    font-size: xx-small;
41
+    font-family: monospace;
40 42
 }
41 43
 .storyHref {
42 44
     font-weight: bold;
@@ -56,7 +58,7 @@ a img {
56 58
     background-color: blue;
57 59
 }
58 60
 
59
-#storyList richlistitem{
61
+#dsStoryListBox richlistitem{
60 62
     border-bottom: 1px solid black;
61 63
     color: #000000!important;
62 64
     -moz-binding: url("chrome://diggsidebar/content/diggstory.xml#diggstoryitem");

Loading…
Cancel
Save