]> git.wh0rd.org Git - tt-rss.git/blob - tt-rss.js
start work on showing number of unread articles in title
[tt-rss.git] / tt-rss.js
1 /*
2         This program is Copyright (c) 2003-2005 Andrew Dolgov <cthulhoo@gmail.com>              
3         Licensed under GPL v.2 or (at your preference) any later version.
4 */
5
6 var xmlhttp = false;
7
8 var total_unread = 0;
9 var first_run = true;
10
11 var search_query = "";
12 var search_mode = "";
13
14 var display_tags = false;
15
16 var global_unread = 0;
17
18 /*@cc_on @*/
19 /*@if (@_jscript_version >= 5)
20 // JScript gives us Conditional compilation, we can cope with old IE versions.
21 // and security blocked creation of the objects.
22 try {
23         xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
24 } catch (e) {
25         try {
26                 xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
27         } catch (E) {
28                 xmlhttp = false;
29         }
30 }
31 @end @*/
32
33 if (!xmlhttp && typeof XMLHttpRequest!='undefined') {
34         xmlhttp = new XMLHttpRequest();
35 }
36
37 function toggleTags() {
38         display_tags = !display_tags;
39
40         var p = document.getElementById("dispSwitchPrompt");
41
42         if (display_tags) {
43                 p.innerHTML = "display feeds";
44         } else {
45                 p.innerHTML = "display tags";
46         }
47         
48         updateFeedList();
49 }
50
51 /*
52 function feedlist_callback() {
53         var container = document.getElementById('feeds');
54         if (xmlhttp.readyState == 4) {
55                 container.innerHTML=xmlhttp.responseText;
56
57                 if (first_run) {
58                         scheduleFeedUpdate(false);
59                         if (getCookie("ttrss_vf_actfeed")) {
60                                 viewfeed(getCookie("ttrss_vf_actfeed"), 0, "");
61                         }
62                         first_run = false;
63                 } else {
64                         notify("");
65                 } 
66         } 
67 }
68 */
69
70 function refetch_callback() {
71         if (xmlhttp.readyState == 4) {
72
73 //              document.title = "Tiny Tiny RSS";
74
75                 if (!xmlhttp.responseXML) {
76                         notify("refetch_callback: backend did not return valid XML");
77                         return;
78                 }
79
80                 var reply = xmlhttp.responseXML.firstChild;
81
82                 if (!reply) {
83                         notify("refetch_callback: backend did not return expected XML object");
84                         return;
85                 } 
86
87                 var f_document = window.frames["feeds-frame"].document;
88
89                 for (var l = 0; l < reply.childNodes.length; l++) {
90                         var id = reply.childNodes[l].getAttribute("id");
91                         var ctr = reply.childNodes[l].getAttribute("counter");
92
93                         var feedctr = f_document.getElementById("FEEDCTR-" + id);
94                         var feedu = f_document.getElementById("FEEDU-" + id);
95                         var feedr = f_document.getElementById("FEEDR-" + id);
96
97 /*                      TODO figure out how to update this from viewfeed.js->view()
98                         disabled for now...
99
100                         if (id == "global-unread") {
101                                 global_unread = ctr;
102                         } */
103
104                         if (feedctr && feedu && feedr) {
105
106                                 feedu.innerHTML = ctr;
107         
108                                 if (ctr > 0) {
109                                         feedctr.className = "odd";
110                                         if (!feedr.className.match("Unread")) {
111                                                 feedr.className = feedr.className + "Unread";
112                                         }
113                                 } else {
114                                         feedctr.className = "invisible";
115                                         feedr.className = feedr.className.replace("Unread", "");
116                                 }
117                         }
118                 }  
119
120                 updateTitle("");
121                 notify("All feeds updated.");
122
123         }
124 }
125
126 function updateFeed(feed_id) {
127
128         var query_str = "backend.php?op=rpc&subop=updateFeed&feed=" + feed_id;
129
130         if (xmlhttp_ready(xmlhttp)) {
131                 xmlhttp.open("GET", query_str, true);
132                 xmlhttp.onreadystatechange=feed_update_callback;
133                 xmlhttp.send(null);
134         } else {
135                 printLockingError();
136         }   
137
138 }
139
140 function scheduleFeedUpdate(force) {
141
142         notify("Updating feeds in background...");
143
144 //      document.title = "Tiny Tiny RSS - Updating...";
145
146         updateTitle("Updating...");
147
148         var query_str = "backend.php?op=rpc&subop=";
149
150         if (force) {
151                 query_str = query_str + "forceUpdateAllFeeds";
152         } else {
153                 query_str = query_str + "updateAllFeeds";
154         }
155
156         if (xmlhttp_ready(xmlhttp)) {
157                 xmlhttp.open("GET", query_str, true);
158                 xmlhttp.onreadystatechange=refetch_callback;
159                 xmlhttp.send(null);
160         } else {
161                 printLockingError();
162         }   
163 }
164
165 function updateFeedList(silent, fetch) {
166
167 //      if (silent != true) {
168 //              notify("Loading feed list...");
169 //      }
170
171         var query_str = "backend.php?op=feeds";
172
173         if (display_tags) {
174                 query_str = query_str + "&tags=1";
175         }
176
177         if (getActiveFeedId()) {
178                 query_str = query_str + "&actid=" + getActiveFeedId();
179         }
180
181         if (fetch) query_str = query_str + "&fetch=yes";
182
183         var feeds_frame = document.getElementById("feeds-frame");
184
185         feeds_frame.src = query_str;
186 }
187
188 function catchupAllFeeds() {
189
190         var query_str = "backend.php?op=feeds&subop=catchupAll";
191
192         notify("Marking all feeds as read...");
193
194         var feeds_frame = document.getElementById("feeds-frame");
195
196         feeds_frame.src = query_str;
197
198         global_unread = 0;
199         updateTitle();
200
201 }
202
203 function viewCurrentFeed(skip, subop) {
204
205         if (getActiveFeedId()) {
206                 viewfeed(getActiveFeedId(), skip, subop);
207         }
208 }
209
210 function viewfeed(feed, skip, subop) {
211
212 //      notify("Loading headlines...");
213
214         enableHotkeys();
215
216         var searchbox = document.getElementById("searchbox");
217
218         if (searchbox) {
219                 search_query = searchbox.value;
220         } else {
221                 search_query = "";
222         } 
223
224         var searchmodebox = document.getElementById("searchmodebox");
225
226         if (searchmodebox) {
227                 search_mode = searchmodebox.value;
228         } else {
229                 search_mode = "";
230         }
231
232         setCookie("ttrss_vf_smode", search_mode);
233
234         var viewbox = document.getElementById("viewbox");
235
236         var view_mode;
237
238         if (viewbox) {
239                 view_mode = viewbox[viewbox.selectedIndex].text;
240         } else {
241                 view_mode = "All Posts";
242         }
243
244         setCookie("ttrss_vf_vmode", view_mode);
245
246         var limitbox = document.getElementById("limitbox");
247
248         var limit;
249
250         if (limitbox) {
251                 limit = limitbox[limitbox.selectedIndex].text;
252                 setCookie("ttrss_vf_limit", limit);
253         } else {
254                 limit = "All";
255         }
256
257         setActiveFeedId(feed);
258
259         var f_doc = frames["feeds-frame"].document;
260
261 //      f_doc.getElementById("ACTFEEDID").innerHTML = feed;
262
263         if (subop == "MarkAllRead") {
264
265                 var feedr = f_doc.getElementById("FEEDR-" + feed);
266                 var feedctr = f_doc.getElementById("FEEDCTR-" + feed);
267                 
268                 feedctr.className = "invisible";
269
270                 if (feedr.className.match("Unread")) {
271                         feedr.className = feedr.className.replace("Unread", "");
272                 }
273         }
274
275         var query = "backend.php?op=viewfeed&feed=" + param_escape(feed) +
276                 "&skip=" + param_escape(skip) + "&subop=" + param_escape(subop) +
277                 "&view=" + param_escape(view_mode) + "&limit=" + limit + 
278                 "&smode=" + param_escape(search_mode);
279
280         if (search_query != "") {
281                 query = query + "&search=" + param_escape(search_query);
282         }
283         
284         var headlines_frame = parent.frames["headlines-frame"];
285
286         headlines_frame.location.href = query + "&addheader=true";
287
288         cleanSelected("feedsList");
289         var feedr = document.getElementById("FEEDR-" + feed);
290         if (feedr) {
291                 feedr.className = feedr.className + "Selected";
292         }
293         
294         disableContainerChildren("headlinesToolbar", false, doc);
295
296         var btnMarkAsRead = document.getElementById("btnMarkFeedAsRead");
297
298         if (btnMarkAsRead && (feed < 0 || !isNumeric(feed))) {
299                 btnMarkAsRead.disabled = true;
300                 btnMarkAsRead.className = "disabledButton";
301         }
302
303 //      notify("");
304
305 }
306
307
308 function timeout() {
309         scheduleFeedUpdate(true);
310         setTimeout("timeout()", 1800*1000);
311 }
312
313 function resetSearch() {
314         var searchbox = document.getElementById("searchbox")
315
316         if (searchbox.value != "" && getActiveFeedId()) {       
317                 searchbox.value = "";
318                 viewfeed(getActiveFeedId(), 0, "");
319         }
320 }
321
322 function search() {
323         viewCurrentFeed(0, "");
324 }
325
326 function localPiggieFunction(enable) {
327         if (enable) {
328                 var query_str = "backend.php?op=feeds&subop=piggie";
329
330                 if (xmlhttp_ready(xmlhttp)) {
331
332                         xmlhttp.open("GET", query_str, true);
333                         xmlhttp.onreadystatechange=feedlist_callback;
334                         xmlhttp.send(null);
335                 }
336         }
337 }
338
339 function localHotkeyHandler(keycode) {
340
341 /*      if (keycode == 78) {
342                 return moveToPost('next');
343         }
344
345         if (keycode == 80) {
346                 return moveToPost('prev');
347         } */
348
349         if (keycode == 82) {
350                 return scheduleFeedUpdate(true);
351         }
352
353         if (keycode == 85) {
354                 return viewfeed(getActiveFeedId(), 0, "ForceUpdate");
355         }
356
357 //      notify("KC: " + keycode);
358
359 }
360
361 function updateTitle(s) {
362         var tmp = "Tiny Tiny RSS";
363         
364         if (global_unread > 0) {
365                 tmp = tmp + " (" + global_unread + ")";
366         }
367
368         if (s) {
369                 tmp = tmp + " - " + s;
370         }
371         document.title = tmp;
372 }
373
374 function genericSanityCheck() {
375
376         if (!xmlhttp) {
377                 document.getElementById("headlines").innerHTML = 
378                         "<b>Fatal error:</b> This program requires XmlHttpRequest " + 
379                         "to function properly. Your browser doesn't seem to support it.";
380                 return false;
381         }
382
383         setCookie("ttrss_vf_test", "TEST");
384         if (getCookie("ttrss_vf_test") != "TEST") {
385
386                 document.getElementById("headlines").innerHTML = 
387                         "<b>Fatal error:</b> This program requires cookies " + 
388                         "to function properly. Your browser doesn't seem to support them.";
389
390                 return false;
391         }
392
393         return true;
394 }
395
396 function init() {
397
398         disableContainerChildren("headlinesToolbar", true);
399
400         if (!genericSanityCheck()) 
401                 return;
402
403         setCookie("ttrss_vf_actfeed", "");
404
405         updateFeedList(false, false);
406         document.onkeydown = hotkey_handler;
407
408         setTimeout("timeout()", 1800*1000);
409         scheduleFeedUpdate(true);
410
411         var content = document.getElementById("content");
412
413         if (getCookie("ttrss_vf_vmode")) {
414                 var viewbox = document.getElementById("viewbox");
415                 viewbox.value = getCookie("ttrss_vf_vmode");
416         }
417
418         if (getCookie("ttrss_vf_limit")) {
419                 var limitbox = document.getElementById("limitbox");
420                 limitbox.value = getCookie("ttrss_vf_limit");
421         }
422
423 //      if (getCookie("ttrss_vf_actfeed")) {
424 //              viewfeed(getCookie("ttrss_vf_actfeed"), 0, '');
425 //      }
426
427 }
428
429