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