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