main.js 10.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422
  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 mark_read (active, only_not_read) {
  62. if (active[0] === undefined || (
  63. only_not_read === true && !active.hasClass("not_read"))) {
  64. return false;
  65. }
  66. url = active.find ("a.read").attr ("href");
  67. if (url === undefined) {
  68. return false;
  69. }
  70. $.ajax ({
  71. type: 'POST',
  72. url: url,
  73. data : { ajax: true }
  74. }).done (function (data) {
  75. res = jQuery.parseJSON(data);
  76. active.find ("a.read").attr ("href", res.url);
  77. if (active.hasClass ("not_read")) {
  78. active.removeClass ("not_read");
  79. } else if (only_not_read !== true || active.hasClass("not_read")) {
  80. active.addClass ("not_read");
  81. }
  82. });
  83. }
  84. function mark_favorite (active) {
  85. if (active[0] === undefined) {
  86. return false;
  87. }
  88. url = active.find ("a.bookmark").attr ("href");
  89. if (url === undefined) {
  90. return false;
  91. }
  92. $.ajax ({
  93. type: 'POST',
  94. url: url,
  95. data : { ajax: true }
  96. }).done (function (data) {
  97. res = jQuery.parseJSON(data);
  98. active.find ("a.bookmark").attr ("href", res.url);
  99. if (active.hasClass ("favorite")) {
  100. active.removeClass ("favorite");
  101. } else {
  102. active.addClass ("favorite");
  103. }
  104. });
  105. }
  106. function prev_entry() {
  107. old_active = $(".flux.active");
  108. last_active = $(".flux:last");
  109. new_active = old_active.prevAll (".flux:first");
  110. if (new_active.hasClass("flux")) {
  111. toggleContent (new_active, old_active);
  112. } else if (old_active[0] === undefined &&
  113. new_active[0] === undefined) {
  114. toggleContent (last_active, old_active);
  115. }
  116. }
  117. function next_entry() {
  118. old_active = $(".flux.active");
  119. first_active = $(".flux:first");
  120. last_active = $(".flux:last");
  121. new_active = old_active.nextAll (".flux:first");
  122. if (new_active.hasClass("flux")) {
  123. toggleContent (new_active, old_active);
  124. } else if (old_active[0] === undefined &&
  125. new_active[0] === undefined) {
  126. toggleContent (first_active, old_active);
  127. }
  128. if ((!auto_load_more) && (last_active.attr("id") === new_active.attr("id"))) {
  129. load_more_posts ();
  130. }
  131. }
  132. function init_img () {
  133. var maxWidth = $(".flux_content .content").width() / 2;
  134. $(".flux_content .content img").each (function () {
  135. if ($(this).width () > maxWidth) {
  136. $(this).addClass("big");
  137. }
  138. });
  139. }
  140. function inMarkViewport(flux, box_to_follow, relative_follow) {
  141. var top = flux.position().top;
  142. if (relative_follow) {
  143. top += box_to_follow.scrollTop();
  144. }
  145. var height = flux.height();
  146. var begin = top + 3 * height / 4;
  147. var bot = Math.min(begin + 75, top + height);
  148. var windowTop = box_to_follow.scrollTop();
  149. var windowBot = windowTop + box_to_follow.height() / 2;
  150. return (windowBot >= begin && bot >= windowBot);
  151. }
  152. function init_posts () {
  153. init_img ();
  154. if (does_lazyload) {
  155. if (is_global_mode()) {
  156. $(".flux .content img").lazyload({
  157. container: $("#panel")
  158. });
  159. } else {
  160. $(".flux .content img").lazyload();
  161. }
  162. }
  163. if (hide_posts) {
  164. $(".flux:not(.active) .flux_content").hide ();
  165. }
  166. var box_to_follow = $(window);
  167. var relative_follow = false;
  168. if (is_global_mode()) {
  169. box_to_follow = $("#panel");
  170. relative_follow = true;
  171. }
  172. if (auto_mark_scroll) {
  173. box_to_follow.scroll(function() {
  174. $('.flux.not_read:visible').each(function() {
  175. if ($(this).children(".flux_content").is(':visible') &&
  176. inMarkViewport($(this), box_to_follow, relative_follow)) {
  177. mark_read($(this), true);
  178. }
  179. });
  180. });
  181. }
  182. if (auto_load_more) {
  183. box_to_follow.scroll(function() {
  184. var load_more = $("#load_more");
  185. if (!load_more.is(':visible')) return;
  186. var boxBot = box_to_follow.scrollTop() + box_to_follow.height();
  187. var load_more_top = load_more.position().top;
  188. if (relative_follow) {
  189. load_more_top += box_to_follow.scrollTop();
  190. }
  191. if (boxBot >= load_more_top) {
  192. load_more_posts ();
  193. }
  194. });
  195. }
  196. }
  197. function init_column_categories () {
  198. if (!is_normal_mode()) {
  199. return;
  200. }
  201. $(".category").addClass ("stick");
  202. $(".categories .category .btn:first-child").width ("160px");
  203. $(".category").append ("<a class=\"btn dropdown-toggle\" href=\"#\"><i class=\"icon i_down\"></i></a>");
  204. $(".category + .feeds").not(".active").hide();
  205. $(".category.active a.dropdown-toggle i").toggleClass ("i_up");
  206. $(".category a.dropdown-toggle").click (function () {
  207. $(this).children ().toggleClass ("i_up");
  208. $(this).parent ().next (".feeds").slideToggle();
  209. return false;
  210. });
  211. }
  212. function init_shortcuts () {
  213. // Touches de manipulation
  214. shortcut.add(shortcuts['mark_read'], function () {
  215. // on marque comme lu ou non lu
  216. active = $(".flux.active");
  217. mark_read (active, false);
  218. }, {
  219. 'disable_in_input':true
  220. });
  221. shortcut.add("shift+"+shortcuts['mark_read'], function () {
  222. // on marque tout comme lu
  223. url = $(".nav_menu a.read_all").attr ("href");
  224. redirect (url, false);
  225. }, {
  226. 'disable_in_input':true
  227. });
  228. shortcut.add(shortcuts['mark_favorite'], function () {
  229. // on marque comme favori ou non favori
  230. active = $(".flux.active");
  231. mark_favorite (active);
  232. }, {
  233. 'disable_in_input':true
  234. });
  235. // Touches de navigation
  236. shortcut.add(shortcuts['prev_entry'], prev_entry, {
  237. 'disable_in_input':true
  238. });
  239. shortcut.add("shift+"+shortcuts['prev_entry'], function () {
  240. old_active = $(".flux.active");
  241. first = $(".flux:first");
  242. if (first.hasClass("flux")) {
  243. toggleContent (first, old_active);
  244. }
  245. }, {
  246. 'disable_in_input':true
  247. });
  248. shortcut.add(shortcuts['next_entry'], next_entry, {
  249. 'disable_in_input':true
  250. });
  251. shortcut.add("shift+"+shortcuts['next_entry'], function () {
  252. old_active = $(".flux.active");
  253. last = $(".flux:last");
  254. if (last.hasClass("flux")) {
  255. toggleContent (last, old_active);
  256. }
  257. }, {
  258. 'disable_in_input':true
  259. });
  260. shortcut.add(shortcuts['go_website'], function () {
  261. url_website = $(".flux.active .link a").attr ("href");
  262. if (auto_mark_site) {
  263. $(".flux.active").each (function () {
  264. mark_read($(this), true);
  265. });
  266. }
  267. redirect (url_website, true);
  268. }, {
  269. 'disable_in_input':true
  270. });
  271. }
  272. function init_stream_delegates(divStream) {
  273. divStream.on('click', '.flux_header .item.title, .flux_header .item.date', function (e) { //flux_header_toggle
  274. old_active = $(".flux.active");
  275. new_active = $(this).parent ().parent ();
  276. if (e.target.tagName.toUpperCase() === 'A') { //Leave real links alone
  277. if (auto_mark_article) {
  278. mark_read(new_active, true);
  279. }
  280. return true;
  281. }
  282. toggleContent (new_active, old_active);
  283. });
  284. divStream.on('click', '.flux a.read', function () {
  285. active = $(this).parents (".flux");
  286. mark_read (active, false);
  287. return false;
  288. });
  289. divStream.on('click', '.flux a.bookmark', function () {
  290. active = $(this).parents (".flux");
  291. mark_favorite (active);
  292. return false;
  293. });
  294. divStream.on('click', '.flux .content a', function () {
  295. $(this).attr ('target', '_blank');
  296. });
  297. divStream.on('click', '.item.title>a',function (e) {
  298. if (e.ctrlKey) return true; //Allow default control-click behaviour such as open in backround-tab
  299. $(this).parent ().click (); //Will perform toggle flux_content
  300. return false;
  301. });
  302. divStream.on('click', '.bigMarkAsRead', function() {
  303. url = $(".nav_menu a.read_all").attr ("href");
  304. redirect (url, false);
  305. return false;
  306. });
  307. if (auto_mark_site) {
  308. divStream.on('click', '.flux .link a', function () {
  309. mark_read($(this).parent().parent().parent(), true);
  310. });
  311. }
  312. }
  313. function init_nav_entries() {
  314. $('.nav_entries a.previous_entry').click(function() {
  315. prev_entry();
  316. return false;
  317. });
  318. $('.nav_entries a.next_entry').click(function() {
  319. next_entry();
  320. return false;
  321. });
  322. $('.nav_entries a.up').click(function() {
  323. var active_item = $(".flux.active");
  324. var windowTop = $(window).scrollTop();
  325. var item_top = active_item.position ().top;
  326. if (windowTop > item_top) {
  327. $("html,body").scrollTop (item_top);
  328. } else {
  329. $("html,body").scrollTop (0);
  330. }
  331. return false;
  332. });
  333. }
  334. function init_templates() {
  335. $('#aside_flux').on('click', '.dropdown-toggle', function () {
  336. if ($(this).nextAll('.dropdown-menu').length === 0) {
  337. var feed_id = $(this).data('fid'),
  338. feed_web = $(this).data('fweb'),
  339. template = $('#feed_config_template').html().replace(/!!!!!!/g, feed_id).replace('http://example.net/', feed_web);
  340. $(this).attr('href', '#dropdown-' + feed_id).prev('.dropdown-target').attr('id', 'dropdown-' + feed_id).parent().append(template);
  341. }
  342. });
  343. }
  344. function init_actualize() {
  345. $("#actualize").click (function () {
  346. $.getScript('./?c=javascript&a=actualize').done(function() {
  347. updateFeeds ();
  348. });
  349. return false;
  350. });
  351. }
  352. $(function () {
  353. if (is_reader_mode()) {
  354. hide_posts = false;
  355. }
  356. init_posts ();
  357. init_column_categories ();
  358. init_shortcuts ();
  359. init_stream_delegates($('#stream'));
  360. init_nav_entries();
  361. init_templates();
  362. init_actualize();
  363. });