main.js 26 KB

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