'use strict';

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.defaultMouseCoordinatesPosition = undefined;

var _extends2 = require('babel-runtime/helpers/extends');

var _extends3 = _interopRequireDefault(_extends2);

var _antdv = require('../../../_util/antdv');

var _baseProps = require('../baseProps');

var _baseProps2 = _interopRequireDefault(_baseProps);

var _baseMixin = require('../baseMixin');

var _baseMixin2 = _interopRequireDefault(_baseMixin);

var _round = require('lodash/round');

var _round2 = _interopRequireDefault(_round);

function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { 'default': obj }; }

// 默认MouseCoordinates控件的位置
/**
 * @文件说明: Controls.MouseCoordinates - 鼠标跟随坐标控件
 * @创建人: pjtools@vip.qq.com
 * @创建时间: 2020-05-09 13:44:54
 */

var defaultMouseCoordinatesPosition = exports.defaultMouseCoordinatesPosition = 'bottom-left';

var MouseCoordinates = {
  name: 'PjMap.Controls.MouseCoordinates',
  inheritAttrs: false,
  mixins: [_baseMixin2['default']],
  props: (0, _antdv.initDefaultProps)((0, _extends3['default'])({}, (0, _baseProps2['default'])(), {
    // 坐标值的精度，其中：-1则全精度，0或小于-1则为无精度，其他则裁剪精度
    precision: _antdv.PropTypes.number,
    // 比例尺显示单位
    wgs84: _antdv.PropTypes.bool
  }), {
    position: defaultMouseCoordinatesPosition,
    offset: [15, 0],
    precision: 5,
    wgs84: false
  }),
  data: function data() {
    return {
      // 当前鼠标的坐标对象
      coordinates: [0, 0]
    };
  },

  methods: {
    // 执行地图MouseMove事件执驱动坐标重新计算
    handleMapMouseMove: function handleMapMouseMove(e) {
      var coordinates = null;
      if (e && e.lngLat) {
        coordinates = e.lngLat.toArray();
      } else if (!e) {
        coordinates = this.iMapApi.getCenter();
      }
      if (coordinates) {
        // 判断是否需求转换成WGS84经纬度坐标
        if (this.wgs84) {
          coordinates = this.iMapApi.toWGS84(coordinates);
        }
        // 转换精度
        if (this.precision > 0) {
          coordinates[0] = (0, _round2['default'])(coordinates[0], this.precision);
          coordinates[1] = (0, _round2['default'])(coordinates[1], this.precision);
        } else if (this.precision === 0 || this.precision < -1) {
          coordinates[0] = (0, _round2['default'])(coordinates[0], 0);
          coordinates[1] = (0, _round2['default'])(coordinates[1], 0);
        }
        this.coordinates = coordinates;
      }
    }
  },
  mounted: function mounted() {
    // 联动地图mousemove事件驱动更新鼠标坐标
    this.iMapApi.on('pjmap.controls.mousecoordinates.mousemove', 'mousemove', this.handleMapMouseMove);
    this.handleMapMouseMove();
  },
  beforeDestroy: function beforeDestroy() {
    this.iMapApi.off('pjmap.controls.mousecoordinates.mousemove');
  },
  render: function render() {
    var h = arguments[0];
    var id = this.id,
        classes = this.classes,
        ctrlStyles = this.ctrlStyles,
        coordinates = this.coordinates;


    return h(
      'div',
      {
        attrs: { 'data-id': id },
        'class': classes, style: ctrlStyles },
      [h(
        'div',
        { 'class': 'coordinates-text' },
        [coordinates[0] + ', ' + coordinates[1]]
      )]
    );
  }
};

exports['default'] = MouseCoordinates;