@krainovsd/graph 0.14.0-beta.1 → 0.14.0-beta.2

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/lib/cjs/index.cjs CHANGED
@@ -2844,13 +2844,13 @@ class GraphCanvas {
2844
2844
  get dpi() {
2845
2845
  return devicePixelRatio;
2846
2846
  }
2847
- getData() {
2847
+ getData = () => {
2848
2848
  return {
2849
2849
  links: this.links,
2850
2850
  nodes: this.nodes,
2851
2851
  };
2852
- }
2853
- fitToView(margin = this.graphSettings.zoomToFitMargin, duration = this.graphSettings.zoomAnimationDuration) {
2852
+ };
2853
+ fitToView = (margin = this.graphSettings.zoomToFitMargin, duration = this.graphSettings.zoomAnimationDuration) => {
2854
2854
  const area = this.area;
2855
2855
  if (!area)
2856
2856
  return;
@@ -2877,8 +2877,8 @@ class GraphCanvas {
2877
2877
  return;
2878
2878
  }
2879
2879
  this.animateZoom(area, target, this.areaTransform, duration);
2880
- }
2881
- focusOnNode(nodeId, scale = this.graphSettings.zoomToNodeScale, duration = this.graphSettings.zoomAnimationDuration) {
2880
+ };
2881
+ focusOnNode = (nodeId, scale = this.graphSettings.zoomToNodeScale, duration = this.graphSettings.zoomAnimationDuration) => {
2882
2882
  const area = this.area;
2883
2883
  if (!area)
2884
2884
  return;
@@ -2899,92 +2899,8 @@ class GraphCanvas {
2899
2899
  return;
2900
2900
  }
2901
2901
  this.animateZoom(area, target, this.areaTransform, duration);
2902
- }
2903
- animateHighlight(node, link, baseDuration = this.highlightSettings.highlightDuration) {
2904
- let positive = true;
2905
- if (node && (this.highlightedNode !== node || !this.highlightPositive)) {
2906
- this.highlightedNode = node;
2907
- this.highlightedNeighbors = new Set(node.neighbors);
2908
- this.particles = [];
2909
- this.highlightedLink = null;
2910
- this.highlightStart = performance.now();
2911
- }
2912
- else if (link && (this.highlightedLink !== link || !this.highlightPositive)) {
2913
- const { sourceId, targetId } = extractLinkPointIds(link);
2914
- this.highlightProgress = 0;
2915
- this.highlightedLink = link;
2916
- this.highlightedNeighbors = new Set([sourceId, targetId]);
2917
- this.particles = [];
2918
- this.highlightedNode = null;
2919
- this.highlightStart = performance.now();
2920
- }
2921
- else if (!node && !link && this.highlightPositive) {
2922
- positive = false;
2923
- }
2924
- else {
2925
- return;
2926
- }
2927
- if (this.highlightController) {
2928
- this.highlightController.abort();
2929
- }
2930
- const controller = new AbortController();
2931
- this.highlightPositive = positive;
2932
- this.highlightController = controller;
2933
- const startTime = performance.now();
2934
- const startProgress = this.highlightProgress;
2935
- const targetProgress = positive ? 1 : 0;
2936
- const delta = targetProgress - startProgress;
2937
- const duration = baseDuration * Math.abs(delta);
2938
- const animate = () => {
2939
- if (controller.signal.aborted)
2940
- return;
2941
- const elapsed = performance.now() - startTime;
2942
- const t = Math.min(elapsed / duration, 1);
2943
- const current = startProgress + delta * t;
2944
- const eased = current < 0.5 ? 4 * current * current * current : 1 - (-2 * current + 2) ** 3 / 2;
2945
- this.highlightProgress = eased;
2946
- if (t < 1 || positive) {
2947
- requestAnimationFrame(animate);
2948
- this.draw();
2949
- }
2950
- else {
2951
- this.highlightedNode = null;
2952
- this.highlightedLink = null;
2953
- this.highlightedNeighbors = null;
2954
- this.highlightStart = null;
2955
- this.particles = [];
2956
- this.tick();
2957
- }
2958
- };
2959
- requestAnimationFrame(animate);
2960
- }
2961
- animateZoom(area, target, start, duration) {
2962
- this._zoomAnimating = true;
2963
- const startTime = performance.now();
2964
- const animate = () => {
2965
- const elapsed = performance.now() - startTime;
2966
- const t = Math.min(elapsed / duration, 1);
2967
- const eased = t < 0.5 ? 4 * t * t * t : 1 - (-2 * t + 2) ** 3 / 2;
2968
- const x = start.x + (target.x - start.x) * eased;
2969
- const y = start.y + (target.y - start.y) * eased;
2970
- const k = start.k + (target.k - start.k) * eased;
2971
- this.areaTransform = new d3Zoom.ZoomTransform(k, x, y);
2972
- d3Zoom.zoom().transform(d3Selection.select(area), this.areaTransform);
2973
- updateLinkCache.call(this);
2974
- updateNodeCache.call(this);
2975
- this.tick();
2976
- if (t < 1) {
2977
- requestAnimationFrame(animate);
2978
- }
2979
- else {
2980
- this._zoomAnimating = false;
2981
- this.clearCache(true);
2982
- this.tick();
2983
- }
2984
- };
2985
- requestAnimationFrame(animate);
2986
- }
2987
- changeData(options, alpha = 0.5, clearCache = true) {
2902
+ };
2903
+ changeData = (options, alpha = 0.5, clearCache = true) => {
2988
2904
  if (options.links != undefined)
2989
2905
  this.links = options.links;
2990
2906
  if (options.nodes != undefined)
@@ -2992,8 +2908,8 @@ class GraphCanvas {
2992
2908
  if (options.nodes != undefined || options.links != undefined) {
2993
2909
  this.updateData(alpha, clearCache);
2994
2910
  }
2995
- }
2996
- changeSettings(options, clearCache = true) {
2911
+ };
2912
+ changeSettings = (options, clearCache = true) => {
2997
2913
  if (options.graphSettings) {
2998
2914
  this.graphSettings = graphSettingsGetter(options.graphSettings, this.graphSettings);
2999
2915
  this.draw = initDraw.call(this);
@@ -3043,13 +2959,13 @@ class GraphCanvas {
3043
2959
  return void this.updateSimulation();
3044
2960
  }
3045
2961
  this.tick();
3046
- }
3047
- updateRect() {
2962
+ };
2963
+ updateRect = () => {
3048
2964
  if (!this.area)
3049
2965
  return;
3050
2966
  this.areaRect = this.area.getBoundingClientRect();
3051
- }
3052
- updateSize() {
2967
+ };
2968
+ updateSize = () => {
3053
2969
  if (!this.area)
3054
2970
  return;
3055
2971
  const { width, height } = this.root.getBoundingClientRect();
@@ -3063,8 +2979,8 @@ class GraphCanvas {
3063
2979
  throw new Error("couldn't create canvas context");
3064
2980
  this.context.scale(this.dpi, this.dpi);
3065
2981
  this.draw();
3066
- }
3067
- clearCache(keys) {
2982
+ };
2983
+ clearCache = (keys) => {
3068
2984
  if (keys === true) {
3069
2985
  this.nodeOptionsCache.length = 0;
3070
2986
  this.linkOptionsCache.length = 0;
@@ -3095,12 +3011,12 @@ class GraphCanvas {
3095
3011
  }
3096
3012
  updateNodeCache.call(this);
3097
3013
  updateLinkCache.call(this);
3098
- }
3099
- tick() {
3014
+ };
3015
+ tick = () => {
3100
3016
  if (!this.simulationWorking)
3101
3017
  this.draw();
3102
- }
3103
- restart(alpha, options) {
3018
+ };
3019
+ restart = (alpha, options) => {
3104
3020
  if (!this.simulation)
3105
3021
  return;
3106
3022
  const settings = {
@@ -3136,23 +3052,23 @@ class GraphCanvas {
3136
3052
  initSimulationForces.call(this);
3137
3053
  this.simulation.restart();
3138
3054
  this.tick();
3139
- }
3140
- start() {
3055
+ };
3056
+ start = () => {
3141
3057
  if (this.simulation)
3142
3058
  this.simulation.alpha(1).restart();
3143
3059
  if (this.container)
3144
3060
  this.container.style.display = "block";
3145
- }
3146
- stop() {
3061
+ };
3062
+ stop = () => {
3147
3063
  if (this.simulation)
3148
3064
  this.simulation.stop();
3149
3065
  if (this.container)
3150
3066
  this.container.style.display = "none";
3151
- }
3152
- create() {
3067
+ };
3068
+ create = () => {
3153
3069
  this.init();
3154
- }
3155
- destroy() {
3070
+ };
3071
+ destroy = () => {
3156
3072
  if (this.simulation) {
3157
3073
  this.simulation.stop();
3158
3074
  this.simulation = undefined;
@@ -3160,22 +3076,22 @@ class GraphCanvas {
3160
3076
  this.clearHTMLElements();
3161
3077
  this.clearState();
3162
3078
  this.clearCache(true);
3163
- }
3164
- clearHTMLElements() {
3079
+ };
3080
+ clearHTMLElements = () => {
3165
3081
  this.root.replaceChildren();
3166
3082
  this.area = undefined;
3167
3083
  this.context = undefined;
3168
3084
  this.container = undefined;
3169
3085
  this.eventAbortController.abort();
3170
3086
  this.eventAbortController = new AbortController();
3171
- }
3172
- updateSimulation() {
3087
+ };
3088
+ updateSimulation = () => {
3173
3089
  if (this.simulation) {
3174
3090
  initSimulationForces.call(this);
3175
3091
  this.restart(1);
3176
3092
  }
3177
- }
3178
- clearState() {
3093
+ };
3094
+ clearState = () => {
3179
3095
  this.isDragging = false;
3180
3096
  this.highlightedNode = null;
3181
3097
  this.highlightedLink = null;
@@ -3183,8 +3099,22 @@ class GraphCanvas {
3183
3099
  this.highlightProgress = 0;
3184
3100
  this.highlightStart = null;
3185
3101
  this.highlightPositive = false;
3186
- }
3187
- updateData(alpha = 0.5, clearCache = true) {
3102
+ };
3103
+ init = () => {
3104
+ initArea.call(this);
3105
+ updateNodeCache.call(this);
3106
+ updateLinkCache.call(this);
3107
+ initSimulation.call(this);
3108
+ this.restart(1);
3109
+ initDnd.call(this);
3110
+ initZoom.call(this);
3111
+ initResize.call(this);
3112
+ initPointer.call(this);
3113
+ updateNodeCache.call(this);
3114
+ updateLinkCache.call(this);
3115
+ this.tick();
3116
+ };
3117
+ updateData = (alpha = 0.5, clearCache = true) => {
3188
3118
  if (clearCache) {
3189
3119
  this.clearCache(clearCache);
3190
3120
  }
@@ -3204,18 +3134,91 @@ class GraphCanvas {
3204
3134
  this.restart(alpha);
3205
3135
  initZoom.call(this, this.areaTransform);
3206
3136
  }
3207
- }
3208
- init() {
3209
- initArea.call(this);
3210
- updateNodeCache.call(this);
3211
- updateLinkCache.call(this);
3212
- initSimulation.call(this);
3213
- this.restart(1);
3214
- initDnd.call(this);
3215
- initZoom.call(this);
3216
- initResize.call(this);
3217
- initPointer.call(this);
3218
- }
3137
+ };
3138
+ animateHighlight = (node, link, baseDuration = this.highlightSettings.highlightDuration) => {
3139
+ let positive = true;
3140
+ if (node && (this.highlightedNode !== node || !this.highlightPositive)) {
3141
+ this.highlightedNode = node;
3142
+ this.highlightedNeighbors = new Set(node.neighbors);
3143
+ this.particles = [];
3144
+ this.highlightedLink = null;
3145
+ this.highlightStart = performance.now();
3146
+ }
3147
+ else if (link && (this.highlightedLink !== link || !this.highlightPositive)) {
3148
+ const { sourceId, targetId } = extractLinkPointIds(link);
3149
+ this.highlightProgress = 0;
3150
+ this.highlightedLink = link;
3151
+ this.highlightedNeighbors = new Set([sourceId, targetId]);
3152
+ this.particles = [];
3153
+ this.highlightedNode = null;
3154
+ this.highlightStart = performance.now();
3155
+ }
3156
+ else if (!node && !link && this.highlightPositive) {
3157
+ positive = false;
3158
+ }
3159
+ else {
3160
+ return;
3161
+ }
3162
+ if (this.highlightController) {
3163
+ this.highlightController.abort();
3164
+ }
3165
+ const controller = new AbortController();
3166
+ this.highlightPositive = positive;
3167
+ this.highlightController = controller;
3168
+ const startTime = performance.now();
3169
+ const startProgress = this.highlightProgress;
3170
+ const targetProgress = positive ? 1 : 0;
3171
+ const delta = targetProgress - startProgress;
3172
+ const duration = baseDuration * Math.abs(delta);
3173
+ const animate = () => {
3174
+ if (controller.signal.aborted)
3175
+ return;
3176
+ const elapsed = performance.now() - startTime;
3177
+ const t = Math.min(elapsed / duration, 1);
3178
+ const current = startProgress + delta * t;
3179
+ const eased = current < 0.5 ? 4 * current * current * current : 1 - (-2 * current + 2) ** 3 / 2;
3180
+ this.highlightProgress = eased;
3181
+ if (t < 1 || positive) {
3182
+ requestAnimationFrame(animate);
3183
+ this.draw();
3184
+ }
3185
+ else {
3186
+ this.highlightedNode = null;
3187
+ this.highlightedLink = null;
3188
+ this.highlightedNeighbors = null;
3189
+ this.highlightStart = null;
3190
+ this.particles = [];
3191
+ this.tick();
3192
+ }
3193
+ };
3194
+ requestAnimationFrame(animate);
3195
+ };
3196
+ animateZoom = (area, target, start, duration) => {
3197
+ this._zoomAnimating = true;
3198
+ const startTime = performance.now();
3199
+ const animate = () => {
3200
+ const elapsed = performance.now() - startTime;
3201
+ const t = Math.min(elapsed / duration, 1);
3202
+ const eased = t < 0.5 ? 4 * t * t * t : 1 - (-2 * t + 2) ** 3 / 2;
3203
+ const x = start.x + (target.x - start.x) * eased;
3204
+ const y = start.y + (target.y - start.y) * eased;
3205
+ const k = start.k + (target.k - start.k) * eased;
3206
+ this.areaTransform = new d3Zoom.ZoomTransform(k, x, y);
3207
+ d3Zoom.zoom().transform(d3Selection.select(area), this.areaTransform);
3208
+ updateLinkCache.call(this);
3209
+ updateNodeCache.call(this);
3210
+ this.tick();
3211
+ if (t < 1) {
3212
+ requestAnimationFrame(animate);
3213
+ }
3214
+ else {
3215
+ this._zoomAnimating = false;
3216
+ this.clearCache(true);
3217
+ this.tick();
3218
+ }
3219
+ };
3220
+ requestAnimationFrame(animate);
3221
+ };
3219
3222
  }
3220
3223
 
3221
3224
  const FORCE_CONTROLS = [