]> git.wh0rd.org Git - tt-rss.git/blob - feedlist.js
misc dialog style updates
[tt-rss.git] / feedlist.js
1 var _feed_cur_page = 0;
2 var _infscroll_disable = 0;
3 var _infscroll_request_sent = 0;
4
5 var counter_timeout_id = false;
6
7 var resize_enabled = false;
8 var counters_last_request = 0;
9
10 function viewCategory(cat) {
11         viewfeed(cat, '', true);
12         return false;
13 }
14
15 function viewNextFeedPage() {
16         try {
17                 //if (!getActiveFeedId()) return;
18
19                 console.log("viewNextFeedPage: calling viewfeed(), p: " + parseInt(_feed_cur_page+1));
20
21                 viewfeed(getActiveFeedId(), '', activeFeedIsCat(), parseInt(_feed_cur_page+1));
22
23         } catch (e) {
24                 exception_error("viewNextFeedPage", e);
25         }
26 }
27
28
29 function viewfeed(feed, subop, is_cat, offset) {
30         try {
31                 if (is_cat == undefined) is_cat = false;
32
33 //              if (!offset) page_offset = 0;
34
35                 last_requested_article = 0;
36                 //counters_last_request = 0;
37
38                 if (feed == getActiveFeedId()) {
39                         cache_invalidate("F:" + feed);
40                 }
41
42 /*              if (getInitParam("theme") == "" || getInitParam("theme") == "compact") {
43                         if (getInitParam("hide_feedlist") == 1) {
44                                 Element.hide("feeds-holder");
45                         }               
46                 } */
47
48                 dijit.byId("content-tabs").selectChild(
49                         dijit.byId("content-tabs").getChildren()[0]);
50
51                 var force_nocache = false;
52
53                 var page_offset = 0;
54
55                 if (offset > 0) {
56                         page_offset = offset;
57                 } else {
58                         page_offset = 0;
59                         _feed_cur_page = 0;
60                         _infscroll_disable = 0;
61                 }
62
63                 if (getActiveFeedId() != feed) {
64                         _feed_cur_page = 0;
65                         active_post_id = 0;
66                         _infscroll_disable = 0;
67                 }
68
69                 if (page_offset != 0 && !subop) {
70                         var date = new Date();
71                         var timestamp = Math.round(date.getTime() / 1000);
72
73                         console.log(_infscroll_request_sent + " : " + timestamp);
74
75                         if (_infscroll_request_sent && _infscroll_request_sent + 30 > timestamp) {
76                                 console.log("infscroll request in progress, aborting");
77                                 return;
78                         }
79
80                         _infscroll_request_sent = timestamp;                    
81                 }
82
83                 enableHotkeys();
84                 hideAuxDlg();
85                 closeInfoBox();
86
87                 Form.enable("main_toolbar_form");
88
89                 var toolbar_form = document.forms["main_toolbar_form"];
90                 var toolbar_query = Form.serialize("main_toolbar_form");
91
92                 if (toolbar_form.query) {
93                         if (toolbar_form.query.value != "") {
94                                 force_nocache = true;
95                         }
96                         toolbar_form.query.value = "";
97                 }
98
99                 var query = "?op=viewfeed&feed=" + feed + "&" +
100                         toolbar_query + "&subop=" + param_escape(subop);
101
102                 if ($("search_form")) {
103                         var search_query = Form.serialize("search_form");
104                         query = query + "&" + search_query;
105                         $("search_form").query.value = "";
106                         closeInfoBox(true);
107                         force_nocache = true;
108                 }
109
110 //              console.log("IS_CAT_STORED: " + activeFeedIsCat() + ", IS_CAT: " + is_cat);
111
112                 if (subop == "MarkAllRead") {
113
114                         var show_next_feed = getInitParam("on_catchup_show_next_feed") == "1";
115
116                         if (show_next_feed) {
117                                 // TODO: implement show_next_feed handling
118                         }
119                 }
120
121                 if (is_cat) {
122                         query = query + "&cat=1";
123                 }
124
125                 if (page_offset != 0) {
126                         query = query + "&skip=" + page_offset;
127
128                         // to prevent duplicate feed titles when showing grouped vfeeds
129                         if (vgroup_last_feed) {
130                                 query = query + "&vgrlf=" + param_escape(vgroup_last_feed);
131                         }
132                 }
133
134                 Form.enable("main_toolbar_form");
135
136                 console.log(query);
137
138                 var container = $("headlinesInnerContainer");
139
140                 var unread_ctr = -1;
141                 
142                 if (!is_cat) unread_ctr = getFeedUnread(feed);
143
144                 var cache_check = false;
145
146                 if (unread_ctr != -1 && !page_offset && !force_nocache && !subop) {
147
148                         var cache_prefix = "";
149                                 
150                         if (is_cat) {
151                                 cache_prefix = "C:";
152                         } else {
153                                 cache_prefix = "F:";
154                         }
155
156                         cache_check = cache_check_param(cache_prefix + feed, unread_ctr);
157                         console.log("headline cache check: " + cache_check);
158                 }
159
160                 if (cache_check) {
161
162                         setActiveFeedId(feed, is_cat);
163                 
164                         $("headlines-frame").innerHTML = cache_find_param(cache_prefix + feed, 
165                                 unread_ctr);
166
167                         request_counters();
168                         remove_splash();
169
170                 } else {
171
172                         if (!is_cat)
173                                 if (!setFeedExpandoIcon(feed, is_cat, 'images/indicator_white.gif'))
174                                         notify_progress("Loading, please wait...", true);
175
176                         new Ajax.Request("backend.php", {
177                                 parameters: query,
178                                 onComplete: function(transport) { 
179                                         setFeedExpandoIcon(feed, is_cat, 'images/blank_icon.gif');
180                                         headlines_callback2(transport, page_offset); 
181                                 } });
182                 }
183
184         } catch (e) {
185                 exception_error("viewfeed", e);
186         }               
187 }
188
189 function feedlist_init() {
190         try {
191                 loading_set_progress(90);
192
193                 console.log("in feedlist init");
194                 
195                 hideOrShowFeeds(getInitParam("hide_read_feeds") == 1);
196                 document.onkeydown = hotkey_handler;
197                 setTimeout("hotkey_prefix_timeout()", 5*1000);
198
199                  if (!getActiveFeedId()) {
200                         if (getInitParam("cdm_auto_catchup") != 1) {
201                                 setTimeout("viewfeed(-3)", 100);
202                         } else {
203                                 setTimeout("viewfeed(-5)", 100);
204                                 remove_splash();
205                         }
206                 } 
207
208                 console.log("T:" + 
209                                 getInitParam("cdm_auto_catchup") + " " + getFeedUnread(-3));
210
211                 hideOrShowFeeds(getInitParam("hide_read_feeds") == 1);
212
213         } catch (e) {
214                 exception_error("feedlist/init", e);
215         }
216 }
217
218 function request_counters_real() {
219         try {
220                 console.log("requesting counters...");
221
222                 var query = "?op=rpc&subop=getAllCounters&seq=" + next_seq();
223
224                 query = query + "&omode=flc";
225
226                 new Ajax.Request("backend.php", {
227                         parameters: query,
228                         onComplete: function(transport) { 
229                                 try {
230                                         handle_rpc_reply(transport);
231                                 } catch (e) {
232                                         exception_error("viewfeed/getcounters", e);
233                                 }
234                         } });
235
236         } catch (e) {
237                 exception_error("request_counters_real", e);
238         }
239 }
240
241
242 function request_counters() {
243
244         try {
245
246                 if (getInitParam("bw_limit") == "1") return;
247
248                 var date = new Date();
249                 var timestamp = Math.round(date.getTime() / 1000);
250
251                 if (timestamp - counters_last_request > 5) {
252                         console.log("scheduling request of counters...");
253
254                         window.clearTimeout(counter_timeout_id);
255                         counter_timeout_id = window.setTimeout("request_counters_real()", 1000);
256
257                         counters_last_request = timestamp;
258                 } else {
259                         console.log("request_counters: rate limit reached: " + (timestamp - counters_last_request));
260                 }
261
262         } catch (e) {
263                 exception_error("request_counters", e);
264         }
265 }
266
267 function displayNewContentPrompt(id) {
268         try {
269
270                 var msg = "<a href='#' onclick='viewCurrentFeed()'>" +
271                         __("New articles available in this feed (click to show)") + "</a>";
272
273                 msg = msg.replace("%s", getFeedName(id));
274
275                 $('auxDlg').innerHTML = msg;
276
277                 new Effect.Appear('auxDlg', {duration : 0.5});
278
279         } catch (e) {
280                 exception_error("displayNewContentPrompt", e);
281         }
282 }
283
284 function parse_counters(reply, scheduled_call) {
285         try {
286
287                 var feeds_found = 0;
288
289                 var elems = JSON.parse(reply.firstChild.nodeValue);
290
291                 for (var l = 0; l < elems.length; l++) {
292
293                         var id = elems[l].id
294                         var kind = elems[l].kind;
295                         var ctr = parseInt(elems[l].counter)
296                         var error = elems[l].error;
297                         var has_img = elems[l].has_img;
298                         var updated = elems[l].updated;
299                         var title = elems[l].title;
300         
301                         if (id == "global-unread") {
302                                 global_unread = ctr;
303                                 updateTitle();
304                                 continue;
305                         }
306
307                         if (id == "subscribed-feeds") {
308                                 feeds_found = ctr;
309                                 continue;
310                         }
311
312                         var treeItem;
313
314                         if (id == getActiveFeedId() && ctr > getFeedUnread(id) && scheduled_call) {
315                                 displayNewContentPrompt(id);
316                         }
317
318                         setFeedUnread(id, (kind == "cat"), ctr);
319
320                         if (kind != "cat") {
321                                 //setFeedValue(id, false, 'error', error);
322                                 setFeedValue(id, false, 'updated', updated);
323
324                                 if (id > 0) {
325                                         if (has_img) {
326                                                 setFeedIcon(id, false, 
327                                                         getInitParam("icons_url") + "/" + id + ".ico");
328                                         } else {
329                                                 setFeedIcon(id, false, 'images/blank_icon.gif');
330                                         }
331                                 }
332                         }
333                 }
334         
335                 hideOrShowFeeds(getInitParam("hide_read_feeds") == 1);
336
337                 var feeds_stored = number_of_feeds;
338
339                 if (feeds_stored != feeds_found) {
340                         number_of_feeds = feeds_found;
341
342                         if (feeds_stored != 0 && feeds_found != 0) {
343                                 console.log("Subscribed feed number changed, refreshing feedlist");
344                                 setTimeout('updateFeedList()', 50);
345                         }
346                 }
347
348         } catch (e) {
349                 exception_error("parse_counters", e);
350         }
351 }
352
353 function getFeedUnread(feed, is_cat) {
354         try {
355                 var tree = dijit.byId("feedTree");
356
357                 if (tree && tree.model) 
358                         return tree.model.getFeedUnread(feed, is_cat);
359
360         } catch (e) {
361                 //
362         }
363
364         return -1;
365 }
366
367 function resort_feedlist() {
368         console.warn("resort_feedlist: function not implemented");
369 }
370
371 function hideOrShowFeeds(hide) {
372         var tree = dijit.byId("feedTree");
373
374         if (tree)
375                 return tree.hideRead(hide, getInitParam("hide_read_shows_special"));
376 }
377
378 function getFeedName(feed, is_cat) {    
379         var tree = dijit.byId("feedTree");
380
381         if (tree && tree.model) 
382                 return tree.model.getFeedValue(feed, is_cat, 'name');
383 }
384
385 function getFeedValue(feed, is_cat, key) {      
386         try {
387                 var tree = dijit.byId("feedTree");
388
389                 if (tree && tree.model) 
390                         return tree.model.getFeedValue(feed, is_cat, key);
391         
392         } catch (e) {
393                 //
394         }
395         return '';
396 }
397
398 function setFeedUnread(feed, is_cat, unread) {
399         try {
400                 var tree = dijit.byId("feedTree");
401
402                 if (tree && tree.model) 
403                         return tree.model.setFeedUnread(feed, is_cat, unread);
404
405         } catch (e) {
406                 exception_error("setFeedUnread", e);
407         }
408 }
409
410 function setFeedValue(feed, is_cat, key, value) {
411         try {
412                 var tree = dijit.byId("feedTree");
413
414                 if (tree && tree.model) 
415                         return tree.model.setFeedValue(feed, is_cat, key, value);
416
417         } catch (e) {
418                 //
419         }
420 }
421
422 function selectFeed(feed, is_cat) {
423         try {
424                 var tree = dijit.byId("feedTree");
425
426                 if (tree) return tree.selectFeed(feed, is_cat);
427
428         } catch (e) {
429                 exception_error("selectFeed", e);
430         }
431 }
432
433 function setFeedIcon(feed, is_cat, src) {
434         try {
435                 var tree = dijit.byId("feedTree");
436
437                 if (tree) return tree.setFeedIcon(feed, is_cat, src);
438
439         } catch (e) {
440                 exception_error("setFeedIcon", e);
441         }
442 }
443
444 function setFeedExpandoIcon(feed, is_cat, src) {
445         try {
446                 var tree = dijit.byId("feedTree");
447
448                 if (tree) return tree.setFeedExpandoIcon(feed, is_cat, src);
449
450         } catch (e) {
451                 exception_error("setFeedIcon", e);
452         }
453         return false;
454 }