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