main.js 32 KB

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