<commit_before>var $ = require('jQuery');
var onReady = require('kwf/on-ready');
var isVisible = require('kwf/element/is-visible');
var getCachedWidth = require('kwf/element/get-cached-width');
var _ = require('underscore');

var DONT_HASH_TYPE_PREFIX = 'dh-';
var $w = $(window);
var deferredImages = [];

module.exports = function (selector) {
    onReady.onResize(selector, function responsiveImg(el) {
        if (el[0].responsiveImgInitDone) {
            checkResponsiveImgEl(el);
        } else {
            if (el.hasClass('kwfUp-loadImmediately') || isElementInView(el)) {
                initResponsiveImgEl(el);
            } else {
                if (!el.data('responsiveImgInitDeferred')) {
                    deferredImages.push(el);
                    el.data('responsiveImgInitDeferred', true);
                }
            }
        }
    }, { defer: true });
};

var lastScrollTop = null;
$(function() {
    function showImageWhenVisible()
    {
        if (lastScrollTop && Math.abs($w.scrollTop()-lastScrollTop) < 50) {
            //only check for images to load in steps of 50px, we can do that as we load 50px in advance
            return;
        }

        lastScrollTop = $w.scrollTop()
        for (var i=0; i<deferredImages.length; ++i) {
            var el = deferredImages[i];
            if (isElementInView(el)) {
                deferredImages.splice(i, 1);
                i--;
                initResponsiveImgEl(el);
            }
        }
    }
    $w.scroll(_.throttle(showImageWhenVisible, 150));
});
<commit_msg>Fix duplicated imagetags If an element comes into view without scrolling, for example in a slider, it has to be removed from deferedImages array. Because orientationchange triggers a scrollevent on some devices, a check has to be made if the element is not already initialized in the showImageWhenVisible function.<commit_after>var $ = require('jQuery');
var onReady = require('kwf/on-ready');
var isVisible = require('kwf/element/is-visible');
var getCachedWidth = require('kwf/element/get-cached-width');
var _ = require('underscore');

var DONT_HASH_TYPE_PREFIX = 'dh-';
var $w = $(window);
var deferredImages = [];

module.exports = function (selector) {
    onReady.onResize(selector, function responsiveImg(el) {
        if (el[0].responsiveImgInitDone) {
            checkResponsiveImgEl(el);
        } else {
            if (el.hasClass('kwfUp-loadImmediately') || isElementInView(el)) {
                initResponsiveImgEl(el);
                if (deferredImages.indexOf(el) != -1) {
                    deferredImages.splice(deferredImages.indexOf(el), 1);
                }
            } else {
                if (!el.data('responsiveImgInitDeferred')) {
                    deferredImages.push(el);
                    el.data('responsiveImgInitDeferred', true);
                }
            }
        }
    }, { defer: true });
};

var lastScrollTop = null;
$(function() {
    function showImageWhenVisible()
    {
        if (lastScrollTop && Math.abs($w.scrollTop()-lastScrollTop) < 50) {
            //only check for images to load in steps of 50px, we can do that as we load 50px in advance
            return;
        }

        lastScrollTop = $w.scrollTop()
        for (var i=0; i<deferredImages.length; ++i) {
            var el = deferredImages[i];
            if (isElementInView(el)) {
                deferredImages.splice(i, 1);
                i--;
                if (!el[0].responsiveImgInitDone) {
                    initResponsiveImgEl(el);
                }
            }
        }
    }
    $w.scroll(_.throttle(showImageWhenVisible, 150));
});

{'fraction_non_alphanumeric': 0.1233177698434496, 'fraction_numerical': 0.007415545179895633, 'mean_word_length': 2.139655172413793, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4522463', 'n_tokens_mistral': 1110, 'n_tokens_neox': 1043, 'n_words': 239}