main.js 34 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352
  1. "use strict";
  2. var $stream = null,
  3. isCollapsed = true,
  4. shares = 0,
  5. ajax_loading = false;
  6. function redirect(url, new_tab) {
  7. if (url) {
  8. if (new_tab) {
  9. window.open(url);
  10. } else {
  11. location.href = url;
  12. }
  13. }
  14. }
  15. function needsScroll($elem) {
  16. var $win = $(window),
  17. winTop = $win.scrollTop(),
  18. winHeight = $win.height(),
  19. winBottom = winTop + winHeight,
  20. elemTop = $elem.offset().top,
  21. elemBottom = elemTop + $elem.outerHeight();
  22. return (elemTop < winTop || elemBottom > winBottom) ? elemTop - (winHeight / 2) : 0;
  23. }
  24. function str2int(str) {
  25. if (!str) {
  26. return 0;
  27. }
  28. return parseInt(str.replace(/\D/g, ''), 10) || 0;
  29. }
  30. function numberFormat(nStr) {
  31. if (nStr < 0) {
  32. return 0;
  33. }
  34. // http://www.mredkj.com/javascript/numberFormat.html
  35. nStr += '';
  36. var x = nStr.split('.'),
  37. x1 = x[0],
  38. x2 = x.length > 1 ? '.' + x[1] : '',
  39. rgx = /(\d+)(\d{3})/;
  40. while (rgx.test(x1)) {
  41. x1 = x1.replace(rgx, '$1' + ' ' + '$2');
  42. }
  43. return x1 + x2;
  44. }
  45. function incLabel(p, inc, spaceAfter) {
  46. var i = str2int(p) + inc;
  47. return i > 0
  48. ? ((spaceAfter ? '' : ' ') + '(' + numberFormat(i) + ')' + (spaceAfter ? ' ' : ''))
  49. : '';
  50. }
  51. function incUnreadsFeed(article, feed_id, nb) {
  52. //Update unread: feed
  53. var elem = $('#' + feed_id).get(0),
  54. feed_unreads = elem ? str2int(elem.getAttribute('data-unread')) : 0,
  55. feed_priority = elem ? str2int(elem.getAttribute('data-priority')) : 0;
  56. if (elem) {
  57. elem.setAttribute('data-unread', feed_unreads + nb);
  58. elem = $(elem).children('.item-title').get(0);
  59. if (elem) {
  60. elem.setAttribute('data-unread', numberFormat(feed_unreads + nb));
  61. }
  62. }
  63. //Update unread: category
  64. elem = $('#' + feed_id).parents('.category').get(0);
  65. feed_unreads = elem ? str2int(elem.getAttribute('data-unread')) : 0;
  66. if (elem) {
  67. elem.setAttribute('data-unread', feed_unreads + nb);
  68. elem = $(elem).find('.title').get(0);
  69. if (elem) {
  70. elem.setAttribute('data-unread', numberFormat(feed_unreads + nb));
  71. }
  72. }
  73. //Update unread: all
  74. if (feed_priority > 0) {
  75. elem = $('#aside_feed .all .title').get(0);
  76. if (elem) {
  77. feed_unreads = elem ? str2int(elem.getAttribute('data-unread')) : 0;
  78. elem.setAttribute('data-unread', numberFormat(feed_unreads + nb));
  79. }
  80. }
  81. //Update unread: favourites
  82. if (article && article.closest('div').hasClass('favorite')) {
  83. elem = $('#aside_feed .favorites .title').get(0);
  84. if (elem) {
  85. feed_unreads = elem ? str2int(elem.getAttribute('data-unread')) : 0;
  86. elem.setAttribute('data-unread', numberFormat(feed_unreads + nb));
  87. }
  88. }
  89. var isCurrentView = false;
  90. // Update unread: title
  91. document.title = document.title.replace(/^((?:\([ 0-9]+\) )?)/, function (m, p1) {
  92. var $feed = $('#' + feed_id);
  93. if (article || ($feed.closest('.active').length > 0 && $feed.siblings('.active').length === 0)) {
  94. isCurrentView = true;
  95. return incLabel(p1, nb, true);
  96. } else if ($('.all.active').length > 0) {
  97. isCurrentView = feed_priority > 0;
  98. return incLabel(p1, feed_priority > 0 ? nb : 0, true);
  99. } else {
  100. return p1;
  101. }
  102. });
  103. return isCurrentView;
  104. }
  105. var pending_feeds = [];
  106. function mark_read(active, only_not_read) {
  107. if (active.length === 0 ||
  108. (only_not_read === true && !active.hasClass("not_read"))) {
  109. return false;
  110. }
  111. var url = active.find("a.read").attr("href");
  112. if (url === undefined) {
  113. return false;
  114. }
  115. var feed_url = active.find(".website>a").attr("href"),
  116. feed_id = feed_url.substr(feed_url.lastIndexOf('f_')),
  117. index_pending = pending_feeds.indexOf(feed_id);
  118. if (index_pending !== -1) {
  119. return false;
  120. }
  121. pending_feeds.push(feed_id);
  122. $.ajax({
  123. type: 'POST',
  124. url: url,
  125. data : { ajax: true }
  126. }).done(function (data) {
  127. var $r = active.find("a.read").attr("href", data.url),
  128. inc = 0;
  129. if (active.hasClass("not_read")) {
  130. active.removeClass("not_read");
  131. inc--;
  132. } else if (only_not_read !== true || active.hasClass("not_read")) {
  133. active.addClass("not_read");
  134. inc++;
  135. }
  136. $r.find('.icon').replaceWith(data.icon);
  137. incUnreadsFeed(active, feed_id, inc);
  138. faviconNbUnread();
  139. pending_feeds.splice(index_pending, 1);
  140. }).fail(function (data) {
  141. openNotification(i18n.notif_request_failed, 'bad');
  142. pending_feeds.splice(index_pending, 1);
  143. });
  144. }
  145. function mark_favorite(active) {
  146. if (active.length === 0) {
  147. return false;
  148. }
  149. var url = active.find("a.bookmark").attr("href");
  150. if (url === undefined) {
  151. return false;
  152. }
  153. var feed_url = active.find(".website>a").attr("href"),
  154. feed_id = feed_url.substr(feed_url.lastIndexOf('f_')),
  155. index_pending = pending_feeds.indexOf(feed_id);
  156. if (index_pending !== -1) {
  157. return false;
  158. }
  159. pending_feeds.push(feed_id);
  160. $.ajax({
  161. type: 'POST',
  162. url: url,
  163. data : { ajax: true }
  164. }).done(function (data) {
  165. var $b = active.find("a.bookmark").attr("href", data.url),
  166. inc = 0;
  167. if (active.hasClass("favorite")) {
  168. active.removeClass("favorite");
  169. inc--;
  170. } else {
  171. active.addClass("favorite").find('.bookmark');
  172. inc++;
  173. }
  174. $b.find('.icon').replaceWith(data.icon);
  175. var favourites = $('#aside_feed .favorites .title').contents().last().get(0);
  176. if (favourites && favourites.textContent) {
  177. favourites.textContent = favourites.textContent.replace(/((?: \([ 0-9]+\))?\s*)$/, function (m, p1) {
  178. return incLabel(p1, inc, false);
  179. });
  180. }
  181. if (active.closest('div').hasClass('not_read')) {
  182. var elem = $('#aside_feed .favorites .title').get(0),
  183. feed_unreads = elem ? str2int(elem.getAttribute('data-unread')) : 0;
  184. if (elem) {
  185. elem.setAttribute('data-unread', numberFormat(feed_unreads + inc));
  186. }
  187. }
  188. pending_feeds.splice(index_pending, 1);
  189. }).fail(function (data) {
  190. openNotification(i18n.notif_request_failed, 'bad');
  191. pending_feeds.splice(index_pending, 1);
  192. });
  193. }
  194. function toggleContent(new_active, old_active) {
  195. if (new_active.length === 0) {
  196. return;
  197. }
  198. if (context['does_lazyload']) {
  199. new_active.find('img[data-original], iframe[data-original]').each(function () {
  200. this.setAttribute('src', this.getAttribute('data-original'));
  201. this.removeAttribute('data-original');
  202. });
  203. }
  204. if (old_active[0] !== new_active[0]) {
  205. if (isCollapsed) {
  206. new_active.addClass("active");
  207. }
  208. old_active.removeClass("active current");
  209. new_active.addClass("current");
  210. if (context['auto_remove_article'] && !old_active.hasClass('not_read')) {
  211. auto_remove(old_active);
  212. }
  213. } else {
  214. new_active.toggleClass('active');
  215. }
  216. var relative_move = context['current_view'] === 'global',
  217. box_to_move = $(relative_move ? "#panel" : "html,body");
  218. if (context['sticky_post']) {
  219. var prev_article = new_active.prevAll('.flux'),
  220. new_pos = new_active.offset().top,
  221. old_scroll = box_to_move.scrollTop();
  222. if (prev_article.length > 0 && new_pos - prev_article.offset().top <= 150) {
  223. new_pos = prev_article.offset().top;
  224. if (relative_move) {
  225. new_pos -= box_to_move.offset().top;
  226. }
  227. }
  228. if (context['hide_posts']) {
  229. if (relative_move) {
  230. new_pos += old_scroll;
  231. }
  232. if (old_active[0] !== new_active[0]) {
  233. new_active.children(".flux_content").first().each(function () {
  234. box_to_move.scrollTop(new_pos).scrollTop();
  235. });
  236. }
  237. } else {
  238. if (relative_move) {
  239. new_pos += old_scroll;
  240. }
  241. box_to_move.scrollTop(new_pos).scrollTop();
  242. }
  243. }
  244. if (context['auto_mark_article'] && new_active.hasClass('active')) {
  245. mark_read(new_active, true);
  246. }
  247. }
  248. function auto_remove(element) {
  249. var p = element.prev();
  250. var n = element.next();
  251. if (p.hasClass('day') && n.hasClass('day')) {
  252. p.remove();
  253. }
  254. element.remove();
  255. $('#stream > .flux:not(.not_read):not(.active)').remove();
  256. }
  257. function prev_entry() {
  258. var old_active = $(".flux.current"),
  259. new_active = old_active.length === 0 ? $(".flux:last") : old_active.prevAll(".flux:first");
  260. toggleContent(new_active, old_active);
  261. }
  262. function next_entry() {
  263. var old_active = $(".flux.current"),
  264. new_active = old_active.length === 0 ? $(".flux:first") : old_active.nextAll(".flux:first");
  265. toggleContent(new_active, old_active);
  266. if (new_active.nextAll().length < 3) {
  267. load_more_posts();
  268. }
  269. }
  270. function prev_feed() {
  271. var active_feed = $("#aside_feed .tree-folder-items .item.active");
  272. if (active_feed.length > 0) {
  273. active_feed.prevAll(':visible:first').find('a').each(function(){this.click();});
  274. } else {
  275. last_feed();
  276. }
  277. }
  278. function next_feed() {
  279. var active_feed = $("#aside_feed .tree-folder-items .item.active");
  280. if (active_feed.length > 0) {
  281. active_feed.nextAll(':visible:first').find('a').each(function(){this.click();});
  282. } else {
  283. first_feed();
  284. }
  285. }
  286. function first_feed() {
  287. var feed = $("#aside_feed .tree-folder-items.active .item:visible:first");
  288. if (feed.length > 0) {
  289. feed.find('a')[1].click();
  290. }
  291. }
  292. function last_feed() {
  293. var feed = $("#aside_feed .tree-folder-items.active .item:visible:last");
  294. if (feed.length > 0) {
  295. feed.find('a')[1].click();
  296. }
  297. }
  298. function prev_category() {
  299. var active_cat = $("#aside_feed .tree-folder.active");
  300. if (active_cat.length > 0) {
  301. var prev_cat = active_cat.prevAll(':visible:first').find('.tree-folder-title .title');
  302. if (prev_cat.length > 0) {
  303. prev_cat[0].click();
  304. }
  305. } else {
  306. last_category();
  307. }
  308. return;
  309. }
  310. function next_category() {
  311. var active_cat = $("#aside_feed .tree-folder.active");
  312. if (active_cat.length > 0) {
  313. var next_cat = active_cat.nextAll(':visible:first').find('.tree-folder-title .title');
  314. if (next_cat.length > 0) {
  315. next_cat[0].click();
  316. }
  317. } else {
  318. first_category();
  319. }
  320. return;
  321. }
  322. function first_category() {
  323. var cat = $("#aside_feed .tree-folder:visible:first");
  324. if (cat.length > 0) {
  325. cat.find('.tree-folder-title .title')[0].click();
  326. }
  327. }
  328. function last_category() {
  329. var cat = $("#aside_feed .tree-folder:visible:last");
  330. if (cat.length > 0) {
  331. cat.find('.tree-folder-title .title')[0].click();
  332. }
  333. }
  334. function collapse_entry() {
  335. isCollapsed = !isCollapsed;
  336. var flux_current = $(".flux.current");
  337. flux_current.toggleClass("active");
  338. if (isCollapsed && context['auto_mark_article']) {
  339. mark_read(flux_current, true);
  340. }
  341. }
  342. function user_filter(key) {
  343. var filter = $('#dropdown-query');
  344. var filters = filter.siblings('.dropdown-menu').find('.item.query a');
  345. if (typeof key === "undefined") {
  346. if (!filter.length) {
  347. return;
  348. }
  349. // Display the filter div
  350. window.location.hash = filter.attr('id');
  351. // Force scrolling to the filter div
  352. var scroll = needsScroll($('.header'));
  353. if (scroll !== 0) {
  354. $('html,body').scrollTop(scroll);
  355. }
  356. // Force the key value if there is only one action, so we can trigger it automatically
  357. if (filters.length === 1) {
  358. key = 1;
  359. } else {
  360. return;
  361. }
  362. }
  363. // Trigger selected share action
  364. key = parseInt(key);
  365. if (key <= filters.length) {
  366. filters[key - 1].click();
  367. }
  368. }
  369. function auto_share(key) {
  370. var share = $(".flux.current.active").find('.dropdown-target[id^="dropdown-share"]');
  371. var shares = share.siblings('.dropdown-menu').find('.item a');
  372. if (typeof key === "undefined") {
  373. if (!share.length) {
  374. return;
  375. }
  376. // Display the share div
  377. window.location.hash = share.attr('id');
  378. // Force scrolling to the share div
  379. var scroll = needsScroll(share.closest('.bottom'));
  380. if (scroll !== 0) {
  381. $('html,body').scrollTop(scroll);
  382. }
  383. // Force the key value if there is only one action, so we can trigger it automatically
  384. if (shares.length === 1) {
  385. key = 1;
  386. } else {
  387. return;
  388. }
  389. }
  390. // Trigger selected share action and hide the share div
  391. key = parseInt(key);
  392. if (key <= shares.length) {
  393. shares[key - 1].click();
  394. share.siblings('.dropdown-menu').find('.dropdown-close a')[0].click();
  395. }
  396. }
  397. function inMarkViewport(flux, box_to_follow) {
  398. var top = flux.offset().top;
  399. var height = flux.height(),
  400. begin = top + 3 * height / 4,
  401. bot = Math.min(begin + 75, top + height),
  402. windowTop = box_to_follow.scrollTop(),
  403. windowBot = windowTop + box_to_follow.height() / 2;
  404. return (windowBot >= begin && bot >= windowBot);
  405. }
  406. function init_posts() {
  407. var box_to_follow = $(window);
  408. if (context['current_view'] === 'global') {
  409. box_to_follow = $("#panel");
  410. }
  411. if (context['auto_mark_scroll']) {
  412. box_to_follow.scroll(function () {
  413. $('.not_read:visible').each(function () {
  414. if ($(this).children(".flux_content").is(':visible') && inMarkViewport($(this), box_to_follow)) {
  415. mark_read($(this), true);
  416. }
  417. });
  418. });
  419. }
  420. if (context['auto_load_more']) {
  421. box_to_follow.scroll(function () {
  422. var load_more = $("#load_more");
  423. if (!load_more.is(':visible')) {
  424. return;
  425. }
  426. var boxBot = box_to_follow.scrollTop() + box_to_follow.height(),
  427. load_more_top = load_more.offset().top;
  428. if (boxBot >= load_more_top) {
  429. load_more_posts();
  430. }
  431. });
  432. box_to_follow.scroll();
  433. }
  434. }
  435. function init_column_categories() {
  436. if (context['current_view'] !== 'normal') {
  437. return;
  438. }
  439. $('#aside_feed').on('click', '.tree-folder>.tree-folder-title>a.dropdown-toggle', function () {
  440. $(this).children().each(function() {
  441. if (this.alt === '▽') {
  442. this.src = this.src.replace('/icons/down.', '/icons/up.');
  443. this.alt = '△';
  444. } else {
  445. this.src = this.src.replace('/icons/up.', '/icons/down.');
  446. this.alt = '▽';
  447. }
  448. });
  449. $(this).parent().next(".tree-folder-items").slideToggle();
  450. return false;
  451. });
  452. $('#aside_feed').on('click', '.tree-folder-items .item .dropdown-toggle', function () {
  453. if ($(this).nextAll('.dropdown-menu').length === 0) {
  454. var feed_id = $(this).closest('.item').attr('id').substr(2),
  455. feed_web = $(this).data('fweb'),
  456. template = $('#feed_config_template').html().replace(/------/g, feed_id).replace('http://example.net/', feed_web);
  457. $(this).attr('href', '#dropdown-' + feed_id).prev('.dropdown-target').attr('id', 'dropdown-' + feed_id).parent().append(template);
  458. }
  459. });
  460. }
  461. function init_shortcuts() {
  462. if (!(window.shortcut && window.shortcuts)) {
  463. if (window.console) {
  464. console.log('FreshRSS waiting for sortcut.js…');
  465. }
  466. window.setTimeout(init_shortcuts, 50);
  467. return;
  468. }
  469. // Touches de manipulation
  470. shortcut.add(shortcuts.mark_read, function () {
  471. // on marque comme lu ou non lu
  472. var active = $(".flux.current");
  473. mark_read(active, false);
  474. }, {
  475. 'disable_in_input': true
  476. });
  477. shortcut.add("shift+" + shortcuts.mark_read, function () {
  478. // on marque tout comme lu
  479. $(".nav_menu .read_all").click();
  480. }, {
  481. 'disable_in_input': true
  482. });
  483. shortcut.add(shortcuts.mark_favorite, function () {
  484. // on marque comme favori ou non favori
  485. var active = $(".flux.current");
  486. mark_favorite(active);
  487. }, {
  488. 'disable_in_input': true
  489. });
  490. shortcut.add(shortcuts.collapse_entry, function () {
  491. collapse_entry();
  492. }, {
  493. 'disable_in_input': true
  494. });
  495. shortcut.add(shortcuts.auto_share, function () {
  496. auto_share();
  497. }, {
  498. 'disable_in_input': true
  499. });
  500. shortcut.add(shortcuts.user_filter, function () {
  501. user_filter();
  502. }, {
  503. 'disable_in_input': true
  504. });
  505. for(var i = 1; i < 10; i++){
  506. shortcut.add(i.toString(), function (e) {
  507. if ($('#dropdown-query').siblings('.dropdown-menu').is(':visible')) {
  508. user_filter(String.fromCharCode(e.keyCode));
  509. } else {
  510. auto_share(String.fromCharCode(e.keyCode));
  511. }
  512. }, {
  513. 'disable_in_input': true
  514. });
  515. }
  516. // Touches de navigation pour les articles
  517. shortcut.add(shortcuts.prev_entry, prev_entry, {
  518. 'disable_in_input': true
  519. });
  520. shortcut.add(shortcuts.first_entry, function () {
  521. var old_active = $(".flux.current"),
  522. first = $(".flux:first");
  523. if (first.hasClass("flux")) {
  524. toggleContent(first, old_active);
  525. }
  526. }, {
  527. 'disable_in_input': true
  528. });
  529. shortcut.add(shortcuts.next_entry, next_entry, {
  530. 'disable_in_input': true
  531. });
  532. shortcut.add(shortcuts.last_entry, function () {
  533. var old_active = $(".flux.current"),
  534. last = $(".flux:last");
  535. if (last.hasClass("flux")) {
  536. toggleContent(last, old_active);
  537. }
  538. }, {
  539. 'disable_in_input': true
  540. });
  541. // Touches de navigation pour les flux
  542. shortcut.add("shift+" + shortcuts.prev_entry, prev_feed, {
  543. 'disable_in_input': true
  544. });
  545. shortcut.add("shift+" + shortcuts.next_entry, next_feed, {
  546. 'disable_in_input': true
  547. });
  548. shortcut.add("shift+" + shortcuts.first_entry, first_feed, {
  549. 'disable_in_input': true
  550. });
  551. shortcut.add("shift+" + shortcuts.last_entry, last_feed, {
  552. 'disable_in_input': true
  553. });
  554. // Touches de navigation pour les categories
  555. shortcut.add("alt+" + shortcuts.prev_entry, prev_category, {
  556. 'disable_in_input': true
  557. });
  558. shortcut.add("alt+" + shortcuts.next_entry, next_category, {
  559. 'disable_in_input': true
  560. });
  561. shortcut.add("alt+" + shortcuts.first_entry, first_category, {
  562. 'disable_in_input': true
  563. });
  564. shortcut.add("alt+" + shortcuts.last_entry, last_category, {
  565. 'disable_in_input': true
  566. });
  567. shortcut.add(shortcuts.go_website, function () {
  568. var url_website = $('.flux.current > .flux_header > .title > a').attr("href");
  569. if (context['auto_mark_site']) {
  570. $(".flux.current").each(function () {
  571. mark_read($(this), true);
  572. });
  573. }
  574. redirect(url_website, true);
  575. }, {
  576. 'disable_in_input': true
  577. });
  578. shortcut.add(shortcuts.load_more, function () {
  579. load_more_posts();
  580. }, {
  581. 'disable_in_input': true
  582. });
  583. shortcut.add(shortcuts.focus_search, function () {
  584. focus_search();
  585. }, {
  586. 'disable_in_input': true
  587. });
  588. shortcut.add(shortcuts.help, function () {
  589. redirect(url['help'], true);
  590. }, {
  591. 'disable_in_input': true
  592. });
  593. shortcut.add(shortcuts.close_dropdown, function () {
  594. window.location.hash = null;
  595. }, {
  596. 'disable_in_input': true
  597. });
  598. }
  599. function init_stream(divStream) {
  600. divStream.on('click', '.flux_header,.flux_content', function (e) { //flux_toggle
  601. if ($(e.target).closest('.content, .item.website, .item.link').length > 0) {
  602. return;
  603. }
  604. var old_active = $(".flux.current"),
  605. new_active = $(this).parent();
  606. isCollapsed = true;
  607. if (e.target.tagName.toUpperCase() === 'A') { //Leave real links alone
  608. if (context['auto_mark_article']) {
  609. mark_read(new_active, true);
  610. }
  611. return true;
  612. }
  613. toggleContent(new_active, old_active);
  614. });
  615. divStream.on('click', '.flux a.read', function () {
  616. var active = $(this).parents(".flux");
  617. if (context['auto_remove_article'] && active.hasClass('not_read')) {
  618. auto_remove(active);
  619. }
  620. mark_read(active, false);
  621. return false;
  622. });
  623. divStream.on('click', '.flux a.bookmark', function () {
  624. var active = $(this).parents(".flux");
  625. mark_favorite(active);
  626. return false;
  627. });
  628. divStream.on('click', '.item.title > a', function (e) {
  629. // Allow default control-click behaviour such as open in backround-tab.
  630. return e.ctrlKey;
  631. });
  632. divStream.on('mouseup', '.item.title > a', function (e) {
  633. // Mouseup enables us to catch middle click.
  634. if (e.ctrlKey) {
  635. // CTRL+click, it will be manage by previous rule.
  636. return;
  637. }
  638. if (e.which == 2) {
  639. // If middle click, we want same behaviour as CTRL+click.
  640. var e = jQuery.Event("click");
  641. e.ctrlKey = true;
  642. $(this).trigger(e);
  643. } else if(e.which == 1) {
  644. // Normal click, just toggle article.
  645. $(this).parent().click();
  646. }
  647. });
  648. divStream.on('click', '.flux .content a', function () {
  649. $(this).attr('target', '_blank');
  650. });
  651. if (context['auto_mark_site']) {
  652. // catch mouseup instead of click so we can have the correct behaviour
  653. // with middle button click (scroll button).
  654. divStream.on('mouseup', '.flux .link > a', function (e) {
  655. if (e.which == 3) {
  656. return;
  657. }
  658. mark_read($(this).parents(".flux"), true);
  659. });
  660. }
  661. }
  662. function init_nav_entries() {
  663. var $nav_entries = $('#nav_entries');
  664. $nav_entries.find('.previous_entry').click(function () {
  665. prev_entry();
  666. return false;
  667. });
  668. $nav_entries.find('.next_entry').click(function () {
  669. next_entry();
  670. return false;
  671. });
  672. $nav_entries.find('.up').click(function () {
  673. var active_item = $(".flux.current"),
  674. windowTop = $(window).scrollTop(),
  675. item_top = active_item.offset().top;
  676. if (windowTop > item_top) {
  677. $("html,body").scrollTop(item_top);
  678. } else {
  679. $("html,body").scrollTop(0);
  680. }
  681. return false;
  682. });
  683. }
  684. // <actualize>
  685. var feed_processed = 0;
  686. function updateFeed(feeds, feeds_count) {
  687. var feed = feeds.pop();
  688. if (feed == undefined) {
  689. return;
  690. }
  691. $.ajax({
  692. type: 'POST',
  693. url: feed['url'],
  694. }).complete(function (data) {
  695. feed_processed++;
  696. $("#actualizeProgress .progress").html(feed_processed + " / " + feeds_count);
  697. $("#actualizeProgress .title").html(feed['title']);
  698. if (feed_processed === feeds_count) {
  699. window.location.reload();
  700. } else {
  701. updateFeed(feeds, feeds_count);
  702. }
  703. });
  704. }
  705. function init_actualize() {
  706. var auto = false;
  707. $("#actualize").click(function () {
  708. if (ajax_loading) {
  709. return false;
  710. }
  711. ajax_loading = true;
  712. $.getJSON('./?c=javascript&a=actualize').done(function (data) {
  713. if (auto && data.feeds.length < 1) {
  714. auto = false;
  715. ajax_loading = false;
  716. return false;
  717. }
  718. if (data.feeds.length === 0) {
  719. openNotification(data.feedback_no_refresh, "good");
  720. ajax_loading = false;
  721. return;
  722. }
  723. //Progress bar
  724. var feeds_count = data.feeds.length;
  725. $('body').after('<div id="actualizeProgress" class="notification good">' + data.feedback_actualize + '<br /><span class="title">/</span><br /><span class="progress">0 / ' + feeds_count + '</span></div>');
  726. for (var i = 10; i > 0; i--) {
  727. updateFeed(data.feeds, feeds_count);
  728. }
  729. });
  730. return false;
  731. });
  732. if (context['auto_actualize_feeds']) {
  733. auto = true;
  734. $("#actualize").click();
  735. }
  736. }
  737. // </actualize>
  738. // <notification>
  739. var notification = null,
  740. notification_interval = null,
  741. notification_working = false;
  742. function openNotification(msg, status) {
  743. if (notification_working === true) {
  744. return false;
  745. }
  746. notification_working = true;
  747. notification.removeClass();
  748. notification.addClass("notification");
  749. notification.addClass(status);
  750. notification.find(".msg").html(msg);
  751. notification.fadeIn(300);
  752. notification_interval = window.setTimeout(closeNotification, 4000);
  753. }
  754. function closeNotification() {
  755. notification.fadeOut(600, function() {
  756. notification.removeClass();
  757. notification.addClass('closed');
  758. window.clearInterval(notification_interval);
  759. notification_working = false;
  760. });
  761. }
  762. function init_notifications() {
  763. notification = $("#notification");
  764. notification.find("a.close").click(function () {
  765. closeNotification();
  766. return false;
  767. });
  768. if (notification.find(".msg").html().length > 0) {
  769. notification_working = true;
  770. notification_interval = window.setTimeout(closeNotification, 4000);
  771. }
  772. }
  773. // </notification>
  774. // <notifs html5>
  775. var notifs_html5_permission = 'denied';
  776. function notifs_html5_is_supported() {
  777. return window.Notification !== undefined;
  778. }
  779. function notifs_html5_ask_permission() {
  780. window.Notification.requestPermission(function () {
  781. notifs_html5_permission = window.Notification.permission;
  782. });
  783. }
  784. function notifs_html5_show(nb) {
  785. if (notifs_html5_permission !== "granted") {
  786. return
  787. }
  788. var notification = new window.Notification(i18n['notif_title_articles'], {
  789. icon: "../themes/icons/favicon-256.png",
  790. body: i18n['notif_body_articles'].replace('%d', nb),
  791. tag: "freshRssNewArticles"
  792. });
  793. notification.onclick = function() {
  794. window.location.reload();
  795. }
  796. if (context['html5_notif_timeout'] !== 0) {
  797. setTimeout(function() {
  798. notification.close();
  799. }, context['html5_notif_timeout'] * 1000);
  800. }
  801. }
  802. function init_notifs_html5() {
  803. if (!notifs_html5_is_supported()) {
  804. return;
  805. }
  806. notifs_html5_permission = notifs_html5_ask_permission();
  807. }
  808. // </notifs html5>
  809. function refreshUnreads() {
  810. $.getJSON('./?c=javascript&a=nbUnreadsPerFeed').done(function (data) {
  811. var isAll = $('.category.all.active').length > 0,
  812. new_articles = false;
  813. $.each(data, function(feed_id, nbUnreads) {
  814. feed_id = 'f_' + feed_id;
  815. var elem = $('#' + feed_id).get(0),
  816. feed_unreads = elem ? str2int(elem.getAttribute('data-unread')) : 0;
  817. if ((incUnreadsFeed(null, feed_id, nbUnreads - feed_unreads) || isAll) && //Update of current view?
  818. (nbUnreads - feed_unreads > 0)) {
  819. $('#new-article').attr('aria-hidden', 'false').show();
  820. new_articles = true;
  821. };
  822. });
  823. var nb_unreads = str2int($('.category.all .title').attr('data-unread'));
  824. if (nb_unreads > 0 && new_articles) {
  825. faviconNbUnread(nb_unreads);
  826. notifs_html5_show(nb_unreads);
  827. }
  828. });
  829. }
  830. //<endless_mode>
  831. var url_load_more = "",
  832. load_more = false,
  833. box_load_more = null;
  834. function load_more_posts() {
  835. if (load_more || url_load_more === '' || box_load_more === null) {
  836. return;
  837. }
  838. load_more = true;
  839. $('#load_more').addClass('loading');
  840. $.get(url_load_more, function (data) {
  841. box_load_more.children('.flux:last').after($('#stream', data).children('.flux, .day'));
  842. $('.pagination').replaceWith($('.pagination', data));
  843. if (context['display_order'] === 'ASC') {
  844. $('#nav_menu_read_all > .read_all').attr(
  845. 'formaction', $('#bigMarkAsRead').attr('formaction')
  846. );
  847. } else {
  848. $('#bigMarkAsRead').attr(
  849. 'formaction', $('#nav_menu_read_all > .read_all').attr('formaction')
  850. );
  851. }
  852. $('[id^=day_]').each(function (i) {
  853. var ids = $('[id="' + this.id + '"]');
  854. if (ids.length > 1) {
  855. $('[id="' + this.id + '"]:gt(0)').remove();
  856. }
  857. });
  858. init_load_more(box_load_more);
  859. $('#load_more').removeClass('loading');
  860. load_more = false;
  861. });
  862. }
  863. function focus_search() {
  864. $('#search').focus();
  865. }
  866. function init_load_more(box) {
  867. box_load_more = box;
  868. if (!context['does_lazyload']) {
  869. $('img[postpone], audio[postpone], iframe[postpone], video[postpone]').each(function () {
  870. this.removeAttribute('postpone');
  871. });
  872. }
  873. var $next_link = $("#load_more");
  874. if (!$next_link.length) {
  875. // no more article to load
  876. url_load_more = "";
  877. return;
  878. }
  879. url_load_more = $next_link.attr("href");
  880. var $prefetch = $('#prefetch');
  881. if ($prefetch.attr('href') !== url_load_more) {
  882. $prefetch.attr('rel', 'next'); //Remove prefetch
  883. $.ajax({url: url_load_more, ifModified: true }); //TODO: Try to find a less agressive solution
  884. $prefetch.attr('href', url_load_more);
  885. }
  886. $next_link.click(function () {
  887. load_more_posts();
  888. return false;
  889. });
  890. }
  891. //</endless_mode>
  892. //<crypto form (Web login)>
  893. function poormanSalt() { //If crypto.getRandomValues is not available
  894. var text = '$2a$04$',
  895. base = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ.0123456789/abcdefghijklmnopqrstuvwxyz';
  896. for (var i = 22; i > 0; i--) {
  897. text += base.charAt(Math.floor(Math.random() * 64));
  898. }
  899. return text;
  900. }
  901. function init_crypto_form() {
  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_all() {
  1134. if (!window.$) {
  1135. if (window.console) {
  1136. console.log('FreshRSS waiting for JS…');
  1137. }
  1138. window.setTimeout(init_all, 50);
  1139. return;
  1140. }
  1141. parseJsonVars();
  1142. init_notifications();
  1143. init_confirm_action();
  1144. $stream = $('#stream');
  1145. if ($stream.length > 0) {
  1146. init_actualize();
  1147. init_column_categories();
  1148. init_load_more($stream);
  1149. init_posts();
  1150. init_stream($stream);
  1151. init_nav_entries();
  1152. init_shortcuts();
  1153. faviconNbUnread();
  1154. init_print_action();
  1155. init_notifs_html5();
  1156. window.setInterval(refreshUnreads, 120000);
  1157. } else {
  1158. init_subscription();
  1159. init_crypto_form();
  1160. init_share_observers();
  1161. init_remove_observers();
  1162. init_feed_observers();
  1163. init_password_observers();
  1164. init_stats_observers();
  1165. init_slider_observers();
  1166. init_configuration_alert();
  1167. }
  1168. if (window.console) {
  1169. console.log('FreshRSS init done.');
  1170. }
  1171. }
  1172. if (document.readyState && document.readyState !== 'loading') {
  1173. if (window.console) {
  1174. console.log('FreshRSS immediate init…');
  1175. }
  1176. init_all();
  1177. } else if (document.addEventListener) {
  1178. document.addEventListener('DOMContentLoaded', function () {
  1179. if (window.console) {
  1180. console.log('FreshRSS waiting for DOMContentLoaded…');
  1181. }
  1182. init_all();
  1183. }, false);
  1184. }