<commit_before>import Ember from 'ember';
import { v1 } from 'ember-uuid';
import layout from './template';
import overlayToFeature from '../../utils/overlay-to-feature';
import MODE from '../../utils/modes';
import featureCenter from '../../utils/feature-center';
import DRAWING_MODE from '../../utils/drawing-modes';
import initMeasureLabel from '../../utils/init-measure-label';
import MapLabel from '../../utils/map-label';
import DynamicLabel from '../../utils/dynamic-label';
import labelPlotter from '../../utils/label-plotter';

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

const {
  on,
  run,
  set,
  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'),
  textGeoJson: computed.alias('markupData.textGeoJson'),
  dm: new google.maps.drawing.DrawingManager({
    drawingControl: false
  }),
  listeners: boundArray(),
  toolListeners: boundArray(),
  currentPoints: boundArray(),
  currentLabel: new MapLabel(undefined, {
    dontScale: true
  }),
  resultsHidden: false,
  activeLayer: undefined,
  drawingMode: DRAWING_MODE.pan.id,
  modes: [
    MODE.draw,
    MODE.measure
  ],
  drawingModes: boundArray([
    DRAWING_MODE.pan,
    DRAWING_MODE.text,
    DRAWING_MODE.marker,
    DRAWING_MODE.polyline,
    DRAWING_MODE.circle,
    DRAWING_MODE.rectangle,
    DRAWING_MODE.polygon
  ]),
  measureModes: boundArray([
    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', run.bind(this, function () {
        Ember.set(popup, 'lastData.editing', false);
        Ember.set(popup, 'lastData', undefined);
        // cleanup?
      }));

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

  getTool(id, mode) {
    var toolIds = mode ? this.get((mode === 'draw' ? 'drawing' : mode) + 'Modes') : DRAWING_MODE;
    return Array.isArray(toolIds) ? toolIds.findBy('id', id) : toolIds[id];
  },

  addTextLabel(tool, position) {
    let autoResetToPan = this.get('autoResetToPan');
    let results = this.get('results');
    let mode = this.get('mode');
    let map = this.get('map');
    let style = Ember.copy(tool.style);
    let labelMarker = new DynamicLabel(position, {
      color: style.color,
      autoFocus: true
    });
    let item = {
      mode,
      style,
      isVisible: true,
      type: tool.id,
      feature: labelMarker
    };

    labelMarker.setMap(map);
    results.pushObject(item);
    map.setOptions({ draggableCursor: undefined });
    // TODO: convert to geojson and add to active layer
    // later load during results process
    let feature = this.markerToFeature(item, labelMarker);
    let textGeoJson = this.get('textGeoJson');

    feature.toGeoJson(data => {
      item.geojson = data;
      textGeoJson.pushObject(data);
    });

    if (autoResetToPan) {
      google.maps.event.addListenerOnce(labelMarker, 'focusout', () => {
        run.later(this, function () {
          let freshTool = this.getTool(tool.id);
          labelMarker.color = freshTool.style.color;
          this.send('changeTool', DRAWING_MODE.pan.id);
        }, 250);
      });
    }
  },

  markerToFeature(result, marker) {
    var id = v1();
    var properties = {
      mode: result.mode,
      type: result.type,
      style: result.style,
      isVisible: true
    };
    var feature = new google.maps.Data.Feature({
      geometry: marker.position,
      properties,
      id
    });

    return feature;
  },

  actions: {
    updateOptionValue(tool, prop, value) {
      set(tool, prop, value);
    },<commit_msg>Update text styles for share if after create<commit_after>import Ember from 'ember';
import { v1 } from 'ember-uuid';
import layout from './template';
import overlayToFeature from '../../utils/overlay-to-feature';
import MODE from '../../utils/modes';
import featureCenter from '../../utils/feature-center';
import DRAWING_MODE from '../../utils/drawing-modes';
import initMeasureLabel from '../../utils/init-measure-label';
import MapLabel from '../../utils/map-label';
import DynamicLabel from '../../utils/dynamic-label';
import labelPlotter from '../../utils/label-plotter';

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

const {
  on,
  run,
  set,
  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'),
  textGeoJson: computed.alias('markupData.textGeoJson'),
  dm: new google.maps.drawing.DrawingManager({
    drawingControl: false
  }),
  listeners: boundArray(),
  toolListeners: boundArray(),
  currentPoints: boundArray(),
  currentLabel: new MapLabel(undefined, {
    dontScale: true
  }),
  resultsHidden: false,
  activeLayer: undefined,
  drawingMode: DRAWING_MODE.pan.id,
  modes: [
    MODE.draw,
    MODE.measure
  ],
  drawingModes: boundArray([
    DRAWING_MODE.pan,
    DRAWING_MODE.text,
    DRAWING_MODE.marker,
    DRAWING_MODE.polyline,
    DRAWING_MODE.circle,
    DRAWING_MODE.rectangle,
    DRAWING_MODE.polygon
  ]),
  measureModes: boundArray([
    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', run.bind(this, function () {
        Ember.set(popup, 'lastData.editing', false);
        Ember.set(popup, 'lastData', undefined);
        // cleanup?
      }));

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

  getTool(id, mode) {
    var toolIds = mode ? this.get((mode === 'draw' ? 'drawing' : mode) + 'Modes') : DRAWING_MODE;
    return Array.isArray(toolIds) ? toolIds.findBy('id', id) : toolIds[id];
  },

  addTextLabel(tool, position) {
    let autoResetToPan = this.get('autoResetToPan');
    let results = this.get('results');
    let mode = this.get('mode');
    let map = this.get('map');
    let style = Ember.copy(tool.style);
    let labelMarker = new DynamicLabel(position, {
      color: style.color,
      autoFocus: true
    });
    let item = {
      mode,
      style,
      isVisible: true,
      type: tool.id,
      feature: labelMarker
    };

    labelMarker.setMap(map);
    results.pushObject(item);
    map.setOptions({ draggableCursor: undefined });
    // TODO: convert to geojson and add to active layer
    // later load during results process
    let feature = this.markerToFeature(item, labelMarker);
    let textGeoJson = this.get('textGeoJson');

    feature.toGeoJson(data => {
      item.geojson = data;
      textGeoJson.pushObject(data);
    });

    if (autoResetToPan) {
      google.maps.event.addListenerOnce(labelMarker, 'focusout', () => {
        run.later(this, function () {
          let freshTool = this.getTool(tool.id);
          let freshStyle = Ember.copy(freshTool.style);

          labelMarker.color = freshStyle.color;
          item.style = freshStyle;
          item.geojson.properties.style = freshStyle;
          
          this.send('changeTool', DRAWING_MODE.pan.id);
        }, 250);
      });
    }
  },

  markerToFeature(result, marker) {
    var id = v1();
    var properties = {
      mode: result.mode,
      type: result.type,
      style: result.style,
      isVisible: true
    };
    var feature = new google.maps.Data.Feature({
      geometry: marker.position,
      properties,
      id
    });

    return feature;
  },

  actions: {
    updateOptionValue(tool, prop, value) {
      set(tool, prop, value);
    },
{'fraction_non_alphanumeric': 0.1432146513479695, 'fraction_numerical': 0.0011375270162666364, 'mean_word_length': 3.027485112230875, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 11, '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': '3130914', 'n_tokens_mistral': 2895, 'n_tokens_neox': 2824, 'n_words': 598}