@zpc_npm/vue-iclient-common
Version:
KQGIS iClient for Vue.js
153 lines (132 loc) • 4.67 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 */ Mitt; }
});
;// 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/_utils/mitt.js
/**
* Author: ZL
* Date: 2022.6.21
* Description: 自定义事件操作类。支持事件的注册、移除、触发。
* vue3自带的事件不支持了。
*/
/**
* 向外暴露的默认函数
* @param 入参为 EventHandlerMap
* @returns 返回一个对象,对象包含属性 all,方法 on,off,emit
*/
class Mitt {
constructor(all) {
/**
* 事件集合
* @type {Map}
*/
this.all = all || new Map();
}
/**
* 注册一个命名的事件处理
* @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} fn
*/
once(type, fn) {
const handler = (0,util_namespaceObject.bind)(() => {
this.off(type, fn);
this.off(type, handler);
}, this); // 添加执行事件的监听
this.on(type, fn); // 添加移除执行事件的监听 执行一次后就执行移除
this.on(type, handler);
}
/**
* 移除指定的事件处理
* @param {string} type 事件名,和第二个参数一起用来移除指定的事件。
* @param {Function} handler 事件处理函数
*/
off(type = undefined, handler = null) {
// 不传就清空所有事件。
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__;
/******/ })()
;