<commit_before>var all_tts;
var annotations;
var original_data = {};
var layer_data;
var h2o_annotator;
var all_collage_data = {};
var tocId = 'toc';

var collages = {
  listenToRecordAnnotatedItemState: function() {},
  set_highlights: function(data) {},
  set_highlights_for_highlight_only: function(data) {},
  rehighlight: function() {},
  updateWordCount: function() {},
  clean_layer: function(layer_name) {
    //Note: Implemented in multiple areas in our javascript/ruby
    if(layer_name === undefined) {
      return '';
    }
    return layer_name.replace(/ /g, 'whitespace').replace(/\./g, 'specialsymbol').replace(/'/g, 'apostrophe').replace(/\(/g, 'leftparen').replace(/\)/g, 'rightparen').replace(/,/g, 'c0mma').replace(/\&/g, 'amp3r');
  },
  getHexes: function() {
    return $('<div>');
  },
  loadState: function(collage_id, data) {
    export_functions.highlightAnnotatedItem(collage_id, data.highlights, data.highlight_only_highlights);

    var cannotations = all_collage_data["collage" + collage_id].annotations;
    $.each(cannotations, function(i, ann) {
      var annotation = $.parseJSON(ann);
      if(annotation.annotation != '' && !annotation.hidden && !annotation.error && !annotation.discussion && !annotation.feedback) {
        $('<span>').addClass('annotation-content annotation-content-' + annotation.id).html(annotation.annotation).insertAfter($('.annotation-' + annotation.id + ':last'));
      } else if(annotation.link !== undefined && annotation.link !== null) {
        var link_html = '<a href="' + annotation.link + '">' + annotation.link + '</a>'; 
        $('<span>').addClass('annotation-content annotation-content-' + annotation.id).html(link_html).insertAfter($('.annotation-' + annotation.id + ':last'));
      }
    });

    if($('#printannotations').val() == 'yes') {
      $('#collage' + collage_id + ' span.annotation-content').show();
    }
    if($('#hiddentext').val() == 'show') {
      $('#collage' + collage_id + ' .layered-ellipsis-hidden').hide();
      $('#collage' + collage_id + ' .original_content,#collage' + collage_id + ' .annotation-hidden').show();
    }
    if($('#printhighlights').val() == 'all') {
      export_functions.highlightAnnotatedItem(collage_id, all_collage_data["collage" + collage_id].layer_data, all_collage_data["collage" + collage_id].highlights_only);
    }
  }
};

var export_functions = {
    show_toc: function(toc_levels) {
        var toc_node = $('#' + tocId);
        toc_node.remove();
        export_functions.generate_toc(toc_levels);
        toc_node.show();  //do we need this?
        $('#toc_container').show();
    },
    hide_toc: function() {
        var toc_node = $('#' + tocId);
        toc_node.remove();
        $('#toc_container').hide();
    },
    generate_toc: function(toc_levels) {
        var toc_nodes = export_functions.build_branch();
        export_functions.wrap_toc(toc_nodes);
    },
    toc_max_depth: function() {
        //console.log( 'TMD: ' + $('#toc_levels').val() );
        return $('#toc_levels').val();
    },
    build_branch: function(parent, depth) {
        //$depth indicates the current depth of $parent
        //Returns nested array of nodes that are children of parent, including any children of those children

        //wkhtmltopdf cannot handle default argument values in function definitions
        parent = typeof parent !== 'undefined' ? parent : $(':root');
        depth = typeof depth !== 'undefined' ? depth : 1;

        var max_depth = export_functions.toc_max_depth();
        //console.log( 'BB.max_depth: ' + max_depth);
        if (depth > max_depth) {
            //TODO: This should never happen and can probably be removed completely
            //console.log('max depth reached!'); 
            return null; 
        }

        parent.toc_level = depth-1;
        var nodes = [parent];
        var children = parent.find('.playlists > ul').first().children();

        children.each(function () {
            var child = $(this);
            child.toc_level = depth;

            //if we are currently at our max depth, return this kid rather than make a recursive call.
            //nodes.push( (depth == max_depth) ? child : export_functions.build_branch( child, depth+1 ) );
            if (depth == max_depth) {
                nodes.push( child );
            }
            else {
                nodes.push( export_functions.build_branch( child, depth+1 ) );
            }
        });
        return nodes;
    },
    wrap_toc: function(nodes) {
        var toc_root_node = $('#toc_container');
        var flat_results = export_functions.flatten(nodes)
        var toc = $('<ol/>', { id: tocId });
        
        for(var i = 0; i<flat_results.length; i++) {
            var toc_line = export_functions.get_text(flat_results[i])
            toc.append($('<li/>', { html: toc_line }));
            toc.appendTo(toc_root_node);
        }
    },
    get_text: function( node ) {
        //TODO: Clean this up and use less raw HTML
        var header_node = node.children('h3').first();;
        var content = $(header_node).children('.hcontent');
        var anchor = $(header_node).children('.number').children('a');
        var toc_line = '<span class="toc_hcontent toc_level' + node.toc_level;
        toc_line += '"><a href="#' + anchor.attr('name') + '">';
        toc_line += content.text() + '</a></span>';

        return toc_line;
    },
    flatten: function(arr) {
        //TODO: exit safely if arr is null
        return arr.reduce(function (flat, toFlatten) {
            return flat.concat(Array.isArray(toFlatten) ? export_functions.flatten(toFlatten) : toFlatten);
        }, []);
    },
  initiate_collage_data: function(id, data) {
    all_collage_data["collage" + id] = data;
  },
  init_hash_detail: function() {
    if(document.location.hash.match('fontface')) {
      var vals = document.location.hash.replace('#', '').split('-');
      for(var i in vals) {
        var font_values = vals[i].split('=');
        if(font_values[0] == 'fontsize' || font_values[0] == 'fontface') {
          $('#' + font_values[0]).val(font_values[1]);
        }
      }
    }
  },
    title_debug: function(msg) {
        $("h1").text( $("h1").text() + ", " + msg );
    },
    init_user_settings: function() {
        //export_functions.title_debug('IUS firing');
        //$("h1").text( $("h1").text() + "," + 'IUS firing' );
    $('#printhighlights').val('original');

    if($.cookie('user_id') !== null) {
      if($.cookie('print_titles') == 'false') {
        $('#printtitle').val('no');
        $('h1').hide();
        $('.playlists h3').hide();
      }
      if($.cookie('print_dates_details') == 'false') {
        $('#printdetails').val('no');
        $('.details').hide();
      }
      if($.cookie('print_paragraph_numbers') == 'false') {
        $('#printparagraphnumbers').val('no');
        $('.paragraph-numbering').hide();
        $('.collage-content').css('padding-left', '0px');
      }
      if($.cookie('print_annotations') == 'true') {
        $('#printannotations').val('yes');
      }
      if($.cookie('hidden_text_display') == 'true') {
        $('#hiddentext').val('show');
      }
      if($.cookie('print_highlights') == 'none') {
        $('#printhighlights').val('none');
        $('.collage-content').each(function(i, el) {
          var id = $(el).data('id');
          export_functions.highlightAnnotatedItem(id, {}, {});
        });
      } else if($.cookie('print_highlights') == 'all') {
        $('#printhighlights').val('all');
      }
      $('#fontface').val($.cookie('print_font_face'));
      $('#fontsize').val($.cookie('print_font_size'));
      $('#toc_levels').val($.cookie('toc_levels'));
      if($.cookie('toc_levels') !== null) {
        //console.log( 'IUS.toc_levels: ' + $.cookie('toc_levels') );
        export_functions.show_toc($.cookie('toc_levels'));
      }
    }
  },
  init_listeners: function() {
    $('#export_format').selectbox({
      className: "jsb", replaceInvisible: true
    }).change(function() {
      //console.log('Format set to: ' + $('#export_format').val());
    });
    $('#toc_levels').selectbox({
      className: "jsb", replaceInvisible: true
    }).change(function() {
      export_functions.setTocLevels($('#toc_levels').val());
    });
    $('#fontface').selectbox({
      className: "jsb", replaceInvisible: true
    }).change(function() {
      export_functions.setFontPrint();
    });
    $('#fontsize').selectbox({<commit_msg>Remove dependency on user_id cookie for other export cookies<commit_after>var all_tts;
var annotations;
var original_data = {};
var layer_data;
var h2o_annotator;
var all_collage_data = {};
var tocId = 'toc';

var collages = {
  listenToRecordAnnotatedItemState: function() {},
  set_highlights: function(data) {},
  set_highlights_for_highlight_only: function(data) {},
  rehighlight: function() {},
  updateWordCount: function() {},
  clean_layer: function(layer_name) {
    //Note: Implemented in multiple areas in our javascript/ruby
    if(layer_name === undefined) {
      return '';
    }
    return layer_name.replace(/ /g, 'whitespace').replace(/\./g, 'specialsymbol').replace(/'/g, 'apostrophe').replace(/\(/g, 'leftparen').replace(/\)/g, 'rightparen').replace(/,/g, 'c0mma').replace(/\&/g, 'amp3r');
  },
  getHexes: function() {
    return $('<div>');
  },
  loadState: function(collage_id, data) {
    export_functions.highlightAnnotatedItem(collage_id, data.highlights, data.highlight_only_highlights);

    var cannotations = all_collage_data["collage" + collage_id].annotations;
    $.each(cannotations, function(i, ann) {
      var annotation = $.parseJSON(ann);
      if(annotation.annotation != '' && !annotation.hidden && !annotation.error && !annotation.discussion && !annotation.feedback) {
        $('<span>').addClass('annotation-content annotation-content-' + annotation.id).html(annotation.annotation).insertAfter($('.annotation-' + annotation.id + ':last'));
      } else if(annotation.link !== undefined && annotation.link !== null) {
        var link_html = '<a href="' + annotation.link + '">' + annotation.link + '</a>'; 
        $('<span>').addClass('annotation-content annotation-content-' + annotation.id).html(link_html).insertAfter($('.annotation-' + annotation.id + ':last'));
      }
    });

    if($('#printannotations').val() == 'yes') {
      $('#collage' + collage_id + ' span.annotation-content').show();
    }
    if($('#hiddentext').val() == 'show') {
      $('#collage' + collage_id + ' .layered-ellipsis-hidden').hide();
      $('#collage' + collage_id + ' .original_content,#collage' + collage_id + ' .annotation-hidden').show();
    }
    if($('#printhighlights').val() == 'all') {
      export_functions.highlightAnnotatedItem(collage_id, all_collage_data["collage" + collage_id].layer_data, all_collage_data["collage" + collage_id].highlights_only);
    }
  }
};

var export_functions = {
    show_toc: function(toc_levels) {
        var toc_node = $('#' + tocId);
        toc_node.remove();
        export_functions.generate_toc(toc_levels);
        toc_node.show();  //do we need this?
        $('#toc_container').show();
    },
    hide_toc: function() {
        var toc_node = $('#' + tocId);
        toc_node.remove();
        $('#toc_container').hide();
    },
    generate_toc: function(toc_levels) {
        var toc_nodes = export_functions.build_branch();
        export_functions.wrap_toc(toc_nodes);
    },
    toc_max_depth: function() {
        //console.log( 'TMD: ' + $('#toc_levels').val() );
        return $('#toc_levels').val();
    },
    build_branch: function(parent, depth) {
        //$depth indicates the current depth of $parent
        //Returns nested array of nodes that are children of parent, including any children of those children

        //wkhtmltopdf cannot handle default argument values in function definitions
        parent = typeof parent !== 'undefined' ? parent : $(':root');
        depth = typeof depth !== 'undefined' ? depth : 1;

        var max_depth = export_functions.toc_max_depth();
        //console.log( 'BB.max_depth: ' + max_depth);
        if (depth > max_depth) {
            //TODO: This should never happen and can probably be removed completely
            //console.log('max depth reached!'); 
            return null; 
        }

        parent.toc_level = depth-1;
        var nodes = [parent];
        var children = parent.find('.playlists > ul').first().children();

        children.each(function () {
            var child = $(this);
            child.toc_level = depth;

            //if we are currently at our max depth, return this kid rather than make a recursive call.
            //nodes.push( (depth == max_depth) ? child : export_functions.build_branch( child, depth+1 ) );
            if (depth == max_depth) {
                nodes.push( child );
            }
            else {
                nodes.push( export_functions.build_branch( child, depth+1 ) );
            }
        });
        return nodes;
    },
    wrap_toc: function(nodes) {
        var toc_root_node = $('#toc_container');
        var flat_results = export_functions.flatten(nodes)
        var toc = $('<ol/>', { id: tocId });
        
        for(var i = 0; i<flat_results.length; i++) {
            var toc_line = export_functions.get_text(flat_results[i])
            toc.append($('<li/>', { html: toc_line }));
            toc.appendTo(toc_root_node);
        }
    },
    get_text: function( node ) {
        //TODO: Clean this up and use less raw HTML
        var header_node = node.children('h3').first();;
        var content = $(header_node).children('.hcontent');
        var anchor = $(header_node).children('.number').children('a');
        var toc_line = '<span class="toc_hcontent toc_level' + node.toc_level;
        toc_line += '"><a href="#' + anchor.attr('name') + '">';
        toc_line += content.text() + '</a></span>';

        return toc_line;
    },
    flatten: function(arr) {
        //TODO: exit safely if arr is null
        return arr.reduce(function (flat, toFlatten) {
            return flat.concat(Array.isArray(toFlatten) ? export_functions.flatten(toFlatten) : toFlatten);
        }, []);
    },
  initiate_collage_data: function(id, data) {
    all_collage_data["collage" + id] = data;
  },
  init_hash_detail: function() {
    if(document.location.hash.match('fontface')) {
      var vals = document.location.hash.replace('#', '').split('-');
      for(var i in vals) {
        var font_values = vals[i].split('=');
        if(font_values[0] == 'fontsize' || font_values[0] == 'fontface') {
          $('#' + font_values[0]).val(font_values[1]);
        }
      }
    }
  },
    title_debug: function(msg) {
        $("h1").text( $("h1").text() + ", " + msg );
    },
    init_user_settings: function() {
        //export_functions.title_debug('IUS firing');
        //$("h1").text( $("h1").text() + "," + 'IUS firing' );
    $('#printhighlights').val('original');

    if($.cookie('print_titles') == 'false') {
      $('#printtitle').val('no');
      $('h1').hide();
      $('.playlists h3').hide();
    }
    if($.cookie('print_dates_details') == 'false') {
      $('#printdetails').val('no');
      $('.details').hide();
    }
    if($.cookie('print_paragraph_numbers') == 'false') {
      $('#printparagraphnumbers').val('no');
      $('.paragraph-numbering').hide();
      $('.collage-content').css('padding-left', '0px');
    }
    if($.cookie('print_annotations') == 'true') {
      $('#printannotations').val('yes');
    }
    if($.cookie('hidden_text_display') == 'true') {
      $('#hiddentext').val('show');
    }
    if($.cookie('print_highlights') == 'none') {
      $('#printhighlights').val('none');
      $('.collage-content').each(function(i, el) {
        var id = $(el).data('id');
        export_functions.highlightAnnotatedItem(id, {}, {});
      });
    } else if($.cookie('print_highlights') == 'all') {
      $('#printhighlights').val('all');
    }
    $('#fontface').val($.cookie('print_font_face'));
    $('#fontsize').val($.cookie('print_font_size'));
    $('#toc_levels').val($.cookie('toc_levels'));
    if($.cookie('toc_levels') !== null) {
      //console.log( 'IUS.toc_levels: ' + $.cookie('toc_levels') );
      export_functions.show_toc($.cookie('toc_levels'));
    }
  },
  init_listeners: function() {
    $('#export_format').selectbox({
      className: "jsb", replaceInvisible: true
    }).change(function() {
      //console.log('Format set to: ' + $('#export_format').val());
    });
    $('#toc_levels').selectbox({
      className: "jsb", replaceInvisible: true
    }).change(function() {
      export_functions.setTocLevels($('#toc_levels').val());
    });
    $('#fontface').selectbox({
      className: "jsb", replaceInvisible: true
    }).change(function() {
      export_functions.setFontPrint();
    });
    $('#fontsize').selectbox({
{'fraction_non_alphanumeric': 0.18845290154828034, 'fraction_numerical': 0.0023637867864318637, 'mean_word_length': 2.8984105044920527, 'pattern_counts': {'":': 0, '<': 27, '<?xml version=': 0, '>': 29, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5606786', 'n_tokens_mistral': 5375, 'n_tokens_neox': 5214, 'n_words': 941}