]> git.wh0rd.org - tt-rss.git/blob - tt-rss.js
iframify main interface, numerous javascript cleanups
[tt-rss.git] / tt-rss.js
1 /*
2 This program is Copyright (c) 2003-2005 Andrew Dolgov <cthulhoo@gmail.com>
3 Licensed under GPL v.2 or (at your preference) any later version.
4 */
5
6 var xmlhttp = false;
7 var xmlhttp_rpc = false;
8 var xmlhttp_view = false;
9
10 var total_unread = 0;
11 var first_run = true;
12
13 var active_post_id = false;
14 var active_feed_id = false;
15 var active_offset = false;
16
17 var total_feed_entries = false;
18
19 var search_query = "";
20
21 /*@cc_on @*/
22 /*@if (@_jscript_version >= 5)
23 // JScript gives us Conditional compilation, we can cope with old IE versions.
24 // and security blocked creation of the objects.
25 try {
26 xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
27 } catch (e) {
28 try {
29 xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
30 xmlhttp_rpc = new ActiveXObject("Microsoft.XMLHTTP");
31 xmlhttp_view = new ActiveXObject("Microsoft.XMLHTTP");
32 } catch (E) {
33 xmlhttp = false;
34 xmlhttp_rpc = false;
35 xmlhttp_view = false;
36 }
37 }
38 @end @*/
39
40 if (!xmlhttp && typeof XMLHttpRequest!='undefined') {
41 xmlhttp = new XMLHttpRequest();
42 xmlhttp_rpc = new XMLHttpRequest();
43 xmlhttp_view = new XMLHttpRequest();
44 }
45
46 function feedlist_callback() {
47 var container = document.getElementById('feeds');
48 if (xmlhttp.readyState == 4) {
49 container.innerHTML=xmlhttp.responseText;
50
51 if (first_run) {
52 scheduleFeedUpdate(false);
53 first_run = false;
54 } else {
55 notify("");
56 }
57 }
58 }
59
60 /*
61 function viewfeed_callback() {
62 var container = document.getElementById('headlines');
63 if (xmlhttp.readyState == 4) {
64 container.innerHTML = xmlhttp.responseText;
65
66 var factive = document.getElementById("FACTIVE");
67 var funread = document.getElementById("FUNREAD");
68 var ftotal = document.getElementById("FTOTAL");
69
70 if (_viewfeed_autoselect_first == true) {
71 _viewfeed_autoselect_first = false;
72 view(getFirstVisibleHeadlineId(), active_feed_id);
73 }
74
75 if (_viewfeed_autoselect_last == true) {
76 _viewfeed_autoselect_last = false;
77 view(getLastVisibleHeadlineId(), active_feed_id);
78 }
79
80 if (ftotal && factive && funread) {
81 var feed_id = factive.innerHTML;
82
83 var feedr = document.getElementById("FEEDR-" + feed_id);
84 var feedt = document.getElementById("FEEDT-" + feed_id);
85 var feedu = document.getElementById("FEEDU-" + feed_id);
86
87 feedt.innerHTML = ftotal.innerHTML;
88 feedu.innerHTML = funread.innerHTML;
89
90 total_feed_entries = ftotal.innerHTML;
91
92 if (feedu.innerHTML > 0 && !feedr.className.match("Unread")) {
93 feedr.className = feedr.className + "Unread";
94 } else if (feedu.innerHTML <= 0) {
95 feedr.className = feedr.className.replace("Unread", "");
96 }
97
98 cleanSelected("feedsList");
99
100 feedr.className = feedr.className + "Selected";
101 }
102
103 var searchbox = document.getElementById("searchbox");
104 searchbox.value = search_query;
105
106 markHeadline(active_post_id);
107
108 notify("");
109
110 }
111 } */
112
113 function refetch_callback() {
114
115 if (xmlhttp_rpc.readyState == 4) {
116 notify("All feeds updated");
117 var container = document.getElementById('feeds');
118 container.innerHTML = xmlhttp_rpc.responseText;
119 document.title = "Tiny Tiny RSS";
120 }
121 }
122
123 function updateFeed(feed_id) {
124
125 var query_str = "backend.php?op=rpc&subop=updateFeed&feed=" + feed_id;
126
127 if (xmlhttp_ready(xmlhttp_rpc)) {
128 xmlhttp_rpc.open("GET", query_str, true);
129 xmlhttp_rpc.onreadystatechange=feed_update_callback;
130 xmlhttp_rpc.send(null);
131 } else {
132 printLockingError();
133 }
134
135 }
136
137 function scheduleFeedUpdate(force) {
138
139 notify("Updating feeds in background...");
140
141 document.title = "Tiny Tiny RSS - Updating...";
142
143 var query_str = "backend.php?op=rpc&subop=";
144
145 if (force) {
146 query_str = query_str + "forceUpdateAllFeeds";
147 } else {
148 query_str = query_str + "updateAllFeeds";
149 }
150
151 if (xmlhttp_ready(xmlhttp_rpc)) {
152 xmlhttp_rpc.open("GET", query_str, true);
153 xmlhttp_rpc.onreadystatechange=refetch_callback;
154 xmlhttp_rpc.send(null);
155 } else {
156 printLockingError();
157 }
158 }
159
160 function updateFeedList(silent, fetch) {
161
162 if (silent != true) {
163 notify("Loading feed list...");
164 }
165
166 var query_str = "backend.php?op=feeds";
167
168 if (fetch) query_str = query_str + "&fetch=yes";
169
170 if (xmlhttp_ready(xmlhttp)) {
171 xmlhttp.open("GET", query_str, true);
172 xmlhttp.onreadystatechange=feedlist_callback;
173 xmlhttp.send(null);
174 } else {
175 printLockingError();
176 }
177 }
178
179 /*
180 function catchupPage(feed) {
181
182 if (!xmlhttp_ready(xmlhttp)) {
183 printLockingError();
184 return
185 }
186
187 var content = document.getElementById("headlinesList");
188
189 var rows = new Array();
190
191 for (i = 0; i < content.rows.length; i++) {
192 var row_id = content.rows[i].id.replace("RROW-", "");
193 if (row_id.length > 0) {
194 if (content.rows[i].className.match("Unread")) {
195 rows.push(row_id);
196 content.rows[i].className = content.rows[i].className.replace("Unread", "");
197 }
198
199 var upd_img_pic = document.getElementById("FUPDPIC-" + row_id);
200 if (upd_img_pic) {
201 upd_img_pic.innerHTML = "";
202 }
203 }
204 }
205
206 if (rows.length > 0) {
207
208 var feedr = document.getElementById("FEEDR-" + feed);
209 var feedu = document.getElementById("FEEDU-" + feed);
210
211 feedu.innerHTML = feedu.innerHTML - rows.length;
212
213 if (feedu.innerHTML > 0 && !feedr.className.match("Unread")) {
214 feedr.className = feedr.className + "Unread";
215 } else if (feedu.innerHTML <= 0) {
216 feedr.className = feedr.className.replace("Unread", "");
217 }
218
219 var query_str = "backend.php?op=rpc&subop=catchupPage&ids=" +
220 param_escape(rows.toString());
221
222 notify("Marking this page as read...");
223
224 var button = document.getElementById("btnCatchupPage");
225
226 if (button) {
227 button.className = "disabledButton";
228 button.href = "";
229 }
230
231 xmlhttp.open("GET", query_str, true);
232 xmlhttp.onreadystatechange=notify_callback;
233 xmlhttp.send(null);
234
235 } else {
236 notify("No unread items on this page.");
237
238 }
239 } */
240
241 function catchupAllFeeds() {
242
243 if (!xmlhttp_ready(xmlhttp)) {
244 printLockingError();
245 return
246 }
247 var query_str = "backend.php?op=feeds&subop=catchupAll";
248
249 notify("Marking all feeds as read...");
250
251 xmlhttp.open("GET", query_str, true);
252 xmlhttp.onreadystatechange=feedlist_callback;
253 xmlhttp.send(null);
254
255 }
256
257 function viewCurrentFeed(skip, subop) {
258 if (active_feed_id) {
259 viewfeed(active_feed_id, skip, subop);
260 }
261 }
262
263 function viewfeed(feed, skip, subop) {
264
265 enableHotkeys();
266
267 var searchbox = document.getElementById("searchbox");
268
269 if (searchbox) {
270 search_query = searchbox.value;
271 } else {
272 search_query = "";
273 }
274
275 var viewbox = document.getElementById("viewbox");
276
277 var view_mode;
278
279 if (viewbox) {
280 view_mode = viewbox.value;
281 } else {
282 view_mode = "All Posts";
283 }
284
285 if (skip < 0 || skip > total_feed_entries) {
286 return;
287 }
288
289 if (!xmlhttp_ready(xmlhttp)) {
290 printLockingError();
291 return
292 }
293
294 if (active_feed_id != feed || skip != active_offset) {
295 active_post_id = false;
296 }
297
298 active_feed_id = feed;
299 active_offset = skip;
300
301 if (subop == "MarkAllRead") {
302
303 var feedr = document.getElementById("FEEDR-" + feed);
304 var feedt = document.getElementById("FEEDT-" + feed);
305 var feedu = document.getElementById("FEEDU-" + feed);
306
307 feedu.innerHTML = "0";
308
309 if (feedr.className.match("Unread")) {
310 feedr.className = feedr.className.replace("Unread", "");
311 }
312 }
313
314 var query = "backend.php?op=viewfeed&feed=" + param_escape(feed) +
315 "&skip=" + param_escape(skip) + "&subop=" + param_escape(subop) +
316 "&view=" + param_escape(view_mode);
317
318 if (search_query != "") {
319 query = query + "&search=" + param_escape(search_query);
320 }
321
322 var headlines_frame = document.getElementById("headlines-frame");
323
324 headlines_frame.src = query + "&addheader=true";
325
326 var feedr = document.getElementById("FEEDR-" + feed);
327
328 cleanSelected("feedsList");
329 feedr.className = feedr.className + "Selected";
330
331 var ftitle_d = document.getElementById("headlinesTitle");
332 var ftitle_s = document.getElementById("FEEDN-" + feed);
333
334 ftitle_d.innerHTML = ftitle_s.innerHTML;
335
336 notify("");
337
338 }
339
340 /*
341 function view(id,feed_id) {
342
343 enableHotkeys();
344
345 if (!xmlhttp_ready(xmlhttp_view)) {
346 printLockingError();
347 return
348 }
349
350 var crow = document.getElementById("RROW-" + id);
351
352 if (crow.className.match("Unread")) {
353 var umark = document.getElementById("FEEDU-" + feed_id);
354 umark.innerHTML = umark.innerHTML - 1;
355 crow.className = crow.className.replace("Unread", "");
356
357 if (umark.innerHTML == "0") {
358 var feedr = document.getElementById("FEEDR-" + feed_id);
359 feedr.className = feedr.className.replace("Unread", "");
360 }
361
362 total_unread--;
363 }
364
365 cleanSelected("headlinesList");
366 // crow.className = crow.className + "Selected";
367
368 var upd_img_pic = document.getElementById("FUPDPIC-" + id);
369
370 if (upd_img_pic) {
371 upd_img_pic.innerHTML = "";
372 }
373
374 // document.getElementById('content').innerHTML='Loading, please wait...';
375
376 var unread_rows = getVisibleUnreadHeadlines();
377
378 if (unread_rows.length == 0) {
379 var button = document.getElementById("btnCatchupPage");
380 if (button) {
381 button.className = "disabledButton";
382 button.href = "";
383 }
384 }
385
386 active_post_id = id;
387
388 var content = document.getElementById("content-frame");
389
390 content.src = "backend.php?op=view&addheader=true&id=" + param_escape(id);
391 markHeadline(active_post_id);
392
393 }
394 */
395
396 function timeout() {
397 scheduleFeedUpdate(true);
398 setTimeout("timeout()", 1800*1000);
399 }
400
401 function resetSearch() {
402 document.getElementById("searchbox").value = "";
403 viewfeed(active_feed_id, 0, "");
404 }
405
406 function search() {
407 if (active_feed_id) {
408 viewfeed(active_feed_id, 0, "");
409 } else {
410 notify("Please select some feed first.");
411 }
412 }
413
414 function localPiggieFunction(enable) {
415 if (enable) {
416 var query_str = "backend.php?op=feeds&subop=piggie";
417
418 if (xmlhttp_ready(xmlhttp)) {
419
420 xmlhttp.open("GET", query_str, true);
421 xmlhttp.onreadystatechange=feedlist_callback;
422 xmlhttp.send(null);
423 }
424 }
425 }
426
427 /*
428 function moveToPost(mode) {
429
430 var rows = getVisibleHeadlineIds();
431
432 var prev_id;
433 var next_id;
434
435 if (active_post_id == false) {
436 next_id = getFirstVisibleHeadlineId();
437 prev_id = getLastVisibleHeadlineId();
438 } else {
439 for (var i = 0; i < rows.length; i++) {
440 if (rows[i] == active_post_id) {
441 prev_id = rows[i-1];
442 next_id = rows[i+1];
443 }
444 }
445 }
446
447 if (mode == "next") {
448 if (next_id != undefined) {
449 view(next_id, active_feed_id);
450 } else {
451 _viewfeed_autoselect_first = true;
452 viewfeed(active_feed_id, active_offset+15);
453 }
454 }
455
456 if (mode == "prev") {
457 if ( prev_id != undefined) {
458 view(prev_id, active_feed_id);
459 } else {
460 _viewfeed_autoselect_last = true;
461 viewfeed(active_feed_id, active_offset-15);
462 }
463 }
464
465 }
466 */
467
468 function localHotkeyHandler(keycode) {
469
470 /* if (keycode == 78) {
471 return moveToPost('next');
472 }
473
474 if (keycode == 80) {
475 return moveToPost('prev');
476 } */
477
478 if (keycode == 82) {
479 return scheduleFeedUpdate(true);
480 }
481
482 if (keycode == 85) {
483 return viewfeed(active_feed_id, active_offset, "ForceUpdate");
484 }
485
486 // notify("KC: " + keycode);
487
488 }
489
490 function init() {
491
492 // IE kludge
493
494 if (xmlhttp && !xmlhttp_rpc) {
495 xmlhttp_rpc = xmlhttp;
496 xmlhttp_view = xmlhttp;
497 }
498
499 if (!xmlhttp || !xmlhttp_rpc || !xmlhttp_view) {
500 document.getElementById("headlines").innerHTML =
501 "<b>Fatal error:</b> This program needs XmlHttpRequest " +
502 "to function properly. Your browser doesn't seem to support it.";
503 return;
504 }
505
506 updateFeedList(false, false);
507 document.onkeydown = hotkey_handler;
508 setTimeout("timeout()", 1800*1000);
509
510 var content = document.getElementById("content");
511 }