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