@zpc_npm/vue-iclient-common
Version:
KQGIS iClient for Vue.js
325 lines (288 loc) • 9.34 kB
JavaScript
/******/ (function() { // webpackBootstrap
/******/ "use strict";
/******/ // The require scope
/******/ var __webpack_require__ = {};
/******/
/************************************************************************/
/******/ /* webpack/runtime/define property getters */
/******/ !function() {
/******/ // define getter functions for harmony exports
/******/ __webpack_require__.d = function(exports, definition) {
/******/ for(var key in definition) {
/******/ if(__webpack_require__.o(definition, key) && !__webpack_require__.o(exports, key)) {
/******/ Object.defineProperty(exports, key, { enumerable: true, get: definition[key] });
/******/ }
/******/ }
/******/ };
/******/ }();
/******/
/******/ /* webpack/runtime/hasOwnProperty shorthand */
/******/ !function() {
/******/ __webpack_require__.o = function(obj, prop) { return Object.prototype.hasOwnProperty.call(obj, prop); }
/******/ }();
/******/
/******/ /* webpack/runtime/make namespace object */
/******/ !function() {
/******/ // define __esModule on exports
/******/ __webpack_require__.r = function(exports) {
/******/ if(typeof Symbol !== 'undefined' && Symbol.toStringTag) {
/******/ Object.defineProperty(exports, Symbol.toStringTag, { value: 'Module' });
/******/ }
/******/ Object.defineProperty(exports, '__esModule', { value: true });
/******/ };
/******/ }();
/******/
/************************************************************************/
var __webpack_exports__ = {};
// ESM COMPAT FLAG
__webpack_require__.r(__webpack_exports__);
// EXPORTS
__webpack_require__.d(__webpack_exports__, {
"default": function() { return /* binding */ global_event; }
});
;// CONCATENATED MODULE: external "@zpc_npm/vue-iclient-common/_utils/util"
var util_namespaceObject = require("@zpc_npm/vue-iclient-common/_utils/util");
;// CONCATENATED MODULE: ./src/common/_types/global-event.js
/**
* Author: ZL
* Date: 2022.6.6
* Description: 全局mapview管理类、全局事件处理类
* 原则:
* 应用组件指定了mapTarget,就绑定了,不会变;
* 如果需要动态绑定就不指定,从存储的地方实时获取.
*
* 必要条件,
* 一、必须在地图初始化前,执行setDefaultMapInfo;
* 二、单个map时,应用组件只需要设置默认的
* 三、多个map时,除了设置默认的,其他的map都必须先执行setWebMap,其中的webMap传null.
* 四、mapview、scenceview必须指定target
*
* 四种情况:
* 一、单个leaflet, 应用组件支持不指定mapTarget,获取当前存储_currentMapTarget的。
* 二、单个cesium, 应用组件支持不指定mapTarget,获取当前存储_currentMapTarget的。
* 三、N个地图公用同一个通用应用组件, 通用应用组件必须不指定mapTarget, 根据当前的_currentMapTarget,切换适配不同的webmap
* 四、N个地图,每个地图一个组件, 应用组件必须指定mapTarget,绑定固定的webmap
*/
/**
* @event changeMapType
*/
/* harmony default export */ var global_event = ({
/**
* 地图逻辑类集合
* @type Map
* @private
*/
_maps: new Map(),
/**
* 当前操作地图的key
* @type string
* @private
*/
_currentMapTarget: "",
/**
* 当前对象事件集合
* @type Map
*/
all: new Map(),
/**
* 设置默认地图key和地图类型。必须保证_maps不是空值,在应用组件先初始化,地图组件后初始化的时候
* @param {string} mapTarget
* @param {string} mapType 地图类型 -leaflet -cesium
*/
setDefaultMapInfo(mapTarget, mapType) {
this.destroy();
this._currentMapTarget = mapTarget;
if (!this._maps.get(mapTarget)) {
this.setWebMap(mapTarget, mapType, null);
}
},
/**
* 添加地图视图到集合
* @param {string} mapTarget
* @param {string} mapType 地图类型 leaflet、cesium
* @param {MapViewViewModel|ScenceViewViewModel} webMap
*/
setWebMap(mapTarget, mapType, webMap = null) {
this._maps.set(mapTarget, {
mapTarget,
webMap,
mapType: mapType.toLowerCase()
});
},
/**
* 获取当前操作地图的target
* @returns {string}
*/
getCurrentMapTarget() {
return this._currentMapTarget;
},
/**
* 获取地图的信息
* @param mapTarget
* @returns {Object}
*/
getMapInfoByMapTarget(mapTarget) {
if (mapTarget) {
return this._maps.get(mapTarget);
} else {
return this._maps.get(this._currentMapTarget);
}
},
/**
* 根据target获取地图视图。webmap可能是空的 是空值时 根据类型给地图视图添加加载监听
* @param {string} mapTarget
* @param {Function} cb
*/
getWebMapByMapTarget(mapTarget, cb) {
if (!mapTarget) {
mapTarget = this._currentMapTarget;
}
let wm = this._maps.get(mapTarget) ? this._maps.get(mapTarget).webMap : null;
if (wm) {
cb(wm);
} else {
const mapType = this.getMapTypeByMapTarget(mapTarget);
let _webmap = null;
if (mapType === "leaflet") {
this.once("load-map", e => {
if (e.eventMapTarget === mapTarget) {
_webmap = this._maps.get(mapTarget).webMap;
if (_webmap) {
cb(_webmap);
}
}
});
} else if (mapType === "cesium") {
this.once("load-viewer", e => {
if (e.eventMapTarget === mapTarget) {
_webmap = this._maps.get(mapTarget).webMap;
if (_webmap) {
cb(_webmap);
}
}
});
}
}
},
/**
* 根据target获取地图类型
* @param {string} mapTarget
* @returns {string|*|null}
*/
getMapTypeByMapTarget(mapTarget) {
if (mapTarget) {
return this._maps.get(mapTarget) ? this._maps.get(mapTarget).mapType : null;
} else {
if (!this._currentMapTarget || this._currentMapTarget === "") {
return this._maps.get(0) ? this._maps.get(0).mapType : "";
} else {
return this._maps.get(this._currentMapTarget) ? this._maps.get(this._currentMapTarget).mapType : "";
}
}
},
/**
* 当前操作地图切换
* 当前方法只会改变当前存储的地图状态和触发改变事件,不会做任何其他操作
* 触发changeMapType事件,会返回切换前的target和当前的target
* @param {string} mapTarget
* @fires changeMapType
*/
changeMapType(mapTarget) {
if (mapTarget === this._currentMapTarget) {
return;
}
const preMapTarget = this._currentMapTarget;
this._currentMapTarget = mapTarget; // 触发切换事件
this.fire("changeMapType", {
preMapTarget,
currentMapTarget: this._currentMapTarget
});
},
/**
* 从集合删除数据
* @param {string} mapTarget
*/
removeWebMap(mapTarget) {
this._maps.delete(mapTarget);
},
/**
* 清空集合
*/
removeALLWebMap() {
this._maps.clear();
},
/**
* 销毁
*/
destroy() {
this.removeALLWebMap();
this.off();
this._currentMapTarget = "";
},
/**
* 注册一个命名的事件处理
* @param {string} type 事件名,官方表示事件名如是 *,用来标记为通用事件,调用任何事件,都会触发命名为 * 的事件
* @param {Function} handler 事件处理函数
*/
on(type, handler) {
// 根据type去查找事件
const handlers = this.all.get(type);
if (handlers) {
for (let i = 0; i < handlers.length; i++) {
// 防止重复监听 事件执行的方法相同时 不添加
if (handlers[i] === handler) {
return;
}
}
handlers.push(handler);
} else {
this.all.set(type, [handler]);
}
},
/**
* 只执行一次 就会被移除
* @param {string} type
* @param {Function} handler
*/
once(type, handler) {
const _handler = (0,util_namespaceObject.bind)(function () {
this.off(type, handler);
this.off(type, _handler);
}, this); // 添加执行事件和移除执行事件的监听
this.on(type, handler);
this.on(type, _handler);
},
/**
* 移除指定的事件处理
* @param {string} type 事件名,和第二个参数一起用来移除指定的事件,
* @param {Function} handler 事件处理函数
*/
off(type, handler) {
if (!type) {
this.all.clear();
return;
} // 根据type去查找事件
const handlers = this.all.get(type); // 如果找到则进行删除操作
if (handlers) {
// 这里用了个骚操作,其实就是找到了,则删除(多个相同的只会删除找到的第一个),没找到则不会对原数组有任何影响
handlers.splice(handlers.indexOf(handler) >>> 0, 1);
}
},
/**
* 触发所有 type 事件,如果有type为 * 的事件,则最后会执行。
* @param {string} type 事件名
* @param {Object} evt 传递给处理函数的参数
*/
fire(type, evt) {
// 找到type的事件循环执行
(this.all.get(type) || []).slice().map(handler => {
handler(evt);
}); // 然后找到所有为*的事件,循环执行
(this.all.get("*") || []).slice().map(handler => {
handler(type, evt);
});
}
});
module.exports = __webpack_exports__;
/******/ })()
;