]> git.wh0rd.org Git - tt-rss.git/blob - tt-rss.js
update UPGRADING
[tt-rss.git] / tt-rss.js
1 var xmlhttp = false;
2
3 var total_unread = 0;
4 var first_run = true;
5
6 var display_tags = false;
7
8 var global_unread = -1;
9
10 var active_title_text = "";
11
12 var current_subtitle = "";
13
14 /*@cc_on @*/
15 /*@if (@_jscript_version >= 5)
16 // JScript gives us Conditional compilation, we can cope with old IE versions.
17 // and security blocked creation of the objects.
18 try {
19         xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
20 } catch (e) {
21         try {
22                 xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
23         } catch (E) {
24                 xmlhttp = false;
25         }
26 }
27 @end @*/
28
29 if (!xmlhttp && typeof XMLHttpRequest!='undefined') {
30         xmlhttp = new XMLHttpRequest();
31 }
32
33 function toggleTags() {
34         display_tags = !display_tags;
35
36         var p = document.getElementById("dispSwitchPrompt");
37
38         if (display_tags) {
39                 p.innerHTML = "display feeds";
40         } else {
41                 p.innerHTML = "display tags";
42         }
43         
44         updateFeedList();
45 }
46
47 function dlg_frefresh_callback() {
48         if (xmlhttp.readyState == 4) {
49                 updateFeedList(false, false);
50                 closeDlg();
51         } 
52 }
53
54 function dialog_refresh_callback() {
55         if (xmlhttp.readyState == 4) {
56                 var dlg = document.getElementById("userDlg");
57                 var dlg_s = document.getElementById("userDlgShadow");
58
59                 dlg.innerHTML = xmlhttp.responseText;
60                 dlg_s.style.display = "block";
61         } 
62 }
63
64 function refetch_callback() {
65         if (xmlhttp.readyState == 4) {
66                 try {
67
68                         if (!xmlhttp.responseXML) {
69                                 notify("refetch_callback: backend did not return valid XML");
70                                 return;
71                         }
72                 
73                         var reply = xmlhttp.responseXML.firstChild;
74         
75                         if (!reply) {
76                                 notify("refetch_callback: backend did not return expected XML object");
77                                 return;
78                         } 
79         
80                         var error_code = reply.getAttribute("error-code");
81                 
82                         if (error_code && error_code != 0) {
83                                 return fatalError(error_code);
84                         }
85         
86                         var f_document = window.frames["feeds-frame"].document;
87         
88                         for (var l = 0; l < reply.childNodes.length; l++) {
89                                 var id = reply.childNodes[l].getAttribute("id");
90                                 var ctr = reply.childNodes[l].getAttribute("counter");
91         
92                                 var feedctr = f_document.getElementById("FEEDCTR-" + id);
93                                 var feedu = f_document.getElementById("FEEDU-" + id);
94                                 var feedr = f_document.getElementById("FEEDR-" + id);
95         
96                                 if (id == "global-unread") {
97                                         global_unread = ctr;
98                                         continue;
99                                 }
100         
101                                 if (feedctr && feedu && feedr) {
102         
103                                         feedu.innerHTML = ctr;
104                 
105                                         if (ctr > 0) {
106                                                 feedctr.className = "odd";
107                                                 if (!feedr.className.match("Unread")) {
108                                                         feedr.className = feedr.className + "Unread";
109                                                 }
110                                         } else {
111                                                 feedctr.className = "invisible";
112                                                 feedr.className = feedr.className.replace("Unread", "");
113                                         }
114                                 }
115                         }  
116         
117                         updateTitle("");
118                         notify("All feeds updated.");
119                 } catch (e) {
120                         exception_error("refetch_callback", e);
121                 }
122         }
123 }
124
125 function backend_sanity_check_callback() {
126
127         if (xmlhttp.readyState == 4) {
128
129                 try {
130                 
131                         if (!xmlhttp.responseXML) {
132                                 fatalError(3);
133                                 return;
134                         }
135         
136                         var reply = xmlhttp.responseXML.firstChild;
137         
138                         if (!reply) {
139                                 fatalError(3);
140                                 return;
141                         }
142         
143                         var error_code = reply.getAttribute("error-code");
144                 
145                         if (error_code && error_code != 0) {
146                                 return fatalError(error_code);
147                         }
148         
149                         init_second_stage();
150
151                 } catch (e) {
152                         exception_error("backend_sanity_check_callback", e);
153                 }
154         } 
155 }
156
157 function scheduleFeedUpdate(force) {
158
159         notify("Updating feeds in background...");
160
161 //      document.title = "Tiny Tiny RSS - Updating...";
162
163         updateTitle("Updating");
164
165         var query_str = "backend.php?op=rpc&subop=";
166
167         if (force) {
168                 query_str = query_str + "forceUpdateAllFeeds";
169         } else {
170                 query_str = query_str + "updateAllFeeds";
171         }
172
173         var omode;
174
175         if (display_tags) {
176                 omode = "t";
177         } else {
178                 omode = "fl";
179         }
180
181         query_str = query_str + "&omode=" + omode;
182
183         if (xmlhttp_ready(xmlhttp)) {
184                 xmlhttp.open("GET", query_str, true);
185                 xmlhttp.onreadystatechange=refetch_callback;
186                 xmlhttp.send(null);
187         } else {
188                 printLockingError();
189         }   
190 }
191
192 function updateFeedList(silent, fetch) {
193
194 //      if (silent != true) {
195 //              notify("Loading feed list...");
196 //      }
197
198         var query_str = "backend.php?op=feeds";
199
200         if (display_tags) {
201                 query_str = query_str + "&tags=1";
202         }
203
204         if (getActiveFeedId()) {
205                 query_str = query_str + "&actid=" + getActiveFeedId();
206         }
207
208         if (fetch) query_str = query_str + "&fetch=yes";
209
210         var feeds_frame = document.getElementById("feeds-frame");
211
212         feeds_frame.src = query_str;
213 }
214
215 function catchupAllFeeds() {
216
217         var query_str = "backend.php?op=feeds&subop=catchupAll";
218
219         notify("Marking all feeds as read...");
220
221         var feeds_frame = document.getElementById("feeds-frame");
222
223         feeds_frame.src = query_str;
224
225         global_unread = 0;
226         updateTitle("");
227
228 }
229
230 function viewCurrentFeed(skip, subop) {
231
232         if (getActiveFeedId()) {
233                 viewfeed(getActiveFeedId(), skip, subop);
234         } else {
235                 disableContainerChildren("headlinesToolbar", false, document);
236                 viewfeed(-1, skip, subop); // FIXME
237         }
238 }
239
240 function viewfeed(feed, skip, subop) {
241         var f = window.frames["feeds-frame"];
242         f.viewfeed(feed, skip, subop);
243 }
244
245 function timeout() {
246         scheduleFeedUpdate(false);
247         setTimeout("timeout()", 1800*1000);
248 }
249
250 function resetSearch() {
251         var searchbox = document.getElementById("searchbox")
252
253         if (searchbox.value != "" && getActiveFeedId()) {       
254                 searchbox.value = "";
255                 viewfeed(getActiveFeedId(), 0, "");
256         }
257 }
258
259 function search() {
260         viewCurrentFeed(0, "");
261 }
262
263 function localPiggieFunction(enable) {
264         if (enable) {
265                 var query_str = "backend.php?op=feeds&subop=piggie";
266
267                 if (xmlhttp_ready(xmlhttp)) {
268
269                         xmlhttp.open("GET", query_str, true);
270                         xmlhttp.onreadystatechange=feedlist_callback;
271                         xmlhttp.send(null);
272                 }
273         }
274 }
275
276 function localHotkeyHandler(keycode) {
277
278 /*      if (keycode == 78) {
279                 return moveToPost('next');
280         }
281
282         if (keycode == 80) {
283                 return moveToPost('prev');
284         } */
285
286         if (keycode == 82) { // r
287                 return scheduleFeedUpdate(true);
288         }
289
290         if (keycode == 85) { // u
291                 if (getActiveFeedId()) {
292                         return viewfeed(getActiveFeedId(), 0, "ForceUpdate");
293                 }
294         }
295
296         if (keycode == 65) { // a
297                 return toggleDispRead();
298         }
299
300         var f_doc = window.frames["feeds-frame"].document;
301         var feedlist = f_doc.getElementById('feedList');
302
303         if (keycode == 74) { // j
304                 var feed = getActiveFeedId();
305                 var new_feed = getRelativeFeedId(feedlist, feed, 'prev');
306                 if (new_feed) viewfeed(new_feed, 0, '');
307         }
308
309         if (keycode == 75) { // k
310                 var feed = getActiveFeedId();
311                 var new_feed = getRelativeFeedId(feedlist, feed, 'next');
312                 if (new_feed) viewfeed(new_feed, 0, '');
313         }
314
315 //      notify("KC: " + keycode);
316
317 }
318
319 // if argument is undefined, current subtitle is not updated
320 // use blank string to clear subtitle
321 function updateTitle(s) {
322         var tmp = "Tiny Tiny RSS";
323
324         if (s && s.length > 0) {
325                 current_subtitle = s;
326         }
327
328         if (global_unread > 0) {
329                 tmp = tmp + " (" + global_unread + ")";
330         }
331
332         if (s) {
333                 tmp = tmp + " - " + current_subtitle;
334         }
335
336         if (active_title_text.length > 0) {
337                 tmp = tmp + " > " + active_title_text;
338         }
339
340         document.title = tmp;
341 }
342
343 function genericSanityCheck() {
344
345         if (!xmlhttp) fatalError(1);
346
347         setCookie("ttrss_vf_test", "TEST");
348         
349         if (getCookie("ttrss_vf_test") != "TEST") {
350                 fatalError(2);
351         }
352
353         return true;
354 }
355
356 function init() {
357
358         try {
359
360                 disableContainerChildren("headlinesToolbar", true);
361
362                 if (!genericSanityCheck()) 
363                         return;
364
365                 xmlhttp.open("GET", "backend.php?op=rpc&subop=sanityCheck", true);
366                 xmlhttp.onreadystatechange=backend_sanity_check_callback;
367                 xmlhttp.send(null);
368
369         } catch (e) {
370                 exception_error("init", e);
371         }
372 }
373
374 function init_second_stage() {
375
376         try {
377
378                 setCookie("ttrss_vf_actfeed", "");
379         
380                 updateFeedList(false, false);
381                 document.onkeydown = hotkey_handler;
382         
383                 var content = document.getElementById("content");
384         
385                 if (getCookie("ttrss_vf_vmode")) {
386                         var viewbox = document.getElementById("viewbox");
387                         viewbox.value = getCookie("ttrss_vf_vmode");
388                 }
389         
390                 if (getCookie("ttrss_vf_limit")) {
391                         var limitbox = document.getElementById("limitbox");
392                         limitbox.value = getCookie("ttrss_vf_limit");
393                 }
394         
395         //      if (getCookie("ttrss_vf_actfeed")) {
396         //              viewfeed(getCookie("ttrss_vf_actfeed"), 0, '');
397         //      }
398         
399         //      setTimeout("timeout()", 2*1000);
400         //      scheduleFeedUpdate(true);
401         
402         } catch (e) {
403                 exception_error("init_second_stage", e);
404         }
405 }
406
407 function quickMenuGo() {
408
409         var chooser = document.getElementById("quickMenuChooser");
410         var opid = chooser[chooser.selectedIndex].id;
411
412         if (opid == "qmcPrefs") {
413                 gotoPreferences();
414         }
415
416         if (opid == "qmcAdvSearch") {
417                 displayDlg("search");
418                 return;
419         }
420
421         if (opid == "qmcAddFeed") {
422                 displayDlg("quickAddFeed");
423                 return;
424         }
425
426         if (opid == "qmcRemoveFeed") {
427                 var actid = getActiveFeedId();
428
429                 if (!actid) {
430                         notify("Please select some feed first.");
431                         return;
432                 }
433         
434                 displayDlg("quickDelFeed", actid);
435                 return;
436         }
437
438         if (opid == "qmcUpdateFeeds") {
439                 scheduleFeedUpdate(true);
440                 return;
441         }
442
443         if (opid == "qmcCatchupAll") {
444                 catchupAllFeeds();
445                 return;
446         }
447
448         if (opid == "qmcShowOnlyUnread") {
449                 toggleDispRead();
450                 return;
451         }
452
453 }
454
455 function qafAdd() {
456
457         if (!xmlhttp_ready(xmlhttp)) {
458                 printLockingError();
459                 return
460         }
461
462         var link = document.getElementById("qafInput");
463
464         if (link.value.length == 0) {
465                 notify("Missing feed URL.");
466         } else {
467                 notify("Adding feed...");
468                 
469                 var feeds_doc = window.frames["feeds-frame"].document;
470
471                 feeds_doc.location.href = "backend.php?op=error&msg=Loading,%20please wait...";
472
473                 xmlhttp.open("GET", "backend.php?op=pref-feeds&subop=add&link=" +
474                         param_escape(link.value), true);
475                 xmlhttp.onreadystatechange=dlg_frefresh_callback;
476                 xmlhttp.send(null);
477
478                 link.value = "";
479
480         }
481 }
482
483 function displayDlg(id, param) {
484
485         notify("");
486
487         xmlhttp.open("GET", "backend.php?op=dlg&id=" +
488                 param_escape(id) + "&param=" + param_escape(param), true);
489         xmlhttp.onreadystatechange=dialog_refresh_callback;
490         xmlhttp.send(null);
491
492 }
493
494 function closeDlg() {
495         var dlg = document.getElementById("userDlgShadow");
496         dlg.style.display = "none";
497 }
498
499 function qfdDelete(feed_id) {
500
501         notify("Removing feed...");
502
503         var feeds_doc = window.frames["feeds-frame"].document;
504         feeds_doc.location.href = "backend.php?op=error&msg=Loading,%20please wait...";
505
506         xmlhttp.open("GET", "backend.php?op=pref-feeds&subop=remove&ids=" + feed_id);
507         xmlhttp.onreadystatechange=dlg_frefresh_callback;
508         xmlhttp.send(null);
509 }
510
511
512 function allFeedsMenuGo() {
513         var chooser = document.getElementById("allFeedsChooser");
514
515         var opname = chooser[chooser.selectedIndex].text;
516
517         if (opname == "Update") {
518                 scheduleFeedUpdate(true);
519                 return;
520         }
521
522         if (opname == "Mark as read") {
523                 catchupAllFeeds();
524                 return;
525         }
526
527         if (opname == "Show only unread") {
528                 toggleDispRead();
529                 return;
530         }
531
532 }
533
534 function updateFeedTitle(t) {
535         active_title_text = t;
536         updateTitle();
537 }
538
539 function toggleDispRead() {
540         var hide_read_feeds = (getCookie("ttrss_vf_hreadf") == 1);
541
542         hide_read_feeds = !hide_read_feeds;
543
544         var feeds_doc = window.frames["feeds-frame"].document;
545
546         hideOrShowFeeds(feeds_doc, hide_read_feeds);
547
548         if (hide_read_feeds) {
549                 setCookie("ttrss_vf_hreadf", 1);
550         } else {
551                 setCookie("ttrss_vf_hreadf", 0);
552         }
553
554 }
555
556