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