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