UNPKG

@antv/g2plot

Version:

G2 Plot, a market of plots built with the Grammar of Graphics'

116 lines 4.65 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); var tslib_1 = require("tslib"); var _ = tslib_1.__importStar(require("@antv/util")); function isSameShape(shape1, shape2) { if (shape1 && shape2 && shape1 === shape2) { return true; } return false; } function isPointInBBox(point, bbox) { if (point.x >= bbox.minX && point.x <= bbox.maxX && point.y >= bbox.minY && point.y <= bbox.maxY) { return true; } return false; } var EventController = /** @class */ (function () { function EventController(cfg) { this.plot = cfg.plot; this.canvas = cfg.canvas; this.pixelRatio = this.canvas.get('pixelRatio'); this.eventHandlers = []; } EventController.prototype.bindEvents = function () { this.addEvent(this.canvas, 'mousedown', _.wrapBehavior(this, 'onEvents')); this.addEvent(this.canvas, 'mousemove', _.wrapBehavior(this, 'onMove')); this.addEvent(this.canvas, 'mouseup', _.wrapBehavior(this, 'onEvents')); this.addEvent(this.canvas, 'click', _.wrapBehavior(this, 'onEvents')); this.addEvent(this.canvas, 'dblclick', _.wrapBehavior(this, 'onEvents')); this.addEvent(this.canvas, 'contextmenu', _.wrapBehavior(this, 'onEvents')); this.addEvent(this.canvas, 'wheel', _.wrapBehavior(this, 'onEvents')); }; EventController.prototype.clearEvents = function () { var eventHandlers = this.eventHandlers; _.each(eventHandlers, function (eh) { eh.target.off(eh.type, eh.handler); }); }; EventController.prototype.addEvent = function (target, eventType, handler) { target.on(eventType, handler); this.eventHandlers.push({ target: target, type: eventType, handler: handler }); }; EventController.prototype.onEvents = function (ev) { var eventObj = this.getEventObj(ev); var target = ev.target; // 判断是否拾取到view以外的shape if (target.isShape && !this.isShapeInView(target) && target.name) { this.plot.emit(target.name + ":" + ev.type, ev); } this.plot.emit("" + ev.type, eventObj); // layer事件 var layers = this.plot.getLayers(); if (layers.length > 0) { this.onLayerEvent(layers, eventObj, ev.type); } }; EventController.prototype.onMove = function (ev) { var target = ev.target; var eventObj = this.getEventObj(ev); // shape的mouseenter, mouseleave和mousemove事件 if (target.isShape && !this.isShapeInView(target) && target.name) { this.plot.emit(target.name + ":" + ev.type, eventObj); // mouseleave & mouseenter if (this.lastShape && !isSameShape(target, this.lastShape)) { if (this.lastShape) { this.plot.emit(this.lastShape.name + ":mouseleave", eventObj); } this.plot.emit(target.name + ":mouseenter", eventObj); } this.lastShape = target; } this.plot.emit('mousemove', eventObj); // layer事件 var layers = this.plot.getLayers(); if (layers.length > 0) { this.onLayerEvent(layers, eventObj, 'mousemove'); } }; EventController.prototype.isShapeInView = function (shape) { var groupName = ['frontgroundGroup', 'backgroundGroup', 'panelGroup']; var parent = shape.get('parent'); while (parent) { var parentName = parent.get('name'); if (parentName && _.contains(groupName, parentName)) { return true; } parent = parent.get('parent'); } return false; }; EventController.prototype.getEventObj = function (ev) { var obj = { x: ev.x / this.pixelRatio, y: ev.y / this.pixelRatio, target: ev.target, event: ev.event, }; return obj; }; EventController.prototype.onLayerEvent = function (layers, eventObj, eventName) { var _this = this; _.each(layers, function (layer) { var bbox = layer.getGlobalBBox(); if (isPointInBBox({ x: eventObj.x, y: eventObj.y }, bbox)) { layer.emit("" + eventName, eventObj); var subLayers = layer.layers; if (subLayers.length > 0) { _this.onLayerEvent(subLayers, eventObj, eventName); } } }); }; return EventController; }()); exports.default = EventController; //# sourceMappingURL=event.js.map