]> git.wh0rd.org - tt-rss.git/blob - tt-rss.js
add sanity check & debug mode to prefs; misc code cleanups
[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("<b>xmlhttp seems to be stuck, aborting</b>");
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
394 debug("second stage ok");
395
396 } catch (e) {
397 exception_error("init_second_stage", e);
398 }
399 }
400
401 function quickMenuChange() {
402 var chooser = document.getElementById("quickMenuChooser");
403 var opid = chooser[chooser.selectedIndex].value;
404
405 chooser.selectedIndex = 0;
406 quickMenuGo(opid);
407 }
408
409 function quickMenuGo(opid) {
410 try {
411
412 if (opid == "qmcPrefs") {
413 gotoPreferences();
414 }
415
416 if (opid == "qmcSearch") {
417 displayDlg("search", getActiveFeedId());
418 return;
419 }
420
421 if (opid == "qmcAddFeed") {
422 displayDlg("quickAddFeed");
423 return;
424 }
425
426 if (opid == "qmcRemoveFeed") {
427 var actid = getActiveFeedId();
428
429 if (!actid) {
430 alert("Please select some feed first.");
431 return;
432 }
433
434 if (confirm("Unsubscribe from current feed?")) {
435 qfdDelete(actid);
436 }
437
438 return;
439 }
440
441 if (opid == "qmcUpdateFeeds") {
442 scheduleFeedUpdate(true);
443 return;
444 }
445
446 if (opid == "qmcCatchupAll") {
447 catchupAllFeeds();
448 return;
449 }
450
451 if (opid == "qmcShowOnlyUnread") {
452 toggleDispRead();
453 return;
454 }
455
456 if (opid == "qmcAddFilter") {
457 displayDlg("quickAddFilter", getActiveFeedId());
458 }
459 } catch (e) {
460 exception_error("quickMenuGo", e);
461 }
462 }
463
464 function qfdDelete(feed_id) {
465
466 notify("Removing feed...");
467
468 if (!xmlhttp_ready(xmlhttp)) {
469 printLockingError();
470 return
471 }
472
473 _qfd_deleted_feed = feed_id;
474
475 xmlhttp.open("GET", "backend.php?op=pref-feeds&quiet=1&subop=remove&ids=" + feed_id);
476 xmlhttp.onreadystatechange=dlg_frefresh_callback;
477 xmlhttp.send(null);
478 }
479
480
481 function updateFeedTitle(t) {
482 active_title_text = t;
483 updateTitle();
484 }
485
486 function toggleDispRead() {
487 try {
488
489 if (!xmlhttp_ready(xmlhttp)) {
490 printLockingError();
491 return
492 }
493
494 var hide_read_feeds = (getInitParam("hide_read_feeds") == "1");
495
496 hide_read_feeds = !hide_read_feeds;
497
498 debug("toggle_disp_read => " + hide_read_feeds);
499
500 hideOrShowFeeds(getFeedsContext().document, hide_read_feeds);
501
502 var query = "backend.php?op=rpc&subop=setpref" +
503 "&key=HIDE_READ_FEEDS&value=" + param_escape(hide_read_feeds);
504
505 storeInitParam("hide_read_feeds", hide_read_feeds, true);
506
507 new Ajax.Request(query);
508
509 } catch (e) {
510 exception_error("toggleDispRead", e);
511 }
512 }
513
514