<commit_before>$(function() {
    if ($('body').hasClass('compendium_landing_page')) {
        $('.js-print-chapters').click(function (e) {
            addLoadingOverlay();

            $('.chapter').each(function (index) {
                // Synchronously adds div with id to get around Ajax working asynchronously
                $('main').append("<div id='compendium-print" + index + "'></div>");

                var url = $(this).attr('href');

                // Set what content from each page we want to retrieve for printing
                var childIntro = ('.page-intro');
                var childContent = ('.page-content');

                $.get(url, function (data) {
                    $(data).find(childIntro).addClass('print--break-before').appendTo('#compendium-print' + index);
                    $(data).find(childContent).appendTo('#compendium-print' + index);
                });


                e.preventDefault();

            });

            $(document).ajaxStop(function () {
                window.print();
                location.reload();
            });
        });
    }
});<commit_msg>Fix charts not showing in printed compendium<commit_after>$(function () {
    if ($('body').hasClass('compendium_landing_page')) {
        $('.js-print-chapters').click(function (e) {
            e.preventDefault();
            addLoadingOverlay();

            var $chapters =  $('.chapter'),
            chapterLength = $chapters.length;

            $chapters.each(function (index) {
                // Synchronously adds div with id to get around Ajax working asynchronously
                $('main').append("<div id='compendium-print" + index + "'></div>");

                var url = $(this).attr('href');

                // Set what content from each page we want to retrieve for printing
                var childIntro = ('.page-intro');
                var childContent = ('.page-content');

                // Get chapter content
                $.get(url, function (response) {
                    // Remove noscript tags around images, they break the charts when requested
                    var html = response.replace(/<\/noscript>/g, '').replace(/<noscript>/g, '');

                    // Add in print page breaks before each chapter and add to compendium landing page
                    var $response = $(html);
                    $response.find(childIntro).addClass('print--break-before').appendTo('#compendium-print' + index);
                    $response.find(childContent).appendTo('#compendium-print' + index).imagesLoaded().then(function() {
                        chaptersComplete(index);
                    });
                });
            });

            // Tally number of chapters complete and print window when done
            function chaptersComplete(index) {
                if (index+1 == chapterLength) {
                    window.print();
                    location.reload();
                }
            }
        });

        // Function to wait until all images are loaded in the DOM - stackoverflow.com/questions/4774746/jquery-ajax-wait-until-all-images-are-loaded
        $.fn.imagesLoaded = function () {

            // get all the images (excluding those with no src attribute)
            var $imgs = this.find('img[src!=""]');
            // if there's no images, just return an already resolved promise
            if (!$imgs.length) {return $.Deferred().resolve().promise();}

            // for each image, add a deferred object to the array which resolves when the image is loaded (or if loading fails)
            var dfds = [];
            $imgs.each(function(){

                var dfd = $.Deferred();
                dfds.push(dfd);
                var img = new Image();
                img.onload = function(){dfd.resolve();}
                img.onerror = function(){dfd.resolve();}
                img.src = this.src;

            });

            // return a master promise object which will resolve when all the deferred objects have resolved
            // IE - when all the images are loaded
            return $.when.apply($,dfds);

        };
    }
});
{'fraction_non_alphanumeric': 0.1332691845080587, 'fraction_numerical': 0.0019244647582391148, 'mean_word_length': 1.6517857142857142, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 9, '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': '3298880', 'n_tokens_mistral': 1064, 'n_tokens_neox': 990, 'n_words': 274}