main.js 31 KB

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