main.js 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489
  1. function is_reader_mode() {
  2. var stream = $("#stream.reader");
  3. return stream.html() != null;
  4. }
  5. function is_normal_mode() {
  6. var stream = $("#stream.normal");
  7. return stream.html() != null;
  8. }
  9. function is_global_mode() {
  10. var stream = $("#stream.global");
  11. return stream.html() != null;
  12. }
  13. function redirect (url, new_tab) {
  14. if (url) {
  15. if (new_tab) {
  16. window.open (url);
  17. } else {
  18. location.href = url;
  19. }
  20. }
  21. }
  22. function toggleContent (new_active, old_active) {
  23. if (does_lazyload) {
  24. new_active.find('img[data-original]').each(function() {
  25. this.setAttribute('src', this.getAttribute('data-original'));
  26. this.removeAttribute('data-original');
  27. });
  28. }
  29. old_active.removeClass ("active");
  30. if (old_active[0] != new_active[0]) {
  31. new_active.addClass ("active");
  32. }
  33. var box_to_move = "html,body";
  34. var relative_move = false;
  35. if (is_global_mode()) {
  36. box_to_move = "#panel";
  37. relative_move = true;
  38. }
  39. var new_pos = new_active.position ().top,
  40. old_scroll = $(box_to_move).scrollTop (),
  41. new_scroll = old_scroll;
  42. if (hide_posts) {
  43. old_active.children (".flux_content").toggle (0);
  44. new_pos = new_active.position ().top;
  45. old_scroll = $(box_to_move).scrollTop ();
  46. if (relative_move) {
  47. new_pos += old_scroll;
  48. }
  49. if (old_active[0] != new_active[0]) {
  50. new_active.children (".flux_content").toggle (0, function () {
  51. new_scroll = $(box_to_move).scrollTop (new_pos).scrollTop ();
  52. });
  53. }
  54. } else {
  55. if (relative_move) {
  56. new_pos += old_scroll;
  57. }
  58. new_scroll = $(box_to_move).scrollTop (new_pos).scrollTop ();
  59. }
  60. if (auto_mark_article) {
  61. mark_read(new_active, true);
  62. }
  63. }
  64. function _incLabel(p, inc) {
  65. var i = (parseInt(p.replace(/\D/g, '')) || 0) + inc;
  66. return i > 0 ? ' (' + i + ')' : '';
  67. }
  68. function mark_read (active, only_not_read) {
  69. if (active[0] === undefined || (
  70. only_not_read === true && !active.hasClass("not_read"))) {
  71. return false;
  72. }
  73. url = active.find ("a.read").attr ("href");
  74. if (url === undefined) {
  75. return false;
  76. }
  77. $.ajax ({
  78. type: 'POST',
  79. url: url,
  80. data : { ajax: true }
  81. }).done (function (data) {
  82. res = jQuery.parseJSON(data);
  83. active.find ("a.read").attr ("href", res.url);
  84. var inc = 0;
  85. if (active.hasClass ("not_read")) {
  86. active.removeClass ("not_read");
  87. inc--;
  88. } else if (only_not_read !== true || active.hasClass("not_read")) {
  89. active.addClass ("not_read");
  90. inc++;
  91. }
  92. //Update unread: feed //Alex
  93. var feed_url = active.find(".website>a").attr("href"),
  94. feed_id = feed_url.substr(feed_url.lastIndexOf('f_')),
  95. elem = $('#' + feed_id + ' .feed').get(0),
  96. attr_unread = elem ? elem.getAttributeNode('data-unread') : null,
  97. feed_priority = elem ? parseInt(elem.getAttribute('data-priority')) : 0;
  98. if (attr_unread)
  99. attr_unread.value = Math.max(0, parseInt(attr_unread.value) + inc);
  100. //Update unread: category
  101. elem = $('#' + feed_id).parent().prevAll('.category').children(':first').get(0);
  102. attr_unread = elem ? elem.getAttributeNode('data-unread') : null;
  103. if (attr_unread)
  104. attr_unread.value = Math.max(0, parseInt(attr_unread.value) + inc);
  105. if (feed_priority > 0) { //Update unread: all
  106. elem = $('#aside_flux .all').children(':first').get(0);
  107. attr_unread = elem ? elem.getAttributeNode('data-unread') : null;
  108. if (attr_unread)
  109. attr_unread.value = Math.max(0, parseInt(attr_unread.value) + inc);
  110. }
  111. //Update unread: title
  112. document.title = document.title.replace(/((?: \(\d+\))?)( - .*?)((?: \(\d+\))?)$/, function(m, p1, p2, p3) {
  113. return _incLabel(p1, inc) + p2 + _incLabel(p3, feed_priority > 0 ? inc : 0);
  114. });
  115. });
  116. }
  117. function mark_favorite (active) {
  118. if (active[0] === undefined) {
  119. return false;
  120. }
  121. url = active.find ("a.bookmark").attr ("href");
  122. if (url === undefined) {
  123. return false;
  124. }
  125. $.ajax ({
  126. type: 'POST',
  127. url: url,
  128. data : { ajax: true }
  129. }).done (function (data) {
  130. res = jQuery.parseJSON(data);
  131. active.find ("a.bookmark").attr ("href", res.url);
  132. var inc = 0;
  133. if (active.hasClass ("favorite")) {
  134. active.removeClass ("favorite");
  135. inc--;
  136. } else {
  137. active.addClass ("favorite");
  138. inc++;
  139. }
  140. var favourites = $('.favorites>a').contents().last().get(0);
  141. if (favourites && favourites.textContent)
  142. favourites.textContent = favourites.textContent.replace(/((?: \(\d+\))?\s*)$/, function(m, p1) {
  143. return _incLabel(p1, inc);
  144. });
  145. });
  146. }
  147. function prev_entry() {
  148. old_active = $(".flux.active");
  149. last_active = $(".flux:last");
  150. new_active = old_active.prevAll (".flux:first");
  151. if (new_active.hasClass("flux")) {
  152. toggleContent (new_active, old_active);
  153. } else if (old_active[0] === undefined &&
  154. new_active[0] === undefined) {
  155. toggleContent (last_active, old_active);
  156. }
  157. }
  158. function next_entry() {
  159. old_active = $(".flux.active");
  160. first_active = $(".flux:first");
  161. last_active = $(".flux:last");
  162. new_active = old_active.nextAll (".flux:first");
  163. if (new_active.hasClass("flux")) {
  164. toggleContent (new_active, old_active);
  165. } else if (old_active[0] === undefined &&
  166. new_active[0] === undefined) {
  167. toggleContent (first_active, old_active);
  168. }
  169. if ((!auto_load_more) && (last_active.attr("id") === new_active.attr("id"))) {
  170. load_more_posts ();
  171. }
  172. }
  173. function init_img () {
  174. var maxWidth = $(".flux_content .content").width() / 2;
  175. $(".flux_content .content img").each (function () {
  176. if ($(this).width () > maxWidth) {
  177. $(this).addClass("big");
  178. }
  179. });
  180. }
  181. function inMarkViewport(flux, box_to_follow, relative_follow) {
  182. var top = flux.position().top;
  183. if (relative_follow) {
  184. top += box_to_follow.scrollTop();
  185. }
  186. var height = flux.height();
  187. var begin = top + 3 * height / 4;
  188. var bot = Math.min(begin + 75, top + height);
  189. var windowTop = box_to_follow.scrollTop();
  190. var windowBot = windowTop + box_to_follow.height() / 2;
  191. return (windowBot >= begin && bot >= windowBot);
  192. }
  193. function init_posts () {
  194. init_img ();
  195. if ($.fn.lazyload) {
  196. if (is_global_mode()) {
  197. $(".flux .content img").lazyload({
  198. container: $("#panel")
  199. });
  200. } else {
  201. $(".flux .content img").lazyload();
  202. }
  203. }
  204. if (hide_posts) {
  205. $(".flux:not(.active) .flux_content").hide ();
  206. }
  207. var box_to_follow = $(window);
  208. var relative_follow = false;
  209. if (is_global_mode()) {
  210. box_to_follow = $("#panel");
  211. relative_follow = true;
  212. }
  213. if (auto_mark_scroll) {
  214. box_to_follow.scroll(function() {
  215. $('.flux.not_read:visible').each(function() {
  216. if ($(this).children(".flux_content").is(':visible') &&
  217. inMarkViewport($(this), box_to_follow, relative_follow)) {
  218. mark_read($(this), true);
  219. }
  220. });
  221. });
  222. }
  223. if (auto_load_more) {
  224. box_to_follow.scroll(function() {
  225. var load_more = $("#load_more");
  226. if (!load_more.is(':visible')) return;
  227. var boxBot = box_to_follow.scrollTop() + box_to_follow.height();
  228. var load_more_top = load_more.position().top;
  229. if (relative_follow) {
  230. load_more_top += box_to_follow.scrollTop();
  231. }
  232. if (boxBot >= load_more_top) {
  233. load_more_posts ();
  234. }
  235. });
  236. }
  237. }
  238. function init_column_categories () {
  239. if (!is_normal_mode()) {
  240. return;
  241. }
  242. //TODO: toggle class in PHP and remove the CSS changes done in JavaScript
  243. $(".category:not(.all):not(.favorites) .btn:first-child").width ("160px");
  244. $(".category:not(.all):not(.favorites)").addClass("stick").
  245. append ("<a class=\"btn dropdown-toggle\" href=\"#\"><i class=\"icon i_down\"></i></a>");
  246. $(".category + .feeds").not(".active").hide();
  247. $(".category.active a.dropdown-toggle i").toggleClass ("i_up");
  248. $(".category a.dropdown-toggle").click (function () {
  249. $(this).children ().toggleClass ("i_up");
  250. $(this).parent ().next (".feeds").slideToggle();
  251. return false;
  252. });
  253. }
  254. function init_shortcuts () {
  255. // Touches de manipulation
  256. shortcut.add(shortcuts['mark_read'], function () {
  257. // on marque comme lu ou non lu
  258. active = $(".flux.active");
  259. mark_read (active, false);
  260. }, {
  261. 'disable_in_input':true
  262. });
  263. shortcut.add("shift+"+shortcuts['mark_read'], function () {
  264. // on marque tout comme lu
  265. url = $(".nav_menu a.read_all").attr ("href");
  266. redirect (url, false);
  267. }, {
  268. 'disable_in_input':true
  269. });
  270. shortcut.add(shortcuts['mark_favorite'], function () {
  271. // on marque comme favori ou non favori
  272. active = $(".flux.active");
  273. mark_favorite (active);
  274. }, {
  275. 'disable_in_input':true
  276. });
  277. // Touches de navigation
  278. shortcut.add(shortcuts['prev_entry'], prev_entry, {
  279. 'disable_in_input':true
  280. });
  281. shortcut.add("shift+"+shortcuts['prev_entry'], function () {
  282. old_active = $(".flux.active");
  283. first = $(".flux:first");
  284. if (first.hasClass("flux")) {
  285. toggleContent (first, old_active);
  286. }
  287. }, {
  288. 'disable_in_input':true
  289. });
  290. shortcut.add(shortcuts['next_entry'], next_entry, {
  291. 'disable_in_input':true
  292. });
  293. shortcut.add("shift+"+shortcuts['next_entry'], function () {
  294. old_active = $(".flux.active");
  295. last = $(".flux:last");
  296. if (last.hasClass("flux")) {
  297. toggleContent (last, old_active);
  298. }
  299. }, {
  300. 'disable_in_input':true
  301. });
  302. shortcut.add(shortcuts['go_website'], function () {
  303. url_website = $(".flux.active .link a").attr ("href");
  304. if (auto_mark_site) {
  305. $(".flux.active").each (function () {
  306. mark_read($(this), true);
  307. });
  308. }
  309. redirect (url_website, true);
  310. }, {
  311. 'disable_in_input':true
  312. });
  313. }
  314. function init_stream_delegates(divStream) {
  315. divStream.on('click', '.flux_header .item.title, .flux_header .item.date', function (e) { //flux_header_toggle
  316. old_active = $(".flux.active");
  317. new_active = $(this).parent ().parent ();
  318. if (e.target.tagName.toUpperCase() === 'A') { //Leave real links alone
  319. if (auto_mark_article) {
  320. mark_read(new_active, true);
  321. }
  322. return true;
  323. }
  324. toggleContent (new_active, old_active);
  325. });
  326. divStream.on('click', '.flux a.read', function () {
  327. active = $(this).parents (".flux");
  328. mark_read (active, false);
  329. return false;
  330. });
  331. divStream.on('click', '.flux a.bookmark', function () {
  332. active = $(this).parents (".flux");
  333. mark_favorite (active);
  334. return false;
  335. });
  336. divStream.on('click', '.flux .content a', function () {
  337. $(this).attr ('target', '_blank');
  338. });
  339. divStream.on('click', '.item.title>a',function (e) {
  340. if (e.ctrlKey) return true; //Allow default control-click behaviour such as open in backround-tab
  341. $(this).parent ().click (); //Will perform toggle flux_content
  342. return false;
  343. });
  344. divStream.on('click', '.bigMarkAsRead', function() {
  345. url = $(".nav_menu a.read_all").attr ("href");
  346. redirect (url, false);
  347. return false;
  348. });
  349. if (auto_mark_site) {
  350. divStream.on('click', '.flux .link a', function () {
  351. mark_read($(this).parent().parent().parent(), true);
  352. });
  353. }
  354. }
  355. function init_nav_entries() {
  356. $('.nav_entries a.previous_entry').click(function() {
  357. prev_entry();
  358. return false;
  359. });
  360. $('.nav_entries a.next_entry').click(function() {
  361. next_entry();
  362. return false;
  363. });
  364. $('.nav_entries a.up').click(function() {
  365. var active_item = $(".flux.active");
  366. var windowTop = $(window).scrollTop();
  367. var item_top = active_item.position ().top;
  368. if (windowTop > item_top) {
  369. $("html,body").scrollTop (item_top);
  370. } else {
  371. $("html,body").scrollTop (0);
  372. }
  373. return false;
  374. });
  375. }
  376. function init_templates() {
  377. $('#aside_flux').on('click', '.dropdown-toggle', function () {
  378. if ($(this).nextAll('.dropdown-menu').length === 0) {
  379. var feed_id = $(this).closest('li').attr('id').substr(2),
  380. feed_web = $(this).data('fweb'),
  381. template = $('#feed_config_template').html().replace(/!!!!!!/g, feed_id).replace('http://example.net/', feed_web);
  382. $(this).attr('href', '#dropdown-' + feed_id).prev('.dropdown-target').attr('id', 'dropdown-' + feed_id).parent().append(template);
  383. }
  384. });
  385. }
  386. function init_actualize() {
  387. $("#actualize").click (function () {
  388. $.getScript('./?c=javascript&a=actualize').done(function() {
  389. updateFeeds ();
  390. });
  391. return false;
  392. });
  393. }
  394. function closeNotification () {
  395. $(".notification").slideUp (200, function () {
  396. $(".notification").remove ();
  397. });
  398. }
  399. function init_notifications() {
  400. notif = $(".notification");
  401. if (notif[0] !== undefined) {
  402. timer = setInterval('closeNotification()', 5000);
  403. notif.find ("a.close").click (function () {
  404. closeNotification ();
  405. return false;
  406. });
  407. }
  408. }
  409. $(function () {
  410. if (is_reader_mode()) {
  411. hide_posts = false;
  412. }
  413. init_posts ();
  414. init_column_categories ();
  415. init_shortcuts ();
  416. init_stream_delegates($('#stream'));
  417. init_nav_entries();
  418. init_templates();
  419. init_notifications();
  420. init_actualize();
  421. });