sigma
Version:
A JavaScript library aimed at visualizing graphs of thousands of nodes and edges.
135 lines (134 loc) • 4.77 kB
JavaScript
;
var __extends = (this && this.__extends) || (function () {
var extendStatics = function (d, b) {
extendStatics = Object.setPrototypeOf ||
({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) ||
function (d, b) { for (var p in b) if (Object.prototype.hasOwnProperty.call(b, p)) d[p] = b[p]; };
return extendStatics(d, b);
};
return function (d, b) {
if (typeof b !== "function" && b !== null)
throw new TypeError("Class extends value " + String(b) + " is not a constructor or null");
extendStatics(d, b);
function __() { this.constructor = d; }
d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
};
})();
var __assign = (this && this.__assign) || function () {
__assign = Object.assign || function(t) {
for (var s, i = 1, n = arguments.length; i < n; i++) {
s = arguments[i];
for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p))
t[p] = s[p];
}
return t;
};
return __assign.apply(this, arguments);
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.getWheelDelta = exports.getTouchCoords = exports.getTouchesArray = exports.getWheelCoords = exports.getMouseCoords = exports.getPosition = void 0;
/**
* Sigma.js Captor Class
* ======================
* @module
*/
var types_1 = require("../../types");
/**
* Captor utils functions
* ======================
*/
/**
* Extract the local X and Y coordinates from a mouse event or touch object. If
* a DOM element is given, it uses this element's offset to compute the position
* (this allows using events that are not bound to the container itself and
* still have a proper position).
*
* @param {event} e - A mouse event or touch object.
* @param {HTMLElement} dom - A DOM element to compute offset relatively to.
* @return {number} The local Y value of the mouse.
*/
function getPosition(e, dom) {
var bbox = dom.getBoundingClientRect();
return {
x: e.clientX - bbox.left,
y: e.clientY - bbox.top,
};
}
exports.getPosition = getPosition;
/**
* Convert mouse coords to sigma coords.
*
* @param {event} e - A mouse event or touch object.
* @param {HTMLElement} dom - A DOM element to compute offset relatively to.
* @return {object}
*/
function getMouseCoords(e, dom) {
var res = __assign(__assign({}, getPosition(e, dom)), { sigmaDefaultPrevented: false, preventSigmaDefault: function () {
res.sigmaDefaultPrevented = true;
}, original: e });
return res;
}
exports.getMouseCoords = getMouseCoords;
/**
* Convert mouse wheel event coords to sigma coords.
*
* @param {event} e - A wheel mouse event.
* @param {HTMLElement} dom - A DOM element to compute offset relatively to.
* @return {object}
*/
function getWheelCoords(e, dom) {
return __assign(__assign({}, getMouseCoords(e, dom)), { delta: getWheelDelta(e) });
}
exports.getWheelCoords = getWheelCoords;
var MAX_TOUCHES = 2;
function getTouchesArray(touches) {
var arr = [];
for (var i = 0, l = Math.min(touches.length, MAX_TOUCHES); i < l; i++)
arr.push(touches[i]);
return arr;
}
exports.getTouchesArray = getTouchesArray;
/**
* Convert touch coords to sigma coords.
*
* @param {event} e - A touch event.
* @param {HTMLElement} dom - A DOM element to compute offset relatively to.
* @return {object}
*/
function getTouchCoords(e, dom) {
return {
touches: getTouchesArray(e.touches).map(function (touch) { return getPosition(touch, dom); }),
original: e,
};
}
exports.getTouchCoords = getTouchCoords;
/**
* Extract the wheel delta from a mouse event or touch object.
*
* @param {event} e - A mouse event or touch object.
* @return {number} The wheel delta of the mouse.
*/
function getWheelDelta(e) {
// TODO: check those ratios again to ensure a clean Chrome/Firefox compat
if (typeof e.deltaY !== "undefined")
return (e.deltaY * -3) / 360;
if (typeof e.detail !== "undefined")
return e.detail / -9;
throw new Error("Captor: could not extract delta from event.");
}
exports.getWheelDelta = getWheelDelta;
/**
* Abstract class representing a captor like the user's mouse or touch controls.
*/
var Captor = /** @class */ (function (_super) {
__extends(Captor, _super);
function Captor(container, renderer) {
var _this = _super.call(this) || this;
// Properties
_this.container = container;
_this.renderer = renderer;
return _this;
}
return Captor;
}(types_1.TypedEventEmitter));
exports.default = Captor;