]> git.wh0rd.org - tt-rss.git/blob - tt-rss.js
change short php tags to long ones
[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 var daemon_enabled = false;
9 var daemon_refresh_only = false;
10 var _qfd_deleted_feed = 0;
11 var firsttime_update = true;
12 var last_refetch = 0;
13 var cookie_lifetime = 0;
14 var active_feed_id = 0;
15 var active_feed_is_cat = false;
16 var number_of_feeds = 0;
17
18 var xmlhttp = Ajax.getTransport();
19
20 var init_params = new Object();
21
22 function toggleTags() {
23 display_tags = !display_tags;
24
25 var p = document.getElementById("dispSwitchPrompt");
26
27 if (display_tags) {
28 p.innerHTML = "display feeds";
29 } else {
30 p.innerHTML = "display tags";
31 }
32
33 updateFeedList();
34 }
35
36 function dlg_frefresh_callback() {
37 if (xmlhttp.readyState == 4) {
38 notify(xmlhttp.responseText);
39 updateFeedList(false, false);
40 if (_qfd_deleted_feed) {
41 var hframe = document.getElementById("headlines-frame");
42 if (hframe) {
43 hframe.src = "backend.php?op=error&msg=No%20feed%20selected.";
44 }
45 }
46 closeInfoBox();
47 }
48 }
49
50 function refetch_callback() {
51 if (xmlhttp.readyState == 4) {
52 try {
53
54 var date = new Date();
55
56 last_refetch = date.getTime() / 1000;
57
58 if (!xmlhttp.responseXML) {
59 notify("refetch_callback: backend did not return valid XML", true, true);
60 return;
61 }
62
63 var reply = xmlhttp.responseXML.firstChild;
64
65 if (!reply) {
66 notify("refetch_callback: backend did not return expected XML object", true, true);
67 updateTitle("");
68 return;
69 }
70
71 var error_code = reply.getAttribute("error-code");
72
73 if (error_code && error_code != 0) {
74 return fatalError(error_code, reply.getAttribute("error-msg"));
75 }
76
77 var counters = reply.firstChild;
78
79 parse_counters(counters, true);
80
81 var runtime_info = counters.nextSibling;
82
83 parse_runtime_info(runtime_info);
84
85 debug("refetch_callback: done");
86
87 if (!daemon_enabled && !daemon_refresh_only) {
88 notify("All feeds updated.");
89 updateTitle("");
90 } else {
91 notify("");
92 }
93 } catch (e) {
94 exception_error("refetch_callback", e);
95 updateTitle("");
96 }
97 }
98 }
99
100 function backend_sanity_check_callback() {
101
102 if (xmlhttp.readyState == 4) {
103
104 try {
105
106 if (!xmlhttp.responseXML) {
107 fatalError(3, "[D001, Received reply is not XML]: " + xmlhttp.responseText);
108 return;
109 }
110
111 var reply = xmlhttp.responseXML.firstChild.firstChild;
112
113 if (!reply) {
114 fatalError(3, "[D002, Invalid RPC reply]: " + xmlhttp.responseText);
115 return;
116 }
117
118 var error_code = reply.getAttribute("error-code");
119
120 if (error_code && error_code != 0) {
121 return fatalError(error_code, reply.getAttribute("error-msg"));
122 }
123
124 debug("sanity check ok");
125
126 var params = reply.nextSibling;
127
128 if (params) {
129 debug('reading init-params...');
130 var param = params.firstChild;
131
132 while (param) {
133 var k = param.getAttribute("key");
134 var v = param.getAttribute("value");
135 debug(k + " => " + v);
136 init_params[k] = v;
137 param = param.nextSibling;
138 }
139 }
140
141 init_second_stage();
142
143 } catch (e) {
144 exception_error("backend_sanity_check_callback", e);
145 }
146 }
147 }
148
149 function scheduleFeedUpdate(force) {
150
151 if (!daemon_enabled && !daemon_refresh_only) {
152 notify("Updating feeds, please wait.", true);
153 updateTitle("Updating");
154 }
155
156 var query_str = "backend.php?op=rpc&subop=";
157
158 if (force) {
159 query_str = query_str + "forceUpdateAllFeeds";
160 } else {
161 query_str = query_str + "updateAllFeeds";
162 }
163
164 var omode;
165
166 if (firsttime_update && !navigator.userAgent.match("Opera")) {
167 firsttime_update = false;
168 omode = "T";
169 } else {
170 if (display_tags) {
171 omode = "t";
172 } else {
173 omode = "flc";
174 }
175 }
176
177 query_str = query_str + "&omode=" + omode;
178 query_str = query_str + "&uctr=" + global_unread;
179
180 debug("in scheduleFeedUpdate");
181
182 var date = new Date();
183
184 if (!xmlhttp_ready(xmlhttp) && last_refetch < date.getTime() / 1000 - 60) {
185 debug("<b>xmlhttp seems to be stuck, aborting</b>");
186 xmlhttp.abort();
187 }
188
189 if (xmlhttp_ready(xmlhttp)) {
190 xmlhttp.open("GET", query_str, true);
191 xmlhttp.onreadystatechange=refetch_callback;
192 xmlhttp.send(null);
193 } else {
194 debug("xmlhttp busy");
195 //printLockingError();
196 }
197 }
198
199 function updateFeedList(silent, fetch) {
200
201 // if (silent != true) {
202 // notify("Loading feed list...");
203 // }
204
205 var query_str = "backend.php?op=feeds";
206
207 if (display_tags) {
208 query_str = query_str + "&tags=1";
209 }
210
211 if (getActiveFeedId() && !activeFeedIsCat()) {
212 query_str = query_str + "&actid=" + getActiveFeedId();
213 }
214
215 if (navigator.userAgent.match("Opera")) {
216 var date = new Date();
217 var timestamp = Math.round(date.getTime() / 1000);
218 query_str = query_str + "&ts=" + timestamp
219 }
220
221 if (fetch) query_str = query_str + "&fetch=yes";
222
223 var feeds_frame = document.getElementById("feeds-frame");
224
225 feeds_frame.src = query_str;
226 }
227
228 function catchupAllFeeds() {
229
230 var query_str = "backend.php?op=feeds&subop=catchupAll";
231
232 notify("Marking all feeds as read...");
233
234 var feeds_frame = document.getElementById("feeds-frame");
235
236 feeds_frame.src = query_str;
237
238 global_unread = 0;
239 updateTitle("");
240
241 }
242
243 function viewCurrentFeed(subop) {
244
245 // if (getActiveFeedId()) {
246 if (getActiveFeedId() != undefined) {
247 viewfeed(getActiveFeedId(), subop);
248 } else {
249 disableContainerChildren("headlinesToolbar", false, document);
250 // viewfeed(-1, subop); // FIXME
251 }
252 return false; // block unneeded form submits
253 }
254
255 function viewfeed(feed, subop) {
256 var f = window.frames["feeds-frame"];
257 f.viewfeed(feed, subop);
258 }
259
260 function timeout() {
261 scheduleFeedUpdate(false);
262
263 var refresh_time = getInitParam("feeds_frame_refresh");
264
265 if (!refresh_time) refresh_time = 600;
266
267 setTimeout("timeout()", refresh_time*1000);
268 }
269
270 function resetSearch() {
271 var searchbox = document.getElementById("searchbox")
272
273 if (searchbox.value != "" && getActiveFeedId()) {
274 searchbox.value = "";
275 viewfeed(getActiveFeedId(), "");
276 }
277 }
278
279 function searchCancel() {
280 closeInfoBox(true);
281 }
282
283 function search() {
284 closeInfoBox();
285 viewCurrentFeed(0, "");
286 }
287
288 function localPiggieFunction(enable) {
289 if (enable) {
290 var query_str = "backend.php?op=feeds&subop=piggie";
291
292 if (xmlhttp_ready(xmlhttp)) {
293
294 xmlhttp.open("GET", query_str, true);
295 xmlhttp.onreadystatechange=feedlist_callback;
296 xmlhttp.send(null);
297 }
298 }
299 }
300
301 // if argument is undefined, current subtitle is not updated
302 // use blank string to clear subtitle
303 function updateTitle(s) {
304 var tmp = "Tiny Tiny RSS";
305
306 if (s != undefined) {
307 current_subtitle = s;
308 }
309
310 if (global_unread > 0) {
311 tmp = tmp + " (" + global_unread + ")";
312 }
313
314 if (current_subtitle) {
315 tmp = tmp + " - " + current_subtitle;
316 }
317
318 if (active_title_text.length > 0) {
319 tmp = tmp + " > " + active_title_text;
320 }
321
322 document.title = tmp;
323 }
324
325 function genericSanityCheck() {
326
327 if (!xmlhttp) fatalError(1);
328
329 setCookie("ttrss_vf_test", "TEST");
330
331 if (getCookie("ttrss_vf_test") != "TEST") {
332 fatalError(2);
333 }
334
335 return true;
336 }
337
338 function init() {
339
340 try {
341
342 // this whole shebang is based on http://www.birnamdesigns.com/misc/busted2.html
343
344 if (arguments.callee.done) return;
345 arguments.callee.done = true;
346
347 disableContainerChildren("headlinesToolbar", true);
348
349 Form.disable("main_toolbar_form");
350
351 if (!genericSanityCheck())
352 return;
353
354 if (getURLParam('debug')) {
355 document.getElementById('debug_output').style.display = 'block';
356 debug('debug mode activated');
357 }
358
359 xmlhttp.open("GET", "backend.php?op=rpc&subop=sanityCheck", true);
360 xmlhttp.onreadystatechange=backend_sanity_check_callback;
361 xmlhttp.send(null);
362
363 } catch (e) {
364 exception_error("init", e);
365 }
366 }
367
368 function resize_feeds_frame() {
369 var f = document.getElementById("feeds-frame");
370 var tf = document.getElementById("mainFooter");
371 var th = document.getElementById("mainHeader");
372
373 var footer_height = 0;
374 var header_height = 0;
375
376 if (tf) {
377 footer_height = tf.scrollHeight;
378 }
379
380 if (th) {
381 header_height = th.scrollHeight;
382 }
383
384 f.style.height = document.body.scrollHeight - footer_height -
385 header_height - 50 + "px";
386 }
387
388 function init_second_stage() {
389
390 try {
391
392 cookie_lifetime = getCookie("ttrss_cltime");
393
394 delCookie("ttrss_vf_test");
395
396 updateFeedList(false, false);
397 document.onkeydown = hotkey_handler;
398
399 var tb = parent.document.forms["main_toolbar_form"];
400
401 dropboxSelect(tb.view_mode, getInitParam("toolbar_view_mode"));
402 dropboxSelect(tb.limit, getInitParam("toolbar_limit"));
403
404 daemon_enabled = getInitParam("daemon_enabled") == 1;
405 daemon_refresh_only = getInitParam("daemon_refresh_only") == 1;
406
407 // FIXME should be callled after window resize
408
409 var h = document.getElementById("headlines");
410 var c = document.getElementById("content");
411
412 if (navigator.userAgent.match("Opera")) {
413 resize_feeds_frame();
414 }
415
416 debug("second stage ok");
417
418 } catch (e) {
419 exception_error("init_second_stage", e);
420 }
421 }
422
423 function quickMenuChange() {
424 var chooser = document.getElementById("quickMenuChooser");
425 var opid = chooser[chooser.selectedIndex].value;
426
427 chooser.selectedIndex = 0;
428 quickMenuGo(opid);
429 }
430
431 function quickMenuGo(opid) {
432 try {
433
434 if (opid == "qmcPrefs") {
435 gotoPreferences();
436 }
437
438 if (opid == "qmcSearch") {
439 displayDlg("search", getActiveFeedId() + ":" + activeFeedIsCat());
440 return;
441 }
442
443 if (opid == "qmcAddFeed") {
444 displayDlg("quickAddFeed");
445 return;
446 }
447
448 if (opid == "qmcRemoveFeed") {
449 var actid = getActiveFeedId();
450
451 if (!actid || activeFeedIsCat()) {
452 alert("Please select some feed first.");
453 return;
454 }
455
456 var fn = getFeedName(actid);
457
458 if (confirm("Unsubscribe from " + fn + "?")) {
459 qfdDelete(actid);
460 }
461
462 return;
463 }
464
465 if (opid == "qmcUpdateFeeds") {
466 scheduleFeedUpdate(true);
467 return;
468 }
469
470 if (opid == "qmcCatchupAll") {
471 catchupAllFeeds();
472 return;
473 }
474
475 if (opid == "qmcShowOnlyUnread") {
476 toggleDispRead();
477 return;
478 }
479
480 if (opid == "qmcAddFilter") {
481 displayDlg("quickAddFilter", getActiveFeedId());
482 }
483 } catch (e) {
484 exception_error("quickMenuGo", e);
485 }
486 }
487
488 function qfdDelete(feed_id) {
489
490 notify("Removing feed...");
491
492 if (!xmlhttp_ready(xmlhttp)) {
493 printLockingError();
494 return
495 }
496
497 _qfd_deleted_feed = feed_id;
498
499 xmlhttp.open("GET", "backend.php?op=pref-feeds&quiet=1&subop=remove&ids=" + feed_id);
500 xmlhttp.onreadystatechange=dlg_frefresh_callback;
501 xmlhttp.send(null);
502 }
503
504
505 function updateFeedTitle(t) {
506 active_title_text = t;
507 updateTitle();
508 }
509
510 function toggleDispRead() {
511 try {
512
513 if (!xmlhttp_ready(xmlhttp)) {
514 printLockingError();
515 return
516 }
517
518 var hide_read_feeds = (getInitParam("hide_read_feeds") == "1");
519
520 hide_read_feeds = !hide_read_feeds;
521
522 debug("toggle_disp_read => " + hide_read_feeds);
523
524 hideOrShowFeeds(getFeedsContext().document, hide_read_feeds);
525
526 var query = "backend.php?op=rpc&subop=setpref" +
527 "&key=HIDE_READ_FEEDS&value=" + param_escape(hide_read_feeds);
528
529 storeInitParam("hide_read_feeds", hide_read_feeds, true);
530
531 new Ajax.Request(query);
532
533 } catch (e) {
534 exception_error("toggleDispRead", e);
535 }
536 }
537
538 function parse_runtime_info(elem) {
539 var param = elem.firstChild;
540
541 debug("parse_runtime_info");
542
543 while (param) {
544 var k = param.getAttribute("key");
545 var v = param.getAttribute("value");
546
547 debug("RI: " + k + " => " + v);
548
549 var w = document.getElementById("noDaemonWarning");
550
551 if (w) {
552 if (k == "daemon_is_running" && v != 1) {
553 w.style.display = "block";
554 } else {
555 w.style.display = "none";
556 }
557 }
558 param = param.nextSibling;
559 }
560 }
561
562 function catchupCurrentFeed() {
563
564 var fn = getFeedName(getActiveFeedId(), active_feed_is_cat);
565
566 var str = "Mark all articles in " + fn + " as read?";
567
568 /* if (active_feed_is_cat) {
569 str = "Mark all articles in this category as read?";
570 } */
571
572 if (confirm(str)) {
573 return viewCurrentFeed('MarkAllRead')
574 }
575 }
576