"use strict"; /* globals $, jQuery, context, i18n, shortcut, shortcuts, url */ /* jshint strict:global */ var $stream = null, isCollapsed = true, shares = 0, ajax_loading = false; function redirect(url, new_tab) { if (url) { if (new_tab) { window.open(url); } else { location.href = url; } } } function needsScroll($elem) { var $win = $(window), winTop = $win.scrollTop(), winHeight = $win.height(), winBottom = winTop + winHeight, elemTop = $elem.offset().top, elemBottom = elemTop + $elem.outerHeight(); return (elemTop < winTop || elemBottom > winBottom) ? elemTop - (winHeight / 2) : 0; } function str2int(str) { if (!str) { return 0; } return parseInt(str.replace(/\D/g, ''), 10) || 0; } function numberFormat(nStr) { if (nStr < 0) { return 0; } // http://www.mredkj.com/javascript/numberFormat.html nStr += ''; var x = nStr.split('.'), x1 = x[0], x2 = x.length > 1 ? '.' + x[1] : '', rgx = /(\d+)(\d{3})/; while (rgx.test(x1)) { x1 = x1.replace(rgx, '$1' + ' ' + '$2'); } return x1 + x2; } function incLabel(p, inc, spaceAfter) { var i = str2int(p) + inc; return i > 0 ? ((spaceAfter ? '' : ' ') + '(' + numberFormat(i) + ')' + (spaceAfter ? ' ' : '')) : ''; } function incUnreadsFeed(article, feed_id, nb) { //Update unread: feed var elem = $('#' + feed_id).get(0), feed_unreads = elem ? str2int(elem.getAttribute('data-unread')) : 0, feed_priority = elem ? str2int(elem.getAttribute('data-priority')) : 0; if (elem) { elem.setAttribute('data-unread', feed_unreads + nb); elem = $(elem).children('.item-title').get(0); if (elem) { elem.setAttribute('data-unread', numberFormat(feed_unreads + nb)); } } //Update unread: category elem = $('#' + feed_id).parents('.category').get(0); feed_unreads = elem ? str2int(elem.getAttribute('data-unread')) : 0; if (elem) { elem.setAttribute('data-unread', feed_unreads + nb); elem = $(elem).find('.title').get(0); if (elem) { elem.setAttribute('data-unread', numberFormat(feed_unreads + nb)); } } //Update unread: all if (feed_priority > 0) { elem = $('#aside_feed .all .title').get(0); if (elem) { feed_unreads = elem ? str2int(elem.getAttribute('data-unread')) : 0; elem.setAttribute('data-unread', numberFormat(feed_unreads + nb)); } } //Update unread: favourites if (article && article.closest('div').hasClass('favorite')) { elem = $('#aside_feed .favorites .title').get(0); if (elem) { feed_unreads = elem ? str2int(elem.getAttribute('data-unread')) : 0; elem.setAttribute('data-unread', numberFormat(feed_unreads + nb)); } } var isCurrentView = false; // Update unread: title document.title = document.title.replace(/^((?:\([ 0-9]+\) )?)/, function (m, p1) { var $feed = $('#' + feed_id); if (article || ($feed.closest('.active').length > 0 && $feed.siblings('.active').length === 0)) { isCurrentView = true; return incLabel(p1, nb, true); } else if ($('.all.active').length > 0) { isCurrentView = feed_priority > 0; return incLabel(p1, feed_priority > 0 ? nb : 0, true); } else { return p1; } }); return isCurrentView; } function incUnreadsTag(tag_id, nb) { var $t = $('#t_' + tag_id); var unreads = str2int($t.attr('data-unread')); $t.attr('data-unread', unreads + nb) .children('.item-title').attr('data-unread', numberFormat(unreads + nb)); $t = $('.category.tags').find('.title'); unreads = str2int($t.attr('data-unread')); $t.attr('data-unread', numberFormat(unreads + nb)); } var pending_entries = {}; function mark_read(active, only_not_read) { if ((active.length === 0) || (!active.attr('id')) || context.anonymous || (only_not_read && !active.hasClass("not_read"))) { return false; } if (pending_entries[active.attr('id')]) { return false; } pending_entries[active.attr('id')] = true; var url = '.?c=entry&a=read&id=' + active.attr('id').replace(/^flux_/, '') + (active.hasClass('not_read') ? '' : '&is_read=0'); $.ajax({ type: 'POST', url: url, data: { ajax: true, _csrf: context.csrf, }, }).done(function (data) { var $r = active.find("a.read").attr("href", data.url), inc = 0; if (active.hasClass("not_read")) { active.removeClass("not_read"); inc--; } else { active.addClass("not_read"); active.addClass("keep_unread"); inc++; } $r.find('.icon').replaceWith(data.icon); var feed_url = active.find(".website>a").attr("href"); if (feed_url) { var feed_id = feed_url.substr(feed_url.lastIndexOf('f_')); incUnreadsFeed(active, feed_id, inc); } faviconNbUnread(); if (data.tags) { for (var i = data.tags.length - 1; i >= 0; i--) { incUnreadsTag(data.tags[i], inc); } } delete pending_entries[active.attr('id')]; }).fail(function (data) { openNotification(i18n.notif_request_failed, 'bad'); delete pending_entries[active.attr('id')]; }); } function mark_favorite(active) { if (active.length === 0) { return false; } var url = active.find("a.bookmark").attr("href"); if (url === undefined) { return false; } if (pending_entries[active.attr('id')]) { return false; } pending_entries[active.attr('id')] = true; $.ajax({ type: 'POST', url: url, data: { ajax: true, _csrf: context.csrf, }, }).done(function (data) { var $b = active.find("a.bookmark").attr("href", data.url), inc = 0; if (active.hasClass("favorite")) { active.removeClass("favorite"); inc--; } else { active.addClass("favorite").find('.bookmark'); inc++; } $b.find('.icon').replaceWith(data.icon); var favourites = $('#aside_feed .favorites .title').contents().last().get(0); if (favourites && favourites.textContent) { favourites.textContent = favourites.textContent.replace(/((?: \([ 0-9]+\))?\s*)$/, function (m, p1) { return incLabel(p1, inc, false); }); } if (active.closest('div').hasClass('not_read')) { var elem = $('#aside_feed .favorites .title').get(0), feed_unreads = elem ? str2int(elem.getAttribute('data-unread')) : 0; if (elem) { elem.setAttribute('data-unread', numberFormat(feed_unreads + inc)); } } delete pending_entries[active.attr('id')]; }).fail(function (data) { openNotification(i18n.notif_request_failed, 'bad'); delete pending_entries[active.attr('id')]; }); } function toggleContent(new_active, old_active, skipping) { // If skipping, move current without activating or marking as read if (new_active.length === 0) { return; } if (context.does_lazyload && !skipping) { new_active.find('img[data-original], iframe[data-original]').each(function () { this.setAttribute('src', this.getAttribute('data-original')); this.removeAttribute('data-original'); }); } if (old_active[0] !== new_active[0]) { if (isCollapsed && !skipping) { // BUG?: isCollapsed can only ever be true new_active.addClass("active"); } old_active.removeClass("active current"); new_active.addClass("current"); if (context.auto_remove_article && !old_active.hasClass('not_read') && !skipping) { auto_remove(old_active); } } else { // collapse_entry calls toggleContent(flux_current, flux_current, false) new_active.toggleClass('active'); } var relative_move = context.current_view === 'global', box_to_move = $(relative_move ? "#panel" : "html,body"); if (context.sticky_post) { var prev_article = new_active.prevAll('.flux'), new_pos = new_active.offset().top, old_scroll = box_to_move.scrollTop(); if (prev_article.length > 0 && new_pos - prev_article.offset().top <= 150) { new_pos = prev_article.offset().top; if (relative_move) { new_pos -= box_to_move.offset().top; } } if (skipping) { // when skipping, this feels more natural if it's not so near the top new_pos -= $(window).height() / 4; } if (context.hide_posts) { if (relative_move) { new_pos += old_scroll; } new_active.children(".flux_content").first().each(function () { box_to_move.scrollTop(new_pos).scrollTop(); }); } else { if (relative_move) { new_pos += old_scroll; } box_to_move.scrollTop(new_pos).scrollTop(); } } if (context.auto_mark_article && new_active.hasClass('active') && !skipping) { mark_read(new_active, true); } } function auto_remove(element) { var p = element.prev(); var n = element.next(); if (p.hasClass('day') && n.hasClass('day')) { p.remove(); } element.remove(); $('#stream > .flux:not(.not_read):not(.active)').remove(); } function prev_entry() { var old_active = $(".flux.current"), new_active = old_active.length === 0 ? $(".flux:last") : old_active.prevAll(".flux:first"); toggleContent(new_active, old_active, false); } function next_entry() { var old_active = $(".flux.current"), new_active = old_active.length === 0 ? $(".flux:first") : old_active.nextAll(".flux:first"); toggleContent(new_active, old_active, false); if (new_active.nextAll().length < 3) { load_more_posts(); } } function skip_prev_entry() { var old_active = $(".flux.current"), new_active = old_active.length === 0 ? $(".flux:last") : old_active.prevAll(".flux:first"); toggleContent(new_active, old_active, true); } function skip_next_entry() { var old_active = $(".flux.current"), new_active = old_active.length === 0 ? $(".flux:first") : old_active.nextAll(".flux:first"); toggleContent(new_active, old_active, true); if (new_active.nextAll().length < 3) { load_more_posts(); } } function prev_feed() { var active_feed = $("#aside_feed .tree-folder-items .item.active"); if (active_feed.length > 0) { active_feed.prevAll(':visible:first').find('a').each(function(){this.click();}); } else { last_feed(); } } function next_feed() { var active_feed = $("#aside_feed .tree-folder-items .item.active"); if (active_feed.length > 0) { active_feed.nextAll(':visible:first').find('a').each(function(){this.click();}); } else { first_feed(); } } function first_feed() { var feed = $("#aside_feed .tree-folder-items.active .item:visible:first"); if (feed.length > 0) { feed.find('a')[1].click(); } } function last_feed() { var feed = $("#aside_feed .tree-folder-items.active .item:visible:last"); if (feed.length > 0) { feed.find('a')[1].click(); } } function prev_category() { var active_cat = $("#aside_feed .tree-folder.active"); if (active_cat.length > 0) { var prev_cat = active_cat.prevAll(':visible:first').find('.tree-folder-title .title'); if (prev_cat.length > 0) { prev_cat[0].click(); } } else { last_category(); } return; } function next_category() { var active_cat = $("#aside_feed .tree-folder.active"); if (active_cat.length > 0) { var next_cat = active_cat.nextAll(':visible:first').find('.tree-folder-title .title'); if (next_cat.length > 0) { next_cat[0].click(); } } else { first_category(); } return; } function first_category() { var cat = $("#aside_feed .tree-folder:visible:first"); if (cat.length > 0) { cat.find('.tree-folder-title .title')[0].click(); } } function last_category() { var cat = $("#aside_feed .tree-folder:visible:last"); if (cat.length > 0) { cat.find('.tree-folder-title .title')[0].click(); } } function collapse_entry() { var flux_current = $(".flux.current"); toggleContent(flux_current, flux_current, false); } function user_filter(key) { var filter = $('#dropdown-query'); var filters = filter.siblings('.dropdown-menu').find('.item.query a'); if (typeof key === "undefined") { if (!filter.length) { return; } // Display the filter div window.location.hash = filter.attr('id'); // Force scrolling to the filter div var scroll = needsScroll($('.header')); if (scroll !== 0) { $('html,body').scrollTop(scroll); } // Force the key value if there is only one action, so we can trigger it automatically if (filters.length === 1) { key = 1; } else { return; } } // Trigger selected share action key = parseInt(key); if (key <= filters.length) { filters[key - 1].click(); } } function auto_share(key) { var share = $(".flux.current.active").find('.dropdown-target[id^="dropdown-share"]'); var shares = share.siblings('.dropdown-menu').find('.item a'); if (typeof key === "undefined") { if (!share.length) { return; } // Display the share div window.location.hash = share.attr('id'); // Force scrolling to the share div var scroll = needsScroll(share.closest('.bottom')); if (scroll !== 0) { $('html,body').scrollTop(scroll); } // Force the key value if there is only one action, so we can trigger it automatically if (shares.length === 1) { key = 1; } else { return; } } // Trigger selected share action and hide the share div key = parseInt(key); if (key <= shares.length) { shares[key - 1].click(); share.siblings('.dropdown-menu').find('.dropdown-close a')[0].click(); } } function scrollAsRead(box_to_follow) { var minTop = 40 + (context.current_view === 'global' ? box_to_follow.offset().top : box_to_follow.scrollTop()); $('.not_read:not(.keep_unread):visible').each(function () { var $this = $(this); if ($this.offset().top + $this.height() < minTop) { mark_read($this, true); } }); } function init_posts() { var box_to_follow = context.current_view === 'global' ? $("#panel") : $(window); if (context.auto_mark_scroll) { var lastScroll = 0, //Throttle timerId = 0; box_to_follow.scroll(function () { window.clearTimeout(timerId); if (lastScroll + 500 < Date.now()) { lastScroll = Date.now(); scrollAsRead(box_to_follow); } else { timerId = window.setTimeout(function() { scrollAsRead(box_to_follow); }, 500); } }); } if (context.auto_load_more) { box_to_follow.scroll(function () { var load_more = $("#load_more"); if (!load_more.is(':visible')) { return; } var boxBot = box_to_follow.scrollTop() + box_to_follow.height(), load_more_top = load_more.offset().top; if (boxBot >= load_more_top) { load_more_posts(); } }); box_to_follow.scroll(); } } function init_column_categories() { if (context.current_view !== 'normal' && context.current_view !== 'reader') { return; } $('#aside_feed').on('click', '.tree-folder>.tree-folder-title>a.dropdown-toggle', function () { $(this).children().each(function() { if (this.alt === '▽') { this.src = this.src.replace('/icons/down.', '/icons/up.'); this.alt = '△'; } else { this.src = this.src.replace('/icons/up.', '/icons/down.'); this.alt = '▽'; } }); $(this).parent().next(".tree-folder-items").slideToggle(300, function () { //Workaround for Gecko bug in Firefox 64-65(+?): var sidebar = document.getElementById('sidebar'); if (sidebar && sidebar.scrollHeight > sidebar.clientHeight && //if needs scrollbar sidebar.scrollWidth >= sidebar.offsetWidth) { //but no scrollbar sidebar.style['overflow-y'] = 'scroll'; //then force scrollbar setTimeout(function () { sidebar.style['overflow-y'] = ''; }, 0); } }); return false; }); $('#aside_feed').on('click', '.tree-folder-items .feed .dropdown-toggle', function () { var itemId = $(this).closest('.item').attr('id'), templateId = itemId.substring(0, 2) === 't_' ? 'tag_config_template' : 'feed_config_template', id = itemId.substr(2), feed_web = $(this).data('fweb'), template = $('#' + templateId) .html().replace(/------/g, id).replace('http://example.net/', feed_web); if ($(this).next('.dropdown-menu').length === 0) { $(this).attr('href', '#dropdown-' + id).prev('.dropdown-target').attr('id', 'dropdown-' + id).parent() .append(template).find('button.confirm').removeAttr('disabled'); } else { if ($(this).next('.dropdown-menu').css('display') === 'none') { id = $(this).closest('.item').attr('id').substr(2); $(this).attr('href', '#dropdown-' + id); } else { $(this).attr('href', "#close"); } } }); } function init_shortcuts() { if (!(window.shortcut && window.shortcuts)) { if (window.console) { console.log('FreshRSS waiting for shortcut.js…'); } window.setTimeout(init_shortcuts, 200); return; } // Manipulation shortcuts shortcut.add(shortcuts.mark_read, function () { // Toggle the read state var active = $(".flux.current"); mark_read(active, false); }, { 'disable_in_input': true }); shortcut.add("shift+" + shortcuts.mark_read, function () { // Mark everything as read $(".nav_menu .read_all").click(); }, { 'disable_in_input': true }); shortcut.add(shortcuts.mark_favorite, function () { // Toggle the favorite state var active = $(".flux.current"); mark_favorite(active); }, { 'disable_in_input': true }); shortcut.add(shortcuts.collapse_entry, function () { // Toggle the collapse state collapse_entry(); }, { 'disable_in_input': true }); shortcut.add(shortcuts.auto_share, function () { // Display the share options auto_share(); }, { 'disable_in_input': true }); shortcut.add(shortcuts.user_filter, function () { // Display the user filters user_filter(); }, { 'disable_in_input': true }); function addShortcut(evt) { if ($('#dropdown-query').siblings('.dropdown-menu').is(':visible')) { user_filter(String.fromCharCode(evt.keyCode)); } else { auto_share(String.fromCharCode(evt.keyCode)); } } for (var i = 1; i < 10; i++) { shortcut.add(i.toString(), addShortcut, { 'disable_in_input': true }); } // Entry navigation shortcuts shortcut.add(shortcuts.prev_entry, prev_entry, { 'disable_in_input': true }); shortcut.add(shortcuts.skip_prev_entry, skip_prev_entry, { 'disable_in_input': true }); shortcut.add(shortcuts.first_entry, function () { var old_active = $(".flux.current"), first = $(".flux:first"); if (first.hasClass("flux")) { toggleContent(first, old_active, false); } }, { 'disable_in_input': true }); shortcut.add(shortcuts.next_entry, next_entry, { 'disable_in_input': true }); shortcut.add(shortcuts.skip_next_entry, skip_next_entry, { 'disable_in_input': true }); shortcut.add(shortcuts.last_entry, function () { var old_active = $(".flux.current"), last = $(".flux:last"); if (last.hasClass("flux")) { toggleContent(last, old_active, false); } }, { 'disable_in_input': true }); // Feed navigation shortcuts shortcut.add("shift+" + shortcuts.prev_entry, prev_feed, { 'disable_in_input': true }); shortcut.add("shift+" + shortcuts.next_entry, next_feed, { 'disable_in_input': true }); shortcut.add("shift+" + shortcuts.first_entry, first_feed, { 'disable_in_input': true }); shortcut.add("shift+" + shortcuts.last_entry, last_feed, { 'disable_in_input': true }); // Category navigation shortcuts shortcut.add("alt+" + shortcuts.prev_entry, prev_category, { 'disable_in_input': true }); shortcut.add("alt+" + shortcuts.next_entry, next_category, { 'disable_in_input': true }); shortcut.add("alt+" + shortcuts.first_entry, first_category, { 'disable_in_input': true }); shortcut.add("alt+" + shortcuts.last_entry, last_category, { 'disable_in_input': true }); shortcut.add(shortcuts.go_website, function () { var url_website = $('.flux.current a.go_website').attr("href"); if (context.auto_mark_site) { $(".flux.current").each(function () { mark_read($(this), true); }); } redirect(url_website, true); }, { 'disable_in_input': true }); shortcut.add(shortcuts.load_more, function () { load_more_posts(); }, { 'disable_in_input': true }); shortcut.add(shortcuts.focus_search, function () { focus_search(); }, { 'disable_in_input': true }); shortcut.add(shortcuts.help, function () { redirect(url.help, true); }, { 'disable_in_input': true }); shortcut.add(shortcuts.close_dropdown, function () { window.location.hash = null; }, { 'disable_in_input': true }); shortcut.add(shortcuts.normal_view, function () { $('#nav_menu_views .view-normal').get(0).click(); }, { 'disable_in_input': true }); shortcut.add(shortcuts.global_view, function () { $('#nav_menu_views .view-global').get(0).click(); }, { 'disable_in_input': true }); shortcut.add(shortcuts.reading_view, function () { $('#nav_menu_views .view-reader').get(0).click(); }, { 'disable_in_input': true }); shortcut.add(shortcuts.rss_view, function () { $('#nav_menu_views .view-rss').get(0).click(); }, { 'disable_in_input': true }); } function init_stream(divStream) { divStream.on('click', '.flux_header,.flux_content', function (e) { //flux_toggle if ($(e.target).closest('.content, .item.website, .item.link, .dropdown-menu').length > 0) { return; } if (!context.sides_close_article && $(e.target).is('div.flux_content')) { // setting for not-closing after clicking outside article area return; } var old_active = $(".flux.current"), new_active = $(this).parent(); isCollapsed = true; if (e.target.tagName.toUpperCase() === 'A') { //Leave real links alone if (context.auto_mark_article) { mark_read(new_active, true); } return true; } toggleContent(new_active, old_active, false); }); divStream.on('click', '.flux a.read', function () { var active = $(this).parents(".flux"); if (context.auto_remove_article && active.hasClass('not_read')) { auto_remove(active); } mark_read(active, false); return false; }); divStream.on('click', '.flux a.bookmark', function () { var active = $(this).parents(".flux"); mark_favorite(active); return false; }); divStream.on('click', '.item.title > a', function (e) { // Allow default control-click behaviour such as open in backround-tab. return e.ctrlKey; }); divStream.on('mouseup', '.item.title > a', function (e) { // Mouseup enables us to catch middle click. if (e.ctrlKey) { // CTRL+click, it will be manage by previous rule. return; } if (e.which == 2) { // If middle click, we want same behaviour as CTRL+click. var ev = jQuery.Event("click"); ev.ctrlKey = true; $(this).trigger(ev); } else if(e.which == 1) { // Normal click, just toggle article. $(this).parent().click(); } }); divStream.on('click', '.flux .content a', function () { if (!$(this).closest('div').hasClass('author')) { $(this).attr('target', '_blank').attr('rel', 'noreferrer'); } }); if (context.auto_mark_site) { // catch mouseup instead of click so we can have the correct behaviour // with middle button click (scroll button). divStream.on('mouseup', '.flux .link > a', function (e) { if (e.which == 3) { return; } mark_read($(this).parents(".flux"), true); }); } } var $nav_entries = null; function init_nav_entries() { $nav_entries = $('#nav_entries'); $nav_entries.find('.previous_entry').click(function () { prev_entry(); return false; }); $nav_entries.find('.next_entry').click(function () { next_entry(); return false; }); $nav_entries.find('.up').click(function () { var active_item = $(".flux.current"), windowTop = $(window).scrollTop(), item_top = active_item.offset().top; if (windowTop > item_top) { $("html,body").scrollTop(item_top); } else { $("html,body").scrollTop(0); } return false; }); } function loadDynamicTags($div) { $div.removeClass('dynamictags'); $div.find('li.item').remove(); var entryId = $div.closest('div.flux').attr('id').replace(/^flux_/, ''); $.getJSON('./?c=tag&a=getTagsForEntry&id_entry=' + entryId) .done(function (data) { var $ul = $div.find('.dropdown-menu'); $ul.append('
'); if (data && data.length) { for (var i = 0; i < data.length; i++) { var tag = data[i]; $ul.append(''); } } }) .fail(function () { $div.find('li.item').remove(); $div.addClass('dynamictags'); }); } function init_dynamic_tags() { $stream.on('click', '.dynamictags', function () { loadDynamicTags($(this)); }); $stream.on('change', '.checkboxTag', function (ev) { var $checkbox = $(this); $checkbox.prop('disabled', true); var isChecked = $checkbox.prop('checked'); var tagId = $checkbox.attr('name').replace(/^t_/, ''); var tagName = $checkbox.siblings('input[name]').val(); var $entry = $checkbox.closest('div.flux'); var entryId = $entry.attr('id').replace(/^flux_/, ''); $.ajax({ type: 'POST', url: './?c=tag&a=tagEntry', data: { _csrf: context.csrf, id_tag: tagId, name_tag: tagId == 0 ? tagName : '', id_entry: entryId, checked: isChecked, }, }) .done(function () { if ($entry.hasClass('not_read')) { incUnreadsTag(tagId, isChecked ? 1 : -1); } }) .fail(function () { $checkbox.prop('checked', !isChecked); }) .always(function () { $checkbox.prop('disabled', false); if (tagId == 0) { loadDynamicTags($checkbox.closest('div.dropdown')); } }); }); } //