jqtree 1.8.5 → 1.8.7

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.
@@ -1,6 +1,3 @@
1
- import { isInt } from "./util";
2
- import { Node } from "./node";
3
- import { OnGetStateFromStorage, OnSetStateFromStorage } from "./jqtreeOptions";
4
1
  import {
5
2
  AddToSelection,
6
3
  GetNodeById,
@@ -10,10 +7,13 @@ import {
10
7
  RefreshElements,
11
8
  RemoveFromSelection,
12
9
  } from "./jqtreeMethodTypes";
10
+ import { OnGetStateFromStorage, OnSetStateFromStorage } from "./jqtreeOptions";
11
+ import { Node } from "./node";
12
+ import { isInt } from "./util";
13
13
 
14
14
  export interface SavedState {
15
- open_nodes: NodeId[];
16
- selected_node: NodeId[];
15
+ open_nodes?: NodeId[];
16
+ selected_node?: NodeId[];
17
17
  }
18
18
 
19
19
  interface SaveStateHandlerParams {
@@ -30,6 +30,7 @@ interface SaveStateHandlerParams {
30
30
  }
31
31
 
32
32
  export default class SaveStateHandler {
33
+ private _supportsLocalStorage: boolean | null;
33
34
  private addToSelection: AddToSelection;
34
35
  private getNodeById: GetNodeById;
35
36
  private getSelectedNodes: GetSelectedNodes;
@@ -40,7 +41,6 @@ export default class SaveStateHandler {
40
41
  private refreshElements: RefreshElements;
41
42
  private removeFromSelection: RemoveFromSelection;
42
43
  private saveStateOption: boolean | string;
43
- private _supportsLocalStorage: boolean | null;
44
44
 
45
45
  constructor({
46
46
  addToSelection,
@@ -66,21 +66,104 @@ export default class SaveStateHandler {
66
66
  this.saveStateOption = saveState;
67
67
  }
68
68
 
69
- public saveState(): void {
70
- const state = JSON.stringify(this.getState());
69
+ private getKeyName(): string {
70
+ if (typeof this.saveStateOption === "string") {
71
+ return this.saveStateOption;
72
+ } else {
73
+ return "tree";
74
+ }
75
+ }
71
76
 
72
- if (this.onSetStateFromStorage) {
73
- this.onSetStateFromStorage(state);
77
+ private loadFromStorage(): null | string {
78
+ if (this.onGetStateFromStorage) {
79
+ return this.onGetStateFromStorage();
74
80
  } else if (this.supportsLocalStorage()) {
75
- localStorage.setItem(this.getKeyName(), state);
81
+ return localStorage.getItem(this.getKeyName());
82
+ } else {
83
+ return null;
76
84
  }
77
85
  }
78
86
 
79
- public getStateFromStorage(): SavedState | null {
80
- const jsonData = this.loadFromStorage();
87
+ private openInitialNodes(nodeIds: NodeId[]): boolean {
88
+ let mustLoadOnDemand = false;
81
89
 
82
- if (jsonData) {
83
- return this.parseState(jsonData) as unknown as SavedState;
90
+ for (const nodeId of nodeIds) {
91
+ const node = this.getNodeById(nodeId);
92
+
93
+ if (node) {
94
+ if (!node.load_on_demand) {
95
+ node.is_open = true;
96
+ } else {
97
+ mustLoadOnDemand = true;
98
+ }
99
+ }
100
+ }
101
+
102
+ return mustLoadOnDemand;
103
+ }
104
+
105
+ private parseState(jsonData: string): SavedState {
106
+ const state = JSON.parse(jsonData) as Record<string, unknown>;
107
+
108
+ // Check if selected_node is an int (instead of an array)
109
+ if (state.selected_node && isInt(state.selected_node)) {
110
+ // Convert to array
111
+ state.selected_node = [state.selected_node];
112
+ }
113
+
114
+ return state as unknown as SavedState;
115
+ }
116
+
117
+ private resetSelection(): void {
118
+ const selectedNodes = this.getSelectedNodes();
119
+
120
+ selectedNodes.forEach((node) => {
121
+ this.removeFromSelection(node);
122
+ });
123
+ }
124
+
125
+ private selectInitialNodes(nodeIds: NodeId[]): boolean {
126
+ let selectCount = 0;
127
+
128
+ for (const nodeId of nodeIds) {
129
+ const node = this.getNodeById(nodeId);
130
+
131
+ if (node) {
132
+ selectCount += 1;
133
+
134
+ this.addToSelection(node);
135
+ }
136
+ }
137
+
138
+ return selectCount !== 0;
139
+ }
140
+
141
+ private supportsLocalStorage(): boolean {
142
+ const testSupport = (): boolean => {
143
+ // Check if it's possible to store an item. Safari does not allow this in private browsing mode.
144
+ try {
145
+ const key = "_storage_test";
146
+ sessionStorage.setItem(key, "value");
147
+ sessionStorage.removeItem(key);
148
+ } catch {
149
+ return false;
150
+ }
151
+
152
+ return true;
153
+ };
154
+
155
+ if (this._supportsLocalStorage == null) {
156
+ this._supportsLocalStorage = testSupport();
157
+ }
158
+
159
+ return this._supportsLocalStorage;
160
+ }
161
+
162
+ public getNodeIdToBeSelected(): NodeId | null {
163
+ const state = this.getStateFromStorage();
164
+
165
+ if (state?.selected_node) {
166
+ return state.selected_node[0] ?? null;
84
167
  } else {
85
168
  return null;
86
169
  }
@@ -118,6 +201,26 @@ export default class SaveStateHandler {
118
201
  };
119
202
  }
120
203
 
204
+ public getStateFromStorage(): null | SavedState {
205
+ const jsonData = this.loadFromStorage();
206
+
207
+ if (jsonData) {
208
+ return this.parseState(jsonData) as unknown as SavedState;
209
+ } else {
210
+ return null;
211
+ }
212
+ }
213
+
214
+ public saveState(): void {
215
+ const state = JSON.stringify(this.getState());
216
+
217
+ if (this.onSetStateFromStorage) {
218
+ this.onSetStateFromStorage(state);
219
+ } else if (this.supportsLocalStorage()) {
220
+ localStorage.setItem(this.getKeyName(), state);
221
+ }
222
+ }
223
+
121
224
  /*
122
225
  Set initial state
123
226
  Don't handle nodes that are loaded on demand
@@ -125,22 +228,19 @@ export default class SaveStateHandler {
125
228
  result: must load on demand
126
229
  */
127
230
  public setInitialState(state: SavedState): boolean {
128
- if (!state) {
129
- return false;
130
- } else {
131
- let mustLoadOnDemand = false;
231
+ let mustLoadOnDemand = false;
132
232
 
133
- if (state.open_nodes) {
134
- mustLoadOnDemand = this.openInitialNodes(state.open_nodes);
135
- }
233
+ if (state.open_nodes) {
234
+ mustLoadOnDemand = this.openInitialNodes(state.open_nodes);
235
+ }
136
236
 
137
- if (state.selected_node) {
138
- this.resetSelection();
139
- this.selectInitialNodes(state.selected_node);
140
- }
237
+ this.resetSelection();
141
238
 
142
- return mustLoadOnDemand;
239
+ if (state.selected_node) {
240
+ this.selectInitialNodes(state.selected_node);
143
241
  }
242
+
243
+ return mustLoadOnDemand;
144
244
  }
145
245
 
146
246
  public setInitialStateOnDemand(
@@ -151,6 +251,10 @@ export default class SaveStateHandler {
151
251
  let nodeIds = state.open_nodes;
152
252
 
153
253
  const openNodes = (): void => {
254
+ if (!nodeIds) {
255
+ return;
256
+ }
257
+
154
258
  const newNodesIds = [];
155
259
 
156
260
  for (const nodeId of nodeIds) {
@@ -171,8 +275,10 @@ export default class SaveStateHandler {
171
275
 
172
276
  nodeIds = newNodesIds;
173
277
 
174
- if (this.selectInitialNodes(state.selected_node)) {
175
- this.refreshElements(null);
278
+ if (state.selected_node) {
279
+ if (this.selectInitialNodes(state.selected_node)) {
280
+ this.refreshElements(null);
281
+ }
176
282
  }
177
283
 
178
284
  if (loadingCount === 0) {
@@ -190,112 +296,4 @@ export default class SaveStateHandler {
190
296
 
191
297
  openNodes();
192
298
  }
193
-
194
- public getNodeIdToBeSelected(): NodeId | null {
195
- const state = this.getStateFromStorage();
196
-
197
- if (state?.selected_node) {
198
- return state.selected_node[0] || null;
199
- } else {
200
- return null;
201
- }
202
- }
203
-
204
- private parseState(jsonData: string): SavedState {
205
- const state = JSON.parse(jsonData) as Record<string, unknown>;
206
-
207
- // Check if selected_node is an int (instead of an array)
208
- if (state && state.selected_node && isInt(state.selected_node)) {
209
- // Convert to array
210
- state.selected_node = [state.selected_node];
211
- }
212
-
213
- return state as unknown as SavedState;
214
- }
215
-
216
- private loadFromStorage(): string | null {
217
- if (this.onGetStateFromStorage) {
218
- return this.onGetStateFromStorage();
219
- } else if (this.supportsLocalStorage()) {
220
- return localStorage.getItem(this.getKeyName());
221
- } else {
222
- return null;
223
- }
224
- }
225
-
226
- private openInitialNodes(nodeIds: NodeId[]): boolean {
227
- let mustLoadOnDemand = false;
228
-
229
- for (const nodeId of nodeIds) {
230
- const node = this.getNodeById(nodeId);
231
-
232
- if (node) {
233
- if (!node.load_on_demand) {
234
- node.is_open = true;
235
- } else {
236
- mustLoadOnDemand = true;
237
- }
238
- }
239
- }
240
-
241
- return mustLoadOnDemand;
242
- }
243
-
244
- private selectInitialNodes(nodeIds: NodeId[]): boolean {
245
- let selectCount = 0;
246
-
247
- for (const nodeId of nodeIds) {
248
- const node = this.getNodeById(nodeId);
249
-
250
- if (node) {
251
- selectCount += 1;
252
-
253
- this.addToSelection(node);
254
- }
255
- }
256
-
257
- return selectCount !== 0;
258
- }
259
-
260
- private resetSelection(): void {
261
- const selectedNodes = this.getSelectedNodes();
262
-
263
- selectedNodes.forEach((node) => {
264
- this.removeFromSelection(node);
265
- });
266
- }
267
-
268
- private getKeyName(): string {
269
- if (typeof this.saveStateOption === "string") {
270
- return this.saveStateOption;
271
- } else {
272
- return "tree";
273
- }
274
- }
275
-
276
- private supportsLocalStorage(): boolean {
277
- const testSupport = (): boolean => {
278
- // Is local storage supported?
279
- if (localStorage == null) {
280
- return false;
281
- } else {
282
- // Check if it's possible to store an item. Safari does not allow this in private browsing mode.
283
- try {
284
- const key = "_storage_test";
285
- sessionStorage.setItem(key, "value");
286
- sessionStorage.removeItem(key);
287
- } catch (error) {
288
- return false;
289
- }
290
-
291
- return true;
292
- }
293
- };
294
-
295
- if (this._supportsLocalStorage == null) {
296
- this._supportsLocalStorage = testSupport();
297
- }
298
-
299
- return this._supportsLocalStorage;
300
- }
301
299
  }
@@ -1,4 +1,5 @@
1
1
  import type { ScrollParent } from "./types";
2
+
2
3
  import { getElementPosition, getOffsetTop } from '../util'
3
4
 
4
5
  type HorizontalScrollDirection = "left" | "right";
@@ -16,70 +17,14 @@ export default class ContainerScrollParent implements ScrollParent {
16
17
  private refreshHitAreas: () => void;
17
18
  private scrollParentBottom?: number;
18
19
  private scrollParentTop?: number;
19
- private verticalScrollTimeout?: number;
20
20
  private verticalScrollDirection?: VerticalScrollDirection;
21
+ private verticalScrollTimeout?: number;
21
22
 
22
23
  constructor({ container, refreshHitAreas }: Params) {
23
24
  this.container = container;
24
25
  this.refreshHitAreas = refreshHitAreas;
25
26
  }
26
27
 
27
- public checkHorizontalScrolling(pageX: number): void {
28
- const newHorizontalScrollDirection =
29
- this.getNewHorizontalScrollDirection(pageX);
30
-
31
- if (this.horizontalScrollDirection !== newHorizontalScrollDirection) {
32
- this.horizontalScrollDirection = newHorizontalScrollDirection;
33
-
34
- if (this.horizontalScrollTimeout != null) {
35
- window.clearTimeout(this.verticalScrollTimeout);
36
- }
37
-
38
- if (newHorizontalScrollDirection) {
39
- this.horizontalScrollTimeout = window.setTimeout(
40
- this.scrollHorizontally.bind(this),
41
- 40,
42
- );
43
- }
44
- }
45
- }
46
-
47
- public checkVerticalScrolling(pageY: number) {
48
- const newVerticalScrollDirection =
49
- this.getNewVerticalScrollDirection(pageY);
50
-
51
- if (this.verticalScrollDirection !== newVerticalScrollDirection) {
52
- this.verticalScrollDirection = newVerticalScrollDirection;
53
-
54
- if (this.verticalScrollTimeout != null) {
55
- window.clearTimeout(this.verticalScrollTimeout);
56
- this.verticalScrollTimeout = undefined;
57
- }
58
-
59
- if (newVerticalScrollDirection) {
60
- this.verticalScrollTimeout = window.setTimeout(
61
- this.scrollVertically.bind(this),
62
- 40,
63
- );
64
- }
65
- }
66
- }
67
-
68
- public getScrollLeft(): number {
69
- return this.container.scrollLeft;
70
- }
71
-
72
- public scrollToY(top: number): void {
73
- this.container.scrollTop = top;
74
- }
75
-
76
- public stopScrolling() {
77
- this.horizontalScrollDirection = undefined;
78
- this.verticalScrollDirection = undefined;
79
- this.scrollParentTop = undefined;
80
- this.scrollParentBottom = undefined;
81
- }
82
-
83
28
  private getNewHorizontalScrollDirection(
84
29
  pageX: number,
85
30
  ): HorizontalScrollDirection | undefined {
@@ -101,7 +46,7 @@ export default class ContainerScrollParent implements ScrollParent {
101
46
 
102
47
  private getNewVerticalScrollDirection(
103
48
  pageY: number,
104
- ): VerticalScrollDirection | undefined {
49
+ ): undefined | VerticalScrollDirection {
105
50
  if (pageY < this.getScrollParentTop()) {
106
51
  return "top";
107
52
  }
@@ -113,6 +58,22 @@ export default class ContainerScrollParent implements ScrollParent {
113
58
  return undefined;
114
59
  }
115
60
 
61
+ private getScrollParentBottom() {
62
+ if (this.scrollParentBottom == null) {
63
+ this.scrollParentBottom = this.getScrollParentTop() + this.container.clientHeight;
64
+ }
65
+
66
+ return this.scrollParentBottom;
67
+ }
68
+
69
+ private getScrollParentTop() {
70
+ if (this.scrollParentTop == null) {
71
+ this.scrollParentTop = getOffsetTop(this.container)
72
+ }
73
+
74
+ return this.scrollParentTop;
75
+ }
76
+
116
77
  private scrollHorizontally() {
117
78
  if (!this.horizontalScrollDirection) {
118
79
  return;
@@ -121,9 +82,9 @@ export default class ContainerScrollParent implements ScrollParent {
121
82
  const distance = this.horizontalScrollDirection === "left" ? -20 : 20;
122
83
 
123
84
  this.container.scrollBy({
85
+ behavior: "instant",
124
86
  left: distance,
125
87
  top: 0,
126
- behavior: "instant",
127
88
  });
128
89
 
129
90
  this.refreshHitAreas();
@@ -139,9 +100,9 @@ export default class ContainerScrollParent implements ScrollParent {
139
100
  const distance = this.verticalScrollDirection === "top" ? -20 : 20;
140
101
 
141
102
  this.container.scrollBy({
103
+ behavior: "instant",
142
104
  left: 0,
143
105
  top: distance,
144
- behavior: "instant",
145
106
  });
146
107
 
147
108
  this.refreshHitAreas();
@@ -149,19 +110,59 @@ export default class ContainerScrollParent implements ScrollParent {
149
110
  setTimeout(this.scrollVertically.bind(this), 40);
150
111
  }
151
112
 
152
- private getScrollParentTop() {
153
- if (this.scrollParentTop == null) {
154
- this.scrollParentTop = getOffsetTop(this.container)
155
- }
113
+ public checkHorizontalScrolling(pageX: number): void {
114
+ const newHorizontalScrollDirection =
115
+ this.getNewHorizontalScrollDirection(pageX);
156
116
 
157
- return this.scrollParentTop;
117
+ if (this.horizontalScrollDirection !== newHorizontalScrollDirection) {
118
+ this.horizontalScrollDirection = newHorizontalScrollDirection;
119
+
120
+ if (this.horizontalScrollTimeout != null) {
121
+ window.clearTimeout(this.verticalScrollTimeout);
122
+ }
123
+
124
+ if (newHorizontalScrollDirection) {
125
+ this.horizontalScrollTimeout = window.setTimeout(
126
+ this.scrollHorizontally.bind(this),
127
+ 40,
128
+ );
129
+ }
130
+ }
158
131
  }
159
132
 
160
- private getScrollParentBottom() {
161
- if (this.scrollParentBottom == null) {
162
- this.scrollParentBottom = this.getScrollParentTop() + this.container.clientHeight;
133
+ public checkVerticalScrolling(pageY: number) {
134
+ const newVerticalScrollDirection =
135
+ this.getNewVerticalScrollDirection(pageY);
136
+
137
+ if (this.verticalScrollDirection !== newVerticalScrollDirection) {
138
+ this.verticalScrollDirection = newVerticalScrollDirection;
139
+
140
+ if (this.verticalScrollTimeout != null) {
141
+ window.clearTimeout(this.verticalScrollTimeout);
142
+ this.verticalScrollTimeout = undefined;
143
+ }
144
+
145
+ if (newVerticalScrollDirection) {
146
+ this.verticalScrollTimeout = window.setTimeout(
147
+ this.scrollVertically.bind(this),
148
+ 40,
149
+ );
150
+ }
163
151
  }
152
+ }
164
153
 
165
- return this.scrollParentBottom;
154
+ public getScrollLeft(): number {
155
+ return this.container.scrollLeft;
156
+ }
157
+
158
+ public scrollToY(top: number): void {
159
+ this.container.scrollTop = top;
160
+ }
161
+
162
+ public stopScrolling() {
163
+ this.horizontalScrollDirection = undefined;
164
+ this.verticalScrollDirection = undefined;
165
+ this.scrollParentTop = undefined;
166
+ this.scrollParentBottom = undefined;
166
167
  }
167
168
  }
@@ -1,4 +1,5 @@
1
1
  import type { ScrollParent } from "./types";
2
+
2
3
  import ContainerScrollParent from "./containerScrollParent";
3
4
  import DocumentScrollParent from "./documentScrollParent";
4
5