main.js 34 KB

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