<commit_before>import Ember from 'ember';
import layout from './template';
import MapLabel from '../../utils/map-label';
import overlayToFeature from '../../utils/overlay-to-feature';
import MODE from '../../utils/modes';
import DRAWING_MODE from '../../utils/drawing-modes';
import featureCenter from '../../utils/feature-center';

if (!window.google) {
  throw new Error('Sorry, but `google` defined globally is required for this addon');
}

const {
  on,
  run,
  inject,
  computed,
  A: boundArray,
  observer: observes
} = Ember;

export default Ember.Component.extend({
  // Start Attrs
  editable: true,
  panForOffscreen: true,
  autoResetToPan: false,
  map: computed.alias('markupData.map'),
  // End Attrs

  layout: layout,
  markupData: inject.service(),
  classNames: ['knownasilya--google-maps-markup'],
  dataLayers: computed.alias('markupData.layers'),
  results: computed.alias('markupData.results'),
  mode: computed.alias('markupData.mode'),
  dm: new google.maps.drawing.DrawingManager({
    drawingControl: false
  }),
  listeners: boundArray(),
  resultsHidden: false,
  activeLayer: undefined,
  drawingMode: DRAWING_MODE.pan.id,
  modes: [
    MODE.draw,
    MODE.measure
  ],
  drawingModes: [
    DRAWING_MODE.pan,
    DRAWING_MODE.marker,
    DRAWING_MODE.polyline,
    DRAWING_MODE.circle,
    DRAWING_MODE.rectangle,
    DRAWING_MODE.polygon
  ],
  measureModes: [
    DRAWING_MODE.pan,
    DRAWING_MODE.polyline,
    DRAWING_MODE.circle,
    DRAWING_MODE.rectangle,
    DRAWING_MODE.polygon
  ],

  initPopupEvents: on('init', function () {
    var editable = this.get('editable');
    if (editable) {
      let popup = new google.maps.InfoWindow();

      popup.setContent(`<div id='google-maps-markup-infowindow'></div>`);

      popup.addListener('closeclick', Ember.run.bind(this, function () {
        Ember.set(popup, 'lastData.editing', false);
        Ember.set(popup, 'lastData', undefined);
        // cleanup?
      }));

      this.set('markupEditPopup', popup);
    }
  }),

  getTool(id) {
    return DRAWING_MODE[id];
  },

  actions: {
    changeMode(mode) {
      this.set('mode', mode.id);
    },

    changeDrawingMode(mode) {
      var activeLayer = this.get('activeLayer');
      var dm = this.get('dm');
      var tool = this.getTool(mode);

      this.resetAllLayers();

      if (activeLayer) {
        if (tool.id === 'pan') {
          activeLayer.data.setDrawingMode(null);
          dm.setDrawingMode(null);
        } else if (tool.dataId) {
          activeLayer.data.setDrawingMode(tool.dataId);
        } else if (tool.dmId) {
          let map = this.get('map');

          dm.setDrawingMode(tool.dmId);
          dm.setMap(map);
        }
      }

      this.set('drawingMode', mode);
    },

    toggleResults() {
      var isHidden = this.toggleProperty('resultsHidden');
      var activeLayer = this.get('activeLayer');
      var map = this.get('map');

      if (!isHidden) {
        activeLayer.data.setMap(map);
      } else {
        activeLayer.data.setMap(null);
      }

      activeLayer.isHidden = isHidden;
    },

    clearResults() {
      if (confirm('This cannot be undone, are you sure you want to clear all markup for this mode?')) {
        let layer = this.get('activeLayer');

        layer.data.forEach((feature) => {
          layer.data.remove(feature);
        });

        this.get('results').clear();

        if (this.get('afterClearResults')) {
          this.sendAction('afterClearResults', layer);
        }
      }
    },

    removeResult(result) {
      var results = this.get('results');
      var layer = this.get('activeLayer');

      layer.data.remove(result.feature);

      this.set('results', results.without(result));
    },

    toggleResult(result) {
      var layer = this.get('activeLayer');

      if (layer.data.contains(result.feature)) {
        Ember.set(result, 'isVisible', false);
        result.feature.setProperty('isVisible', false);
        layer.data.remove(result.feature);
      } else {
        Ember.set(result, 'isVisible', true);
        result.feature.setProperty('isVisible', true);
        layer.data.add(result.feature);
      }
    },

    editResult(data, wormhole) {
      var popup = this.get('markupEditPopup');
      var map = this.get('map');
      var editable = this.get('editable');
<commit_msg>Cleanup measure labels on result remove<commit_after>import Ember from 'ember';
import layout from './template';
import MapLabel from '../../utils/map-label';
import overlayToFeature from '../../utils/overlay-to-feature';
import MODE from '../../utils/modes';
import DRAWING_MODE from '../../utils/drawing-modes';
import featureCenter from '../../utils/feature-center';

if (!window.google) {
  throw new Error('Sorry, but `google` defined globally is required for this addon');
}

const {
  on,
  run,
  inject,
  computed,
  A: boundArray,
  observer: observes
} = Ember;

export default Ember.Component.extend({
  // Start Attrs
  editable: true,
  panForOffscreen: true,
  autoResetToPan: false,
  map: computed.alias('markupData.map'),
  // End Attrs

  layout: layout,
  markupData: inject.service(),
  classNames: ['knownasilya--google-maps-markup'],
  dataLayers: computed.alias('markupData.layers'),
  results: computed.alias('markupData.results'),
  mode: computed.alias('markupData.mode'),
  dm: new google.maps.drawing.DrawingManager({
    drawingControl: false
  }),
  listeners: boundArray(),
  resultsHidden: false,
  activeLayer: undefined,
  drawingMode: DRAWING_MODE.pan.id,
  modes: [
    MODE.draw,
    MODE.measure
  ],
  drawingModes: [
    DRAWING_MODE.pan,
    DRAWING_MODE.marker,
    DRAWING_MODE.polyline,
    DRAWING_MODE.circle,
    DRAWING_MODE.rectangle,
    DRAWING_MODE.polygon
  ],
  measureModes: [
    DRAWING_MODE.pan,
    DRAWING_MODE.polyline,
    DRAWING_MODE.circle,
    DRAWING_MODE.rectangle,
    DRAWING_MODE.polygon
  ],

  initPopupEvents: on('init', function () {
    var editable = this.get('editable');
    if (editable) {
      let popup = new google.maps.InfoWindow();

      popup.setContent(`<div id='google-maps-markup-infowindow'></div>`);

      popup.addListener('closeclick', Ember.run.bind(this, function () {
        Ember.set(popup, 'lastData.editing', false);
        Ember.set(popup, 'lastData', undefined);
        // cleanup?
      }));

      this.set('markupEditPopup', popup);
    }
  }),

  getTool(id) {
    return DRAWING_MODE[id];
  },

  actions: {
    changeMode(mode) {
      this.set('mode', mode.id);
    },

    changeDrawingMode(mode) {
      var activeLayer = this.get('activeLayer');
      var dm = this.get('dm');
      var tool = this.getTool(mode);

      this.resetAllLayers();

      if (activeLayer) {
        if (tool.id === 'pan') {
          activeLayer.data.setDrawingMode(null);
          dm.setDrawingMode(null);
        } else if (tool.dataId) {
          activeLayer.data.setDrawingMode(tool.dataId);
        } else if (tool.dmId) {
          let map = this.get('map');

          dm.setDrawingMode(tool.dmId);
          dm.setMap(map);
        }
      }

      this.set('drawingMode', mode);
    },

    toggleResults() {
      var isHidden = this.toggleProperty('resultsHidden');
      var activeLayer = this.get('activeLayer');
      var map = this.get('map');

      if (!isHidden) {
        activeLayer.data.setMap(map);
      } else {
        activeLayer.data.setMap(null);
      }

      activeLayer.isHidden = isHidden;
    },

    clearResults() {
      if (confirm('This cannot be undone, are you sure you want to clear all markup for this mode?')) {
        let layer = this.get('activeLayer');
        let results = this.get('results');

        layer.data.forEach((feature) => {
          layer.data.remove(feature);
        });

        results.forEach(result => {
          result.label.onRemove();
        });
        results.clear();

        if (this.get('afterClearResults')) {
          this.sendAction('afterClearResults', layer);
        }
      }
    },

    removeResult(result) {
      var results = this.get('results');
      var layer = this.get('activeLayer');

      layer.data.remove(result.feature);
      result.label.onRemove();

      results.removeObject(result);
    },

    toggleResult(result) {
      var layer = this.get('activeLayer');

      if (layer.data.contains(result.feature)) {
        Ember.set(result, 'isVisible', false);
        result.feature.setProperty('isVisible', false);
        layer.data.remove(result.feature);
      } else {
        Ember.set(result, 'isVisible', true);
        result.feature.setProperty('isVisible', true);
        layer.data.add(result.feature);
      }
    },

    editResult(data, wormhole) {
      var popup = this.get('markupEditPopup');
      var map = this.get('map');
      var editable = this.get('editable');

{'fraction_non_alphanumeric': 0.14824035306099356, 'fraction_numerical': 0.0, 'mean_word_length': 2.690187891440501, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 10, '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': '2972640', 'n_tokens_mistral': 2875, 'n_tokens_neox': 2784, 'n_words': 528}