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