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