]> git.wh0rd.org - tt-rss.git/blob - viewfeed.js
rework headline subtoolbar
[tt-rss.git] / viewfeed.js
1 var active_post_id = false;
2 var _catchup_callback_func = false;
3 var last_article_view = false;
4 var active_real_feed_id = false;
5
6 var _tag_active_post_id = false;
7 var _tag_active_feed_id = false;
8
9 // FIXME: kludge, needs proper implementation
10 var _reload_feedlist_after_view = false;
11
12 function catchup_callback() {
13 if (xmlhttp_rpc.readyState == 4) {
14 try {
15 debug("catchup_callback");
16 if (_catchup_callback_func) {
17 setTimeout(_catchup_callback_func, 100);
18 }
19 all_counters_callback();
20 } catch (e) {
21 exception_error("catchup_callback", e);
22 }
23 }
24 }
25
26 function headlines_callback() {
27 if (xmlhttp.readyState == 4) {
28 debug("headlines_callback");
29 var f = document.getElementById("headlines-frame");
30 try {
31 f.scrollTop = 0;
32 } catch (e) { };
33 f.innerHTML = xmlhttp.responseText;
34 update_all_counters();
35 if (typeof correctPNG != 'undefined') {
36 correctPNG();
37 }
38 notify("");
39 }
40 }
41
42 function article_callback() {
43 if (xmlhttp.readyState == 4) {
44 debug("article_callback");
45 var f = document.getElementById("content-frame");
46 try {
47 f.scrollTop = 0;
48 } catch (e) { };
49 f.innerHTML = xmlhttp.responseText;
50
51 var date = new Date();
52 last_article_view = date.getTime() / 1000;
53
54 if (typeof correctPNG != 'undefined') {
55 correctPNG();
56 }
57
58 if (_reload_feedlist_after_view) {
59 setTimeout('updateFeedList(false, false)', 50);
60 _reload_feedlist_after_view = false;
61 } else {
62 update_all_counters();
63 }
64 }
65 }
66
67 function view(id, feed_id, skip_history) {
68
69 try {
70 debug("loading article: " + id + "/" + feed_id);
71
72 active_real_feed_id = feed_id;
73
74 if (!skip_history) {
75 history_push("ARTICLE:" + id + ":" + feed_id);
76 }
77
78 enableHotkeys();
79
80 active_post_id = id;
81 //setActiveFeedId(feed_id);
82
83 var query = "backend.php?op=view&id=" + param_escape(id) +
84 "&feed=" + param_escape(feed_id);
85
86 var date = new Date();
87
88 if (!xmlhttp_ready(xmlhttp) && last_article_view < date.getTime() / 1000 - 15) {
89 debug("<b>xmlhttp seems to be stuck at view, aborting</b>");
90 xmlhttp.abort();
91 }
92
93 if (xmlhttp_ready(xmlhttp)) {
94
95 cleanSelected("headlinesList");
96
97 var crow = document.getElementById("RROW-" + active_post_id);
98 crow.className = crow.className.replace("Unread", "");
99
100 var upd_img_pic = document.getElementById("FUPDPIC-" + active_post_id);
101
102 if (upd_img_pic) {
103 upd_img_pic.src = "images/blank_icon.gif";
104 }
105
106 selectTableRowsByIdPrefix('headlinesList', 'RROW-', 'RCHK-', false);
107 markHeadline(active_post_id);
108
109 xmlhttp.open("GET", query, true);
110 xmlhttp.onreadystatechange=article_callback;
111 xmlhttp.send(null);
112 } else {
113 debug("xmlhttp busy (@view)");
114 printLockingError();
115 }
116
117 } catch (e) {
118 exception_error("view", e);
119 }
120 }
121
122 function toggleMark(id) {
123
124 if (!xmlhttp_ready(xmlhttp_rpc)) {
125 printLockingError();
126 return;
127 }
128
129 var query = "backend.php?op=rpc&id=" + id + "&subop=mark";
130
131 var mark_img = document.getElementById("FMARKPIC-" + id);
132 var vfeedu = document.getElementById("FEEDU--1");
133 var crow = document.getElementById("RROW-" + id);
134
135 if (mark_img.alt != "Reset mark") {
136 mark_img.src = "images/mark_set.png";
137 mark_img.alt = "Reset mark";
138 query = query + "&mark=1";
139
140 if (vfeedu && crow.className.match("Unread")) {
141 vfeedu.innerHTML = (+vfeedu.innerHTML) + 1;
142 }
143
144 } else {
145 mark_img.src = "images/mark_unset.png";
146 mark_img.alt = "Set mark";
147 query = query + "&mark=0";
148
149 if (vfeedu && crow.className.match("Unread")) {
150 vfeedu.innerHTML = (+vfeedu.innerHTML) - 1;
151 }
152
153 }
154
155 var vfeedctr = document.getElementById("FEEDCTR--1");
156 var vfeedr = document.getElementById("FEEDR--1");
157
158 if (vfeedu && vfeedctr) {
159 if ((+vfeedu.innerHTML) > 0) {
160 if (crow.className.match("Unread") && !vfeedr.className.match("Unread")) {
161 vfeedr.className = vfeedr.className + "Unread";
162 vfeedctr.className = "odd";
163 }
164 } else {
165 vfeedctr.className = "invisible";
166 vfeedr.className = vfeedr.className.replace("Unread", "");
167 }
168 }
169
170 debug("toggle starred for aid " + id);
171
172 new Ajax.Request(query);
173
174 }
175
176 function moveToPost(mode) {
177
178 // check for combined mode
179 if (!document.getElementById("headlinesList"))
180 return;
181
182 var rows = getVisibleHeadlineIds();
183
184 var prev_id;
185 var next_id;
186
187 if (!document.getElementById('RROW-' + active_post_id)) {
188 active_post_id = false;
189 }
190
191 if (active_post_id == false) {
192 next_id = getFirstVisibleHeadlineId();
193 prev_id = getLastVisibleHeadlineId();
194 } else {
195 for (var i = 0; i < rows.length; i++) {
196 if (rows[i] == active_post_id) {
197 prev_id = rows[i-1];
198 next_id = rows[i+1];
199 }
200 }
201 }
202
203 if (mode == "next") {
204 if (next_id != undefined) {
205 view(next_id, getActiveFeedId());
206 }
207 }
208
209 if (mode == "prev") {
210 if ( prev_id != undefined) {
211 view(prev_id, getActiveFeedId());
212 }
213 }
214 }
215
216 function toggleUnread(id, cmode) {
217 try {
218 if (!xmlhttp_ready(xmlhttp_rpc)) {
219 printLockingError();
220 return;
221 }
222
223 var row = document.getElementById("RROW-" + id);
224 if (row) {
225 var nc = row.className;
226 nc = nc.replace("Unread", "");
227 nc = nc.replace("Selected", "");
228
229 if (row.className.match("Unread")) {
230 row.className = nc;
231 } else {
232 row.className = nc + "Unread";
233 }
234
235 if (!cmode) cmode = 2;
236
237 var query = "backend.php?op=rpc&subop=catchupSelected&ids=" +
238 param_escape(id) + "&cmode=" + param_escape(cmode);
239
240 xmlhttp_rpc.open("GET", query, true);
241 xmlhttp_rpc.onreadystatechange=all_counters_callback;
242 xmlhttp_rpc.send(null);
243
244 }
245
246
247 } catch (e) {
248 exception_error("toggleUnread", e);
249 }
250 }
251
252 function selectionToggleUnread(cdm_mode, set_state, callback_func) {
253 try {
254 if (!xmlhttp_ready(xmlhttp_rpc)) {
255 printLockingError();
256 return;
257 }
258
259 var rows;
260
261 if (cdm_mode) {
262 rows = cdmGetSelectedArticles();
263 } else {
264 rows = getSelectedTableRowIds("headlinesList", "RROW", "RCHK");
265 }
266
267 for (i = 0; i < rows.length; i++) {
268 var row = document.getElementById("RROW-" + rows[i]);
269 if (row) {
270 var nc = row.className;
271 nc = nc.replace("Unread", "");
272 nc = nc.replace("Selected", "");
273
274 if (row.className.match("Unread")) {
275 row.className = nc + "Selected";
276 } else {
277 row.className = nc + "UnreadSelected";
278 }
279 }
280 }
281
282 if (rows.length > 0) {
283
284 var cmode = "";
285
286 if (set_state == undefined) {
287 cmode = "2";
288 } else if (set_state == true) {
289 cmode = "1";
290 } else if (set_state == false) {
291 cmode = "0";
292 }
293
294 var query = "backend.php?op=rpc&subop=catchupSelected&ids=" +
295 param_escape(rows.toString()) + "&cmode=" + cmode;
296
297 _catchup_callback_func = callback_func;
298
299 xmlhttp_rpc.open("GET", query, true);
300 xmlhttp_rpc.onreadystatechange=catchup_callback;
301 xmlhttp_rpc.send(null);
302
303 }
304
305 } catch (e) {
306 exception_error("selectionToggleUnread", e);
307 }
308 }
309
310 function selectionToggleMarked(cdm_mode) {
311 try {
312 if (!xmlhttp_ready(xmlhttp_rpc)) {
313 printLockingError();
314 return;
315 }
316
317 var rows;
318
319 if (cdm_mode) {
320 rows = cdmGetSelectedArticles();
321 } else {
322 rows = getSelectedTableRowIds("headlinesList", "RROW", "RCHK");
323 }
324
325 for (i = 0; i < rows.length; i++) {
326 var row = document.getElementById("RROW-" + rows[i]);
327 var mark_img = document.getElementById("FMARKPIC-" + rows[i]);
328
329 if (row && mark_img) {
330
331 if (mark_img.alt == "Set mark") {
332 mark_img.src = "images/mark_set.png";
333 mark_img.alt = "Reset mark";
334 mark_img.setAttribute('onclick',
335 'javascript:toggleMark('+rows[i]+', false)');
336
337 } else {
338 mark_img.src = "images/mark_unset.png";
339 mark_img.alt = "Set mark";
340 mark_img.setAttribute('onclick',
341 'javascript:toggleMark('+rows[i]+', true)');
342 }
343 }
344 }
345
346 if (rows.length > 0) {
347
348 var query = "backend.php?op=rpc&subop=markSelected&ids=" +
349 param_escape(rows.toString()) + "&cmode=2";
350
351 xmlhttp_rpc.open("GET", query, true);
352 xmlhttp_rpc.onreadystatechange=all_counters_callback;
353 xmlhttp_rpc.send(null);
354
355 }
356
357 } catch (e) {
358 exception_error("selectionToggleMarked", e);
359 }
360 }
361
362 function cdmGetSelectedArticles() {
363 var sel_articles = new Array();
364 var container = document.getElementById("headlinesInnerContainer");
365
366 for (i = 0; i < container.childNodes.length; i++) {
367 var child = container.childNodes[i];
368
369 if (child.id.match("RROW-") && child.className.match("Selected")) {
370 var c_id = child.id.replace("RROW-", "");
371 sel_articles.push(c_id);
372 }
373 }
374
375 return sel_articles;
376 }
377
378 // mode = all,none,unread
379 function cdmSelectArticles(mode) {
380 var container = document.getElementById("headlinesInnerContainer");
381
382 for (i = 0; i < container.childNodes.length; i++) {
383 var child = container.childNodes[i];
384
385 if (child.id.match("RROW-")) {
386 var aid = child.id.replace("RROW-", "");
387
388 var cb = document.getElementById("RCHK-" + aid);
389
390 if (mode == "all") {
391 if (!child.className.match("Selected")) {
392 child.className = child.className + "Selected";
393 cb.checked = true;
394 }
395 } else if (mode == "unread") {
396 if (child.className.match("Unread") && !child.className.match("Selected")) {
397 child.className = child.className + "Selected";
398 cb.checked = true;
399 }
400 } else {
401 child.className = child.className.replace("Selected", "");
402 cb.checked = false;
403 }
404 }
405 }
406 }
407
408 function catchupPage() {
409
410 if (document.getElementById("headlinesList")) {
411 selectTableRowsByIdPrefix('headlinesList', 'RROW-', 'RCHK-', true, 'Unread', true);
412 selectionToggleUnread(false, false, 'viewCurrentFeed()');
413 selectTableRowsByIdPrefix('headlinesList', 'RROW-', 'RCHK-', false);
414 } else {
415 cdmSelectArticles('all');
416 selectionToggleUnread(true, false, 'viewCurrentFeed()')
417 cdmSelectArticles('none');
418 }
419 }
420
421 function labelFromSearch(search, search_mode, match_on, feed_id, is_cat) {
422
423 if (!xmlhttp_ready(xmlhttp_rpc)) {
424 printLockingError();
425 }
426
427 var title = prompt("Please enter label title:", "");
428
429 if (title) {
430
431 var query = "backend.php?op=labelFromSearch&search=" + param_escape(search) +
432 "&smode=" + param_escape(search_mode) + "&match=" + param_escape(match_on) +
433 "&feed=" + param_escape(feed_id) + "&is_cat=" + param_escape(is_cat) +
434 "&title=" + param_escape(title);
435
436 debug("LFS: " + query);
437
438 xmlhttp_rpc.open("GET", query, true);
439 xmlhttp_rpc.onreadystatechange=dlg_frefresh_callback;
440 xmlhttp_rpc.send(null);
441 }
442
443 }
444
445 function editArticleTags(id, feed_id) {
446 _tag_active_post_id = id;
447 _tag_active_feed_id = feed_id;
448 displayDlg('editArticleTags', id);
449 }
450
451
452 function tag_saved_callback() {
453 if (xmlhttp_rpc.readyState == 4) {
454 try {
455 debug("in tag_saved_callback");
456
457 closeInfoBox();
458 notify("");
459
460 if (tagsAreDisplayed()) {
461 _reload_feedlist_after_view = true;
462 }
463
464 if (active_post_id == _tag_active_post_id) {
465 debug("reloading current article");
466 view(_tag_active_post_id, _tag_active_feed_id);
467 }
468
469 } catch (e) {
470 exception_error("catchup_callback", e);
471 }
472 }
473 }
474
475 function editTagsSave() {
476
477 if (!xmlhttp_ready(xmlhttp_rpc)) {
478 printLockingError();
479 }
480
481 notify("Saving article tags...");
482
483 var form = document.forms["tag_edit_form"];
484
485 var query = Form.serialize("tag_edit_form");
486
487 xmlhttp_rpc.open("GET", "backend.php?op=rpc&subop=setArticleTags&" + query, true);
488 xmlhttp_rpc.onreadystatechange=tag_saved_callback;
489 xmlhttp_rpc.send(null);
490
491 }
492
493 function editTagsInsert() {
494 try {
495
496 var form = document.forms["tag_edit_form"];
497
498 var found_tags = form.found_tags;
499 var tags_str = form.tags_str;
500
501 var tag = found_tags[found_tags.selectedIndex].value;
502
503 if (tags_str.value.length > 0 &&
504 tags_str.value.lastIndexOf(", ") != tags_str.value.length - 2) {
505
506 tags_str.value = tags_str.value + ", ";
507 }
508
509 tags_str.value = tags_str.value + tag + ", ";
510
511 found_tags.selectedIndex = 0;
512
513 } catch (e) {
514 exception_error(e, "editTagsInsert");
515 }
516 }