<commit_before>const isVisible = require('./common_functions').isVisible;
const QueryString = require('./query_string');

const ScrollToAnchor = (() => {
    const init = () => {
        scrollToElement();
        addAnchorsToElements();
    };

    const makeAnchorLink = (id) => {
        const anchor_link = document.createElement('a');
        const { origin, pathname, hash } = window.location;
        anchor_link.href = `${origin}${pathname}?anchor=${encodeURI(id)}${hash}`;
        anchor_link.innerText = '#';
        return anchor_link;
    };

    const addAnchorsToElements = () => {
        const els = document.querySelectorAll('[data-anchor]');
        els.forEach(el => {
            const id = el.dataset.anchor;
            const anchor_link = makeAnchorLink(id);
            el.appendChild(anchor_link);
        });
    };

    const scrollToElement = () => {
        const query = QueryString.queryStringToObject(window.location.search);
        const id = query.anchor;
        if (!id) return;
        const candidates = document.querySelectorAll(`[data-anchor="${id}"]`);
        const el = Array.from(candidates).find(isVisible);
        if (!el) return;
        window.el1 = el; // for testing, remove later
        console.log('scroll to anchor', el.offsetTop, document.readyState);
        $.scrollTo(el, 500);
    };

    return {
        init,
    };
})();

module.exports = ScrollToAnchor;<commit_msg>add click handler to anchor link<commit_after>const isVisible = require('./common_functions').isVisible;
const QueryString = require('./query_string');

const ScrollToAnchor = (() => {
    const init = () => {
        scrollToAnchorInQuery();
        addAnchorsToElements();
    };

    const makeAnchorLink = (id) => {
        const anchor_link = document.createElement('a');
        const { origin, pathname, hash } = window.location;
        anchor_link.href = `${origin}${pathname}?anchor=${encodeURI(id)}${hash}`;
        anchor_link.innerText = '#';
        return anchor_link;
    };

    const addAnchorsToElements = () => {
        const els = document.querySelectorAll('[data-anchor]');
        els.forEach(el => {
            const id = el.dataset.anchor;
            const anchor_link = makeAnchorLink(id);
            el.appendChild(anchor_link);
            anchor_link.addEventListener('click', (e) => {
                e.preventDefault();
                console.log(id);
                $.scrollTo(el, 500);
                QueryString.setQueryStringWithoutReload(
                    // TODO: use appendParamToQueryString to preserve current q.s.
                    `?anchor=${encodeURI(id)}`
                );
            });
        });
    };

    const scrollToAnchorInQuery = () => {
        const query = QueryString.queryStringToObject(window.location.search);
        const id = query.anchor;
        if (!id) return;
        const candidates = document.querySelectorAll(`[data-anchor="${id}"]`);
        const el = Array.from(candidates).find(isVisible);
        if (!el) return;
        window.el1 = el; // for testing, remove later
        console.log('scroll to anchor', el.offsetTop, document.readyState);
        $.scrollTo(el, 500);
    };

    return {
        init,
    };
})();

module.exports = ScrollToAnchor;
{'fraction_non_alphanumeric': 0.14359756097560974, 'fraction_numerical': 0.003353658536585366, 'mean_word_length': 2.365128205128205, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 16, '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': '5889265', 'n_tokens_mistral': 923, 'n_tokens_neox': 902, 'n_words': 175}