]> git.wh0rd.org Git - tt-rss.git/blob - digest.js
2a886a89bd243c55c7f39a61a45ad062873594e5
[tt-rss.git] / digest.js
1 var last_feeds = [];
2
3 var _active_feed_id = false;
4 var _active_feed_offset = false;
5 var _update_timeout = false;
6
7 function zoom(article_id) {
8         try {
9                 var elem = $('A-' + article_id);
10
11                 if (elem) {
12                         var divs = elem.getElementsByTagName('DIV');
13                         
14                         for (var i = 0; i < divs.length; i++) {
15                                 if (divs[i].className == 'excerpt') 
16                                         Element.hide(divs[i]);
17
18                                 if (divs[i].className == 'content') 
19                                         Element.show(divs[i]);
20                         }
21                 }
22
23                 var query = "backend.php?op=rpc&subop=digest-mark&article_id=" + article_id;
24
25                 new Ajax.Request("backend.php", {
26                         parameters: query, 
27                         onComplete: function(transport) {
28                                         window.clearTimeout(_update_timeout);
29                                         _update_timeout = window.setTimeout('update()', 1000);                                  
30                                 } });
31
32         } catch (e) {
33                 exception_error("zoom", e);
34         }
35 }
36
37 function load_more() {
38         try {
39                 var elem = $('MORE-PROMPT');
40
41                 if (elem) {
42                         elem.id = '';
43                         Element.hide(elem);
44                 }
45
46                 viewfeed(_active_feed_id, _active_feed_offset + 10);
47         } catch (e) {
48                 exception_error("load_more", e);
49         }
50 }
51
52 function update() {
53         try {
54                 viewfeed(_active_feed_id, _active_feed_offset);
55         } catch (e) {
56                 exception_error("update", e);
57         }
58 }
59
60 function view(article_id, dismiss_only) {
61         try {
62                 var elem = $('A-' + article_id);
63
64                 elem.id = '';
65
66                 //new Effect.Fade(elem, {duration : 0.3});
67
68                 Element.hide(elem);
69
70                 var query = "backend.php?op=rpc&subop=digest-mark&article_id=" + article_id;
71
72                 new Ajax.Request("backend.php", {
73                         parameters: query, 
74                         onComplete: function(transport) {
75                                         window.clearTimeout(_update_timeout);
76                                         _update_timeout = window.setTimeout('update()', 1000);                                  
77                                 } });
78
79                 return dismiss_only != true;
80         } catch (e) {
81                 exception_error("view", e);
82         }
83 }
84
85 function viewfeed(feed_id, offset) {
86         try {
87
88                 if (!feed_id) feed_id = _active_feed_id;
89
90                 if (!offset) 
91                         offset = 0;
92                 else
93                         offset = _active_feed_offset + offset;
94
95                 var query = "backend.php?op=rpc&subop=digest-update&feed_id=" + feed_id +
96                                 "&offset=" + offset;
97
98                 console.log(query);
99
100                 new Ajax.Request("backend.php", {
101                         parameters: query, 
102                         onComplete: function(transport) {
103                                 digest_update(transport, feed_id);
104                                 _active_feed_id = feed_id;
105                                 _active_feed_offset = offset;
106                                 } });
107
108         } catch (e) {
109                 exception_error("view", e);
110         }
111 }
112
113 function find_article(articles, article_id) {
114         try {
115                 for (var i = 0; i < articles.length; i++) {
116                         if (articles[i].id == article_id)
117                                 return articles[i];
118                 }
119
120                 return false;
121
122         } catch (e) {
123                 exception_error("find_article", e);
124         }
125 }
126
127 function find_feed(feeds, feed_id) {
128         try {
129                 for (var i = 0; i < feeds.length; i++) {
130                         if (feeds[i].id == feed_id)
131                                 return feeds[i];
132                 }
133
134                 return false;
135
136         } catch (e) {
137                 exception_error("find_feed", e);
138         }
139 }
140
141 function get_feed_icon(feed) {
142         try {
143                 if (feed.has_icon)
144                         return 'icons/' + feed.id + '.ico';
145
146                 if (feed.id == -1)
147                         return 'images/mark_set.png';
148
149                 if (feed.id == -2)
150                         return 'images/pub_set.png';
151
152                 if (feed.id == -3)
153                         return 'images/fresh.png';
154
155                 if (feed.id == -4) 
156                         return 'images/tag.png';
157
158                 if (feed.id < -10) 
159                         return 'images/label.png';
160
161         } catch (e) {
162                 exception_error("get_feed_icon", e);
163         }
164 }
165
166 function add_feed_entry(feed) {
167         try {
168                 var icon_part = "";
169
170                 icon_part = "<img src='" + get_feed_icon(feed) + "'/>";
171
172                 var tmp_html = "<li>" + 
173                         icon_part +
174                         "<a href=\"#\" onclick=\"viewfeed("+feed.id+")\">" + feed.title +
175                         "<div class='unread-ctr'>" + feed.unread + "</div>" +   
176                         "</li>";
177
178                 $("feeds-content").innerHTML += tmp_html;
179
180         } catch (e) {
181                 exception_error("add_feed_entry", e);
182         }
183 }
184
185 function add_latest_entry(article, feed) {
186         try {
187                 
188
189                 //$("latest-content").innerHTML += "bbb";
190
191         } catch (e) {
192                 exception_error("add_latest_entry", e);
193         }
194 }
195
196 function add_headline_entry(article, feed) {
197         try {
198
199                 var icon_part = "";
200
201                 if (article.has_icon) 
202                         icon_part = "<img src='icons/" + article.feed_id + ".ico'/>";
203
204                 var tmp_html = "<li id=\"A-"+article.id+"\">" + 
205                         icon_part +
206                         "<div class='digest-check'>" +
207                         "<img title='Set starred' onclick=\"toggleMark(this, "+article.id+")\" src='images/mark_unset.png'>" +
208                         "<img title='Set published' onclick=\"togglePub(this, "+article.id+")\" src='images/pub_unset.png'>" +
209                         "<img title='Dismiss' onclick=\"view("+article.id+", true)\" class='digest-check' src='images/digest_checkbox.png'>" +
210                         "</div>" + 
211                         "<a target=\"_blank\" href=\""+article.link+"\""+
212                                 "onclick=\"return view("+article.id+")\" class='title'>" + 
213                                 article.title + "</a>" +
214                         "<div class='body'>" + 
215                         "<div title=\"Click to expand article\" onclick=\"zoom("+article.id+")\" class='excerpt'>" + 
216                                 article.excerpt + "</div>" +
217                         "<div style='display : none' class='content'>" + 
218                                 article.content + "</div>" +
219                         "<div class='info'><a href=\#\" onclick=\"viewfeed("+feed.id+")\">" + 
220                                 feed.title + "</a> " + " @ " + 
221                                 new Date(article.updated * 1000) + "</div>" +
222                         "</div></li>";
223
224                 $("headlines-content").innerHTML += tmp_html;
225
226         } catch (e) {
227                 exception_error("add_headline_entry", e);
228         }
229 }
230
231 function digest_update(transport, feed_id) {
232         try {
233                 var feeds = transport.responseXML.getElementsByTagName('feeds')[0];
234                 var headlines = transport.responseXML.getElementsByTagName('headlines')[0];
235
236                 if (feeds) {
237                         feeds = eval("(" + feeds.firstChild.nodeValue + ")");
238
239                         last_feeds = feeds;
240
241                         $('feeds-content').innerHTML = "";
242
243                         for (var i = 0; i < feeds.length; i++) {
244                                 add_feed_entry(feeds[i]);
245                         }
246                 } else {
247                         feeds = last_feeds;
248                 }
249
250                 if (headlines) {
251                         headlines = eval("(" + headlines.firstChild.nodeValue + ")");
252
253                         if (_active_feed_id != feed_id) 
254                                 $('headlines-content').innerHTML = "";
255
256                         //Element.hide('headlines-content');
257                         
258                         var pr = $('MORE-PROMPT');
259
260                         if (pr) {
261                                 pr.id = '';
262                                 Element.hide(pr);
263                         }
264
265                         for (var i = 0; i < headlines.length; i++) {
266                                 var elem = $('A-' + headlines[i].id);
267                                 
268                                 if (elem && Element.visible(elem)) {
269
270
271                                 } else {
272                                         add_headline_entry(headlines[i], find_feed(feeds, headlines[i].feed_id));
273                                 }
274                         }
275
276                         $('headlines-content').innerHTML += "<li id='MORE-PROMPT'>" +
277                                 "<div class='body'><a href=\"#\" onclick=\"load_more()\">" +
278                                 __("More articles...") + "</a></div></li>";
279
280                         new Effect.Appear('headlines-content');
281
282                 }
283
284         } catch (e) {
285                 exception_error("digest_update", e);
286         }
287         }
288
289 function digest_init() {
290         try {
291                 
292                 new Ajax.Request("backend.php", {
293                         parameters: "backend.php?op=rpc&subop=digest-init",
294                         onComplete: function(transport) {
295                                 digest_update(transport, -4);
296                                 window.setTimeout('viewfeed(-4)', 100);
297                                 } });
298
299         } catch (e) {
300                 exception_error("digest_init", e);
301         }
302 }
303
304 function tMark_afh_off(effect) {
305         try {
306                 var elem = effect.effects[0].element;
307
308                 console.log("tMark_afh_off : " + elem.id);
309
310                 if (elem) {
311                         elem.src = elem.src.replace("mark_set", "mark_unset");
312                         elem.alt = __("Star article");
313                         Element.show(elem);
314                 }
315
316         } catch (e) {
317                 exception_error("tMark_afh_off", e);
318         }
319 }
320
321 function tPub_afh_off(effect) {
322         try {
323                 var elem = effect.effects[0].element;
324
325                 console.log("tPub_afh_off : " + elem.id);
326
327                 if (elem) {
328                         elem.src = elem.src.replace("pub_set", "pub_unset");
329                         elem.alt = __("Publish article");
330                         Element.show(elem);
331                 }
332
333         } catch (e) {
334                 exception_error("tPub_afh_off", e);
335         }
336 }
337
338 function toggleMark(mark_img, id) {
339
340         try {
341
342                 var query = "?op=rpc&id=" + id + "&subop=mark";
343         
344                 query = query + "&afid=" + _active_feed_id;
345                 query = query + "&omode=c";
346
347                 if (!mark_img) return;
348
349                 var vfeedu = $("FEEDU--1");
350                 var crow = $("RROW-" + id);
351         
352                 if (mark_img.src.match("mark_unset")) {
353                         mark_img.src = mark_img.src.replace("mark_unset", "mark_set");
354                         mark_img.alt = __("Unstar article");
355                         query = query + "&mark=1";
356                 } else {
357                         mark_img.alt = __("Please wait...");
358                         query = query + "&mark=0";
359         
360                         mark_img.src = mark_img.src.replace("mark_set", "mark_unset");
361                         mark_img.alt = __("Star article");
362                 }
363
364                 new Ajax.Request("backend.php", {
365                         parameters: query,
366                         onComplete: function(transport) { 
367                                 //
368                         } });
369
370         } catch (e) {
371                 exception_error("toggleMark", e);
372         }
373 }
374
375 function togglePub(mark_img, id, note) {
376
377         try {
378
379                 var query = "?op=rpc&id=" + id + "&subop=publ";
380         
381                 query = query + "&afid=" + _active_feed_id;
382
383                 if (note != undefined) {
384                         query = query + "&note=" + param_escape(note);
385                 } else {
386                         query = query + "&note=undefined";
387                 }
388
389                 query = query + "&omode=c";
390
391                 if (!mark_img) return;
392
393                 if (mark_img.src.match("pub_unset") || note != undefined) {
394                         mark_img.src = mark_img.src.replace("pub_unset", "pub_set");
395                         mark_img.alt = __("Unpublish article");
396                         query = query + "&pub=1";
397
398                 } else {
399                         mark_img.alt = __("Please wait...");
400                         query = query + "&pub=0";
401         
402                         mark_img.src = mark_img.src.replace("pub_set", "pub_unset");
403                         mark_img.alt = __("Publish article");
404                 }
405
406                 new Ajax.Request("backend.php", {
407                         parameters: query,
408                         onComplete: function(transport) { 
409                                 //
410                         } });
411
412         } catch (e) {
413                 exception_error("togglePub", e);
414         }
415 }
416