main.js 28 KB

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