@bencamus/tree-chart-core 0.1.0 → 0.1.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/base/uuid.js CHANGED
@@ -1,8 +1,5 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.uuid = uuid;
4
1
  /*this is used for assign an id to the nodes of the tree */
5
- function uuid() {
2
+ export function uuid() {
6
3
  const s = [];
7
4
  const hexDigits = '0123456789abcdef';
8
5
  for (let i = 0; i < 36; i++) {
package/dist/index.js CHANGED
@@ -1,23 +1,4 @@
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 __exportStar = (this && this.__exportStar) || function(m, exports) {
14
- for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
15
- };
16
- var __importDefault = (this && this.__importDefault) || function (mod) {
17
- return (mod && mod.__esModule) ? mod : { "default": mod };
18
- };
19
- Object.defineProperty(exports, "__esModule", { value: true });
20
- const index_1 = __importDefault(require("./tree-chart/index"));
21
- exports.default = index_1.default;
22
- __exportStar(require("./tree-chart/constant"), exports);
23
- __exportStar(require("./tree-chart/tree-chart"), exports);
1
+ import TreeChartCore from "./tree-chart/index";
2
+ export default TreeChartCore;
3
+ export * from './tree-chart/constant';
4
+ export * from './tree-chart/tree-chart';
@@ -1,16 +1,13 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.MATCH_SCALE_REGEX = exports.MATCH_TRANSLATE_REGEX = exports.ANIMATION_DURATION = exports.DEFAULT_HEIGHT_DECREMENT = exports.DEFAULT_LEVEL_HEIGHT = exports.DEFAULT_NODE_HEIGHT = exports.DEFAULT_NODE_WIDTH = void 0;
4
- exports.DEFAULT_NODE_WIDTH = 100;
5
- exports.DEFAULT_NODE_HEIGHT = 100;
6
- exports.DEFAULT_LEVEL_HEIGHT = 200;
1
+ export const DEFAULT_NODE_WIDTH = 100;
2
+ export const DEFAULT_NODE_HEIGHT = 100;
3
+ export const DEFAULT_LEVEL_HEIGHT = 200;
7
4
  /**
8
5
  * Used to decrement the height of the 'initTransformY' to center diagrams.
9
6
  * This is only a hotfix caused by the addition of '__invisible_root' node
10
7
  * for multi root purposes.
11
8
  */
12
- exports.DEFAULT_HEIGHT_DECREMENT = 200;
9
+ export const DEFAULT_HEIGHT_DECREMENT = 200;
13
10
  //animation of the branches of the tree
14
- exports.ANIMATION_DURATION = 10; //orginal 800, 10~>instant
15
- exports.MATCH_TRANSLATE_REGEX = /translate\((-?\d+)px, ?(-?\d+)px\)/i;
16
- exports.MATCH_SCALE_REGEX = /scale\((\S*)\)/i;
11
+ export const ANIMATION_DURATION = 10; //orginal 800, 10~>instant
12
+ export const MATCH_TRANSLATE_REGEX = /translate\((-?\d+)px, ?(-?\d+)px\)/i;
13
+ export const MATCH_SCALE_REGEX = /scale\((\S*)\)/i;
@@ -1,57 +1,22 @@
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 () {
19
- var ownKeys = function(o) {
20
- ownKeys = Object.getOwnPropertyNames || function (o) {
21
- var ar = [];
22
- for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k;
23
- return ar;
24
- };
25
- return ownKeys(o);
26
- };
27
- return function (mod) {
28
- if (mod && mod.__esModule) return mod;
29
- var result = {};
30
- if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]);
31
- __setModuleDefault(result, mod);
32
- return result;
33
- };
34
- })();
35
- Object.defineProperty(exports, "__esModule", { value: true });
36
- const d3 = __importStar(require("d3"));
37
- const constant_1 = require("./constant");
38
- const tree_chart_1 = require("./tree-chart");
39
- const util_1 = require("./util");
40
- class TreeChartCore {
1
+ import * as d3 from "d3";
2
+ import { ANIMATION_DURATION, DEFAULT_HEIGHT_DECREMENT, DEFAULT_LEVEL_HEIGHT, DEFAULT_NODE_HEIGHT, DEFAULT_NODE_WIDTH, MATCH_SCALE_REGEX, MATCH_TRANSLATE_REGEX, } from "./constant";
3
+ import { Direction, TreeLinkStyle } from "./tree-chart";
4
+ import { deepCopy, rotatePoint } from "./util";
5
+ export default class TreeChartCore {
41
6
  constructor(params) {
42
7
  this.interactionMode = null;
43
8
  this.treeConfig = {
44
- nodeWidth: constant_1.DEFAULT_NODE_WIDTH,
45
- nodeHeight: constant_1.DEFAULT_NODE_HEIGHT,
46
- levelHeight: constant_1.DEFAULT_LEVEL_HEIGHT,
9
+ nodeWidth: DEFAULT_NODE_WIDTH,
10
+ nodeHeight: DEFAULT_NODE_HEIGHT,
11
+ levelHeight: DEFAULT_LEVEL_HEIGHT,
47
12
  focusToNode: false,
48
13
  initiallyCollapsed: false,
49
14
  useMobileZoom: false,
50
15
  collapseDepth: 0, // default value
51
16
  useMouseZoom: false,
52
17
  };
53
- this.linkStyle = tree_chart_1.TreeLinkStyle.CURVE;
54
- this.direction = tree_chart_1.Direction.VERTICAL;
18
+ this.linkStyle = TreeLinkStyle.CURVE;
19
+ this.direction = Direction.VERTICAL;
55
20
  this.collapseEnabled = true;
56
21
  this.nodeDataList = [];
57
22
  this.linkDataList = [];
@@ -90,7 +55,7 @@ class TreeChartCore {
90
55
  const zoomFactor = event.deltaY > 0 ? 0.9 : 1.1;
91
56
  //adjust zoom
92
57
  const originTransformStr = this.domElement.style.transform;
93
- const scaleMatchResult = originTransformStr.match(constant_1.MATCH_SCALE_REGEX);
58
+ const scaleMatchResult = originTransformStr.match(MATCH_SCALE_REGEX);
94
59
  const originScale = scaleMatchResult
95
60
  ? parseFloat(scaleMatchResult[1])
96
61
  : 1;
@@ -110,7 +75,7 @@ class TreeChartCore {
110
75
  }
111
76
  adjustZoom(factor) {
112
77
  const originTransformStr = this.domElement.style.transform;
113
- const scaleMatchResult = originTransformStr.match(constant_1.MATCH_SCALE_REGEX);
78
+ const scaleMatchResult = originTransformStr.match(MATCH_SCALE_REGEX);
114
79
  const originScale = scaleMatchResult ? parseFloat(scaleMatchResult[1]) : 1;
115
80
  this.setScale(originScale * factor);
116
81
  }
@@ -189,7 +154,7 @@ class TreeChartCore {
189
154
  }
190
155
  getTranslate() {
191
156
  const string = this.svgElement.style.transform;
192
- const match = string.match(constant_1.MATCH_TRANSLATE_REGEX);
157
+ const match = string.match(MATCH_TRANSLATE_REGEX);
193
158
  if (match === null) {
194
159
  return [null, null];
195
160
  }
@@ -198,17 +163,17 @@ class TreeChartCore {
198
163
  return [x, y];
199
164
  }
200
165
  isVertical() {
201
- return this.direction === tree_chart_1.Direction.VERTICAL;
166
+ return this.direction === Direction.VERTICAL;
202
167
  }
203
168
  /**
204
169
  * 根据link数据,生成svg path data
205
170
  */
206
171
  generateLinkPath(d) {
207
172
  const self = this;
208
- if (this.linkStyle === tree_chart_1.TreeLinkStyle.CURVE) {
173
+ if (this.linkStyle === TreeLinkStyle.CURVE) {
209
174
  return this.generateCurceLinkPath(self, d);
210
175
  }
211
- if (this.linkStyle === tree_chart_1.TreeLinkStyle.STRAIGHT) {
176
+ if (this.linkStyle === TreeLinkStyle.STRAIGHT) {
212
177
  // the link path is: source -> secondPoint -> thirdPoint -> target
213
178
  return this.generateStraightLinkPath(d);
214
179
  }
@@ -233,18 +198,18 @@ class TreeChartCore {
233
198
  x: d.source.x,
234
199
  y: d.source.y,
235
200
  };
236
- return self.direction === tree_chart_1.Direction.VERTICAL
201
+ return self.direction === Direction.VERTICAL
237
202
  ? sourcePoint
238
- : (0, util_1.rotatePoint)(sourcePoint);
203
+ : rotatePoint(sourcePoint);
239
204
  })
240
205
  .target(function (d) {
241
206
  const targetPoint = {
242
207
  x: d.target.x,
243
208
  y: d.target.y,
244
209
  };
245
- return self.direction === tree_chart_1.Direction.VERTICAL
210
+ return self.direction === Direction.VERTICAL
246
211
  ? targetPoint
247
- : (0, util_1.rotatePoint)(targetPoint);
212
+ : rotatePoint(targetPoint);
248
213
  });
249
214
  return linkPath(d);
250
215
  }
@@ -253,8 +218,8 @@ class TreeChartCore {
253
218
  let sourcePoint = { x: d.source.x, y: d.source.y };
254
219
  let targetPoint = { x: d.target.x, y: d.target.y };
255
220
  if (!this.isVertical()) {
256
- sourcePoint = (0, util_1.rotatePoint)(sourcePoint);
257
- targetPoint = (0, util_1.rotatePoint)(targetPoint);
221
+ sourcePoint = rotatePoint(sourcePoint);
222
+ targetPoint = rotatePoint(targetPoint);
258
223
  }
259
224
  const xOffset = targetPoint.x - sourcePoint.x;
260
225
  const yOffset = targetPoint.y - sourcePoint.y;
@@ -324,7 +289,7 @@ class TreeChartCore {
324
289
  .append("path")
325
290
  .style("opacity", 0)
326
291
  .transition()
327
- .duration(constant_1.ANIMATION_DURATION)
292
+ .duration(ANIMATION_DURATION)
328
293
  .ease(d3.easeCubicInOut)
329
294
  .style("opacity", 1)
330
295
  //.attr("class", "link")
@@ -334,7 +299,7 @@ class TreeChartCore {
334
299
  });
335
300
  links
336
301
  .transition()
337
- .duration(constant_1.ANIMATION_DURATION)
302
+ .duration(ANIMATION_DURATION)
338
303
  .ease(d3.easeCubicInOut)
339
304
  .attr("d", function (d) {
340
305
  return self.generateLinkPath(d);
@@ -342,7 +307,7 @@ class TreeChartCore {
342
307
  links
343
308
  .exit()
344
309
  .transition()
345
- .duration(constant_1.ANIMATION_DURATION / 2)
310
+ .duration(ANIMATION_DURATION / 2)
346
311
  .ease(d3.easeCubicInOut)
347
312
  .style("opacity", 0)
348
313
  .remove();
@@ -366,7 +331,7 @@ class TreeChartCore {
366
331
  const minExpandedDepth = this.treeConfig.collapseDepth; // min level to expand nodes (-1 is the root)
367
332
  if (Array.isArray(externalData)) {
368
333
  for (let i = externalData.length - 1; i >= 0; i--) {
369
- const rootNode = (0, util_1.deepCopy)(externalData[i]);
334
+ const rootNode = deepCopy(externalData[i]);
370
335
  this.assignDepth(rootNode, 0);
371
336
  if (this.treeConfig.initiallyCollapsed) {
372
337
  this.collapseAllNodes(rootNode);
@@ -376,7 +341,7 @@ class TreeChartCore {
376
341
  }
377
342
  }
378
343
  else {
379
- const rootNode = (0, util_1.deepCopy)(externalData);
344
+ const rootNode = deepCopy(externalData);
380
345
  this.assignDepth(rootNode, 0);
381
346
  if (this.treeConfig.initiallyCollapsed) {
382
347
  this.collapseAllNodes(rootNode);
@@ -434,7 +399,7 @@ class TreeChartCore {
434
399
  let originOffsetX = 0;
435
400
  let originOffsetY = 0;
436
401
  if (originTransform) {
437
- const result = originTransform.match(constant_1.MATCH_TRANSLATE_REGEX);
402
+ const result = originTransform.match(MATCH_TRANSLATE_REGEX);
438
403
  if (result !== null && result.length !== 0) {
439
404
  const [offsetX, offsetY] = result.slice(1);
440
405
  originOffsetX = parseInt(offsetX);
@@ -447,7 +412,7 @@ class TreeChartCore {
447
412
  originOffsetY;
448
413
  let transformStr = `translate(${newX}px, ${newY}px)`;
449
414
  if (originTransform) {
450
- transformStr = originTransform.replace(constant_1.MATCH_TRANSLATE_REGEX, transformStr);
415
+ transformStr = originTransform.replace(MATCH_TRANSLATE_REGEX, transformStr);
451
416
  }
452
417
  this.svgElement.style.transform = transformStr;
453
418
  this.domElement.style.transform = transformStr;
@@ -475,10 +440,10 @@ class TreeChartCore {
475
440
  const containerHeight = this.domElement.offsetHeight;
476
441
  if (this.isVertical()) {
477
442
  this.initTransformX = Math.floor(containerWidth / 2);
478
- this.initTransformY = Math.floor(this.treeConfig.nodeHeight - constant_1.DEFAULT_HEIGHT_DECREMENT);
443
+ this.initTransformY = Math.floor(this.treeConfig.nodeHeight - DEFAULT_HEIGHT_DECREMENT);
479
444
  }
480
445
  else {
481
- this.initTransformX = Math.floor(this.treeConfig.nodeWidth - constant_1.DEFAULT_HEIGHT_DECREMENT);
446
+ this.initTransformX = Math.floor(this.treeConfig.nodeWidth - DEFAULT_HEIGHT_DECREMENT);
482
447
  this.initTransformY = Math.floor(containerHeight / 2);
483
448
  }
484
449
  // Apply initial transform
@@ -572,7 +537,7 @@ class TreeChartCore {
572
537
  const nodeX = this.isVertical() ? node.x : node.y;
573
538
  const nodeY = this.isVertical() ? node.y : node.x;
574
539
  const translateX = Math.floor(containerWidth / 2 - nodeX);
575
- const translateY = Math.floor(containerHeight / 2 - nodeY - constant_1.DEFAULT_NODE_HEIGHT * 2 - 10); // -2 nodes to center the diagram +10 of root node
540
+ const translateY = Math.floor(containerHeight / 2 - nodeY - DEFAULT_NODE_HEIGHT * 2 - 10); // -2 nodes to center the diagram +10 of root node
576
541
  const transformString = `scale(${this.currentScale}) translate(${translateX}px, ${translateY}px)`;
577
542
  this.svgElement.style.transition = "transform 0.3s ease";
578
543
  this.domElement.style.transition = "transform 0.3s ease";
@@ -597,4 +562,3 @@ class TreeChartCore {
597
562
  this.treeContainer = null;
598
563
  }
599
564
  }
600
- exports.default = TreeChartCore;
@@ -1,13 +1,10 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.TreeLinkStyle = exports.Direction = void 0;
4
- var Direction;
1
+ export var Direction;
5
2
  (function (Direction) {
6
3
  Direction["VERTICAL"] = "vertical";
7
4
  Direction["HORIZONTAL"] = "horizontal";
8
- })(Direction || (exports.Direction = Direction = {}));
9
- var TreeLinkStyle;
5
+ })(Direction || (Direction = {}));
6
+ export var TreeLinkStyle;
10
7
  (function (TreeLinkStyle) {
11
8
  TreeLinkStyle["CURVE"] = "curve";
12
9
  TreeLinkStyle["STRAIGHT"] = "straight";
13
- })(TreeLinkStyle || (exports.TreeLinkStyle = TreeLinkStyle = {}));
10
+ })(TreeLinkStyle || (TreeLinkStyle = {}));
@@ -1,9 +1,5 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.rotatePoint = rotatePoint;
4
- exports.deepCopy = deepCopy;
5
- const uuid_1 = require("../base/uuid");
6
- function rotatePoint({ x, y }) {
1
+ import { uuid } from "../base/uuid";
2
+ export function rotatePoint({ x, y }) {
7
3
  return {
8
4
  x: y,
9
5
  y: x,
@@ -13,8 +9,8 @@ function rotatePoint({ x, y }) {
13
9
  * Returns a deep copy of selected node (copy of itself and it's children).
14
10
  * If selected node or it's children have no '_key' attribute it will assign a new one.
15
11
  **/
16
- function deepCopy(node) {
17
- let obj = { _key: (0, uuid_1.uuid)() };
12
+ export function deepCopy(node) {
13
+ let obj = { _key: uuid() };
18
14
  for (var key in node) {
19
15
  if (node[key] === null) {
20
16
  obj[key] = null;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@bencamus/tree-chart-core",
3
- "version": "0.1.0",
3
+ "version": "0.1.1",
4
4
  "main": "dist/index.js",
5
5
  "types": "dist/index.d.ts",
6
6
  "files": [