main.js 34 KB

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