<commit_before>(function() {
  
  window.dc = window.dc || {};
  dc.embed  = dc.embed || {};
  var notes = dc.embed.notes = dc.embed.notes || {};
  
  var _ = dc._             = window._.noConflict();
  var $ = dc.$ = dc.jQuery = window.jQuery.noConflict(true);
  
  dc.embed.loadNote = function(embedUrl, opts) {
    var options = opts || {}
    var id = parseInt(embedUrl.match(/(\d+).js$/)[1], 10);
    var noteModel = new dc.embed.noteModel(options);
    var el = options.container || '#DC-note-' + id;
    notes[id] = notes[id] || new dc.embed.noteView({model: noteModel, el: el});
    $.getScript(embedUrl);
    
    if (dc.recordHit) dc.embed.pingRemoteUrl('note', id);
  };
  
  dc.embed.noteCallback = function(response) {
    var id   = response.id;
    var note = dc.embed.notes[id];
    note.model.attributes = response;
    note.render();
    if (note.model.options && note.model.options.afterLoad) note.model.options.afterLoad(note);
  };

  dc.embed.pingRemoteUrl = function(type, id) {
    var loc = window.location;
    var url = loc.protocol + '//' + loc.host + loc.pathname;
    if (url.match(/^file:/)) return false;
    url = url.replace(/[\/]+$/, '');
    var hitUrl = dc.recordHit;
    var key    = encodeURIComponent(type + ':' + id + ':' + url);
    $(document).ready( function(){ $(document.body).append('<img alt="" width="1" height="1" src="' + hitUrl + '?key=' + key + '" />'); });
  };
  
  dc.embed.noteModel = function(opts) {
    this.options = opts || {};
  };
  
  dc.embed.noteModel.prototype = {
    get : function(key) { return this.attributes[key]; },
    
    option : function(key) {
      return this.attributes.options[key];
    },
    
    imageUrl : function() {
      return (this._imageUrl = this._imageUrl ||
        this.get('image_url').replace('{size}', 'normal').replace('{page}', this.get('page')));
    },

    coordinates : function() {
      if (this._coordinates) return this._coordinates;
      var loc = this.get('location');
      if (!loc) return null;
      var css = _.map(loc.image.split(','), function(num){ return parseInt(num, 10); });
      return (this._coordinates = {
        top:    css[0],
        left:   css[3],
        right:  css[1],
        height: css[2] - css[0],
        width:  css[1] - css[3]
      });
    },
    
    viewerUrl : function() {
      var suffix = '#document/p' + this.get('page') + '/a' + this.get('id');
      return this.get('published_url') + suffix;
    }
    
  };
  
  dc.embed.noteView = function(options){
    // stolen out of Backbone.View.setElement
    var element = options.el;
    this.$el = element instanceof dc.$ ? element : dc.$(element);
    this.el = this.$el[0];

    this.model = options.model;
  };
  
  dc.embed.noteView.prototype = {
    $: function(selector){ return this.$el.find(selector); },

    render : function() {
      this.$el.html(JST['note_embed']({note : this.model}));
      this.renderedWidth = this.$el.width();
      if (this.renderedWidth < 700) this.center();
      return this.$el;
    },
    
    center : function() {
      var $excerpt       = this.$('.DC-note-excerpt');
      var coords         = this.model.coordinates();
      if (!coords) return;
      var annoCenter     = coords.left + (coords.width / 2);
      var viewportWidth  = $excerpt.closest('.DC-note-excerpt-wrap').width();
      var viewportCenter = viewportWidth / 2;

      if (coords.left + coords.width > viewportWidth) {
        if (coords.width > viewportWidth) {
          $excerpt.css('left', -1 * coords.left);
        } else {
          $excerpt.css('left', viewportCenter - annoCenter);
        }
      }
    }
  };
})();
<commit_msg>Add a resize function to noteViews and trigger it when window is resized (and the note has been rendered).<commit_after>(function() {
  
  window.dc = window.dc || {};
  dc.embed  = dc.embed || {};
  var notes = dc.embed.notes = dc.embed.notes || {};
  
  var _ = dc._             = window._.noConflict();
  var $ = dc.$ = dc.jQuery = window.jQuery.noConflict(true);
  
  var resizeNotes = _.debounce(function(){
    _.each(notes, function(note, id){ if (note.renderedWidth) note.resize(); });
  }, 250);
  
  $(window).resize(resizeNotes);
  
  dc.embed.loadNote = function(embedUrl, opts) {
    var options = opts || {}
    var id = parseInt(embedUrl.match(/(\d+).js$/)[1], 10);
    var noteModel = new dc.embed.noteModel(options);
    var el = options.container || '#DC-note-' + id;
    notes[id] = notes[id] || new dc.embed.noteView({model: noteModel, el: el});
    $.getScript(embedUrl);
    
    if (dc.recordHit) dc.embed.pingRemoteUrl('note', id);
  };
  
  dc.embed.noteCallback = function(response) {
    var id   = response.id;
    var note = dc.embed.notes[id];
    note.model.attributes = response;
    note.render();
    if (note.model.options && note.model.options.afterLoad) note.model.options.afterLoad(note);
  };

  dc.embed.pingRemoteUrl = function(type, id) {
    var loc = window.location;
    var url = loc.protocol + '//' + loc.host + loc.pathname;
    if (url.match(/^file:/)) return false;
    url = url.replace(/[\/]+$/, '');
    var hitUrl = dc.recordHit;
    var key    = encodeURIComponent(type + ':' + id + ':' + url);
    $(document).ready( function(){ $(document.body).append('<img alt="" width="1" height="1" src="' + hitUrl + '?key=' + key + '" />'); });
  };
  
  // Note Model 
  dc.embed.noteModel = function(opts) {
    this.options = opts || {};
  };
  
  dc.embed.noteModel.prototype = {
    get : function(key) { return this.attributes[key]; },
    
    option : function(key) {
      return this.attributes.options[key];
    },
    
    imageUrl : function() {
      return (this._imageUrl = this._imageUrl ||
        this.get('image_url').replace('{size}', 'normal').replace('{page}', this.get('page')));
    },

    coordinates : function() {
      if (this._coordinates) return this._coordinates;
      var loc = this.get('location');
      if (!loc) return null;
      var css = _.map(loc.image.split(','), function(num){ return parseInt(num, 10); });
      return (this._coordinates = {
        top:    css[0],
        left:   css[3],
        right:  css[1],
        height: css[2] - css[0],
        width:  css[1] - css[3]
      });
    },
    
    viewerUrl : function() {
      var suffix = '#document/p' + this.get('page') + '/a' + this.get('id');
      return this.get('published_url') + suffix;
    }
    
  };
  
  // Note View
  dc.embed.noteView = function(options){
    // stolen out of Backbone.View.setElement
    var element = options.el;
    this.$el = element instanceof dc.$ ? element : dc.$(element);
    this.el = this.$el[0];

    this.model = options.model;
  };
  
  dc.embed.noteView.prototype = {
    $: function(selector){ return this.$el.find(selector); },

    render : function() {
      this.$el.html(JST['note_embed']({note : this.model}));
      this.renderedWidth = this.$el.width();
      if (this.renderedWidth < 700) this.center();
      return this.$el;
    },
    
    center : function() {
      var $excerpt       = this.$('.DC-note-excerpt');
      var coords         = this.model.coordinates();
      if (!coords) return;
      var annoCenter     = coords.left + (coords.width / 2);
      var viewportWidth  = $excerpt.closest('.DC-note-excerpt-wrap').width();
      var viewportCenter = viewportWidth / 2;

      if (coords.left + coords.width > viewportWidth) {
        if (coords.width > viewportWidth) {
          $excerpt.css('left', -1 * coords.left);
        } else {
          $excerpt.css('left', viewportCenter - annoCenter);
        }
      }
    },
    
    resize: function() { console.log("Resizing!"); }
  };
})();

{'fraction_non_alphanumeric': 0.18312060432404273, 'fraction_numerical': 0.0058609012763740555, 'mean_word_length': 2.5047923322683707, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 9, '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': '1627911', 'n_tokens_mistral': 2587, 'n_tokens_neox': 2533, 'n_words': 506}