main.js 32 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282
  1. "use strict";
  2. var $stream = null,
  3. isCollapsed = true,
  4. shares = 0,
  5. ajax_loading = false;
  6. function redirect(url, new_tab) {
  7. if (url) {
  8. if (new_tab) {
  9. window.open(url);
  10. } else {
  11. location.href = url;
  12. }
  13. }
  14. }
  15. function needsScroll($elem) {
  16. var $win = $(window),
  17. winTop = $win.scrollTop(),
  18. winHeight = $win.height(),
  19. winBottom = winTop + winHeight,
  20. elemTop = $elem.offset().top,
  21. elemBottom = elemTop + $elem.outerHeight();
  22. return (elemTop < winTop || elemBottom > winBottom) ? elemTop - (winHeight / 2) : 0;
  23. }
  24. function str2int(str) {
  25. if (!str) {
  26. return 0;
  27. }
  28. return parseInt(str.replace(/\D/g, ''), 10) || 0;
  29. }
  30. function numberFormat(nStr) {
  31. if (nStr < 0) {
  32. return 0;
  33. }
  34. // http://www.mredkj.com/javascript/numberFormat.html
  35. nStr += '';
  36. var x = nStr.split('.'),
  37. x1 = x[0],
  38. x2 = x.length > 1 ? '.' + x[1] : '',
  39. rgx = /(\d+)(\d{3})/;
  40. while (rgx.test(x1)) {
  41. x1 = x1.replace(rgx, '$1' + ' ' + '$2');
  42. }
  43. return x1 + x2;
  44. }
  45. function incLabel(p, inc, spaceAfter) {
  46. var i = str2int(p) + inc;
  47. return i > 0
  48. ? ((spaceAfter ? '' : ' ') + '(' + numberFormat(i) + ')' + (spaceAfter ? ' ' : ''))
  49. : '';
  50. }
  51. function incUnreadsFeed(article, feed_id, nb) {
  52. //Update unread: feed
  53. var elem = $('#' + feed_id).get(0),
  54. feed_unreads = elem ? str2int(elem.getAttribute('data-unread')) : 0,
  55. feed_priority = elem ? str2int(elem.getAttribute('data-priority')) : 0;
  56. if (elem) {
  57. elem.setAttribute('data-unread', feed_unreads + nb);
  58. elem = $(elem).children('.item-title').get(0);
  59. if (elem) {
  60. elem.setAttribute('data-unread', numberFormat(feed_unreads + nb));
  61. }
  62. }
  63. //Update unread: category
  64. elem = $('#' + feed_id).parents('.category').get(0);
  65. feed_unreads = elem ? str2int(elem.getAttribute('data-unread')) : 0;
  66. if (elem) {
  67. elem.setAttribute('data-unread', feed_unreads + nb);
  68. elem = $(elem).find('.title').get(0);
  69. if (elem) {
  70. elem.setAttribute('data-unread', numberFormat(feed_unreads + nb));
  71. }
  72. }
  73. //Update unread: all
  74. if (feed_priority > 0) {
  75. elem = $('#aside_feed .all .title').get(0);
  76. if (elem) {
  77. feed_unreads = elem ? str2int(elem.getAttribute('data-unread')) : 0;
  78. elem.setAttribute('data-unread', numberFormat(feed_unreads + nb));
  79. }
  80. }
  81. //Update unread: favourites
  82. if (article && article.closest('div').hasClass('favorite')) {
  83. elem = $('#aside_feed .favorites .title').get(0);
  84. if (elem) {
  85. feed_unreads = elem ? str2int(elem.getAttribute('data-unread')) : 0;
  86. elem.setAttribute('data-unread', numberFormat(feed_unreads + nb));
  87. }
  88. }
  89. var isCurrentView = false;
  90. // Update unread: title
  91. document.title = document.title.replace(/^((?:\([ 0-9]+\) )?)/, function (m, p1) {
  92. var $feed = $('#' + feed_id);
  93. if (article || ($feed.closest('.active').length > 0 && $feed.siblings('.active').length === 0)) {
  94. isCurrentView = true;
  95. return incLabel(p1, nb, true);
  96. } else if ($('.all.active').length > 0) {
  97. isCurrentView = feed_priority > 0;
  98. return incLabel(p1, feed_priority > 0 ? nb : 0, true);
  99. } else {
  100. return p1;
  101. }
  102. });
  103. return isCurrentView;
  104. }
  105. var pending_feeds = [];
  106. function mark_read(active, only_not_read) {
  107. if (active.length === 0 ||
  108. (only_not_read === true && !active.hasClass("not_read"))) {
  109. return false;
  110. }
  111. var url = active.find("a.read").attr("href");
  112. if (url === undefined) {
  113. return false;
  114. }
  115. var feed_url = active.find(".website>a").attr("href"),
  116. feed_id = feed_url.substr(feed_url.lastIndexOf('f_')),
  117. index_pending = pending_feeds.indexOf(feed_id);
  118. if (index_pending !== -1) {
  119. return false;
  120. }
  121. pending_feeds.push(feed_id);
  122. $.ajax({
  123. type: 'POST',
  124. url: url,
  125. data : { ajax: true }
  126. }).done(function (data) {
  127. var $r = active.find("a.read").attr("href", data.url),
  128. inc = 0;
  129. if (active.hasClass("not_read")) {
  130. active.removeClass("not_read");
  131. inc--;
  132. } else if (only_not_read !== true || active.hasClass("not_read")) {
  133. active.addClass("not_read");
  134. inc++;
  135. }
  136. $r.find('.icon').replaceWith(data.icon);
  137. incUnreadsFeed(active, feed_id, inc);
  138. faviconNbUnread();
  139. pending_feeds.splice(index_pending, 1);
  140. }).fail(function (data) {
  141. openNotification(i18n.notif_request_failed, 'bad');
  142. pending_feeds.splice(index_pending, 1);
  143. });
  144. }
  145. function mark_favorite(active) {
  146. if (active.length === 0) {
  147. return false;
  148. }
  149. var url = active.find("a.bookmark").attr("href");
  150. if (url === undefined) {
  151. return false;
  152. }
  153. var feed_url = active.find(".website>a").attr("href"),
  154. feed_id = feed_url.substr(feed_url.lastIndexOf('f_')),
  155. index_pending = pending_feeds.indexOf(feed_id);
  156. if (index_pending !== -1) {
  157. return false;
  158. }
  159. pending_feeds.push(feed_id);
  160. $.ajax({
  161. type: 'POST',
  162. url: url,
  163. data : { ajax: true }
  164. }).done(function (data) {
  165. var $b = active.find("a.bookmark").attr("href", data.url),
  166. inc = 0;
  167. if (active.hasClass("favorite")) {
  168. active.removeClass("favorite");
  169. inc--;
  170. } else {
  171. active.addClass("favorite").find('.bookmark');
  172. inc++;
  173. }
  174. $b.find('.icon').replaceWith(data.icon);
  175. var favourites = $('#aside_feed .favorites .title').contents().last().get(0);
  176. if (favourites && favourites.textContent) {
  177. favourites.textContent = favourites.textContent.replace(/((?: \([ 0-9]+\))?\s*)$/, function (m, p1) {
  178. return incLabel(p1, inc, false);
  179. });
  180. }
  181. if (active.closest('div').hasClass('not_read')) {
  182. var elem = $('#aside_feed .favorites .title').get(0),
  183. feed_unreads = elem ? str2int(elem.getAttribute('data-unread')) : 0;
  184. if (elem) {
  185. elem.setAttribute('data-unread', numberFormat(feed_unreads + inc));
  186. }
  187. }
  188. pending_feeds.splice(index_pending, 1);
  189. }).fail(function (data) {
  190. openNotification(i18n.notif_request_failed, 'bad');
  191. pending_feeds.splice(index_pending, 1);
  192. });
  193. }
  194. function toggleContent(new_active, old_active) {
  195. if (new_active.length === 0) {
  196. return;
  197. }
  198. if (context['does_lazyload']) {
  199. new_active.find('img[data-original], iframe[data-original]').each(function () {
  200. this.setAttribute('src', this.getAttribute('data-original'));
  201. this.removeAttribute('data-original');
  202. });
  203. }
  204. if (old_active[0] !== new_active[0]) {
  205. if (isCollapsed) {
  206. new_active.addClass("active");
  207. }
  208. old_active.removeClass("active current");
  209. new_active.addClass("current");
  210. if (context['auto_remove_article'] && !old_active.hasClass('not_read')) {
  211. var p = old_active.prev();
  212. var n = old_active.next();
  213. if (p.hasClass('day') && n.hasClass('day')) {
  214. p.remove();
  215. }
  216. old_active.remove();
  217. }
  218. } else {
  219. new_active.toggleClass('active');
  220. }
  221. var box_to_move = "html,body",
  222. relative_move = false;
  223. if (context['current_view'] == 'global') {
  224. box_to_move = "#panel";
  225. relative_move = true;
  226. }
  227. if (context['sticky_post']) {
  228. var prev_article = new_active.prevAll('.flux'),
  229. new_pos = new_active.position().top,
  230. old_scroll = $(box_to_move).scrollTop();
  231. if (prev_article.length > 0 && new_pos - prev_article.position().top <= 150) {
  232. new_pos = prev_article.position().top;
  233. }
  234. if (context['hide_posts']) {
  235. if (relative_move) {
  236. new_pos += old_scroll;
  237. }
  238. if (old_active[0] !== new_active[0]) {
  239. new_active.children(".flux_content").first().each(function () {
  240. $(box_to_move).scrollTop(new_pos).scrollTop();
  241. });
  242. }
  243. } else {
  244. if (relative_move) {
  245. new_pos += old_scroll;
  246. }
  247. $(box_to_move).scrollTop(new_pos).scrollTop();
  248. }
  249. }
  250. if (context['auto_mark_article'] && new_active.hasClass('active')) {
  251. mark_read(new_active, true);
  252. }
  253. }
  254. function prev_entry() {
  255. var old_active = $(".flux.current"),
  256. new_active = old_active.length === 0 ? $(".flux:last") : old_active.prevAll(".flux:first");
  257. toggleContent(new_active, old_active);
  258. }
  259. function next_entry() {
  260. var old_active = $(".flux.current"),
  261. new_active = old_active.length === 0 ? $(".flux:first") : old_active.nextAll(".flux:first");
  262. toggleContent(new_active, old_active);
  263. if (new_active.nextAll().length < 3) {
  264. load_more_posts();
  265. }
  266. }
  267. function prev_feed() {
  268. var active_feed = $("#aside_feed .tree-folder-items .item.active");
  269. if (active_feed.length > 0) {
  270. active_feed.prevAll(':visible:first').find('a').each(function(){this.click();});
  271. } else {
  272. last_feed();
  273. }
  274. }
  275. function next_feed() {
  276. var active_feed = $("#aside_feed .tree-folder-items .item.active");
  277. if (active_feed.length > 0) {
  278. active_feed.nextAll(':visible:first').find('a').each(function(){this.click();});
  279. } else {
  280. first_feed();
  281. }
  282. }
  283. function first_feed() {
  284. var feed = $("#aside_feed .tree-folder-items.active .item:visible:first");
  285. if (feed.length > 0) {
  286. feed.find('a')[1].click();
  287. }
  288. }
  289. function last_feed() {
  290. var feed = $("#aside_feed .tree-folder-items.active .item:visible:last");
  291. if (feed.length > 0) {
  292. feed.find('a')[1].click();
  293. }
  294. }
  295. function prev_category() {
  296. var active_cat = $("#aside_feed .tree-folder.active");
  297. if (active_cat.length > 0) {
  298. var prev_cat = active_cat.prevAll(':visible:first').find('.tree-folder-title .title');
  299. if (prev_cat.length > 0) {
  300. prev_cat[0].click();
  301. }
  302. } else {
  303. last_category();
  304. }
  305. return;
  306. }
  307. function next_category() {
  308. var active_cat = $("#aside_feed .tree-folder.active");
  309. if (active_cat.length > 0) {
  310. var next_cat = active_cat.nextAll(':visible:first').find('.tree-folder-title .title');
  311. if (next_cat.length > 0) {
  312. next_cat[0].click();
  313. }
  314. } else {
  315. first_category();
  316. }
  317. return;
  318. }
  319. function first_category() {
  320. var cat = $("#aside_feed .tree-folder:visible:first");
  321. if (cat.length > 0) {
  322. cat.find('.tree-folder-title .title')[0].click();
  323. }
  324. }
  325. function last_category() {
  326. var cat = $("#aside_feed .tree-folder:visible:last");
  327. if (cat.length > 0) {
  328. cat.find('.tree-folder-title .title')[0].click();
  329. }
  330. }
  331. function collapse_entry() {
  332. isCollapsed = !isCollapsed;
  333. var flux_current = $(".flux.current");
  334. flux_current.toggleClass("active");
  335. if (isCollapsed && context['auto_mark_article']) {
  336. mark_read(flux_current, true);
  337. }
  338. }
  339. function user_filter(key) {
  340. var filter = $('#dropdown-query');
  341. var filters = filter.siblings('.dropdown-menu').find('.item.query a');
  342. if (typeof key === "undefined") {
  343. if (!filter.length) {
  344. return;
  345. }
  346. // Display the filter div
  347. window.location.hash = filter.attr('id');
  348. // Force scrolling to the filter div
  349. var scroll = needsScroll($('.header'));
  350. if (scroll !== 0) {
  351. $('html,body').scrollTop(scroll);
  352. }
  353. // Force the key value if there is only one action, so we can trigger it automatically
  354. if (filters.length === 1) {
  355. key = 1;
  356. } else {
  357. return;
  358. }
  359. }
  360. // Trigger selected share action
  361. key = parseInt(key);
  362. if (key <= filters.length) {
  363. filters[key - 1].click();
  364. }
  365. }
  366. function auto_share(key) {
  367. var share = $(".flux.current.active").find('.dropdown-target[id^="dropdown-share"]');
  368. var shares = share.siblings('.dropdown-menu').find('.item a');
  369. if (typeof key === "undefined") {
  370. if (!share.length) {
  371. return;
  372. }
  373. // Display the share div
  374. window.location.hash = share.attr('id');
  375. // Force scrolling to the share div
  376. var scroll = needsScroll(share.closest('.bottom'));
  377. if (scroll !== 0) {
  378. $('html,body').scrollTop(scroll);
  379. }
  380. // Force the key value if there is only one action, so we can trigger it automatically
  381. if (shares.length === 1) {
  382. key = 1;
  383. } else {
  384. return;
  385. }
  386. }
  387. // Trigger selected share action and hide the share div
  388. key = parseInt(key);
  389. if (key <= shares.length) {
  390. shares[key - 1].click();
  391. share.siblings('.dropdown-menu').find('.dropdown-close a')[0].click();
  392. }
  393. }
  394. function inMarkViewport(flux, box_to_follow, relative_follow) {
  395. var top = flux.position().top;
  396. if (relative_follow) {
  397. top += box_to_follow.scrollTop();
  398. }
  399. var height = flux.height(),
  400. begin = top + 3 * height / 4,
  401. bot = Math.min(begin + 75, top + height),
  402. windowTop = box_to_follow.scrollTop(),
  403. windowBot = windowTop + box_to_follow.height() / 2;
  404. return (windowBot >= begin && bot >= windowBot);
  405. }
  406. function init_posts() {
  407. var box_to_follow = $(window),
  408. relative_follow = false;
  409. if (context['current_view'] == 'global') {
  410. box_to_follow = $("#panel");
  411. relative_follow = true;
  412. }
  413. if (context['auto_mark_scroll']) {
  414. box_to_follow.scroll(function () {
  415. $('.not_read:visible').each(function () {
  416. if ($(this).children(".flux_content").is(':visible') && inMarkViewport($(this), box_to_follow, relative_follow)) {
  417. mark_read($(this), true);
  418. }
  419. });
  420. });
  421. }
  422. if (context['auto_load_more']) {
  423. box_to_follow.scroll(function () {
  424. var load_more = $("#load_more");
  425. if (!load_more.is(':visible')) {
  426. return;
  427. }
  428. var boxBot = box_to_follow.scrollTop() + box_to_follow.height(),
  429. load_more_top = load_more.position().top;
  430. if (relative_follow) {
  431. load_more_top += box_to_follow.scrollTop();
  432. }
  433. if (boxBot >= load_more_top) {
  434. load_more_posts();
  435. }
  436. });
  437. box_to_follow.scroll();
  438. }
  439. }
  440. function init_column_categories() {
  441. if (context['current_view'] !== 'normal') {
  442. return;
  443. }
  444. $('#aside_feed').on('click', '.tree-folder>.tree-folder-title>a.dropdown-toggle', function () {
  445. $(this).children().each(function() {
  446. if (this.alt === '▽') {
  447. this.src = this.src.replace('/icons/down.', '/icons/up.');
  448. this.alt = '△';
  449. } else {
  450. this.src = this.src.replace('/icons/up.', '/icons/down.');
  451. this.alt = '▽';
  452. }
  453. });
  454. $(this).parent().next(".tree-folder-items").slideToggle();
  455. return false;
  456. });
  457. $('#aside_feed').on('click', '.tree-folder-items .item .dropdown-toggle', function () {
  458. if ($(this).nextAll('.dropdown-menu').length === 0) {
  459. var feed_id = $(this).closest('.item').attr('id').substr(2),
  460. feed_web = $(this).data('fweb'),
  461. template = $('#feed_config_template').html().replace(/------/g, feed_id).replace('http://example.net/', feed_web);
  462. $(this).attr('href', '#dropdown-' + feed_id).prev('.dropdown-target').attr('id', 'dropdown-' + feed_id).parent().append(template);
  463. }
  464. });
  465. }
  466. function init_shortcuts() {
  467. if (!(window.shortcut && window.shortcuts)) {
  468. if (window.console) {
  469. console.log('FreshRSS waiting for sortcut.js…');
  470. }
  471. window.setTimeout(init_shortcuts, 50);
  472. return;
  473. }
  474. // Touches de manipulation
  475. shortcut.add(shortcuts.mark_read, function () {
  476. // on marque comme lu ou non lu
  477. var active = $(".flux.current");
  478. mark_read(active, false);
  479. }, {
  480. 'disable_in_input': true
  481. });
  482. shortcut.add("shift+" + shortcuts.mark_read, function () {
  483. // on marque tout comme lu
  484. $(".nav_menu .read_all").click();
  485. }, {
  486. 'disable_in_input': true
  487. });
  488. shortcut.add(shortcuts.mark_favorite, function () {
  489. // on marque comme favori ou non favori
  490. var active = $(".flux.current");
  491. mark_favorite(active);
  492. }, {
  493. 'disable_in_input': true
  494. });
  495. shortcut.add(shortcuts.collapse_entry, function () {
  496. collapse_entry();
  497. }, {
  498. 'disable_in_input': true
  499. });
  500. shortcut.add(shortcuts.auto_share, function () {
  501. auto_share();
  502. }, {
  503. 'disable_in_input': true
  504. });
  505. shortcut.add(shortcuts.user_filter, function () {
  506. user_filter();
  507. }, {
  508. 'disable_in_input': true
  509. });
  510. for(var i = 1; i < 10; i++){
  511. shortcut.add(i.toString(), function (e) {
  512. if ($('#dropdown-query').siblings('.dropdown-menu').is(':visible')) {
  513. user_filter(String.fromCharCode(e.keyCode));
  514. } else {
  515. auto_share(String.fromCharCode(e.keyCode));
  516. }
  517. }, {
  518. 'disable_in_input': true
  519. });
  520. }
  521. // Touches de navigation pour les articles
  522. shortcut.add(shortcuts.prev_entry, prev_entry, {
  523. 'disable_in_input': true
  524. });
  525. shortcut.add(shortcuts.first_entry, function () {
  526. var old_active = $(".flux.current"),
  527. first = $(".flux:first");
  528. if (first.hasClass("flux")) {
  529. toggleContent(first, old_active);
  530. }
  531. }, {
  532. 'disable_in_input': true
  533. });
  534. shortcut.add(shortcuts.next_entry, next_entry, {
  535. 'disable_in_input': true
  536. });
  537. shortcut.add(shortcuts.last_entry, function () {
  538. var old_active = $(".flux.current"),
  539. last = $(".flux:last");
  540. if (last.hasClass("flux")) {
  541. toggleContent(last, old_active);
  542. }
  543. }, {
  544. 'disable_in_input': true
  545. });
  546. // Touches de navigation pour les flux
  547. shortcut.add("shift+" + shortcuts.prev_entry, prev_feed, {
  548. 'disable_in_input': true
  549. });
  550. shortcut.add("shift+" + shortcuts.next_entry, next_feed, {
  551. 'disable_in_input': true
  552. });
  553. shortcut.add("shift+" + shortcuts.first_entry, first_feed, {
  554. 'disable_in_input': true
  555. });
  556. shortcut.add("shift+" + shortcuts.last_entry, last_feed, {
  557. 'disable_in_input': true
  558. });
  559. // Touches de navigation pour les categories
  560. shortcut.add("alt+" + shortcuts.prev_entry, prev_category, {
  561. 'disable_in_input': true
  562. });
  563. shortcut.add("alt+" + shortcuts.next_entry, next_category, {
  564. 'disable_in_input': true
  565. });
  566. shortcut.add("alt+" + shortcuts.first_entry, first_category, {
  567. 'disable_in_input': true
  568. });
  569. shortcut.add("alt+" + shortcuts.last_entry, last_category, {
  570. 'disable_in_input': true
  571. });
  572. shortcut.add(shortcuts.go_website, function () {
  573. var url_website = $('.flux.current > .flux_header > .title > a').attr("href");
  574. if (context['auto_mark_site']) {
  575. $(".flux.current").each(function () {
  576. mark_read($(this), true);
  577. });
  578. }
  579. redirect(url_website, true);
  580. }, {
  581. 'disable_in_input': true
  582. });
  583. shortcut.add(shortcuts.load_more, function () {
  584. load_more_posts();
  585. }, {
  586. 'disable_in_input': true
  587. });
  588. shortcut.add(shortcuts.focus_search, function () {
  589. focus_search();
  590. }, {
  591. 'disable_in_input': true
  592. });
  593. shortcut.add(shortcuts.help, function () {
  594. redirect(url['help'], true);
  595. }, {
  596. 'disable_in_input': true
  597. });
  598. shortcut.add(shortcuts.close_dropdown, function () {
  599. window.location.hash = null;
  600. }, {
  601. 'disable_in_input': true
  602. });
  603. }
  604. function init_stream(divStream) {
  605. divStream.on('click', '.flux_header,.flux_content', function (e) { //flux_toggle
  606. if ($(e.target).closest('.content, .item.website, .item.link').length > 0) {
  607. return;
  608. }
  609. var old_active = $(".flux.current"),
  610. new_active = $(this).parent();
  611. isCollapsed = true;
  612. if (e.target.tagName.toUpperCase() === 'A') { //Leave real links alone
  613. if (context['auto_mark_article']) {
  614. mark_read(new_active, true);
  615. }
  616. return true;
  617. }
  618. toggleContent(new_active, old_active);
  619. });
  620. divStream.on('click', '.flux a.read', function () {
  621. var active = $(this).parents(".flux");
  622. if (context['auto_remove_article'] && active.hasClass('not_read')) {
  623. var p = active.prev();
  624. var n = active.next();
  625. if (p.hasClass('day') && n.hasClass('day')) {
  626. p.remove();
  627. }
  628. active.remove();
  629. }
  630. mark_read(active, false);
  631. return false;
  632. });
  633. divStream.on('click', '.flux a.bookmark', function () {
  634. var active = $(this).parents(".flux");
  635. mark_favorite(active);
  636. return false;
  637. });
  638. divStream.on('click', '.item.title > a', function (e) {
  639. // Allow default control-click behaviour such as open in backround-tab.
  640. return e.ctrlKey;
  641. });
  642. divStream.on('mouseup', '.item.title > a', function (e) {
  643. // Mouseup enables us to catch middle click.
  644. if (e.ctrlKey) {
  645. // CTRL+click, it will be manage by previous rule.
  646. return;
  647. }
  648. if (e.which == 2) {
  649. // If middle click, we want same behaviour as CTRL+click.
  650. var e = jQuery.Event("click");
  651. e.ctrlKey = true;
  652. $(this).trigger(e);
  653. } else if(e.which == 1) {
  654. // Normal click, just toggle article.
  655. $(this).parent().click();
  656. }
  657. });
  658. divStream.on('click', '.flux .content a', function () {
  659. $(this).attr('target', '_blank');
  660. });
  661. if (context['auto_mark_site']) {
  662. // catch mouseup instead of click so we can have the correct behaviour
  663. // with middle button click (scroll button).
  664. divStream.on('mouseup', '.flux .link > a', function (e) {
  665. if (e.which == 3) {
  666. return;
  667. }
  668. mark_read($(this).parents(".flux"), true);
  669. });
  670. }
  671. }
  672. function init_nav_entries() {
  673. var $nav_entries = $('#nav_entries');
  674. $nav_entries.find('.previous_entry').click(function () {
  675. prev_entry();
  676. return false;
  677. });
  678. $nav_entries.find('.next_entry').click(function () {
  679. next_entry();
  680. return false;
  681. });
  682. $nav_entries.find('.up').click(function () {
  683. var active_item = $(".flux.current"),
  684. windowTop = $(window).scrollTop(),
  685. item_top = active_item.position().top;
  686. if (windowTop > item_top) {
  687. $("html,body").scrollTop(item_top);
  688. } else {
  689. $("html,body").scrollTop(0);
  690. }
  691. return false;
  692. });
  693. }
  694. function init_actualize() {
  695. var auto = false;
  696. $("#actualize").click(function () {
  697. if (ajax_loading) {
  698. return false;
  699. }
  700. ajax_loading = true;
  701. $.getScript('./?c=javascript&a=actualize').done(function () {
  702. if (auto && feed_count < 1) {
  703. auto = false;
  704. ajax_loading = false;
  705. return false;
  706. }
  707. updateFeeds();
  708. });
  709. return false;
  710. });
  711. if (context['auto_actualize_feeds']) {
  712. auto = true;
  713. $("#actualize").click();
  714. }
  715. }
  716. // <notification>
  717. var notification = null,
  718. notification_interval = null,
  719. notification_working = false;
  720. function openNotification(msg, status) {
  721. if (notification_working === true) {
  722. return false;
  723. }
  724. notification_working = true;
  725. notification.removeClass();
  726. notification.addClass("notification");
  727. notification.addClass(status);
  728. notification.find(".msg").html(msg);
  729. notification.fadeIn(300);
  730. notification_interval = window.setTimeout(closeNotification, 4000);
  731. }
  732. function closeNotification() {
  733. notification.fadeOut(600, function() {
  734. notification.removeClass();
  735. notification.addClass('closed');
  736. window.clearInterval(notification_interval);
  737. notification_working = false;
  738. });
  739. }
  740. function init_notifications() {
  741. notification = $("#notification");
  742. notification.find("a.close").click(function () {
  743. closeNotification();
  744. return false;
  745. });
  746. if (notification.find(".msg").html().length > 0) {
  747. notification_working = true;
  748. notification_interval = window.setTimeout(closeNotification, 4000);
  749. }
  750. }
  751. // </notification>
  752. // <notifs html5>
  753. var notifs_html5_permission = 'denied';
  754. function notifs_html5_is_supported() {
  755. return window.Notification !== undefined;
  756. }
  757. function notifs_html5_ask_permission() {
  758. window.Notification.requestPermission(function () {
  759. notifs_html5_permission = window.Notification.permission;
  760. });
  761. }
  762. function notifs_html5_show(nb) {
  763. if (notifs_html5_permission !== "granted") {
  764. return
  765. }
  766. var notification = new window.Notification(i18n['notif_title_articles'], {
  767. icon: "../themes/icons/favicon-256.png",
  768. body: i18n['notif_body_articles'].replace("\d", nb),
  769. tag: "freshRssNewArticles"
  770. });
  771. notification.onclick = function() {
  772. window.location.reload();
  773. }
  774. if (context['html5_notif_timeout'] !== 0){
  775. setTimeout(function() {
  776. notification.close();
  777. }, context['html5_notif_timeout'] * 1000);
  778. }
  779. }
  780. function init_notifs_html5() {
  781. if (!notifs_html5_is_supported()) {
  782. return;
  783. }
  784. notifs_html5_permission = notifs_html5_ask_permission();
  785. }
  786. // </notifs html5>
  787. function refreshUnreads() {
  788. $.getJSON('./?c=javascript&a=nbUnreadsPerFeed').done(function (data) {
  789. var isAll = $('.category.all.active').length > 0,
  790. new_articles = false;
  791. $.each(data, function(feed_id, nbUnreads) {
  792. feed_id = 'f_' + feed_id;
  793. var elem = $('#' + feed_id).get(0),
  794. feed_unreads = elem ? str2int(elem.getAttribute('data-unread')) : 0;
  795. if ((incUnreadsFeed(null, feed_id, nbUnreads - feed_unreads) || isAll) && //Update of current view?
  796. (nbUnreads - feed_unreads > 0)) {
  797. $('#new-article').show();
  798. new_articles = true;
  799. };
  800. });
  801. var nb_unreads = str2int($('.category.all .title').attr('data-unread'));
  802. if (nb_unreads > 0 && new_articles) {
  803. faviconNbUnread(nb_unreads);
  804. notifs_html5_show(nb_unreads);
  805. }
  806. });
  807. }
  808. //<endless_mode>
  809. var url_load_more = "",
  810. load_more = false,
  811. box_load_more = null;
  812. function load_more_posts() {
  813. if (load_more || url_load_more === '' || box_load_more === null) {
  814. return;
  815. }
  816. load_more = true;
  817. $('#load_more').addClass('loading');
  818. $.get(url_load_more, function (data) {
  819. box_load_more.children('.flux:last').after($('#stream', data).children('.flux, .day'));
  820. $('.pagination').replaceWith($('.pagination', data));
  821. if (context['display_order'] === 'ASC') {
  822. $('#nav_menu_read_all > .read_all').attr(
  823. 'formaction', $('#bigMarkAsRead').attr('formaction')
  824. );
  825. } else {
  826. $('#bigMarkAsRead').attr(
  827. 'formaction', $('#nav_menu_read_all > .read_all').attr('formaction')
  828. );
  829. }
  830. $('[id^=day_]').each(function (i) {
  831. var ids = $('[id="' + this.id + '"]');
  832. if (ids.length > 1) {
  833. $('[id="' + this.id + '"]:gt(0)').remove();
  834. }
  835. });
  836. init_load_more(box_load_more);
  837. $('#load_more').removeClass('loading');
  838. load_more = false;
  839. });
  840. }
  841. function focus_search() {
  842. $('#search').focus();
  843. }
  844. function init_load_more(box) {
  845. box_load_more = box;
  846. if (!context['does_lazyload']) {
  847. $('img[postpone], audio[postpone], iframe[postpone], video[postpone]').each(function () {
  848. this.removeAttribute('postpone');
  849. });
  850. }
  851. var $next_link = $("#load_more");
  852. if (!$next_link.length) {
  853. // no more article to load
  854. url_load_more = "";
  855. return;
  856. }
  857. url_load_more = $next_link.attr("href");
  858. var $prefetch = $('#prefetch');
  859. if ($prefetch.attr('href') !== url_load_more) {
  860. $prefetch.attr('rel', 'next'); //Remove prefetch
  861. $.ajax({url: url_load_more, ifModified: true }); //TODO: Try to find a less agressive solution
  862. $prefetch.attr('href', url_load_more);
  863. }
  864. $next_link.click(function () {
  865. load_more_posts();
  866. return false;
  867. });
  868. }
  869. //</endless_mode>
  870. //<crypto form (Web login)>
  871. function poormanSalt() { //If crypto.getRandomValues is not available
  872. var text = '$2a$04$',
  873. base = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ.0123456789/abcdefghijklmnopqrstuvwxyz';
  874. for (var i = 22; i > 0; i--) {
  875. text += base.charAt(Math.floor(Math.random() * 64));
  876. }
  877. return text;
  878. }
  879. function init_crypto_form() {
  880. var $crypto_form = $('#crypto-form');
  881. if ($crypto_form.length === 0) {
  882. return;
  883. }
  884. if (!(window.dcodeIO)) {
  885. if (window.console) {
  886. console.log('FreshRSS waiting for bcrypt.js…');
  887. }
  888. window.setTimeout(init_crypto_form, 100);
  889. return;
  890. }
  891. $crypto_form.on('submit', function() {
  892. var $submit_button = $(this).find('button[type="submit"]');
  893. $submit_button.attr('disabled', '');
  894. var success = false;
  895. $.ajax({
  896. url: './?c=javascript&a=nonce&user=' + $('#username').val(),
  897. dataType: 'json',
  898. async: false
  899. }).done(function (data) {
  900. if (data.salt1 == '' || data.nonce == '') {
  901. openNotification('Invalid user!', 'bad');
  902. } else {
  903. try {
  904. var strong = window.Uint32Array && window.crypto && (typeof window.crypto.getRandomValues === 'function'),
  905. s = dcodeIO.bcrypt.hashSync($('#passwordPlain').val(), data.salt1),
  906. c = dcodeIO.bcrypt.hashSync(data.nonce + s, strong ? dcodeIO.bcrypt.genSaltSync(4) : poormanSalt());
  907. $('#challenge').val(c);
  908. if (s == '' || c == '') {
  909. openNotification('Crypto error!', 'bad');
  910. } else {
  911. success = true;
  912. }
  913. } catch (e) {
  914. openNotification('Crypto exception! ' + e, 'bad');
  915. }
  916. }
  917. }).fail(function() {
  918. openNotification('Communication error!', 'bad');
  919. });
  920. $submit_button.removeAttr('disabled');
  921. return success;
  922. });
  923. }
  924. //</crypto form (Web login)>
  925. function init_confirm_action() {
  926. $('body').on('click', '.confirm', function () {
  927. var str_confirmation = $(this).attr('data-str-confirm');
  928. if (!str_confirmation) {
  929. str_confirmation = i18n['confirmation_default'];
  930. }
  931. return confirm(str_confirmation);
  932. });
  933. }
  934. function init_print_action() {
  935. $('.item.share > a[href="#"]').click(function () {
  936. var content = "<html><head><style>"
  937. + "body { font-family: Serif; text-align: justify; }"
  938. + "a { color: #000; text-decoration: none; }"
  939. + "a:after { content: ' [' attr(href) ']'}"
  940. + "</style></head><body>"
  941. + $(".flux.current .content").html()
  942. + "</body></html>";
  943. var tmp_window = window.open();
  944. tmp_window.document.writeln(content);
  945. tmp_window.document.close();
  946. tmp_window.focus();
  947. tmp_window.print();
  948. tmp_window.close();
  949. return false;
  950. });
  951. }
  952. function init_share_observers() {
  953. shares = $('.group-share').length;
  954. $('.share.add').on('click', function(e) {
  955. var opt = $(this).siblings('select').find(':selected');
  956. var row = $(this).parents('form').data(opt.data('form'));
  957. row = row.replace('##label##', opt.html().trim(), 'g');
  958. row = row.replace('##type##', opt.val(), 'g');
  959. row = row.replace('##help##', opt.data('help'), 'g');
  960. row = row.replace('##key##', shares, 'g');
  961. $(this).parents('.form-group').before(row);
  962. shares++;
  963. return false;
  964. });
  965. }
  966. function init_stats_observers() {
  967. $('.select-change').on('change', function(e) {
  968. redirect($(this).find(':selected').data('url'));
  969. });
  970. }
  971. function init_remove_observers() {
  972. $('.post').on('click', 'a.remove', function(e) {
  973. var remove_what = $(this).attr('data-remove');
  974. if (remove_what !== undefined) {
  975. var remove_obj = $('#' + remove_what);
  976. remove_obj.remove();
  977. }
  978. return false;
  979. });
  980. }
  981. function init_feed_observers() {
  982. $('select[id="category"]').on('change', function() {
  983. var detail = $('#new_category_name').parent();
  984. if ($(this).val() === 'nc') {
  985. detail.show();
  986. detail.find('input').focus();
  987. } else {
  988. detail.hide();
  989. }
  990. });
  991. }
  992. function init_password_observers() {
  993. $('.toggle-password').on('mousedown', function(e) {
  994. var button = $(this);
  995. var passwordField = $('#' + button.attr('data-toggle'));
  996. passwordField.attr('type', 'text');
  997. button.addClass('active');
  998. return false;
  999. }).on('mouseup', function(e) {
  1000. var button = $(this);
  1001. var passwordField = $('#' + button.attr('data-toggle'));
  1002. passwordField.attr('type', 'password');
  1003. button.removeClass('active');
  1004. return false;
  1005. });
  1006. }
  1007. function faviconNbUnread(n) {
  1008. if (typeof n === 'undefined') {
  1009. n = str2int($('.category.all .title').attr('data-unread'));
  1010. }
  1011. //http://remysharp.com/2010/08/24/dynamic-favicons/
  1012. var canvas = document.createElement('canvas'),
  1013. link = document.getElementById('favicon').cloneNode(true);
  1014. if (canvas.getContext && link) {
  1015. canvas.height = canvas.width = 16;
  1016. var img = document.createElement('img');
  1017. img.onload = function () {
  1018. var ctx = canvas.getContext('2d');
  1019. ctx.drawImage(this, 0, 0, canvas.width, canvas.height);
  1020. if (n > 0) {
  1021. var text = '';
  1022. if (n < 1000) {
  1023. text = n;
  1024. } else if (n < 100000) {
  1025. text = Math.floor(n / 1000) + 'k';
  1026. } else {
  1027. text = 'E' + Math.floor(Math.log10(n));
  1028. }
  1029. ctx.font = 'bold 9px "Arial", sans-serif';
  1030. ctx.fillStyle = 'rgba(255, 255, 255, 0.8)';
  1031. ctx.fillRect(0, 7, ctx.measureText(text).width, 9);
  1032. ctx.fillStyle = '#F00';
  1033. ctx.fillText(text, 0, canvas.height - 1);
  1034. }
  1035. link.href = canvas.toDataURL('image/png');
  1036. $('link[rel~=icon]').remove();
  1037. document.head.appendChild(link);
  1038. };
  1039. img.src = '../favicon.ico';
  1040. }
  1041. }
  1042. function init_slider_observers() {
  1043. var slider = $('#slider'),
  1044. closer = $('#close-slider');
  1045. if (slider.length < 1) {
  1046. return;
  1047. }
  1048. $('.post').on('click', '.open-slider', function() {
  1049. if (ajax_loading) {
  1050. return false;
  1051. }
  1052. ajax_loading = true;
  1053. var url_slide = $(this).attr('href');
  1054. $.ajax({
  1055. type: 'GET',
  1056. url: url_slide,
  1057. data : { ajax: true }
  1058. }).done(function (data) {
  1059. slider.html(data);
  1060. closer.addClass('active');
  1061. slider.addClass('active');
  1062. ajax_loading = false;
  1063. });
  1064. return false;
  1065. });
  1066. closer.on('click', function() {
  1067. closer.removeClass('active');
  1068. slider.removeClass('active');
  1069. return false;
  1070. });
  1071. }
  1072. function init_all() {
  1073. if (!(window.$ && window.context)) {
  1074. if (window.console) {
  1075. console.log('FreshRSS waiting for JS…');
  1076. }
  1077. window.setTimeout(init_all, 50);
  1078. return;
  1079. }
  1080. init_notifications();
  1081. init_confirm_action();
  1082. $stream = $('#stream');
  1083. if ($stream.length > 0) {
  1084. init_actualize();
  1085. init_column_categories();
  1086. init_load_more($stream);
  1087. init_posts();
  1088. init_stream($stream);
  1089. init_nav_entries();
  1090. init_shortcuts();
  1091. faviconNbUnread();
  1092. init_print_action();
  1093. init_notifs_html5();
  1094. window.setInterval(refreshUnreads, 120000);
  1095. } else {
  1096. init_crypto_form();
  1097. init_share_observers();
  1098. init_remove_observers();
  1099. init_feed_observers();
  1100. init_password_observers();
  1101. init_stats_observers();
  1102. init_slider_observers();
  1103. }
  1104. if (window.console) {
  1105. console.log('FreshRSS init done.');
  1106. }
  1107. }
  1108. if (document.readyState && document.readyState !== 'loading') {
  1109. if (window.console) {
  1110. console.log('FreshRSS immediate init…');
  1111. }
  1112. init_all();
  1113. } else if (document.addEventListener) {
  1114. document.addEventListener('DOMContentLoaded', function () {
  1115. if (window.console) {
  1116. console.log('FreshRSS waiting for DOMContentLoaded…');
  1117. }
  1118. init_all();
  1119. }, false);
  1120. }