<commit_before>var Backbone = require('backbone'),
  $ = require('jquery'),
  loadTemplate = require('../utils/loadTemplate'),
  baseVw = require('./baseVw'),
  chatHeadVw = require('./chatHeadVw');

module.exports = baseVw.extend({
  events: {
  },

  initialize: function(options) {
    if (!options.collection) {
      throw new Error('Please provide a collection.');
    }

    if (!options.parentEl) {
      throw new Error('Please provide a parent element');
    }

    this.listenTo(this.collection, 'add', (md, cl, opts) => {
      this.$headContainer.prepend(
        this.createChatHead(md).render().el
      );
    });

    this.$chatHeadsContainer = options.parentEl;
    this.$headContainer = $('<div />');

    this.showPerScroll = 12;

    //listen to scrolling on container
    this.scrollHandler = __.bind(
        __.throttle(this.onScroll, 100), this
    );
    this.$chatHeadsContainer.on('scroll', this.scrollHandler);
  },

  setCollection: function(cl) {
    if (cl) {
      this.collection = cl;
    }
  },

  chatHeadClick: function(vw) {
    this.trigger('chatHeadClick', vw);
  },

  createChatHead: function(md) {
    var vw = new chatHeadVw({
      model: md
    });

    this.chatHeadViews.push(vw);
    this.listenTo(vw, 'click', this.chatHeadClick);
    this.registerChild(vw);

    return vw;
  },

  onScroll: function(){
    if (this.$chatHeadsContainer[0].scrollTop + this.$chatHeadsContainer[0].clientHeight + 200 >
          this.$chatHeadsContainer[0].scrollHeight &&
        this.chatHeadViews.length < this.collection.length) {
      this.renderChatHeads(this.chatHeadViews.length, this.chatHeadViews.length + this.showPerScroll);
    }
  },

  render: function(){
    //remove any old chatHead views
    if (this.chatHeadViews) {
      this.chatHeadViews.forEach((vw) => {
        vw.remove();
      });
    }

    this.chatHeadViews = [];
    this.renderChatHeads(0, this.showPerScroll);
    this.$el.html(this.$headContainer);
    
    setTimeout(() => {
      this.checkIfFilled();
    },0);

    return this;
  },

  checkIfFilled: function(){
    if (this.$headContainer[0].childNodes.length < this.collection.length && this.$chatHeadsContainer[0].clientHeight > this.$headContainer[0].scrollHeight){
      this.onScroll();
      this.checkIfFilled();
    }
  },

  renderChatHeads: function(start, end) {
    var chatsToRender = this.collection.slice(start, end);

    chatsToRender.forEach((md, index) => {
      this.$headContainer.append(
        this.createChatHead(md).render().el
      );
    });
  },

  remove: function(){
    this.scrollHandler && this.$headContainer.off('scroll', this.scrollHandler);

    baseVw.prototype.remove.apply(this, arguments);
  }

});<commit_msg>added additional test to checkIfFilled()<commit_after>var Backbone = require('backbone'),
  $ = require('jquery'),
  loadTemplate = require('../utils/loadTemplate'),
  baseVw = require('./baseVw'),
  chatHeadVw = require('./chatHeadVw');

module.exports = baseVw.extend({
  events: {
  },

  initialize: function(options) {
    if (!options.collection) {
      throw new Error('Please provide a collection.');
    }

    if (!options.parentEl) {
      throw new Error('Please provide a parent element');
    }

    this.listenTo(this.collection, 'add', (md, cl, opts) => {
      this.$headContainer.prepend(
        this.createChatHead(md).render().el
      );
    });

    this.$chatHeadsContainer = options.parentEl;
    this.$headContainer = $('<div />');

    this.showPerScroll = 12;

    //listen to scrolling on container
    this.scrollHandler = __.bind(
        __.throttle(this.onScroll, 100), this
    );
    this.$chatHeadsContainer.on('scroll', this.scrollHandler);
  },

  setCollection: function(cl) {
    if (cl) {
      this.collection = cl;
    }
  },

  chatHeadClick: function(vw) {
    this.trigger('chatHeadClick', vw);
  },

  createChatHead: function(md) {
    var vw = new chatHeadVw({
      model: md
    });

    this.chatHeadViews.push(vw);
    this.listenTo(vw, 'click', this.chatHeadClick);
    this.registerChild(vw);

    return vw;
  },

  onScroll: function(){
    if (this.$chatHeadsContainer[0].scrollTop + this.$chatHeadsContainer[0].clientHeight + 200 >
          this.$chatHeadsContainer[0].scrollHeight &&
        this.chatHeadViews.length < this.collection.length) {
      this.renderChatHeads(this.chatHeadViews.length, this.chatHeadViews.length + this.showPerScroll);
    }
  },

  render: function(){
    //remove any old chatHead views
    if (this.chatHeadViews) {
      this.chatHeadViews.forEach((vw) => {
        vw.remove();
      });
    }

    this.chatHeadViews = [];
    this.renderChatHeads(0, this.showPerScroll);
    this.$el.html(this.$headContainer);
    
    setTimeout(() => {
      this.checkIfFilled();
    },0);

    return this;
  },

  checkIfFilled: function(){
    if (this.$headContainer[0].childNodes && this.$headContainer[0].childNodes.length < this.collection.length && this.$chatHeadsContainer[0].clientHeight > this.$headContainer[0].scrollHeight){
      this.onScroll();
      this.checkIfFilled();
    }
  },

  renderChatHeads: function(start, end) {
    var chatsToRender = this.collection.slice(start, end);

    chatsToRender.forEach((md, index) => {
      this.$headContainer.append(
        this.createChatHead(md).render().el
      );
    });
  },

  remove: function(){
    this.scrollHandler && this.$headContainer.off('scroll', this.scrollHandler);

    baseVw.prototype.remove.apply(this, arguments);
  }

});
{'fraction_non_alphanumeric': 0.15569620253164557, 'fraction_numerical': 0.0059674502712477396, 'mean_word_length': 3.0699043414275202, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 17, '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': '7185148', 'n_tokens_mistral': 1795, 'n_tokens_neox': 1738, 'n_words': 287}