main.js 27 KB

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