<commit_before>(function () {
  "use strict";

  // Utils
  function pointerEventCoordinates(evt) {
    var isTouch = evt.type.indexOf('touch') === 0;
    if (isTouch) {
      return {
        x: evt.changedTouches[0].pageX,
        y: evt.changedTouches[0].pageY
      }
    } else {
      return {
        x: evt.clientX + window.pageXOffset,
        y: evt.clientY + window.pageYOffset
      }
    }
  }

  function pointerMoved(start, end) {
    if (!start || !end) return false;
    return Math.abs(start.x - end.x) > 5 || Math.abs(start.y - end.y) > 5;
  }

  function colorForSentiment(sentiment) {
    switch (parseInt(sentiment, 10)) {
      case -1:
        return "#bf292a";
        break;
      case 1:
        return "#79cb59";
        break;
      default:
        return "#bec7d3";
        break;
    };
  };

  function makePersistedMarker(comment, latlng) {
    return new L.CircleMarker(latlng, {
      stroke: true,
      color: '#fff',
      opacity: 1,
      weight: 2,
      fill: true,
      fillColor: colorForSentiment(comment.get('sentiment')),
      fillOpacity: 1,
      clickable: true
    }).setRadius(comment.get('selected') ? 8 : 6);
  }

  function makePlaceableMarker(comment, latlng) {
    var icon = L.icon({
        iconUrl: '/bundles/nzzmytown/images/marker.png',
        iconSize: [23, 38],
        iconAnchor: [12, 38]
    });

    return new L.Marker(latlng, {
      icon: icon,
      riseOnHover: true,
      draggable: true
    });
  }


  // Class definition
  window.app.views.MapView = Backbone.View.extend({
    events: {
      'mousedown':  'onStartPlaceMarker',
      'mouseup':    'onEndPlaceMarker',
      'touchstart': 'onStartPlaceMarker',
      'touchend':   'onEndPlaceMarker'
    },

    initialize: function() {
      _.bindAll(this, 'initMap', 'onStartPlaceMarker', 'onEndPlaceMarker', 'addMarkerForComment', 'updateMarkerForComment', 'selectMarkerForComment', 'removeMarkerForComment');

      // Keep track of all placed markers
      this.markers = {};

      // Bind event listeners
      this.listenTo(this.model.comments, 'add',    this.addMarkerForComment);
      this.listenTo(this.model.comments, 'remove', this.removeMarkerForComment);
      this.listenTo(this.model.comments, 'change:persisted', this.updateMarkerForComment);
      this.listenTo(this.model.comments, 'change:latitude',  this.updateMarkerForComment);
      this.listenTo(this.model.comments, 'change:longitude', this.updateMarkerForComment);
      this.listenTo(this.model.comments, 'change:selected', this.selectMarkerForComment);
    },

    render: function() {
      _.defer(this.initMap);
      return this;
    },

    initMap: function() {
      var mapCenter = this.model.getLatLng();
      this.map = L.mapbox.map(this.el, app.config.mapboxKey);
      this.map.setView(mapCenter, parseInt(this.model.get('defaultzoom'), 10));

      // Add initial markers
      this.model.comments.forEach(this.addMarkerForComment);
    },

    onStartPlaceMarker: function(evt) {
      this._pointerEventStart = pointerEventCoordinates(evt.originalEvent);
    },

    onEndPlaceMarker: function(evt) {
      var comment = this.model.comments.findNew();
      var pointerEnd = pointerEventCoordinates(evt.originalEvent);

      if (comment && !pointerMoved(this._pointerEventStart, pointerEnd)) {
        // Using a Point instead of mouseEventToLatLng because the latter
        // doesn't work with touch events
        var offset = $(this.map.getContainer()).offset();
        var point = new L.Point(pointerEnd.x - offset.left, pointerEnd.y - offset.top);
        var latlng = this.map.containerPointToLatLng(point);
        comment.setLatLng(latlng);
      }
    },

    addMarkerForComment: function(comment) {
      // If a marker already exists, remove it before adding it again
      if (this.markers[comment.cid]) {
        this.removeMarkerForComment(comment);
      }

      var latlng = comment.getLatLng() || this.map.getCenter();
      var comments = this.model.comments;
      var marker;

      if (comment.isPersisted()) {
        marker = makePersistedMarker(comment, latlng);
        marker.on('click', function(evt) {
          comments.selectComment(comment);
          marker.bringToFront();
        });
      } else {
        marker = makePlaceableMarker(comment, latlng);
        marker.on('move', function(evt) {
          comment.setLatLng(evt.latlng);
        });
      }

      comment.setLatLng(latlng);

      marker.addTo(this.map);
      this.markers[comment.cid] = {
        el: marker,
        selected: comment.get('selected'),
        persisted: comment.isPersisted()
      };
    },

    updateMarkerForComment: function(comment) {
      var marker = this.markers[comment.cid];
      if (!marker) return;

      if (marker.persisted == comment.isPersisted()) {
        marker.el.setLatLng(comment.getLatLng());
      } else {
        this.removeMarkerForComment(comment);
        this.addMarkerForComment(comment);
      }
    },

    selectMarkerForComment: function(comment) {
      var marker = this.markers[comment.cid];
      if (!marker || !marker.persisted) return;
      marker.el.setRadius(comment.get('selected') ? 8 : 6);
    },

    removeMarkerForComment: function(comment) {
      var marker = this.markers[comment.cid];
      this.map.removeLayer(marker.el);
      marker.el.off('click');
      marker.el.off('move');
      delete this.markers[comment.id];
    }
  });
}());
<commit_msg>Pan to selected point on map<commit_after>(function () {
  "use strict";

  // Utils
  function pointerEventCoordinates(evt) {
    var isTouch = evt.type.indexOf('touch') === 0;
    if (isTouch) {
      return {
        x: evt.changedTouches[0].pageX,
        y: evt.changedTouches[0].pageY
      }
    } else {
      return {
        x: evt.clientX + window.pageXOffset,
        y: evt.clientY + window.pageYOffset
      }
    }
  }

  function pointerMoved(start, end) {
    if (!start || !end) return false;
    return Math.abs(start.x - end.x) > 5 || Math.abs(start.y - end.y) > 5;
  }

  function colorForSentiment(sentiment) {
    switch (parseInt(sentiment, 10)) {
      case -1:
        return "#bf292a";
        break;
      case 1:
        return "#79cb59";
        break;
      default:
        return "#bec7d3";
        break;
    };
  };

  function makePersistedMarker(comment, latlng) {
    return new L.CircleMarker(latlng, {
      stroke: true,
      color: '#fff',
      opacity: 1,
      weight: 2,
      fill: true,
      fillColor: colorForSentiment(comment.get('sentiment')),
      fillOpacity: 1,
      clickable: true
    }).setRadius(comment.get('selected') ? 8 : 6);
  }

  function makePlaceableMarker(comment, latlng) {
    var icon = L.icon({
        iconUrl: '/bundles/nzzmytown/images/marker.png',
        iconSize: [23, 38],
        iconAnchor: [12, 38]
    });

    return new L.Marker(latlng, {
      icon: icon,
      riseOnHover: true,
      draggable: true
    });
  }


  // Class definition
  window.app.views.MapView = Backbone.View.extend({
    events: {
      'mousedown':  'onStartPlaceMarker',
      'mouseup':    'onEndPlaceMarker',
      'touchstart': 'onStartPlaceMarker',
      'touchend':   'onEndPlaceMarker'
    },

    initialize: function() {
      _.bindAll(this, 'initMap', 'onStartPlaceMarker', 'onEndPlaceMarker', 'addMarkerForComment', 'updateMarkerForComment', 'selectMarkerForComment', 'removeMarkerForComment');

      // Keep track of all placed markers
      this.markers = {};

      // Bind event listeners
      this.listenTo(this.model.comments, 'add',    this.addMarkerForComment);
      this.listenTo(this.model.comments, 'remove', this.removeMarkerForComment);
      this.listenTo(this.model.comments, 'change:persisted', this.updateMarkerForComment);
      this.listenTo(this.model.comments, 'change:latitude',  this.updateMarkerForComment);
      this.listenTo(this.model.comments, 'change:longitude', this.updateMarkerForComment);
      this.listenTo(this.model.comments, 'change:selected', this.selectMarkerForComment);
    },

    render: function() {
      _.defer(this.initMap);
      return this;
    },

    initMap: function() {
      var mapCenter = this.model.getLatLng();
      this.map = L.mapbox.map(this.el, app.config.mapboxKey);
      this.map.setView(mapCenter, parseInt(this.model.get('defaultzoom'), 10));

      // Add initial markers
      this.model.comments.forEach(this.addMarkerForComment);
    },

    onStartPlaceMarker: function(evt) {
      this._pointerEventStart = pointerEventCoordinates(evt.originalEvent);
    },

    onEndPlaceMarker: function(evt) {
      var comment = this.model.comments.findNew();
      var pointerEnd = pointerEventCoordinates(evt.originalEvent);

      if (comment && !pointerMoved(this._pointerEventStart, pointerEnd)) {
        // Using a Point instead of mouseEventToLatLng because the latter
        // doesn't work with touch events
        var offset = $(this.map.getContainer()).offset();
        var point = new L.Point(pointerEnd.x - offset.left, pointerEnd.y - offset.top);
        var latlng = this.map.containerPointToLatLng(point);
        comment.setLatLng(latlng);
      }
    },

    addMarkerForComment: function(comment) {
      // If a marker already exists, remove it before adding it again
      if (this.markers[comment.cid]) {
        this.removeMarkerForComment(comment);
      }

      var latlng = comment.getLatLng() || this.map.getCenter();
      var comments = this.model.comments;
      var marker;

      if (comment.isPersisted()) {
        marker = makePersistedMarker(comment, latlng);
        marker.on('click', function(evt) {
          comments.selectComment(comment);
          marker.bringToFront();
        });
      } else {
        marker = makePlaceableMarker(comment, latlng);
        marker.on('move', function(evt) {
          comment.setLatLng(evt.latlng);
        });
      }

      comment.setLatLng(latlng);

      marker.addTo(this.map);
      this.markers[comment.cid] = {
        el: marker,
        selected: comment.get('selected'),
        persisted: comment.isPersisted()
      };
    },

    updateMarkerForComment: function(comment) {
      var marker = this.markers[comment.cid];
      if (!marker) return;

      if (marker.persisted == comment.isPersisted()) {
        marker.el.setLatLng(comment.getLatLng());
      } else {
        this.removeMarkerForComment(comment);
        this.addMarkerForComment(comment);
      }
    },

    selectMarkerForComment: function(comment) {
      var marker = this.markers[comment.cid];
      if (!marker || !marker.persisted) return;
      marker.el.setRadius(comment.get('selected') ? 8 : 6);

      if (comment.get('selected')) {
        this.map.panTo(comment.getLatLng(), {
          animate: true,
          duration: 0.3
        })
      }
    },

    removeMarkerForComment: function(comment) {
      var marker = this.markers[comment.cid];
      this.map.removeLayer(marker.el);
      marker.el.off('click');
      marker.el.off('move');
      delete this.markers[comment.id];
    }
  });
}());

{'fraction_non_alphanumeric': 0.1343122241239528, 'fraction_numerical': 0.006485902170975588, 'mean_word_length': 2.6292906178489703, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 7, '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': '1886801', 'n_tokens_mistral': 3518, 'n_tokens_neox': 3440, 'n_words': 630}