index.vue 2.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. <template>
  2. <div
  3. class="wk-location"
  4. @mouseenter="hovering = true"
  5. @mouseleave="hovering = false">
  6. <el-input
  7. v-model="dataValue.address"
  8. :disabled="disabled"
  9. readonly
  10. clearable
  11. placeholder="点击定位"
  12. @click.native="inputClick"
  13. @clear="inputClear">
  14. <template slot="suffix">
  15. <i
  16. v-if="dataValue.address && hovering"
  17. class="el-icon-circle-close"
  18. style="cursor: pointer;"
  19. @click.stop="inputClear" />
  20. <i v-else class="wk wk-icon-location" />
  21. </template>
  22. </el-input>
  23. <wk-location-point-dialog
  24. :value="dataValue"
  25. :visible.sync="pointDialogVisible"
  26. @select="pointSelect"
  27. />
  28. </div>
  29. </template>
  30. <script>
  31. import WkLocationPointDialog from '../WkLocationPointDialog'
  32. import { isObject } from '@/utils/types'
  33. import { valueEquals } from 'element-ui/lib/utils/util'
  34. import Emitter from 'element-ui/lib/mixins/emitter'
  35. export default {
  36. // 定位
  37. name: 'WkLocation',
  38. components: {
  39. WkLocationPointDialog
  40. },
  41. mixins: [Emitter],
  42. props: {
  43. // eslint-disable-next-line vue/require-prop-types
  44. value: {
  45. required: true
  46. },
  47. disabled: Boolean
  48. },
  49. data() {
  50. return {
  51. hovering: false,
  52. dataValue: this.getDefaultValue(),
  53. pointDialogVisible: false
  54. }
  55. },
  56. computed: {},
  57. watch: {
  58. value: {
  59. handler(val) {
  60. if (!valueEquals(val, this.dataValue)) {
  61. if (isObject(this.value)) {
  62. this.dataValue = val
  63. } else {
  64. this.dataValue = this.getDefaultValue()
  65. this.$emit('input', this.dataValue)
  66. }
  67. }
  68. },
  69. immediate: true
  70. }
  71. },
  72. created() {
  73. },
  74. mounted() {},
  75. beforeDestroy() {},
  76. methods: {
  77. getDefaultValue() {
  78. return {
  79. lat: '',
  80. lng: '',
  81. address: ''
  82. }
  83. },
  84. inputClick() {
  85. if (!this.disabled) {
  86. this.pointDialogVisible = true
  87. }
  88. },
  89. inputClear() {
  90. this.dataValue = this.getDefaultValue()
  91. this.valueChange()
  92. },
  93. pointSelect(data) {
  94. if (data) {
  95. this.dataValue = {
  96. lat: data.point.lat,
  97. lng: data.point.lng,
  98. address: data.address + data.title
  99. }
  100. }
  101. this.valueChange()
  102. },
  103. valueChange() {
  104. this.$emit('input', this.dataValue)
  105. this.$emit('change', this.dataValue)
  106. this.dispatch('ElFormItem', 'el.form.change', this.dataValue)
  107. }
  108. }
  109. }
  110. </script>
  111. <style lang="scss" scoped>
  112. </style>