<commit_before>;(function() {

Editor = function() {}

Editor.prototype = {
  init: function(elem, data) {
    // UI elements
    this.$container      = elem;
    this.$itemsContainer = elem.find('[data-editor-items]');
    this.$title          = elem.find('[data-editor-title]');
    this.$addButton      = elem.find('[data-editor-add]');
    this.$publishButton  = elem.find('[data-editor-publish]');

    this.lastSearch = '';

    // Content item template
    var template = $('#content-template').html();
    this.contentItem = Handlebars.compile(template);

    // Add event listeners
    this.events();

    if (typeof data === 'object') {
      this.deserialize(data);
    }
    else {
      this.addItem();
    }
  },
  events: function() {
    var _this = this;

    // Make each item sortable
    this.$itemsContainer.sortable({
      handle: '[data-editor-handle]',
      placeholder: 'editor__placeholder'
    });

    // Block carriage returns on title editor, blur the field instead
    this.$title.on('keypress', function(event) {
      if (event.which === 13) {
        $(this).blur();
      }
    });

    // Add text or image units when those buttons are clicked
    this.$addButton.click(function() {
      _this.addItem();
    });

    // Delete an item when that button is clicked
    this.$itemsContainer.on('click', '[data-editor-delete]', function() {
      _this.deleteItem($(this));
    });

    // Serialize and save when publish button is clicked
    this.$publishButton.click(function() {
      _this.save();
      return false;
    })
  },
  addItem: function(data) {
    var _this = this
    var $contentItem = $(this.contentItem(data || {}));
    $contentItem
      .appendTo(this.$itemsContainer)
      .hide(0)
      .slideDown(250);
    var gif_searcher = new GIFSearcher($contentItem.find('.gif-searcher'), this);

    this.$container.find('.content-item__body').on('keypress', function(event) {
      if (event.which == '.'.charCodeAt(0) || event.which == '?'.charCodeAt(0) || event.which == "!".charCodeAt(0)) {
        var content = $(this).val();
        var request = $.getJSON('http://guarded-caverns-4613.herokuapp.com/ner', {text: content});
        request.done(function(data) {
          var ul = _this.$container.find('.gif-searcher__suggest > ul');
          ul.empty();
          $.each(data.best_guess, function(index, item) {
                  var list_item = $('<li>'+item+'</li>')
                  ul.append(list_item);
                  list_item.click(function() {
                          gif_searcher.getImages(item)
                      });
              });
        });
      }
    });
  },
  deleteItem: function(elem) {
    elem.closest('.content-item').remove();
  },
  serialize: function() {
    var json = [];
    this.$itemsContainer.find('.content-item').each(function() {
      json.push({
        'title': $(this).find('.content-item__title').val(),
        'image': $(this).find('.gif-searcher__show > img').attr('src'),
        'body': $(this).find('.content-item__body').val()
      });
    });

    return json;
  },
  save: function() {
    return this.serialize();
  },<commit_msg>Fixed suggest with multiple sections<commit_after>;(function() {

Editor = function() {}

Editor.prototype = {
  init: function(elem, data) {
    // UI elements
    this.$container      = elem;
    this.$itemsContainer = elem.find('[data-editor-items]');
    this.$title          = elem.find('[data-editor-title]');
    this.$addButton      = elem.find('[data-editor-add]');
    this.$publishButton  = elem.find('[data-editor-publish]');

    this.lastSearch = '';

    // Content item template
    var template = $('#content-template').html();
    this.contentItem = Handlebars.compile(template);

    // Add event listeners
    this.events();

    if (typeof data === 'object') {
      this.deserialize(data);
    }
    else {
      this.addItem();
    }
  },
  events: function() {
    var _this = this;

    // Make each item sortable
    this.$itemsContainer.sortable({
      handle: '[data-editor-handle]',
      placeholder: 'editor__placeholder'
    });

    // Block carriage returns on title editor, blur the field instead
    this.$title.on('keypress', function(event) {
      if (event.which === 13) {
        $(this).blur();
      }
    });

    // Add text or image units when those buttons are clicked
    this.$addButton.click(function() {
      _this.addItem();
    });

    // Delete an item when that button is clicked
    this.$itemsContainer.on('click', '[data-editor-delete]', function() {
      _this.deleteItem($(this));
    });

    // Serialize and save when publish button is clicked
    this.$publishButton.click(function() {
      _this.save();
      return false;
    })
  },
  addItem: function(data) {
    var _this = this;
    var $contentItem = $(this.contentItem(data || {}));
    $contentItem
      .appendTo(this.$itemsContainer)
      .hide(0)
      .slideDown(250);
    var gif_searcher = new GIFSearcher($contentItem.find('.gif-searcher'), this);

    $contentItem.find('.content-item__body').on('keypress', function(event) {
      if (event.which == '.'.charCodeAt(0) || event.which == '?'.charCodeAt(0) || event.which == "!".charCodeAt(0)) {
        var content = $(this).val();
        var request = $.getJSON('http://guarded-caverns-4613.herokuapp.com/ner', {text: content});
        request.done(function(data) {
          var ul = $contentItem.find('.gif-searcher__suggest > ul');
          ul.empty();
          $.each(data.best_guess, function(index, item) {
                  var list_item = $('<li>'+item+'</li>')
                  ul.append(list_item);
                  list_item.click(function() {
                          gif_searcher.getImages(item)
                      });
              });
        });
      }
    });
  },
  deleteItem: function(elem) {
    elem.closest('.content-item').remove();
  },
  serialize: function() {
    var json = [];
    this.$itemsContainer.find('.content-item').each(function() {
      json.push({
        'title': $(this).find('.content-item__title').val(),
        'image': $(this).find('.gif-searcher__show > img').attr('src'),
        'body': $(this).find('.content-item__body').val()
      });
    });

    return json;
  },
  save: function() {
    return this.serialize();
  },
{'fraction_non_alphanumeric': 0.17689758560833202, 'fraction_numerical': 0.004102887801798958, 'mean_word_length': 2.4093598708983324, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 11, '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': '2247829', 'n_tokens_mistral': 1939, 'n_tokens_neox': 1904, 'n_words': 356}