<commit_before>"use strict";

var _ = require('underscore');
var util = require('substance-util');
var html = util.html;
var Node = require("../node");

// Substance.Image.View
// ==========================================================================

var ImageView = function(node) {
  Node.View.call(this, node);


  this.$el.addClass('image');
  this.$el.attr('id', this.node.id);
};

ImageView.Prototype = function() {

  // Rendering
  // =============================
  //

  this.render = function() {
    this.$el.html(html.tpl('image', this.node));
    return this;
  };

  this.dispose = function() {
    console.log('disposing image view');
    this.stopListening();
  };

  this.delete = function(pos, length) {
    var content = this.$('.content')[0];
    var spans = content.childNodes;
    for (var i = length - 1; i >= 0; i--) {
      content.removeChild(spans[pos+i]);
    }
  };
};

ImageView.Prototype.prototype = Node.View.prototype;
ImageView.prototype = new ImageView.Prototype();

module.exports = ImageView;<commit_msg>Retrieve correct Substance.Document and DOM positions.<commit_after>"use strict";

var _ = require('underscore');
var util = require('substance-util');
var html = util.html;
var Node = require("../node");

// Substance.Image.View
// ==========================================================================

var ImageView = function(node) {
  Node.View.call(this, node);


  this.$el.addClass('image');
  this.$el.attr('id', this.node.id);
};

ImageView.Prototype = function() {

  // Rendering
  // =============================
  //

  var _indexOf = Array.prototype.indexOf;

  this.render = function() {
    this.$el.html(html.tpl('image', this.node));

    // Keep the position of the
    var imgChar = this.el.querySelector(".image-char");
    var img = this.el.querySelector("img");
    this._imgPos = _indexOf.call(imgChar.childNodes, img);

    return this;
  };

  this.dispose = function() {
    console.log('disposing image view');
    this.stopListening();
  };

  this.delete = function(pos, length) {
    var content = this.$('.content')[0];
    var spans = content.childNodes;
    for (var i = length - 1; i >= 0; i--) {
      content.removeChild(spans[pos+i]);
    }
  };

  this.getCharPosition = function(el, offset) {
    // TODO: is there a more general approach? this is kind of manually coded.
    if (!$(el).is("div.image-char")) {
      throw new Error("Ooops. Expecting div.image-char as source element for looking up the char pos");
    }
    return (offset > this._imgPos) ? 1 : 0;
  };

  this.getDOMPosition = function(charPos) {
    var content = this.$('.content')[0];
    var img = content.querySelector("img");
    var range = document.createRange();

    if (charPos === 0) {
      range.setStartBefore(content.childNodes[0]);
    } else {
      range.setStartAfter(content);
    }

    return range;
  };
};

ImageView.Prototype.prototype = Node.View.prototype;
ImageView.prototype = new ImageView.Prototype();

module.exports = ImageView;
{'fraction_non_alphanumeric': 0.2262652993714853, 'fraction_numerical': 0.0036387694343367515, 'mean_word_length': 3.48, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 6, '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': '7117636', 'n_tokens_mistral': 954, 'n_tokens_neox': 914, 'n_words': 201}