main.js 13 KB

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