]>
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 _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 Object(); | |
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 | 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 | ||
97 | function 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 | ||
146 | function 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 | ||
196 | function 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 | ||
225 | function 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 | ||
240 | function 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 | ||
251 | function viewfeed(feed, skip, subop) { | |
252 | var f = window.frames["feeds-frame"]; | |
253 | f.viewfeed(feed, skip, subop); | |
254 | } | |
255 | ||
256 | function 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 | ||
266 | function resetSearch() { | |
267 | var searchbox = document.getElementById("searchbox") | |
268 | ||
269 | if (searchbox.value != "" && getActiveFeedId()) { | |
270 | searchbox.value = ""; | |
271 | viewfeed(getActiveFeedId(), 0, ""); | |
272 | } | |
273 | } | |
274 | ||
275 | function searchCancel() { | |
276 | closeInfoBox(true); | |
277 | } | |
278 | ||
279 | function search() { | |
280 | closeInfoBox(); | |
281 | viewCurrentFeed(0, ""); | |
282 | } | |
283 | ||
284 | function 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 | |
299 | function 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 | ||
321 | function 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 | ||
334 | function 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 | ||
364 | function resize_feeds_frame() { | |
365 | var f = document.getElementById("feeds-frame"); | |
366 | var tf = document.getElementById("mainFooter"); | |
367 | var th = document.getElementById("mainHeader"); | |
368 | ||
369 | var footer_height = 0; | |
370 | var header_height = 0; | |
371 | ||
372 | if (tf) { | |
373 | footer_height = tf.scrollHeight; | |
374 | } | |
375 | ||
376 | if (th) { | |
377 | header_height = th.scrollHeight; | |
378 | } | |
379 | ||
380 | f.style.height = document.body.scrollHeight - footer_height - | |
381 | header_height - 50 + "px"; | |
382 | } | |
383 | ||
384 | function init_second_stage() { | |
385 | ||
386 | try { | |
387 | ||
388 | cookie_lifetime = getCookie("ttrss_cltime"); | |
389 | ||
390 | delCookie("ttrss_vf_test"); | |
391 | ||
392 | updateFeedList(false, false); | |
393 | document.onkeydown = hotkey_handler; | |
394 | ||
395 | var tb = parent.document.forms["main_toolbar_form"]; | |
396 | ||
397 | dropboxSelect(tb.view_mode, getInitParam("toolbar_view_mode")); | |
398 | dropboxSelect(tb.limit, getInitParam("toolbar_limit")); | |
399 | ||
400 | daemon_enabled = getInitParam("daemon_enabled"); | |
401 | ||
402 | // FIXME should be callled after window resize | |
403 | ||
404 | var h = document.getElementById("headlines"); | |
405 | var c = document.getElementById("content"); | |
406 | ||
407 | if (navigator.userAgent.match("Opera")) { | |
408 | resize_feeds_frame(); | |
409 | } | |
410 | ||
411 | debug("second stage ok"); | |
412 | ||
413 | } catch (e) { | |
414 | exception_error("init_second_stage", e); | |
415 | } | |
416 | } | |
417 | ||
418 | function quickMenuChange() { | |
419 | var chooser = document.getElementById("quickMenuChooser"); | |
420 | var opid = chooser[chooser.selectedIndex].value; | |
421 | ||
422 | chooser.selectedIndex = 0; | |
423 | quickMenuGo(opid); | |
424 | } | |
425 | ||
426 | function quickMenuGo(opid) { | |
427 | try { | |
428 | ||
429 | if (opid == "qmcPrefs") { | |
430 | gotoPreferences(); | |
431 | } | |
432 | ||
433 | if (opid == "qmcSearch") { | |
434 | displayDlg("search", getActiveFeedId()); | |
435 | return; | |
436 | } | |
437 | ||
438 | if (opid == "qmcAddFeed") { | |
439 | displayDlg("quickAddFeed"); | |
440 | return; | |
441 | } | |
442 | ||
443 | if (opid == "qmcRemoveFeed") { | |
444 | var actid = getActiveFeedId(); | |
445 | ||
446 | if (!actid) { | |
447 | alert("Please select some feed first."); | |
448 | return; | |
449 | } | |
450 | ||
451 | var fn = getFeedName(actid); | |
452 | ||
453 | if (confirm("Unsubscribe from " + fn + "?")) { | |
454 | qfdDelete(actid); | |
455 | } | |
456 | ||
457 | return; | |
458 | } | |
459 | ||
460 | if (opid == "qmcUpdateFeeds") { | |
461 | scheduleFeedUpdate(true); | |
462 | return; | |
463 | } | |
464 | ||
465 | if (opid == "qmcCatchupAll") { | |
466 | catchupAllFeeds(); | |
467 | return; | |
468 | } | |
469 | ||
470 | if (opid == "qmcShowOnlyUnread") { | |
471 | toggleDispRead(); | |
472 | return; | |
473 | } | |
474 | ||
475 | if (opid == "qmcAddFilter") { | |
476 | displayDlg("quickAddFilter", getActiveFeedId()); | |
477 | } | |
478 | } catch (e) { | |
479 | exception_error("quickMenuGo", e); | |
480 | } | |
481 | } | |
482 | ||
483 | function qfdDelete(feed_id) { | |
484 | ||
485 | notify("Removing feed..."); | |
486 | ||
487 | if (!xmlhttp_ready(xmlhttp)) { | |
488 | printLockingError(); | |
489 | return | |
490 | } | |
491 | ||
492 | _qfd_deleted_feed = feed_id; | |
493 | ||
494 | xmlhttp.open("GET", "backend.php?op=pref-feeds&quiet=1&subop=remove&ids=" + feed_id); | |
495 | xmlhttp.onreadystatechange=dlg_frefresh_callback; | |
496 | xmlhttp.send(null); | |
497 | } | |
498 | ||
499 | ||
500 | function updateFeedTitle(t) { | |
501 | active_title_text = t; | |
502 | updateTitle(); | |
503 | } | |
504 | ||
505 | function toggleDispRead() { | |
506 | try { | |
507 | ||
508 | if (!xmlhttp_ready(xmlhttp)) { | |
509 | printLockingError(); | |
510 | return | |
511 | } | |
512 | ||
513 | var hide_read_feeds = (getInitParam("hide_read_feeds") == "1"); | |
514 | ||
515 | hide_read_feeds = !hide_read_feeds; | |
516 | ||
517 | debug("toggle_disp_read => " + hide_read_feeds); | |
518 | ||
519 | hideOrShowFeeds(getFeedsContext().document, hide_read_feeds); | |
520 | ||
521 | var query = "backend.php?op=rpc&subop=setpref" + | |
522 | "&key=HIDE_READ_FEEDS&value=" + param_escape(hide_read_feeds); | |
523 | ||
524 | storeInitParam("hide_read_feeds", hide_read_feeds, true); | |
525 | ||
526 | new Ajax.Request(query); | |
527 | ||
528 | } catch (e) { | |
529 | exception_error("toggleDispRead", e); | |
530 | } | |
531 | } | |
532 | ||
533 | function parse_runtime_info(elem) { | |
534 | var param = elem.firstChild; | |
535 | ||
536 | debug("parse_runtime_info"); | |
537 | ||
538 | while (param) { | |
539 | var k = param.getAttribute("key"); | |
540 | var v = param.getAttribute("value"); | |
541 | ||
542 | debug("RI: " + k + " => " + v); | |
543 | ||
544 | var w = document.getElementById("noDaemonWarning"); | |
545 | ||
546 | if (w) { | |
547 | if (k == "daemon_is_running" && v != 1) { | |
548 | w.style.display = "block"; | |
549 | } else { | |
550 | w.style.display = "none"; | |
551 | } | |
552 | } | |
553 | param = param.nextSibling; | |
554 | } | |
555 | } |