main.js 26 KB

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