main.js 27 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103
  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. if ($(".nav_menu a.read_all").hasClass('confirm')) {
  441. if (confirm(str_confirmation)) {
  442. redirect(url, false);
  443. }
  444. } else {
  445. redirect(url, false);
  446. }
  447. }, {
  448. 'disable_in_input': true
  449. });
  450. shortcut.add(shortcuts.mark_favorite, function () {
  451. // on marque comme favori ou non favori
  452. var active = $(".flux.current");
  453. mark_favorite(active);
  454. }, {
  455. 'disable_in_input': true
  456. });
  457. shortcut.add(shortcuts.collapse_entry, function () {
  458. collapse_entry();
  459. }, {
  460. 'disable_in_input': true
  461. });
  462. shortcut.add(shortcuts.auto_share, function () {
  463. auto_share();
  464. }, {
  465. 'disable_in_input': true
  466. });
  467. for(var i = 1; i < 10; i++){
  468. shortcut.add(i.toString(), function (e) {
  469. auto_share(String.fromCharCode(e.keyCode));
  470. }, {
  471. 'disable_in_input': true
  472. });
  473. }
  474. // Touches de navigation pour les articles
  475. shortcut.add(shortcuts.prev_entry, prev_entry, {
  476. 'disable_in_input': true
  477. });
  478. shortcut.add(shortcuts.first_entry, function () {
  479. var old_active = $(".flux.current"),
  480. first = $(".flux:first");
  481. if (first.hasClass("flux")) {
  482. toggleContent(first, old_active);
  483. }
  484. }, {
  485. 'disable_in_input': true
  486. });
  487. shortcut.add(shortcuts.next_entry, next_entry, {
  488. 'disable_in_input': true
  489. });
  490. shortcut.add(shortcuts.last_entry, function () {
  491. var old_active = $(".flux.current"),
  492. last = $(".flux:last");
  493. if (last.hasClass("flux")) {
  494. toggleContent(last, old_active);
  495. }
  496. }, {
  497. 'disable_in_input': true
  498. });
  499. // Touches de navigation pour les flux
  500. shortcut.add("shift+" + shortcuts.prev_entry, prev_feed, {
  501. 'disable_in_input': true
  502. });
  503. shortcut.add("shift+" + shortcuts.next_entry, next_feed, {
  504. 'disable_in_input': true
  505. });
  506. shortcut.add("shift+" + shortcuts.first_entry, first_feed, {
  507. 'disable_in_input': true
  508. });
  509. shortcut.add("shift+" + shortcuts.last_entry, last_feed, {
  510. 'disable_in_input': true
  511. });
  512. // Touches de navigation pour les categories
  513. shortcut.add("alt+" + shortcuts.prev_entry, prev_category, {
  514. 'disable_in_input': true
  515. });
  516. shortcut.add("alt+" + shortcuts.next_entry, next_category, {
  517. 'disable_in_input': true
  518. });
  519. shortcut.add("alt+" + shortcuts.first_entry, first_category, {
  520. 'disable_in_input': true
  521. });
  522. shortcut.add("alt+" + shortcuts.last_entry, last_category, {
  523. 'disable_in_input': true
  524. });
  525. shortcut.add(shortcuts.go_website, function () {
  526. var url_website = $('.flux.current > .flux_header > .title > a').attr("href");
  527. if (auto_mark_site) {
  528. $(".flux.current").each(function () {
  529. mark_read($(this), true);
  530. });
  531. }
  532. redirect(url_website, true);
  533. }, {
  534. 'disable_in_input': true
  535. });
  536. shortcut.add(shortcuts.load_more, function () {
  537. load_more_posts();
  538. }, {
  539. 'disable_in_input': true
  540. });
  541. shortcut.add(shortcuts.focus_search, function () {
  542. focus_search();
  543. }, {
  544. 'disable_in_input': true
  545. });
  546. }
  547. function init_stream(divStream) {
  548. divStream.on('click', '.flux_header,.flux_content', function (e) { //flux_toggle
  549. if ($(e.target).closest('.content, .item.website, .item.link').length > 0) {
  550. return;
  551. }
  552. var old_active = $(".flux.current"),
  553. new_active = $(this).parent();
  554. isCollapsed = true;
  555. if (e.target.tagName.toUpperCase() === 'A') { //Leave real links alone
  556. if (auto_mark_article) {
  557. mark_read(new_active, true);
  558. }
  559. return true;
  560. }
  561. toggleContent(new_active, old_active);
  562. });
  563. divStream.on('click', '.flux a.read', function () {
  564. var active = $(this).parents(".flux");
  565. mark_read(active, false);
  566. return false;
  567. });
  568. divStream.on('click', '.flux a.bookmark', function () {
  569. var active = $(this).parents(".flux");
  570. mark_favorite(active);
  571. return false;
  572. });
  573. divStream.on('click', '.item.title > a', function (e) {
  574. if (e.ctrlKey) {
  575. return true; //Allow default control-click behaviour such as open in backround-tab
  576. }
  577. $(this).parent().click(); //Will perform toggle flux_content
  578. return false;
  579. });
  580. divStream.on('click', '.flux .content a', function () {
  581. $(this).attr('target', '_blank');
  582. });
  583. if (auto_mark_site) {
  584. divStream.on('click', '.flux .link > a', function () {
  585. mark_read($(this).parent().parent().parent(), true);
  586. });
  587. }
  588. }
  589. function init_nav_entries() {
  590. var $nav_entries = $('#nav_entries');
  591. $nav_entries.find('.previous_entry').click(function () {
  592. prev_entry();
  593. return false;
  594. });
  595. $nav_entries.find('.next_entry').click(function () {
  596. next_entry();
  597. return false;
  598. });
  599. $nav_entries.find('.up').click(function () {
  600. var active_item = $(".flux.current"),
  601. windowTop = $(window).scrollTop(),
  602. item_top = active_item.position().top;
  603. if (windowTop > item_top) {
  604. $("html,body").scrollTop(item_top);
  605. } else {
  606. $("html,body").scrollTop(0);
  607. }
  608. return false;
  609. });
  610. }
  611. function init_actualize() {
  612. var auto = false;
  613. $("#actualize").click(function () {
  614. if (ajax_loading) {
  615. return false;
  616. }
  617. ajax_loading = true;
  618. $.getScript('./?c=javascript&a=actualize').done(function () {
  619. if (auto && feed_count < 1) {
  620. auto = false;
  621. ajax_loading = false;
  622. return false;
  623. }
  624. updateFeeds();
  625. });
  626. return false;
  627. });
  628. if (auto_actualize_feeds) {
  629. auto = true;
  630. $("#actualize").click();
  631. }
  632. }
  633. // <notification>
  634. var notification = null,
  635. notification_interval = null,
  636. notification_working = false;
  637. function openNotification(msg, status) {
  638. if (notification_working === true) {
  639. return false;
  640. }
  641. notification_working = true;
  642. notification.removeClass();
  643. notification.addClass("notification");
  644. notification.addClass(status);
  645. notification.find(".msg").html(msg);
  646. notification.fadeIn(300);
  647. notification_interval = window.setInterval(closeNotification, 4000);
  648. }
  649. function closeNotification() {
  650. notification.fadeOut(600, function() {
  651. notification.removeClass();
  652. notification.addClass('closed');
  653. window.clearInterval(notification_interval);
  654. notification_working = false;
  655. });
  656. }
  657. function init_notifications() {
  658. notification = $("#notification");
  659. notification.find("a.close").click(function () {
  660. closeNotification();
  661. return false;
  662. });
  663. if (notification.find(".msg").html().length > 0) {
  664. notification_working = true;
  665. notification_interval = window.setInterval(closeNotification, 4000);
  666. }
  667. }
  668. // </notification>
  669. function refreshUnreads() {
  670. $.getJSON('./?c=javascript&a=nbUnreadsPerFeed').done(function (data) {
  671. var isAll = $('.category.all > .active').length > 0;
  672. $.each(data, function(feed_id, nbUnreads) {
  673. feed_id = 'f_' + feed_id;
  674. var elem = $('#' + feed_id + '>.feed').get(0),
  675. feed_unreads = elem ? str2int(elem.getAttribute('data-unread')) : 0;
  676. if ((incUnreadsFeed(null, feed_id, nbUnreads - feed_unreads) || isAll) && //Update of current view?
  677. (nbUnreads - feed_unreads > 0)) {
  678. $('#new-article').show();
  679. };
  680. });
  681. });
  682. }
  683. //<endless_mode>
  684. var url_load_more = "",
  685. load_more = false,
  686. box_load_more = null;
  687. function load_more_posts() {
  688. if (load_more || url_load_more === '' || box_load_more === null) {
  689. return;
  690. }
  691. load_more = true;
  692. $('#load_more').addClass('loading');
  693. $.get(url_load_more, function (data) {
  694. box_load_more.children('.flux:last').after($('#stream', data).children('.flux, .day'));
  695. $('.pagination').replaceWith($('.pagination', data));
  696. if (display_order === 'ASC') {
  697. $('#nav_menu_read_all>a').attr('href', $('#bigMarkAsRead').attr('href'));
  698. } else {
  699. $('#bigMarkAsRead').attr('href', $('#nav_menu_read_all>a').attr('href'));
  700. }
  701. $('[id^=day_]').each(function (i) {
  702. var ids = $('[id="' + this.id + '"]');
  703. if (ids.length > 1) {
  704. $('[id="' + this.id + '"]:gt(0)').remove();
  705. }
  706. });
  707. init_load_more(box_load_more);
  708. init_lazyload();
  709. $('#load_more').removeClass('loading');
  710. load_more = false;
  711. });
  712. }
  713. function focus_search() {
  714. $('#search').focus();
  715. }
  716. function init_load_more(box) {
  717. box_load_more = box;
  718. var $next_link = $("#load_more");
  719. if (!$next_link.length) {
  720. // no more article to load
  721. url_load_more = "";
  722. return;
  723. }
  724. url_load_more = $next_link.attr("href");
  725. var $prefetch = $('#prefetch');
  726. if ($prefetch.attr('href') !== url_load_more) {
  727. $prefetch.attr('rel', 'next'); //Remove prefetch
  728. $.ajax({url: url_load_more, ifModified: true }); //TODO: Try to find a less agressive solution
  729. $prefetch.attr('href', url_load_more);
  730. }
  731. $next_link.click(function () {
  732. load_more_posts();
  733. return false;
  734. });
  735. }
  736. //</endless_mode>
  737. //<Web login form>
  738. function poormanSalt() { //If crypto.getRandomValues is not available
  739. var text = '$2a$04$',
  740. base = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ.0123456789/abcdefghijklmnopqrstuvwxyz';
  741. for (var i = 22; i > 0; i--) {
  742. text += base.charAt(Math.floor(Math.random() * 64));
  743. }
  744. return text;
  745. }
  746. function init_loginForm() {
  747. var $loginForm = $('#loginForm');
  748. if ($loginForm.length === 0) {
  749. return;
  750. }
  751. if (!(window.dcodeIO)) {
  752. if (window.console) {
  753. console.log('FreshRSS waiting for bcrypt.js…');
  754. }
  755. window.setTimeout(init_loginForm, 100);
  756. return;
  757. }
  758. $loginForm.on('submit', function() {
  759. $('#loginButton').attr('disabled', '');
  760. var success = false;
  761. $.ajax({
  762. url: './?c=javascript&a=nonce&user=' + $('#username').val(),
  763. dataType: 'json',
  764. async: false
  765. }).done(function (data) {
  766. if (data.salt1 == '' || data.nonce == '') {
  767. alert('Invalid user!');
  768. } else {
  769. try {
  770. var strong = window.Uint32Array && window.crypto && (typeof window.crypto.getRandomValues === 'function'),
  771. s = dcodeIO.bcrypt.hashSync($('#passwordPlain').val(), data.salt1),
  772. c = dcodeIO.bcrypt.hashSync(data.nonce + s, strong ? 4 : poormanSalt());
  773. $('#challenge').val(c);
  774. if (s == '' || c == '') {
  775. alert('Crypto error!');
  776. } else {
  777. success = true;
  778. }
  779. } catch (e) {
  780. alert('Crypto exception! ' + e);
  781. }
  782. }
  783. }).fail(function() {
  784. alert('Communication error!');
  785. });
  786. $('#loginButton').removeAttr('disabled');
  787. return success;
  788. });
  789. }
  790. //</Web login form>
  791. //<persona>
  792. function init_persona() {
  793. if (!(navigator.id)) {
  794. if (window.console) {
  795. console.log('FreshRSS waiting for Persona…');
  796. }
  797. window.setTimeout(init_persona, 100);
  798. return;
  799. }
  800. $('a.signin').click(function() {
  801. navigator.id.request();
  802. return false;
  803. });
  804. $('a.signout').click(function() {
  805. navigator.id.logout();
  806. return false;
  807. });
  808. navigator.id.watch({
  809. loggedInUser: current_user_mail,
  810. onlogin: function(assertion) {
  811. // A user has logged in! Here you need to:
  812. // 1. Send the assertion to your backend for verification and to create a session.
  813. // 2. Update your UI.
  814. $.ajax ({
  815. type: 'POST',
  816. url: url_login,
  817. data: {assertion: assertion},
  818. success: function(res, status, xhr) {
  819. /*if (res.status === 'failure') {
  820. alert (res_obj.reason);
  821. } else*/ if (res.status === 'okay') {
  822. location.href = url_freshrss;
  823. }
  824. },
  825. error: function(res, status, xhr) {
  826. alert("Login failure: " + res);
  827. }
  828. });
  829. },
  830. onlogout: function() {
  831. // A user has logged out! Here you need to:
  832. // Tear down the user's session by redirecting the user or making a call to your backend.
  833. // Also, make sure loggedInUser will get set to null on the next page load.
  834. // (That's a literal JavaScript null. Not false, 0, or undefined. null.)
  835. $.ajax ({
  836. type: 'POST',
  837. url: url_logout,
  838. success: function(res, status, xhr) {
  839. location.href = url_freshrss;
  840. },
  841. error: function(res, status, xhr) {
  842. //alert("logout failure" + res);
  843. }
  844. });
  845. }
  846. });
  847. }
  848. //</persona>
  849. function init_confirm_action() {
  850. $('.confirm').click(function () {
  851. return confirm(str_confirmation);
  852. });
  853. }
  854. function init_print_action() {
  855. $('.item.share > a[href="#"]').click(function () {
  856. var content = "<html><head><style>"
  857. + "body { font-family: Serif; text-align: justify; }"
  858. + "a { color: #000; text-decoration: none; }"
  859. + "a:after { content: ' [' attr(href) ']'}"
  860. + "</style></head><body>"
  861. + $(".flux.current .content").html()
  862. + "</body></html>";
  863. var tmp_window = window.open();
  864. tmp_window.document.writeln(content);
  865. tmp_window.document.close();
  866. tmp_window.focus();
  867. tmp_window.print();
  868. tmp_window.close();
  869. return false;
  870. });
  871. }
  872. function init_share_observers() {
  873. shares = $('.form-group:not(".form-actions")').length;
  874. $('.share.add').on('click', function(e) {
  875. var opt = $(this).siblings('select').find(':selected');
  876. var row = $(this).parents('form').data(opt.data('form'));
  877. row = row.replace('##label##', opt.html(), 'g');
  878. row = row.replace('##type##', opt.val(), 'g');
  879. row = row.replace('##help##', opt.data('help'), 'g');
  880. row = row.replace('##key##', shares, 'g');
  881. $(this).parents('.form-group').before(row);
  882. shares++;
  883. return false;
  884. });
  885. }
  886. function init_remove_observers() {
  887. $('.post').on('click', 'a.remove', function(e) {
  888. var remove_what = $(this).attr('data-remove');
  889. if (remove_what !== undefined) {
  890. var remove_obj = $('#' + remove_what);
  891. remove_obj.remove();
  892. }
  893. return false;
  894. });
  895. }
  896. function init_feed_observers() {
  897. $('select[id="category"]').on('change', function() {
  898. var detail = $('#new_category_name').parent();
  899. if ($(this).val() === 'nc') {
  900. detail.show();
  901. detail.find('input').focus();
  902. } else {
  903. detail.hide();
  904. }
  905. });
  906. }
  907. function init_password_observers() {
  908. $('input[type="password"] + a.btn.toggle-password').on('click', function(e) {
  909. var button = $(this);
  910. var passwordField = $(this).siblings('input[type="password"]');
  911. passwordField.attr('type', 'text');
  912. button.addClass('active');
  913. setTimeout(function() {
  914. passwordField.attr('type', 'password');
  915. button.removeClass('active');
  916. }, 2000);
  917. return false;
  918. });
  919. }
  920. function init_all() {
  921. if (!(window.$ && window.url_freshrss && ((!full_lazyload) || $.fn.lazyload))) {
  922. if (window.console) {
  923. console.log('FreshRSS waiting for JS…');
  924. }
  925. window.setTimeout(init_all, 50);
  926. return;
  927. }
  928. init_notifications();
  929. switch (authType) {
  930. case 'form':
  931. init_loginForm();
  932. break;
  933. case 'persona':
  934. init_persona();
  935. break;
  936. }
  937. init_confirm_action();
  938. $stream = $('#stream');
  939. if ($stream.length > 0) {
  940. init_actualize();
  941. init_column_categories();
  942. init_load_more($stream);
  943. init_posts();
  944. init_stream($stream);
  945. init_nav_entries();
  946. init_shortcuts();
  947. init_print_action();
  948. window.setInterval(refreshUnreads, 120000);
  949. } else {
  950. init_share_observers();
  951. init_remove_observers();
  952. init_feed_observers();
  953. init_password_observers();
  954. }
  955. if (window.console) {
  956. console.log('FreshRSS init done.');
  957. }
  958. }
  959. if (document.readyState && document.readyState !== 'loading') {
  960. if (window.console) {
  961. console.log('FreshRSS immediate init…');
  962. }
  963. init_all();
  964. } else if (document.addEventListener) {
  965. document.addEventListener('DOMContentLoaded', function () {
  966. if (window.console) {
  967. console.log('FreshRSS waiting for DOMContentLoaded…');
  968. }
  969. init_all();
  970. }, false);
  971. }