index.js 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392
  1. "use strict";
  2. var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
  3. if (k2 === undefined) k2 = k;
  4. var desc = Object.getOwnPropertyDescriptor(m, k);
  5. if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
  6. desc = { enumerable: true, get: function() { return m[k]; } };
  7. }
  8. Object.defineProperty(o, k2, desc);
  9. }) : (function(o, m, k, k2) {
  10. if (k2 === undefined) k2 = k;
  11. o[k2] = m[k];
  12. }));
  13. var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
  14. Object.defineProperty(o, "default", { enumerable: true, value: v });
  15. }) : function(o, v) {
  16. o["default"] = v;
  17. });
  18. var __importStar = (this && this.__importStar) || function (mod) {
  19. if (mod && mod.__esModule) return mod;
  20. var result = {};
  21. if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
  22. __setModuleDefault(result, mod);
  23. return result;
  24. };
  25. Object.defineProperty(exports, "__esModule", { value: true });
  26. exports.Vector = void 0;
  27. const Dom = __importStar(require("../dom/main"));
  28. class Vector {
  29. get [Symbol.toStringTag]() {
  30. return Vector.toStringTag;
  31. }
  32. get type() {
  33. return this.node.nodeName;
  34. }
  35. get id() {
  36. return this.node.id;
  37. }
  38. set id(id) {
  39. this.node.id = id;
  40. }
  41. constructor(elem, attrs, children) {
  42. if (!elem) {
  43. throw new TypeError('Invalid element to create vector');
  44. }
  45. let node;
  46. if (Vector.isVector(elem)) {
  47. node = elem.node;
  48. }
  49. else if (typeof elem === 'string') {
  50. if (elem.toLowerCase() === 'svg') {
  51. node = Dom.createSvgDocument();
  52. }
  53. else if (elem[0] === '<') {
  54. const doc = Dom.createSvgDocument(elem);
  55. // only import the first child
  56. node = document.importNode(doc.firstChild, true);
  57. }
  58. else {
  59. node = document.createElementNS(Dom.ns.svg, elem);
  60. }
  61. }
  62. else {
  63. node = elem;
  64. }
  65. this.node = node;
  66. if (attrs) {
  67. this.setAttributes(attrs);
  68. }
  69. if (children) {
  70. this.append(children);
  71. }
  72. }
  73. transform(matrix, options) {
  74. if (matrix == null) {
  75. return Dom.transform(this.node);
  76. }
  77. Dom.transform(this.node, matrix, options);
  78. return this;
  79. }
  80. translate(tx, ty = 0, options = {}) {
  81. if (tx == null) {
  82. return Dom.translate(this.node);
  83. }
  84. Dom.translate(this.node, tx, ty, options);
  85. return this;
  86. }
  87. rotate(angle, cx, cy, options = {}) {
  88. if (angle == null) {
  89. return Dom.rotate(this.node);
  90. }
  91. Dom.rotate(this.node, angle, cx, cy, options);
  92. return this;
  93. }
  94. scale(sx, sy) {
  95. if (sx == null) {
  96. return Dom.scale(this.node);
  97. }
  98. Dom.scale(this.node, sx, sy);
  99. return this;
  100. }
  101. /**
  102. * Returns an SVGMatrix that specifies the transformation necessary
  103. * to convert this coordinate system into `target` coordinate system.
  104. */
  105. getTransformToElement(target) {
  106. const ref = Vector.toNode(target);
  107. return Dom.getTransformToElement(this.node, ref);
  108. }
  109. removeAttribute(name) {
  110. Dom.removeAttribute(this.node, name);
  111. return this;
  112. }
  113. getAttribute(name) {
  114. return Dom.getAttribute(this.node, name);
  115. }
  116. setAttribute(name, value) {
  117. Dom.setAttribute(this.node, name, value);
  118. return this;
  119. }
  120. setAttributes(attrs) {
  121. Dom.setAttributes(this.node, attrs);
  122. return this;
  123. }
  124. attr(name, value) {
  125. if (name == null) {
  126. return Dom.attr(this.node);
  127. }
  128. if (typeof name === 'string' && value === undefined) {
  129. return Dom.attr(this.node, name);
  130. }
  131. if (typeof name === 'object') {
  132. Dom.attr(this.node, name);
  133. }
  134. else {
  135. Dom.attr(this.node, name, value);
  136. }
  137. return this;
  138. }
  139. svg() {
  140. return this.node instanceof SVGSVGElement
  141. ? this
  142. : Vector.create(this.node.ownerSVGElement);
  143. }
  144. defs() {
  145. const context = this.svg() || this;
  146. const defsNode = context.node.getElementsByTagName('defs')[0];
  147. if (defsNode) {
  148. return Vector.create(defsNode);
  149. }
  150. return Vector.create('defs').appendTo(context);
  151. }
  152. text(content, options = {}) {
  153. Dom.text(this.node, content, options);
  154. return this;
  155. }
  156. tagName() {
  157. return Dom.tagName(this.node);
  158. }
  159. clone() {
  160. return Vector.create(this.node.cloneNode(true));
  161. }
  162. remove() {
  163. Dom.remove(this.node);
  164. return this;
  165. }
  166. empty() {
  167. Dom.empty(this.node);
  168. return this;
  169. }
  170. append(elems) {
  171. Dom.append(this.node, Vector.toNodes(elems));
  172. return this;
  173. }
  174. appendTo(target) {
  175. Dom.appendTo(this.node, Vector.isVector(target) ? target.node : target);
  176. return this;
  177. }
  178. prepend(elems) {
  179. Dom.prepend(this.node, Vector.toNodes(elems));
  180. return this;
  181. }
  182. before(elems) {
  183. Dom.before(this.node, Vector.toNodes(elems));
  184. return this;
  185. }
  186. replace(elem) {
  187. if (this.node.parentNode) {
  188. this.node.parentNode.replaceChild(Vector.toNode(elem), this.node);
  189. }
  190. return Vector.create(elem);
  191. }
  192. first() {
  193. return this.node.firstChild
  194. ? Vector.create(this.node.firstChild)
  195. : null;
  196. }
  197. last() {
  198. return this.node.lastChild
  199. ? Vector.create(this.node.lastChild)
  200. : null;
  201. }
  202. get(index) {
  203. const child = this.node.childNodes[index];
  204. return child ? Vector.create(child) : null;
  205. }
  206. indexOf(elem) {
  207. const children = Array.prototype.slice.call(this.node.childNodes);
  208. return children.indexOf(Vector.toNode(elem));
  209. }
  210. find(selector) {
  211. const vels = [];
  212. const nodes = Dom.find(this.node, selector);
  213. if (nodes) {
  214. for (let i = 0, ii = nodes.length; i < ii; i += 1) {
  215. vels.push(Vector.create(nodes[i]));
  216. }
  217. }
  218. return vels;
  219. }
  220. findOne(selector) {
  221. const found = Dom.findOne(this.node, selector);
  222. return found ? Vector.create(found) : null;
  223. }
  224. findParentByClass(className, terminator) {
  225. const node = Dom.findParentByClass(this.node, className, terminator);
  226. return node ? Vector.create(node) : null;
  227. }
  228. matches(selector) {
  229. const node = this.node;
  230. const matches = this.node.matches;
  231. const matcher = node.matches ||
  232. node.matchesSelector ||
  233. node.msMatchesSelector ||
  234. node.mozMatchesSelector ||
  235. node.webkitMatchesSelector ||
  236. node.oMatchesSelector ||
  237. null;
  238. return matcher && matcher.call(node, selector);
  239. }
  240. contains(child) {
  241. return Dom.contains(this.node, Vector.isVector(child) ? child.node : child);
  242. }
  243. wrap(node) {
  244. const vel = Vector.create(node);
  245. const parentNode = this.node.parentNode;
  246. if (parentNode != null) {
  247. parentNode.insertBefore(vel.node, this.node);
  248. }
  249. return vel.append(this);
  250. }
  251. parent(type) {
  252. let parent = this; // eslint-disable-line @typescript-eslint/no-this-alias
  253. // check for parent
  254. if (parent.node.parentNode == null) {
  255. return null;
  256. }
  257. // get parent element
  258. parent = Vector.create(parent.node.parentNode);
  259. if (type == null) {
  260. return parent;
  261. }
  262. // loop trough ancestors if type is given
  263. do {
  264. if (typeof type === 'string' ? parent.matches(type) : parent instanceof type) {
  265. return parent;
  266. }
  267. } while ((parent = Vector.create(parent.node.parentNode)));
  268. return parent;
  269. }
  270. children() {
  271. const children = this.node.childNodes;
  272. const vels = [];
  273. for (let i = 0; i < children.length; i += 1) {
  274. const currentChild = children[i];
  275. if (currentChild.nodeType === 1) {
  276. vels.push(Vector.create(children[i]));
  277. }
  278. }
  279. return vels;
  280. }
  281. eachChild(fn, deep) {
  282. const children = this.children();
  283. for (let i = 0, l = children.length; i < l; i += 1) {
  284. fn.call(children[i], children[i], i, children);
  285. if (deep) {
  286. children[i].eachChild(fn, deep);
  287. }
  288. }
  289. return this;
  290. }
  291. index() {
  292. return Dom.index(this.node);
  293. }
  294. hasClass(className) {
  295. return Dom.hasClass(this.node, className);
  296. }
  297. addClass(className) {
  298. Dom.addClass(this.node, className);
  299. return this;
  300. }
  301. removeClass(className) {
  302. Dom.removeClass(this.node, className);
  303. return this;
  304. }
  305. toggleClass(className, stateVal) {
  306. Dom.toggleClass(this.node, className, stateVal);
  307. return this;
  308. }
  309. toLocalPoint(x, y) {
  310. return Dom.toLocalPoint(this.node, x, y);
  311. }
  312. /**
  313. * Samples the underlying SVG element (it currently works only on
  314. * paths - where it is most useful anyway). Returns an array of objects
  315. * of the form `{ x: Number, y: Number, distance: Number }`. Each of these
  316. * objects represent a point on the path. This basically creates a discrete
  317. * representation of the path (which is possible a curve). The sampling
  318. * interval defines the accuracy of the sampling. In other words, we travel
  319. * from the beginning of the path to the end by interval distance (on the
  320. * path, not between the resulting points) and collect the discrete points
  321. * on the path. This is very useful in many situations. For example, SVG
  322. * does not provide a built-in mechanism to find intersections between two
  323. * paths. Using sampling, we can just generate bunch of points for each of
  324. * the path and find the closest ones from each set.
  325. */
  326. sample(interval = 1) {
  327. if (this.node instanceof SVGPathElement) {
  328. return Dom.sample(this.node, interval);
  329. }
  330. return [];
  331. }
  332. toPath() {
  333. return Vector.create(Dom.toPath(this.node));
  334. }
  335. toPathData() {
  336. return Dom.toPathData(this.node);
  337. }
  338. }
  339. exports.Vector = Vector;
  340. (function (Vector) {
  341. Vector.toStringTag = `X6.${Vector.name}`;
  342. function isVector(instance) {
  343. if (instance == null) {
  344. return false;
  345. }
  346. if (instance instanceof Vector) {
  347. return true;
  348. }
  349. const tag = instance[Symbol.toStringTag];
  350. const vector = instance;
  351. if ((tag == null || tag === Vector.toStringTag) &&
  352. vector.node instanceof SVGElement &&
  353. typeof vector.sample === 'function' &&
  354. typeof vector.toPath === 'function') {
  355. return true;
  356. }
  357. return false;
  358. }
  359. Vector.isVector = isVector;
  360. function create(elem, attrs, children) {
  361. return new Vector(elem, attrs, children);
  362. }
  363. Vector.create = create;
  364. function createVectors(markup) {
  365. if (markup[0] === '<') {
  366. const svgDoc = Dom.createSvgDocument(markup);
  367. const vels = [];
  368. for (let i = 0, ii = svgDoc.childNodes.length; i < ii; i += 1) {
  369. const childNode = svgDoc.childNodes[i];
  370. vels.push(create(document.importNode(childNode, true)));
  371. }
  372. return vels;
  373. }
  374. return [create(markup)];
  375. }
  376. Vector.createVectors = createVectors;
  377. function toNode(elem) {
  378. if (isVector(elem)) {
  379. return elem.node;
  380. }
  381. return elem;
  382. }
  383. Vector.toNode = toNode;
  384. function toNodes(elems) {
  385. if (Array.isArray(elems)) {
  386. return elems.map((elem) => toNode(elem));
  387. }
  388. return [toNode(elems)];
  389. }
  390. Vector.toNodes = toNodes;
  391. })(Vector = exports.Vector || (exports.Vector = {}));
  392. //# sourceMappingURL=index.js.map