main.js 25 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040
  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. old_active.removeClass("active");
  181. if (new_active.length === 0) {
  182. return;
  183. }
  184. old_active.removeClass("current");
  185. if (does_lazyload) {
  186. new_active.find('img[data-original], iframe[data-original]').each(function () {
  187. this.setAttribute('src', this.getAttribute('data-original'));
  188. this.removeAttribute('data-original');
  189. });
  190. }
  191. if (old_active[0] !== new_active[0]) {
  192. if (isCollapsed) {
  193. new_active.addClass("active");
  194. }
  195. new_active.addClass("current");
  196. }
  197. var box_to_move = "html,body",
  198. relative_move = false;
  199. if (is_global_mode()) {
  200. box_to_move = "#panel";
  201. relative_move = true;
  202. }
  203. var new_pos = new_active.position().top,
  204. old_scroll = $(box_to_move).scrollTop();
  205. if (hide_posts) {
  206. new_pos = new_active.position().top;
  207. old_scroll = $(box_to_move).scrollTop();
  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. if (auto_mark_article) {
  223. mark_read(new_active, true);
  224. }
  225. }
  226. function prev_entry() {
  227. var old_active = $(".flux.current"),
  228. new_active = old_active.length === 0 ? $(".flux:last") : old_active.prevAll(".flux:first");
  229. toggleContent(new_active, old_active);
  230. }
  231. function next_entry() {
  232. var old_active = $(".flux.current"),
  233. new_active = old_active.length === 0 ? $(".flux:first") : old_active.nextAll(".flux:first");
  234. toggleContent(new_active, old_active);
  235. if (!auto_load_more) {
  236. var last_active = $(".flux:last");
  237. if (last_active.attr("id") === new_active.attr("id")) {
  238. load_more_posts();
  239. }
  240. }
  241. }
  242. function prev_feed() {
  243. var active_feed = $("#aside_flux .feeds li.active");
  244. if (active_feed.length > 0) {
  245. active_feed.prev().find('a.feed').each(function(){this.click();});
  246. } else {
  247. last_feed();
  248. }
  249. }
  250. function next_feed() {
  251. var active_feed = $("#aside_flux .feeds li.active");
  252. if (active_feed.length > 0) {
  253. active_feed.next().find('a.feed').each(function(){this.click();});
  254. } else {
  255. first_feed();
  256. }
  257. }
  258. function first_feed() {
  259. var feed = $("#aside_flux .feeds.active li:first");
  260. if (feed.length > 0) {
  261. feed.find('a')[1].click();
  262. }
  263. }
  264. function last_feed() {
  265. var feed = $("#aside_flux .feeds.active li:last");
  266. if (feed.length > 0) {
  267. feed.find('a')[1].click();
  268. }
  269. }
  270. function prev_category() {
  271. var active_cat = $("#aside_flux .category.stick.active");
  272. if (active_cat.length > 0) {
  273. var prev_cat = active_cat.parent('li').prev().find('.category.stick a.btn');
  274. if (prev_cat.length > 0) {
  275. prev_cat[0].click();
  276. }
  277. } else {
  278. last_category();
  279. }
  280. return;
  281. }
  282. function next_category() {
  283. var active_cat = $("#aside_flux .category.stick.active");
  284. if (active_cat.length > 0) {
  285. var next_cat = active_cat.parent('li').next().find('.category.stick a.btn');
  286. if (next_cat.length > 0) {
  287. next_cat[0].click();
  288. }
  289. } else {
  290. first_category();
  291. }
  292. return;
  293. }
  294. function first_category() {
  295. var cat = $("#aside_flux .category.stick:first");
  296. if (cat.length > 0) {
  297. cat.find('a.btn')[0].click();
  298. }
  299. }
  300. function last_category() {
  301. var cat = $("#aside_flux .category.stick:last");
  302. if (cat.length > 0) {
  303. cat.find('a.btn')[0].click();
  304. }
  305. }
  306. function collapse_entry() {
  307. isCollapsed = !isCollapsed;
  308. $(".flux.current").toggleClass("active");
  309. }
  310. function auto_share(key) {
  311. var share = $(".flux.current.active").find('.dropdown-target[id^="dropdown-share"]');
  312. var shares = share.siblings('.dropdown-menu').find('.item a');
  313. if (typeof key === "undefined") {
  314. if (!share.length) {
  315. return;
  316. }
  317. // Display the share div
  318. window.location.hash = share.attr('id');
  319. // Force scrolling to the share div
  320. var scroll = needsScroll(share.closest('.bottom'));
  321. if (scroll !== 0) {
  322. $('html,body').scrollTop(scroll);
  323. }
  324. // Force the key value if there is only one action, so we can trigger it automatically
  325. if (shares.length === 1) {
  326. key = 1;
  327. } else {
  328. return;
  329. }
  330. }
  331. // Trigger selected share action and hide the share div
  332. key = parseInt(key);
  333. if (key <= shares.length) {
  334. shares[key - 1].click();
  335. share.siblings('.dropdown-menu').find('.dropdown-close a')[0].click();
  336. }
  337. }
  338. function inMarkViewport(flux, box_to_follow, relative_follow) {
  339. var top = flux.position().top;
  340. if (relative_follow) {
  341. top += box_to_follow.scrollTop();
  342. }
  343. var height = flux.height(),
  344. begin = top + 3 * height / 4,
  345. bot = Math.min(begin + 75, top + height),
  346. windowTop = box_to_follow.scrollTop(),
  347. windowBot = windowTop + box_to_follow.height() / 2;
  348. return (windowBot >= begin && bot >= windowBot);
  349. }
  350. function init_lazyload() {
  351. if ($.fn.lazyload) {
  352. if (is_global_mode()) {
  353. $(".flux_content img").lazyload({
  354. container: $("#panel")
  355. });
  356. } else {
  357. $(".flux_content img").lazyload();
  358. }
  359. }
  360. }
  361. function init_posts() {
  362. init_lazyload();
  363. var box_to_follow = $(window),
  364. relative_follow = false;
  365. if (is_global_mode()) {
  366. box_to_follow = $("#panel");
  367. relative_follow = true;
  368. }
  369. if (auto_mark_scroll) {
  370. box_to_follow.scroll(function () {
  371. $('.not_read:visible').each(function () {
  372. if ($(this).children(".flux_content").is(':visible') && inMarkViewport($(this), box_to_follow, relative_follow)) {
  373. mark_read($(this), true);
  374. }
  375. });
  376. });
  377. }
  378. if (auto_load_more) {
  379. box_to_follow.scroll(function () {
  380. var load_more = $("#load_more");
  381. if (!load_more.is(':visible')) {
  382. return;
  383. }
  384. var boxBot = box_to_follow.scrollTop() + box_to_follow.height(),
  385. load_more_top = load_more.position().top;
  386. if (relative_follow) {
  387. load_more_top += box_to_follow.scrollTop();
  388. }
  389. if (boxBot >= load_more_top) {
  390. load_more_posts();
  391. }
  392. });
  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 .link 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. }
  536. function init_stream(divStream) {
  537. divStream.on('click', '.flux_header', function (e) { //flux_header_toggle
  538. if ($(e.target).closest('.item.website, .item.link').length > 0) {
  539. return;
  540. }
  541. var old_active = $(".flux.current"),
  542. new_active = $(this).parent();
  543. isCollapsed = true;
  544. if (e.target.tagName.toUpperCase() === 'A') { //Leave real links alone
  545. if (auto_mark_article) {
  546. mark_read(new_active, true);
  547. }
  548. return true;
  549. }
  550. toggleContent(new_active, old_active);
  551. });
  552. divStream.on('click', '.flux a.read', function () {
  553. var active = $(this).parents(".flux");
  554. mark_read(active, false);
  555. return false;
  556. });
  557. divStream.on('click', '.flux a.bookmark', function () {
  558. var active = $(this).parents(".flux");
  559. mark_favorite(active);
  560. return false;
  561. });
  562. divStream.on('click', '.item.title > a', function (e) {
  563. if (e.ctrlKey) {
  564. return true; //Allow default control-click behaviour such as open in backround-tab
  565. }
  566. $(this).parent().click(); //Will perform toggle flux_content
  567. return false;
  568. });
  569. divStream.on('click', '.flux .content a', function () {
  570. $(this).attr('target', '_blank');
  571. });
  572. if (auto_mark_site) {
  573. divStream.on('click', '.flux .link > a', function () {
  574. mark_read($(this).parent().parent().parent(), true);
  575. });
  576. }
  577. }
  578. function init_nav_entries() {
  579. var $nav_entries = $('#nav_entries');
  580. $nav_entries.find('.previous_entry').click(function () {
  581. prev_entry();
  582. return false;
  583. });
  584. $nav_entries.find('.next_entry').click(function () {
  585. next_entry();
  586. return false;
  587. });
  588. $nav_entries.find('.up').click(function () {
  589. var active_item = $(".flux.current"),
  590. windowTop = $(window).scrollTop(),
  591. item_top = active_item.position().top;
  592. if (windowTop > item_top) {
  593. $("html,body").scrollTop(item_top);
  594. } else {
  595. $("html,body").scrollTop(0);
  596. }
  597. return false;
  598. });
  599. }
  600. function init_actualize() {
  601. var auto = false;
  602. $("#actualize").click(function () {
  603. $.getScript('./?c=javascript&a=actualize').done(function () {
  604. if (auto && feed_count < 1) {
  605. auto = false;
  606. return;
  607. }
  608. updateFeeds();
  609. });
  610. return false;
  611. });
  612. if (auto_actualize_feeds) {
  613. auto = true;
  614. $("#actualize").click();
  615. }
  616. }
  617. // <notification>
  618. var notification = null,
  619. notification_interval = null,
  620. notification_working = false;
  621. function openNotification(msg, status) {
  622. if (notification_working === true) {
  623. return false;
  624. }
  625. notification_working = true;
  626. notification.removeClass();
  627. notification.addClass(status);
  628. notification.find(".msg").html(msg);
  629. notification.fadeIn(300);
  630. notification_interval = window.setInterval(closeNotification, 4000);
  631. }
  632. function closeNotification() {
  633. notification.fadeOut(600, function() {
  634. notification.removeClass();
  635. notification.addClass('closed');
  636. window.clearInterval(notification_interval);
  637. notification_working = false;
  638. });
  639. }
  640. function init_notifications() {
  641. notification = $("#notification");
  642. notification.find("a.close").click(function () {
  643. closeNotification();
  644. return false;
  645. });
  646. if (notification.find(".msg").html().length > 0) {
  647. notification_working = true;
  648. notification_interval = window.setInterval(closeNotification, 4000);
  649. }
  650. }
  651. // </notification>
  652. function refreshUnreads() {
  653. $.getJSON('./?c=javascript&a=nbUnreadsPerFeed').done(function (data) {
  654. var isAll = $('.category.all > .active').length > 0;
  655. $.each(data, function(feed_id, nbUnreads) {
  656. feed_id = 'f_' + feed_id;
  657. var elem = $('#' + feed_id + '>.feed').get(0),
  658. feed_unreads = elem ? str2int(elem.getAttribute('data-unread')) : 0;
  659. if ((incUnreadsFeed(null, feed_id, nbUnreads - feed_unreads) || isAll) && //Update of current view?
  660. (nbUnreads - feed_unreads > 0)) {
  661. $('#new-article').show();
  662. };
  663. });
  664. });
  665. }
  666. //<endless_mode>
  667. var url_load_more = "",
  668. load_more = false,
  669. box_load_more = null;
  670. function load_more_posts() {
  671. if (load_more || url_load_more === '' || box_load_more === null) {
  672. return;
  673. }
  674. load_more = true;
  675. $('#load_more').addClass('loading');
  676. $.get(url_load_more, function (data) {
  677. box_load_more.children('.flux:last').after($('#stream', data).children('.flux, .day'));
  678. $('.pagination').replaceWith($('.pagination', data));
  679. if (display_order === 'ASC') {
  680. $('#nav_menu_read_all>a').attr('href', $('#bigMarkAsRead').attr('href'));
  681. } else {
  682. $('#bigMarkAsRead').attr('href', $('#nav_menu_read_all>a').attr('href'));
  683. }
  684. $('[id^=day_]').each(function (i) {
  685. var ids = $('[id="' + this.id + '"]');
  686. if (ids.length > 1) {
  687. $('[id="' + this.id + '"]:gt(0)').remove();
  688. }
  689. });
  690. init_load_more(box_load_more);
  691. init_lazyload();
  692. $('#load_more').removeClass('loading');
  693. load_more = false;
  694. });
  695. }
  696. function init_load_more(box) {
  697. box_load_more = box;
  698. var $next_link = $("#load_more");
  699. if (!$next_link.length) {
  700. // no more article to load
  701. url_load_more = "";
  702. return;
  703. }
  704. url_load_more = $next_link.attr("href");
  705. var $prefetch = $('#prefetch');
  706. if ($prefetch.attr('href') !== url_load_more) {
  707. $prefetch.attr('rel', 'next'); //Remove prefetch
  708. $.ajax({url: url_load_more, ifModified: true }); //TODO: Try to find a less agressive solution
  709. $prefetch.attr('href', url_load_more);
  710. }
  711. $next_link.click(function () {
  712. load_more_posts();
  713. return false;
  714. });
  715. }
  716. //</endless_mode>
  717. //<Web login form>
  718. function poormanSalt() { //If crypto.getRandomValues is not available
  719. var text = '$2a$04$',
  720. base = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ.0123456789/abcdefghijklmnopqrstuvwxyz';
  721. for (var i = 22; i > 0; i--) {
  722. text += base.charAt(Math.floor(Math.random() * 64));
  723. }
  724. return text;
  725. }
  726. function init_loginForm() {
  727. var $loginForm = $('#loginForm');
  728. if ($loginForm.length === 0) {
  729. return;
  730. }
  731. if (!(window.dcodeIO)) {
  732. if (window.console) {
  733. console.log('FreshRSS waiting for bcrypt.js…');
  734. }
  735. window.setTimeout(init_loginForm, 100);
  736. return;
  737. }
  738. $loginForm.on('submit', function() {
  739. $('#loginButton').attr('disabled', '');
  740. var success = false;
  741. $.ajax({
  742. url: './?c=javascript&a=nonce&user=' + $('#username').val(),
  743. dataType: 'json',
  744. async: false
  745. }).done(function (data) {
  746. if (data.salt1 == '' || data.nonce == '') {
  747. alert('Invalid user!');
  748. } else {
  749. try {
  750. var strong = window.Uint32Array && window.crypto && (typeof window.crypto.getRandomValues === 'function'),
  751. s = dcodeIO.bcrypt.hashSync($('#passwordPlain').val(), data.salt1),
  752. c = dcodeIO.bcrypt.hashSync(data.nonce + s, strong ? 4 : poormanSalt());
  753. $('#challenge').val(c);
  754. if (s == '' || c == '') {
  755. alert('Crypto error!');
  756. } else {
  757. success = true;
  758. }
  759. } catch (e) {
  760. alert('Crypto exception! ' + e);
  761. }
  762. }
  763. }).fail(function() {
  764. alert('Communication error!');
  765. });
  766. $('#loginButton').removeAttr('disabled');
  767. return success;
  768. });
  769. }
  770. //</Web login form>
  771. //<persona>
  772. function init_persona() {
  773. if (!(navigator.id)) {
  774. if (window.console) {
  775. console.log('FreshRSS waiting for Persona…');
  776. }
  777. window.setTimeout(init_persona, 100);
  778. return;
  779. }
  780. $('a.signin').click(function() {
  781. navigator.id.request();
  782. return false;
  783. });
  784. $('a.signout').click(function() {
  785. navigator.id.logout();
  786. return false;
  787. });
  788. navigator.id.watch({
  789. loggedInUser: current_user_mail,
  790. onlogin: function(assertion) {
  791. // A user has logged in! Here you need to:
  792. // 1. Send the assertion to your backend for verification and to create a session.
  793. // 2. Update your UI.
  794. $.ajax ({
  795. type: 'POST',
  796. url: url_login,
  797. data: {assertion: assertion},
  798. success: function(res, status, xhr) {
  799. /*if (res.status === 'failure') {
  800. alert (res_obj.reason);
  801. } else*/ if (res.status === 'okay') {
  802. location.href = url_freshrss;
  803. }
  804. },
  805. error: function(res, status, xhr) {
  806. alert("Login failure: " + res);
  807. }
  808. });
  809. },
  810. onlogout: function() {
  811. // A user has logged out! Here you need to:
  812. // Tear down the user's session by redirecting the user or making a call to your backend.
  813. // Also, make sure loggedInUser will get set to null on the next page load.
  814. // (That's a literal JavaScript null. Not false, 0, or undefined. null.)
  815. $.ajax ({
  816. type: 'POST',
  817. url: url_logout,
  818. success: function(res, status, xhr) {
  819. location.href = url_freshrss;
  820. },
  821. error: function(res, status, xhr) {
  822. //alert("logout failure" + res);
  823. }
  824. });
  825. }
  826. });
  827. }
  828. //</persona>
  829. function init_confirm_action() {
  830. $('.confirm').click(function () {
  831. return confirm(str_confirmation);
  832. });
  833. }
  834. function init_print_action() {
  835. $('.item.share > a[href="#"]').click(function () {
  836. var content = "<html><head><style>"
  837. + "body { font-family: Serif; text-align: justify; }"
  838. + "a { color: #000; text-decoration: none; }"
  839. + "a:after { content: ' [' attr(href) ']'}"
  840. + "</style></head><body>"
  841. + $(".flux.current .content").html()
  842. + "</body></html>";
  843. var tmp_window = window.open();
  844. tmp_window.document.writeln(content);
  845. tmp_window.document.close();
  846. tmp_window.focus();
  847. tmp_window.print();
  848. tmp_window.close();
  849. return false;
  850. });
  851. }
  852. function init_share_observers() {
  853. shares = $('.form-group:not(".form-actions")').length;
  854. $('.post').on('click', '.share.remove', function(e){
  855. e.preventDefault();
  856. $(this).parents('.form-group').remove();
  857. });
  858. $('.share.add').on('click',function(e){
  859. e.preventDefault();
  860. var opt = $(this).siblings('select').find(':selected');
  861. var row = $(this).parents('form').data(opt.data('form'));
  862. row = row.replace('##label##', opt.html(), 'g');
  863. row = row.replace('##type##', opt.val(), 'g');
  864. row = row.replace('##help##', opt.data('help'), 'g');
  865. row = row.replace('##key##', shares, 'g');
  866. $(this).parents('.form-actions').before(row);
  867. shares++;
  868. });
  869. };
  870. function init_all() {
  871. if (!(window.$ && window.url_freshrss && ((!full_lazyload) || $.fn.lazyload))) {
  872. if (window.console) {
  873. console.log('FreshRSS waiting for JS…');
  874. }
  875. window.setTimeout(init_all, 50);
  876. return;
  877. }
  878. init_notifications();
  879. switch (authType) {
  880. case 'form':
  881. init_loginForm();
  882. break;
  883. case 'persona':
  884. init_persona();
  885. break;
  886. }
  887. init_confirm_action();
  888. $stream = $('#stream');
  889. if ($stream.length > 0) {
  890. init_actualize();
  891. init_column_categories();
  892. init_load_more($stream);
  893. init_posts();
  894. init_stream($stream);
  895. init_nav_entries();
  896. init_shortcuts();
  897. init_print_action();
  898. window.setInterval(refreshUnreads, 120000);
  899. } else {
  900. init_share_observers();
  901. }
  902. if (window.console) {
  903. console.log('FreshRSS init done.');
  904. }
  905. }
  906. if (document.readyState && document.readyState !== 'loading') {
  907. if (window.console) {
  908. console.log('FreshRSS immediate init…');
  909. }
  910. init_all();
  911. } else if (document.addEventListener) {
  912. document.addEventListener('DOMContentLoaded', function () {
  913. if (window.console) {
  914. console.log('FreshRSS waiting for DOMContentLoaded…');
  915. }
  916. init_all();
  917. }, false);
  918. }