<commit_before>dc.ui.PageTextEditor = dc.ui.EditorToolbar.extend({

  id : 'edit_page_text_container',

  events : {
    'click .edit_page_text_confirm_input' : 'confirmEditPageText',
    'click .document_page_tile_remove'    : 'resetPage'
  },

  originalPageText: {},
  pageText: {},

  initialize : function(opts) {
    _.bindAll(this, 'confirmEditPageText', 'cachePageText', 'resetPage');
  },

  findSelectors : function() {
    this.$s = {
      guide : $('#edit_page_text_guide'),
      guideButton: $('.edit_page_text.button'),
      page : $('.DV-text'),
      textContents : $('.DV-textContents'),
      pages : $('.DV-pages'),
      viewerContainer : $('.DV-docViewer-Container'),
      header : $('#edit_page_text_container'),
      container : null,
      saveButton : $('.edit_page_text_confirm_input', this.el),
      headerTiles : $('.document_page_tiles', this.el)
    };
  },

  open : function() {
    this.findSelectors();
    this.originalPageText = {};
    this.pageText = {};
    this.setMode('is', 'open');
    this.render();
    this.viewer.api.enterEditPageTextMode();
  },

  render : function() {
    $(this.el).html(JST['edit_page_text']({}));
    this.$s.viewerContainer.append(this.el);
    if (this.viewer.state != 'ViewText') {
        this.viewer.open('ViewText');
    }
    this.$s.pages.addClass('edit_page_text_viewer');
    this.$s.container = $(this.el);
    this.findSelectors();
    this.$s.guideButton.addClass('open');
    this.$s.guide.fadeIn('fast');
    this.$s.saveButton.setMode('not', 'enabled');
    this.$s.header.removeClass('active');
    this.$s.textContents.attr('contentEditable', true);
    this.$s.textContents.addClass('DV-editing');

    this.handleEvents();
  },

  handleEvents : function() {
    this.$s.textContents.bind('keyup', this.cachePageText);
    this.$s.textContents.bind('change', this.cachePageText);
  },

  getPageNumber : function() {
    return this.viewer.api.currentPage();
  },

  getPageText : function(pageNumber) {
    pageNumber = pageNumber || this.getPageNumber();

    return this.viewer.api.getPageText(pageNumber);
  },

  confirmEditPageText : function() {
    var modifiedPages = this.getChangedPageTextPages();
    var documentId = this.viewer.api.getModelId();
    var dialog = dc.ui.Dialog.progress("Saving page text&hellip;");

    $.ajax({
      url       : '/documents/' + documentId + '/save_page_text',
      type      : 'POST',
      data      : { modified_pages : JSON.stringify(modifiedPages) },
      dataType  : 'json',
      success   : _.bind(function(resp) {
        this.viewer.api.resetPageText(true);
        this.close();
        dialog.close();
      }, this)
    });
  },

  cachePageText : function() {
    var pageNumber = this.getPageNumber();
    var pageText = Inflector.trim(this.extractText(this.$s.textContents));

    if (!(pageNumber in this.originalPageText)) {
      this.originalPageText[pageNumber] = $.trim(this.getPageText(pageNumber));
    }

    if (pageText != this.originalPageText[pageNumber]) {
      this.pageText[pageNumber] = pageText;
    } else {
      delete this.originalPageText[pageNumber];
      delete this.pageText[pageNumber];
    }

    this.viewer.api.setPageText(pageText, pageNumber);
    this.redrawHeader();
  },

  resetPage : function(e) {
    var pageNumber = $(e.currentTarget).parents('.document_page_tile').data('pageNumber');

    this.viewer.api.setPageText(this.originalPageText[pageNumber], pageNumber);
    this.viewer.api.enterEditPageTextMode();
    delete this.originalPageText[pageNumber];
    delete this.pageText[pageNumber];
    this.redrawHeader();
  },

  redrawHeader : function() {
    var saveText;
    var editedPages = _.keys(this.originalPageText);
    var pageCount = editedPages.length;
    editedPages = editedPages.sort(function(a, b) { return a - b; });
    $('.document_page_tile', this.$s.headerTiles).empty().remove();

    if (pageCount == 0) {
      this.$s.header.removeClass('active');
      this.$s.saveButton.setMode('not', 'enabled');
    } else {
      this.$s.header.addClass('active');
      this.$s.saveButton.setMode('is', 'enabled');
    }

    // Create each page tile and add it to the page holder
    _.each(editedPages, _.bind(function(pageNumber) {
      var url = this.imageUrl;
      url = url.replace(/\{size\}/, 'thumbnail');
      url = url.replace(/\{page\}/, pageNumber);
      var $thumbnail = $(JST['document_page_tile']({
        url : url,
        pageNumber : pageNumber
      }));
      $thumbnail.data('pageNumber', pageNumber);
      this.$s.headerTiles.append($thumbnail);
    }, this));

    // Update remove button's text
    if (pageCount == 0) {
      saveText = 'Save page text';
    } else {
      saveText = 'Save ' + pageCount + Inflector.pluralize(' page', pageCount);
    }
    $('.edit_page_text_confirm_input', this.el).val(saveText);

    // Set width of container for side-scrolling
    var width = $('.document_page_tile').length * $('.document_page_tile').eq(0).outerWidth(true);
    var confirmWidth = $('.remove_pages_confirm', this.el).outerWidth(true);
    this.$s.headerTiles.width(width + confirmWidth);
    Backbone.View.prototype.delegateEvents.call(this);
  },

  extractText : function(elems) {
    var ret = "", elem;

    _.each(elems, _.bind(function(elem) {
      if (elem.nodeType == 3 || elem.nodeType == 4) {
        // Get the text from text nodes and CDATA nodes
        ret += elem.nodeValue;
      } else if (elem.nodeType != 8) {
        // Traverse everything else, except comment nodes
        ret += this.extractText(elem.childNodes) + '\n';
      }
    }, this));

    return ret;
  },

  getChangedPageTextPages : function() {
    var modifiedPages = {};
    _.each(this.pageText, _.bind(function(pageText, pageNumber) {
      if (this.originalPageText[pageNumber] != pageText) {
        modifiedPages[pageNumber] = pageText;
      }
    }, this));

    return modifiedPages;<commit_msg>remove extractText()<commit_after>dc.ui.PageTextEditor = dc.ui.EditorToolbar.extend({

  id : 'edit_page_text_container',

  events : {
    'click .edit_page_text_confirm_input' : 'confirmEditPageText',
    'click .document_page_tile_remove'    : 'resetPage'
  },

  originalPageText: {},
  pageText: {},

  initialize : function(opts) {
    _.bindAll(this, 'confirmEditPageText', 'cachePageText', 'resetPage');
  },

  findSelectors : function() {
    this.$s = {
      guide : $('#edit_page_text_guide'),
      guideButton: $('.edit_page_text.button'),
      page : $('.DV-text'),
      textContents : $('.DV-textContents'),
      pages : $('.DV-pages'),
      viewerContainer : $('.DV-docViewer-Container'),
      header : $('#edit_page_text_container'),
      container : null,
      saveButton : $('.edit_page_text_confirm_input', this.el),
      headerTiles : $('.document_page_tiles', this.el)
    };
  },

  open : function() {
    this.findSelectors();
    this.originalPageText = {};
    this.pageText = {};
    this.setMode('is', 'open');
    this.render();
    this.viewer.api.enterEditPageTextMode();
  },

  render : function() {
    $(this.el).html(JST['edit_page_text']({}));
    this.$s.viewerContainer.append(this.el);
    if (this.viewer.state != 'ViewText') {
        this.viewer.open('ViewText');
    }
    this.$s.pages.addClass('edit_page_text_viewer');
    this.$s.container = $(this.el);
    this.findSelectors();
    this.$s.guideButton.addClass('open');
    this.$s.guide.fadeIn('fast');
    this.$s.saveButton.setMode('not', 'enabled');
    this.$s.header.removeClass('active');
    this.$s.textContents.attr('contentEditable', true);
    this.$s.textContents.addClass('DV-editing');

    this.handleEvents();
  },

  handleEvents : function() {
    this.$s.textContents.bind('keyup', this.cachePageText);
    this.$s.textContents.bind('change', this.cachePageText);
  },

  getPageNumber : function() {
    return this.viewer.api.currentPage();
  },

  getPageText : function(pageNumber) {
    pageNumber = pageNumber || this.getPageNumber();

    return this.viewer.api.getPageText(pageNumber);
  },

  confirmEditPageText : function() {
    var modifiedPages = this.getChangedPageTextPages();
    var documentId = this.viewer.api.getModelId();
    var dialog = dc.ui.Dialog.progress("Saving page text&hellip;");

    $.ajax({
      url       : '/documents/' + documentId + '/save_page_text',
      type      : 'POST',
      data      : { modified_pages : JSON.stringify(modifiedPages) },
      dataType  : 'json',
      success   : _.bind(function(resp) {
        this.viewer.api.resetPageText(true);
        this.close();
        dialog.close();
      }, this)
    });
  },

  cachePageText : function() {
    var pageNumber = this.getPageNumber();
    var pageText = Inflector.trim(this.$s.textContents.textWithNewlines());

    if (!(pageNumber in this.originalPageText)) {
      this.originalPageText[pageNumber] = $.trim(this.getPageText(pageNumber));
    }

    if (pageText != this.originalPageText[pageNumber]) {
      this.pageText[pageNumber] = pageText;
    } else {
      delete this.originalPageText[pageNumber];
      delete this.pageText[pageNumber];
    }

    this.viewer.api.setPageText(pageText, pageNumber);
    this.redrawHeader();
  },

  resetPage : function(e) {
    var pageNumber = $(e.currentTarget).parents('.document_page_tile').data('pageNumber');

    this.viewer.api.setPageText(this.originalPageText[pageNumber], pageNumber);
    this.viewer.api.enterEditPageTextMode();
    delete this.originalPageText[pageNumber];
    delete this.pageText[pageNumber];
    this.redrawHeader();
  },

  redrawHeader : function() {
    var saveText;
    var editedPages = _.keys(this.originalPageText);
    var pageCount = editedPages.length;
    editedPages = editedPages.sort(function(a, b) { return a - b; });
    $('.document_page_tile', this.$s.headerTiles).empty().remove();

    if (pageCount == 0) {
      this.$s.header.removeClass('active');
      this.$s.saveButton.setMode('not', 'enabled');
    } else {
      this.$s.header.addClass('active');
      this.$s.saveButton.setMode('is', 'enabled');
    }

    // Create each page tile and add it to the page holder
    _.each(editedPages, _.bind(function(pageNumber) {
      var url = this.imageUrl;
      url = url.replace(/\{size\}/, 'thumbnail');
      url = url.replace(/\{page\}/, pageNumber);
      var $thumbnail = $(JST['document_page_tile']({
        url : url,
        pageNumber : pageNumber
      }));
      $thumbnail.data('pageNumber', pageNumber);
      this.$s.headerTiles.append($thumbnail);
    }, this));

    // Update remove button's text
    if (pageCount == 0) {
      saveText = 'Save page text';
    } else {
      saveText = 'Save ' + pageCount + Inflector.pluralize(' page', pageCount);
    }
    $('.edit_page_text_confirm_input', this.el).val(saveText);

    // Set width of container for side-scrolling
    var width = $('.document_page_tile').length * $('.document_page_tile').eq(0).outerWidth(true);
    var confirmWidth = $('.remove_pages_confirm', this.el).outerWidth(true);
    this.$s.headerTiles.width(width + confirmWidth);
    Backbone.View.prototype.delegateEvents.call(this);
  },

  getChangedPageTextPages : function() {
    var modifiedPages = {};
    _.each(this.pageText, _.bind(function(pageText, pageNumber) {
      if (this.originalPageText[pageNumber] != pageText) {
        modifiedPages[pageNumber] = pageText;
      }
    }, this));

    return modifiedPages;
{'fraction_non_alphanumeric': 0.1624542443785951, 'fraction_numerical': 0.0007843820812271222, 'mean_word_length': 3.2203015814637737, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, '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': '2970570', 'n_tokens_mistral': 3685, 'n_tokens_neox': 3589, 'n_words': 589}