<script>
import commonMixin from '@/base/mixins/common.js'
export default {
  name: 'bs-edit',
  render () { },
  mixins: [commonMixin('interaction')],
  props: {
    actived: {
      type: Boolean,
      default: false
    },
    features: {
      type: Object
    }
  },
  watch: {
    actived (flag) {
      if (this.originInstance) {
        if (!flag) {
          this.$rootComponent.removeInteraction(this.originInstance)
        }
      }
    },
    features (val) {
      if (val) {
        this.reload()
      }
    }
  },
  methods: {
    load () {
      const { ol, $rootComponent, actived, features } = this
      if (!features) {
        return
      }
      if (this.originInstance) {
        $rootComponent.removeInteraction(this.originInstance)
      }
      const modify = new ol.interaction.Modify({
        features: new ol.Collection([features])
      })
      this.originInstance = modify
      $rootComponent.addInteraction(modify, actived)
      modify.on('modifyend', (e) => {
        var vector = e.features.item(0)
        const coods = vector.getGeometry().getCoordinates()
        this.$emit('modifyend', coods)
      })
      modify.on('modifystart', (e) => {
        var vector = e.features.item(0)
        const coods = vector.getGeometry().getCoordinates()
        this.$emit('modifystart', coods)
      })
    }
  }
}
</script>
