main.js 26 KB

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