@vuemap/vue-amap
Version:
高德地图vue3版本封装
189 lines (186 loc) • 5.94 kB
JavaScript
import { defineComponent, useAttrs, openBlock, createElementBlock } from 'vue';
import { debounce } from 'lodash-es';
import '../../../mixins/index.mjs';
import { useEditor } from '../../../mixins/useEditor.mjs';
import '../../../utils/index.mjs';
import { propsTypes } from './props.mjs';
import { useRegister } from '../../../mixins/useRegister.mjs';
import { isMapInstance, isOverlayGroupInstance, isVectorLayerInstance } from '../../../utils/util.mjs';
import { toLngLat } from '../../../utils/convert-helper.mjs';
var script = /* @__PURE__ */ defineComponent({
...{
name: "ElAmapRectangle",
inheritAttrs: false
},
__name: "Rectangle",
props: propsTypes,
emits: ["init", "update:bounds"],
setup(__props, { expose: __expose, emit: __emit }) {
const props = __props;
const emits = __emit;
let $amapComponent;
let destroying = false;
const { $$getInstance, parentInstance } = useRegister((options, parentComponent) => {
return new Promise((resolve) => {
$amapComponent = new AMap.Rectangle(options);
if (isMapInstance(parentComponent)) {
parentComponent.add($amapComponent);
} else if (isOverlayGroupInstance(parentComponent)) {
parentComponent.addOverlay($amapComponent);
} else if (isVectorLayerInstance(parentComponent)) {
parentComponent.add($amapComponent);
}
bindModelEvents();
resolve($amapComponent);
});
}, {
emits,
converts: {
bounds: (path) => {
return new AMap.Bounds(toLngLat(path[0]), toLngLat(path[1]));
}
},
watchRedirectFn: {
__zIndex(value) {
if ($amapComponent) {
$amapComponent.setOptions({ zIndex: value });
}
},
__strokeColor(value) {
if ($amapComponent) {
$amapComponent.setOptions({ strokeColor: value });
}
},
__strokeOpacity(value) {
if ($amapComponent) {
$amapComponent.setOptions({ strokeOpacity: value });
}
},
__strokeWeight(value) {
if ($amapComponent) {
$amapComponent.setOptions({ strokeWeight: value });
}
},
__fillColor(value) {
if ($amapComponent) {
$amapComponent.setOptions({ fillColor: value });
}
},
__fillOpacity(value) {
if ($amapComponent) {
$amapComponent.setOptions({ fillOpacity: value });
}
},
__strokeStyle(value) {
if ($amapComponent) {
$amapComponent.setOptions({ strokeStyle: value });
}
},
__strokeDasharray(value) {
if ($amapComponent) {
$amapComponent.setOptions({ strokeDasharray: value });
}
},
__editable(flag) {
createEditor().then(() => {
flag ? resetEditor() : editor.close();
});
},
__bounds(bounds) {
if ($amapComponent) {
$amapComponent.setBounds(bounds);
resetEditor();
}
}
},
destroyComponent() {
destroying = true;
if ($amapComponent && (parentInstance == null ? void 0 : parentInstance.$amapComponent)) {
if (editor) {
if (!parentInstance.isDestroy) {
editor.close();
}
editor = null;
}
if (!parentInstance.isDestroy) {
if (isMapInstance(parentInstance.$amapComponent)) {
parentInstance.$amapComponent.remove($amapComponent);
} else if (isOverlayGroupInstance(parentInstance.$amapComponent)) {
parentInstance.$amapComponent.removeOverlay($amapComponent);
} else if (isVectorLayerInstance(parentInstance.$amapComponent)) {
parentInstance.$amapComponent.remove($amapComponent);
}
}
if ($amapComponent.destroy) {
$amapComponent.destroy();
}
$amapComponent = null;
}
}
});
const resetEditor = debounce(() => {
if (editor && props.editable) {
editor.close();
editor.setTarget();
editor.setTarget($amapComponent);
editor.open();
}
}, 50);
const bindModelEvents = () => {
$amapComponent.on("dragend", () => {
emitModel($amapComponent);
});
$amapComponent.on("touchend", () => {
emitModel($amapComponent);
});
};
const emitModel = (target) => {
if (destroying) {
return;
}
const bounds = target.getBounds();
const southWest = bounds.getSouthWest();
const northEast = bounds.getNorthEast();
const paths = [southWest.toArray(), northEast.toArray()];
emits("update:bounds", paths);
};
let editor;
const attrs = useAttrs();
const createEditor = () => {
return new Promise((resolve) => {
if (editor) {
resolve();
} else {
AMap.plugin(["AMap.RectangleEditor"], () => {
editor = new AMap.RectangleEditor(parentInstance == null ? void 0 : parentInstance.$amapComponent, $amapComponent, props.editOptions);
useEditor(editor, attrs);
bindEditorModelEvents();
resolve();
});
}
});
};
const bindEditorModelEvents = () => {
editor.on("addnode", (e) => {
emitModel(e.target);
});
editor.on("adjust", (e) => {
emitModel(e.target);
});
editor.on("removenode", (e) => {
emitModel(e.target);
});
editor.on("add", (e) => {
emitModel(e.target);
});
};
__expose({
$$getInstance
});
return (_ctx, _cache) => {
return openBlock(), createElementBlock("div");
};
}
});
export { script as default };
//# sourceMappingURL=Rectangle.vue2.mjs.map