main.js 12 KB

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