main.js 35 KB

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