main.js 32 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323
  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 + '>.feed').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', numberFormat(feed_unreads + nb));
  58. elem = $(elem).closest('li').get(0);
  59. if (elem) {
  60. elem.setAttribute('data-unread', feed_unreads + nb);
  61. }
  62. }
  63. //Update unread: category
  64. elem = $('#' + feed_id).parent().prevAll('.category').children(':first').get(0);
  65. feed_unreads = elem ? str2int(elem.getAttribute('data-unread')) : 0;
  66. if (elem) {
  67. elem.setAttribute('data-unread', numberFormat(feed_unreads + nb));
  68. elem = $(elem).closest('li').get(0);
  69. if (elem) {
  70. elem.setAttribute('data-unread', feed_unreads + nb);
  71. }
  72. }
  73. //Update unread: all
  74. if (feed_priority > 0) {
  75. elem = $('#aside_flux .all').children(':first').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_flux .favorites').children(':first').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 = $('.favorites>a').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_flux .favorites').children(':first').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_flux .feeds li.active");
  255. if (active_feed.length > 0) {
  256. active_feed.prevAll(':visible:first').find('a.feed').each(function(){this.click();});
  257. } else {
  258. last_feed();
  259. }
  260. }
  261. function next_feed() {
  262. var active_feed = $("#aside_flux .feeds li.active");
  263. if (active_feed.length > 0) {
  264. active_feed.nextAll(':visible:first').find('a.feed').each(function(){this.click();});
  265. } else {
  266. first_feed();
  267. }
  268. }
  269. function first_feed() {
  270. var feed = $("#aside_flux .feeds.active li:visible:first");
  271. if (feed.length > 0) {
  272. feed.find('a')[1].click();
  273. }
  274. }
  275. function last_feed() {
  276. var feed = $("#aside_flux .feeds.active li:visible:last");
  277. if (feed.length > 0) {
  278. feed.find('a')[1].click();
  279. }
  280. }
  281. function prev_category() {
  282. var active_cat = $("#aside_flux .category.stick.active");
  283. if (active_cat.length > 0) {
  284. var prev_cat = active_cat.parent('li').prevAll(':visible:first').find('.category.stick a.btn');
  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_flux .category.stick.active");
  295. if (active_cat.length > 0) {
  296. var next_cat = active_cat.parent('li').nextAll(':visible:first').find('.category.stick a.btn');
  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_flux .category.stick:visible:first");
  307. if (cat.length > 0) {
  308. cat.find('a.btn')[0].click();
  309. }
  310. }
  311. function last_category() {
  312. var cat = $("#aside_flux .category.stick:visible:last");
  313. if (cat.length > 0) {
  314. cat.find('a.btn')[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_flux').on('click', '.category>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(".feeds").slideToggle();
  441. return false;
  442. });
  443. $('#aside_flux').on('click', '.feeds .dropdown-toggle', function () {
  444. if ($(this).nextAll('.dropdown-menu').length === 0) {
  445. var feed_id = $(this).closest('li').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. //<persona>
  904. function init_persona() {
  905. if (!(navigator.id)) {
  906. if (window.console) {
  907. console.log('FreshRSS waiting for Persona…');
  908. }
  909. window.setTimeout(init_persona, 100);
  910. return;
  911. }
  912. $('a.signin').click(function() {
  913. navigator.id.request();
  914. return false;
  915. });
  916. $('a.signout').click(function() {
  917. navigator.id.logout();
  918. return false;
  919. });
  920. navigator.id.watch({
  921. loggedInUser: context['current_user_mail'],
  922. onlogin: function(assertion) {
  923. // A user has logged in! Here you need to:
  924. // 1. Send the assertion to your backend for verification and to create a session.
  925. // 2. Update your UI.
  926. $.ajax ({
  927. type: 'POST',
  928. url: url['login'],
  929. data: {assertion: assertion},
  930. success: function(res, status, xhr) {
  931. /*if (res.status === 'failure') {
  932. alert (res_obj.reason);
  933. } else*/ if (res.status === 'okay') {
  934. location.href = url['index'];
  935. }
  936. },
  937. error: function(res, status, xhr) {
  938. alert("Login failure: " + res);
  939. }
  940. });
  941. },
  942. onlogout: function() {
  943. // A user has logged out! Here you need to:
  944. // Tear down the user's session by redirecting the user or making a call to your backend.
  945. // Also, make sure loggedInUser will get set to null on the next page load.
  946. // (That's a literal JavaScript null. Not false, 0, or undefined. null.)
  947. $.ajax ({
  948. type: 'POST',
  949. url: url['logout'],
  950. success: function(res, status, xhr) {
  951. location.href = url['index'];
  952. },
  953. error: function(res, status, xhr) {
  954. //alert("logout failure" + res);
  955. }
  956. });
  957. }
  958. });
  959. }
  960. //</persona>
  961. function init_confirm_action() {
  962. $('body').on('click', '.confirm', function () {
  963. var str_confirmation = $(this).attr('data-str-confirm');
  964. if (!str_confirmation) {
  965. str_confirmation = i18n['confirmation_default'];
  966. }
  967. return confirm(str_confirmation);
  968. });
  969. }
  970. function init_print_action() {
  971. $('.item.share > a[href="#"]').click(function () {
  972. var content = "<html><head><style>"
  973. + "body { font-family: Serif; text-align: justify; }"
  974. + "a { color: #000; text-decoration: none; }"
  975. + "a:after { content: ' [' attr(href) ']'}"
  976. + "</style></head><body>"
  977. + $(".flux.current .content").html()
  978. + "</body></html>";
  979. var tmp_window = window.open();
  980. tmp_window.document.writeln(content);
  981. tmp_window.document.close();
  982. tmp_window.focus();
  983. tmp_window.print();
  984. tmp_window.close();
  985. return false;
  986. });
  987. }
  988. function init_share_observers() {
  989. shares = $('.form-group:not(".form-actions")').length;
  990. $('.share.add').on('click', function(e) {
  991. var opt = $(this).siblings('select').find(':selected');
  992. var row = $(this).parents('form').data(opt.data('form'));
  993. row = row.replace('##label##', opt.html(), 'g');
  994. row = row.replace('##type##', opt.val(), 'g');
  995. row = row.replace('##help##', opt.data('help'), 'g');
  996. row = row.replace('##key##', shares, 'g');
  997. $(this).parents('.form-group').before(row);
  998. shares++;
  999. return false;
  1000. });
  1001. }
  1002. function init_stats_observers() {
  1003. $('#feed_select').on('change', function(e) {
  1004. redirect($(this).find(':selected').data('url'));
  1005. });
  1006. }
  1007. function init_remove_observers() {
  1008. $('.post').on('click', 'a.remove', function(e) {
  1009. var remove_what = $(this).attr('data-remove');
  1010. if (remove_what !== undefined) {
  1011. var remove_obj = $('#' + remove_what);
  1012. remove_obj.remove();
  1013. }
  1014. return false;
  1015. });
  1016. }
  1017. function init_feed_observers() {
  1018. $('select[id="category"]').on('change', function() {
  1019. var detail = $('#new_category_name').parent();
  1020. if ($(this).val() === 'nc') {
  1021. detail.show();
  1022. detail.find('input').focus();
  1023. } else {
  1024. detail.hide();
  1025. }
  1026. });
  1027. }
  1028. function init_password_observers() {
  1029. $('input[type="password"] + a.btn.toggle-password').on('click', function(e) {
  1030. var button = $(this);
  1031. var passwordField = $(this).siblings('input[type="password"]');
  1032. passwordField.attr('type', 'text');
  1033. button.addClass('active');
  1034. setTimeout(function() {
  1035. passwordField.attr('type', 'password');
  1036. button.removeClass('active');
  1037. }, 2000);
  1038. return false;
  1039. });
  1040. }
  1041. function faviconNbUnread(n) {
  1042. if (typeof n === 'undefined') {
  1043. n = str2int($('.category.all>a').attr('data-unread'));
  1044. }
  1045. //http://remysharp.com/2010/08/24/dynamic-favicons/
  1046. var canvas = document.createElement('canvas'),
  1047. link = document.getElementById('favicon').cloneNode(true);
  1048. if (canvas.getContext && link) {
  1049. canvas.height = canvas.width = 16;
  1050. var img = document.createElement('img');
  1051. img.onload = function () {
  1052. var ctx = canvas.getContext('2d');
  1053. ctx.drawImage(this, 0, 0, canvas.width, canvas.height);
  1054. if (n > 0) {
  1055. var text = '';
  1056. if (n < 1000) {
  1057. text = n;
  1058. } else if (n < 100000) {
  1059. text = Math.floor(n / 1000) + 'k';
  1060. } else {
  1061. text = 'E' + Math.floor(Math.log10(n));
  1062. }
  1063. ctx.font = 'bold 9px "Arial", sans-serif';
  1064. ctx.fillStyle = 'rgba(255, 255, 255, 0.8)';
  1065. ctx.fillRect(0, 7, ctx.measureText(text).width, 9);
  1066. ctx.fillStyle = '#F00';
  1067. ctx.fillText(text, 0, canvas.height - 1);
  1068. }
  1069. link.href = canvas.toDataURL('image/png');
  1070. $('link[rel~=icon]').remove();
  1071. document.head.appendChild(link);
  1072. };
  1073. img.src = '../favicon.ico';
  1074. }
  1075. }
  1076. function init_slider_observers() {
  1077. var slider = $('#slider'),
  1078. closer = $('#close-slider');
  1079. if (slider.length < 1) {
  1080. return;
  1081. }
  1082. $('.post').on('click', '.open-slider', function() {
  1083. if (ajax_loading) {
  1084. return false;
  1085. }
  1086. ajax_loading = true;
  1087. var url_slide = $(this).attr('href');
  1088. $.ajax({
  1089. type: 'GET',
  1090. url: url_slide,
  1091. data : { ajax: true }
  1092. }).done(function (data) {
  1093. slider.html(data);
  1094. closer.addClass('active');
  1095. slider.addClass('active');
  1096. ajax_loading = false;
  1097. });
  1098. return false;
  1099. });
  1100. closer.on('click', function() {
  1101. closer.removeClass('active');
  1102. slider.removeClass('active');
  1103. return false;
  1104. });
  1105. }
  1106. function init_all() {
  1107. if (!(window.$ && window.context)) {
  1108. if (window.console) {
  1109. console.log('FreshRSS waiting for JS…');
  1110. }
  1111. window.setTimeout(init_all, 50);
  1112. return;
  1113. }
  1114. init_notifications();
  1115. switch (context['auth_type']) {
  1116. case 'persona':
  1117. init_persona();
  1118. break;
  1119. }
  1120. init_confirm_action();
  1121. $stream = $('#stream');
  1122. if ($stream.length > 0) {
  1123. init_actualize();
  1124. init_column_categories();
  1125. init_load_more($stream);
  1126. init_posts();
  1127. init_stream($stream);
  1128. init_nav_entries();
  1129. init_shortcuts();
  1130. faviconNbUnread();
  1131. init_print_action();
  1132. init_notifs_html5();
  1133. window.setInterval(refreshUnreads, 120000);
  1134. } else {
  1135. init_crypto_form();
  1136. init_share_observers();
  1137. init_remove_observers();
  1138. init_feed_observers();
  1139. init_password_observers();
  1140. init_stats_observers();
  1141. init_slider_observers();
  1142. }
  1143. if (window.console) {
  1144. console.log('FreshRSS init done.');
  1145. }
  1146. }
  1147. if (document.readyState && document.readyState !== 'loading') {
  1148. if (window.console) {
  1149. console.log('FreshRSS immediate init…');
  1150. }
  1151. init_all();
  1152. } else if (document.addEventListener) {
  1153. document.addEventListener('DOMContentLoaded', function () {
  1154. if (window.console) {
  1155. console.log('FreshRSS waiting for DOMContentLoaded…');
  1156. }
  1157. init_all();
  1158. }, false);
  1159. }