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