main.js 27 KB

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