]> git.wh0rd.org - tt-rss.git/blob - tt-rss.js
generic popup user dialog mechanism
[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
8 var total_unread = 0;
9 var first_run = true;
10
11 var search_query = "";
12 var search_mode = "";
13
14 var display_tags = false;
15
16 var global_unread = 0;
17
18 /*@cc_on @*/
19 /*@if (@_jscript_version >= 5)
20 // JScript gives us Conditional compilation, we can cope with old IE versions.
21 // and security blocked creation of the objects.
22 try {
23 xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
24 } catch (e) {
25 try {
26 xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
27 } catch (E) {
28 xmlhttp = false;
29 }
30 }
31 @end @*/
32
33 if (!xmlhttp && typeof XMLHttpRequest!='undefined') {
34 xmlhttp = new XMLHttpRequest();
35 }
36
37 function toggleTags() {
38 display_tags = !display_tags;
39
40 var p = document.getElementById("dispSwitchPrompt");
41
42 if (display_tags) {
43 p.innerHTML = "display feeds";
44 } else {
45 p.innerHTML = "display tags";
46 }
47
48 updateFeedList();
49 }
50
51 function qaf_add_callback() {
52 if (xmlhttp.readyState == 4) {
53 updateFeedList(false, false);
54 closeDlg();
55 }
56 }
57
58 function dialog_refresh_callback() {
59 if (xmlhttp.readyState == 4) {
60 var dlg = document.getElementById("userDlg");
61
62 dlg.innerHTML = xmlhttp.responseText;
63 dlg.style.display = "block";
64 }
65 }
66
67 function refetch_callback() {
68 if (xmlhttp.readyState == 4) {
69
70 // document.title = "Tiny Tiny RSS";
71
72 if (!xmlhttp.responseXML) {
73 notify("refetch_callback: backend did not return valid XML");
74 return;
75 }
76
77 var reply = xmlhttp.responseXML.firstChild;
78
79 if (!reply) {
80 notify("refetch_callback: backend did not return expected XML object");
81 return;
82 }
83
84 var f_document = window.frames["feeds-frame"].document;
85
86 for (var l = 0; l < reply.childNodes.length; l++) {
87 var id = reply.childNodes[l].getAttribute("id");
88 var ctr = reply.childNodes[l].getAttribute("counter");
89
90 var feedctr = f_document.getElementById("FEEDCTR-" + id);
91 var feedu = f_document.getElementById("FEEDU-" + id);
92 var feedr = f_document.getElementById("FEEDR-" + id);
93
94 /* TODO figure out how to update this from viewfeed.js->view()
95 disabled for now...
96
97 if (id == "global-unread") {
98 global_unread = ctr;
99 } */
100
101 if (feedctr && feedu && feedr) {
102
103 feedu.innerHTML = ctr;
104
105 if (ctr > 0) {
106 feedctr.className = "odd";
107 if (!feedr.className.match("Unread")) {
108 feedr.className = feedr.className + "Unread";
109 }
110 } else {
111 feedctr.className = "invisible";
112 feedr.className = feedr.className.replace("Unread", "");
113 }
114 }
115 }
116
117 updateTitle("");
118 notify("All feeds updated.");
119
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)) {
128 xmlhttp.open("GET", query_str, true);
129 xmlhttp.onreadystatechange=feed_update_callback;
130 xmlhttp.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 updateTitle("Updating...");
144
145 var query_str = "backend.php?op=rpc&subop=";
146
147 if (force) {
148 query_str = query_str + "forceUpdateAllFeeds";
149 } else {
150 query_str = query_str + "updateAllFeeds";
151 }
152
153 if (xmlhttp_ready(xmlhttp)) {
154 xmlhttp.open("GET", query_str, true);
155 xmlhttp.onreadystatechange=refetch_callback;
156 xmlhttp.send(null);
157 } else {
158 printLockingError();
159 }
160 }
161
162 function updateFeedList(silent, fetch) {
163
164 // if (silent != true) {
165 // notify("Loading feed list...");
166 // }
167
168 var query_str = "backend.php?op=feeds";
169
170 if (display_tags) {
171 query_str = query_str + "&tags=1";
172 }
173
174 if (getActiveFeedId()) {
175 query_str = query_str + "&actid=" + getActiveFeedId();
176 }
177
178 if (fetch) query_str = query_str + "&fetch=yes";
179
180 var feeds_frame = document.getElementById("feeds-frame");
181
182 feeds_frame.src = query_str;
183 }
184
185 function catchupAllFeeds() {
186
187 var query_str = "backend.php?op=feeds&subop=catchupAll";
188
189 notify("Marking all feeds as read...");
190
191 var feeds_frame = document.getElementById("feeds-frame");
192
193 feeds_frame.src = query_str;
194
195 global_unread = 0;
196 updateTitle();
197
198 }
199
200 function viewCurrentFeed(skip, subop) {
201
202 if (getActiveFeedId()) {
203 viewfeed(getActiveFeedId(), skip, subop);
204 }
205 }
206
207 function viewfeed(feed, skip, subop) {
208
209 // notify("Loading headlines...");
210
211 enableHotkeys();
212
213 var searchbox = document.getElementById("searchbox");
214
215 if (searchbox) {
216 search_query = searchbox.value;
217 } else {
218 search_query = "";
219 }
220
221 var searchmodebox = document.getElementById("searchmodebox");
222
223 if (searchmodebox) {
224 search_mode = searchmodebox.value;
225 } else {
226 search_mode = "";
227 }
228
229 setCookie("ttrss_vf_smode", search_mode);
230
231 var viewbox = document.getElementById("viewbox");
232
233 var view_mode;
234
235 if (viewbox) {
236 view_mode = viewbox[viewbox.selectedIndex].text;
237 } else {
238 view_mode = "All Posts";
239 }
240
241 setCookie("ttrss_vf_vmode", view_mode);
242
243 var limitbox = document.getElementById("limitbox");
244
245 var limit;
246
247 if (limitbox) {
248 limit = limitbox[limitbox.selectedIndex].text;
249 setCookie("ttrss_vf_limit", limit);
250 } else {
251 limit = "All";
252 }
253
254 setActiveFeedId(feed);
255
256 var f_doc = frames["feeds-frame"].document;
257
258 // f_doc.getElementById("ACTFEEDID").innerHTML = feed;
259
260 if (subop == "MarkAllRead") {
261
262 var feedr = f_doc.getElementById("FEEDR-" + feed);
263 var feedctr = f_doc.getElementById("FEEDCTR-" + feed);
264
265 feedctr.className = "invisible";
266
267 if (feedr.className.match("Unread")) {
268 feedr.className = feedr.className.replace("Unread", "");
269 }
270 }
271
272 var query = "backend.php?op=viewfeed&feed=" + param_escape(feed) +
273 "&skip=" + param_escape(skip) + "&subop=" + param_escape(subop) +
274 "&view=" + param_escape(view_mode) + "&limit=" + limit +
275 "&smode=" + param_escape(search_mode);
276
277 if (search_query != "") {
278 query = query + "&search=" + param_escape(search_query);
279 }
280
281 var headlines_frame = parent.frames["headlines-frame"];
282
283 headlines_frame.location.href = query + "&addheader=true";
284
285 cleanSelected("feedsList");
286 var feedr = document.getElementById("FEEDR-" + feed);
287 if (feedr) {
288 feedr.className = feedr.className + "Selected";
289 }
290
291 disableContainerChildren("headlinesToolbar", false, doc);
292
293 var btnMarkAsRead = document.getElementById("btnMarkFeedAsRead");
294
295 if (btnMarkAsRead && !isNumeric(feed)) {
296 btnMarkAsRead.disabled = true;
297 btnMarkAsRead.className = "disabledButton";
298 }
299
300 // notify("");
301
302 }
303
304
305 function timeout() {
306 scheduleFeedUpdate(true);
307 setTimeout("timeout()", 1800*1000);
308 }
309
310 function resetSearch() {
311 var searchbox = document.getElementById("searchbox")
312
313 if (searchbox.value != "" && getActiveFeedId()) {
314 searchbox.value = "";
315 viewfeed(getActiveFeedId(), 0, "");
316 }
317 }
318
319 function search() {
320 viewCurrentFeed(0, "");
321 }
322
323 function localPiggieFunction(enable) {
324 if (enable) {
325 var query_str = "backend.php?op=feeds&subop=piggie";
326
327 if (xmlhttp_ready(xmlhttp)) {
328
329 xmlhttp.open("GET", query_str, true);
330 xmlhttp.onreadystatechange=feedlist_callback;
331 xmlhttp.send(null);
332 }
333 }
334 }
335
336 function localHotkeyHandler(keycode) {
337
338 /* if (keycode == 78) {
339 return moveToPost('next');
340 }
341
342 if (keycode == 80) {
343 return moveToPost('prev');
344 } */
345
346 if (keycode == 82) {
347 return scheduleFeedUpdate(true);
348 }
349
350 if (keycode == 85) {
351 return viewfeed(getActiveFeedId(), 0, "ForceUpdate");
352 }
353
354 // notify("KC: " + keycode);
355
356 }
357
358 function updateTitle(s) {
359 var tmp = "Tiny Tiny RSS";
360
361 if (global_unread > 0) {
362 tmp = tmp + " (" + global_unread + ")";
363 }
364
365 if (s) {
366 tmp = tmp + " - " + s;
367 }
368 document.title = tmp;
369 }
370
371 function genericSanityCheck() {
372
373 if (!xmlhttp) {
374 document.getElementById("headlines").innerHTML =
375 "<b>Fatal error:</b> This program requires XmlHttpRequest " +
376 "to function properly. Your browser doesn't seem to support it.";
377 return false;
378 }
379
380 setCookie("ttrss_vf_test", "TEST");
381 if (getCookie("ttrss_vf_test") != "TEST") {
382
383 document.getElementById("headlines").innerHTML =
384 "<b>Fatal error:</b> This program requires cookies " +
385 "to function properly. Your browser doesn't seem to support them.";
386
387 return false;
388 }
389
390 return true;
391 }
392
393 function init() {
394
395 disableContainerChildren("headlinesToolbar", true);
396
397 if (!genericSanityCheck())
398 return;
399
400 setCookie("ttrss_vf_actfeed", "");
401
402 updateFeedList(false, false);
403 document.onkeydown = hotkey_handler;
404
405 setTimeout("timeout()", 1800*1000);
406 scheduleFeedUpdate(true);
407
408 var content = document.getElementById("content");
409
410 if (getCookie("ttrss_vf_vmode")) {
411 var viewbox = document.getElementById("viewbox");
412 viewbox.value = getCookie("ttrss_vf_vmode");
413 }
414
415 if (getCookie("ttrss_vf_limit")) {
416 var limitbox = document.getElementById("limitbox");
417 limitbox.value = getCookie("ttrss_vf_limit");
418 }
419
420 // if (getCookie("ttrss_vf_actfeed")) {
421 // viewfeed(getCookie("ttrss_vf_actfeed"), 0, '');
422 // }
423
424 }
425
426 function quickMenuGo() {
427 var chooser = document.getElementById("quickMenuChooser");
428
429 var opname = chooser[chooser.selectedIndex].text;
430
431 if (opname == "Preferences") {
432 gotoPreferences();
433 }
434
435 if (opname == "Add new feed") {
436 displayDlg("quickAddFeed");
437 }
438 }
439
440 function qafAdd() {
441
442 if (!xmlhttp_ready(xmlhttp)) {
443 printLockingError();
444 return
445 }
446
447 var link = document.getElementById("qafInput");
448
449 if (link.value.length == 0) {
450 notify("Missing feed URL.");
451 } else {
452 notify("Adding feed...");
453
454 var feeds_doc = window.frames["feeds-frame"].document;
455
456 feeds_doc.location.href = "backend.php?op=error&msg=Loading,%20please wait...";
457
458 xmlhttp.open("GET", "backend.php?op=pref-feeds&subop=add&link=" +
459 param_escape(link.value), true);
460 xmlhttp.onreadystatechange=qaf_add_callback;
461 xmlhttp.send(null);
462
463 link.value = "";
464
465 }
466 }
467
468 function displayDlg(id) {
469
470 xmlhttp.open("GET", "backend.php?op=dlg&id=" +
471 param_escape(id), true);
472 xmlhttp.onreadystatechange=dialog_refresh_callback;
473 xmlhttp.send(null);
474
475 }
476
477 function closeDlg() {
478 var dlg = document.getElementById("userDlg");
479 dlg.style.display = "none";
480 }
481