jqtree 1.9.0 → 2.0.0
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/bower.json +1 -1
- package/js-coverage/coverage-final.json +13 -0
- package/lib/jqtreeOptions.js +1 -0
- package/lib/tree.jquery.d.js +0 -0
- package/lib/tree.jquery.js +394 -0
- package/lib/triggerJQueryEvent.js +68 -0
- package/lib/typings.d.js +1 -0
- package/lib/version.js +2 -0
- package/package.json +34 -29
- package/pnpm-workspace.yaml +3 -3
- package/src/jqtreeOptions.ts +20 -62
- package/src/tree.jquery.d.ts +81 -92
- package/src/tree.jquery.ts +234 -938
- package/src/triggerJQueryEvent.ts +97 -0
- package/src/version.ts +2 -2
- package/tree.jquery.debug.js +4226 -3281
- package/tree.jquery.debug.js.map +1 -1
- package/tree.jquery.js +22 -2
- package/tree.jquery.js.map +1 -1
- package/eslint.config.mjs +0 -112
- package/src/animation.ts +0 -48
- package/src/dataLoader.ts +0 -143
- package/src/dragAndDropHandler/binarySearch.ts +0 -27
- package/src/dragAndDropHandler/dragElement.ts +0 -54
- package/src/dragAndDropHandler/generateHitAreas.ts +0 -195
- package/src/dragAndDropHandler/index.ts +0 -432
- package/src/dragAndDropHandler/iterateVisibleNodes.ts +0 -91
- package/src/dragAndDropHandler/types.ts +0 -12
- package/src/elementsRenderer.ts +0 -359
- package/src/jqtreeMethodTypes.ts +0 -40
- package/src/keyHandler.ts +0 -139
- package/src/mouseHandler.ts +0 -383
- package/src/mouseUtils.ts +0 -23
- package/src/node.ts +0 -653
- package/src/nodeElement/borderDropHint.ts +0 -32
- package/src/nodeElement/folderElement.ts +0 -138
- package/src/nodeElement/ghostDropHint.ts +0 -74
- package/src/nodeElement/index.ts +0 -94
- package/src/nodeUtils.ts +0 -10
- package/src/positionUtils.ts +0 -18
- package/src/saveStateHandler.ts +0 -276
- package/src/scrollHandler/containerScrollParent.ts +0 -71
- package/src/scrollHandler/createScrollParent.ts +0 -51
- package/src/scrollHandler/documentScrollParent.ts +0 -104
- package/src/scrollHandler/scrollParent.ts +0 -117
- package/src/scrollHandler.ts +0 -55
- package/src/selectNodeHandler.ts +0 -111
- package/src/simple.widget.ts +0 -134
- package/src/util.ts +0 -5
|
@@ -1,51 +0,0 @@
|
|
|
1
|
-
import type { ScrollParent } from "./scrollParent";
|
|
2
|
-
|
|
3
|
-
import ContainerScrollParent from "./containerScrollParent";
|
|
4
|
-
import DocumentScrollParent from "./documentScrollParent";
|
|
5
|
-
|
|
6
|
-
const isOverflow = (overflowValue: string) =>
|
|
7
|
-
overflowValue === "auto" || overflowValue === "scroll";
|
|
8
|
-
|
|
9
|
-
const hasOverFlow = (element: HTMLElement): boolean => {
|
|
10
|
-
const style = getComputedStyle(element);
|
|
11
|
-
|
|
12
|
-
return isOverflow(style.overflowX) || isOverflow(style.overflowY);
|
|
13
|
-
};
|
|
14
|
-
|
|
15
|
-
const getParentWithOverflow = (
|
|
16
|
-
treeElement: HTMLElement,
|
|
17
|
-
): HTMLElement | null => {
|
|
18
|
-
if (hasOverFlow(treeElement)) {
|
|
19
|
-
return treeElement;
|
|
20
|
-
}
|
|
21
|
-
|
|
22
|
-
let parent = treeElement.parentElement;
|
|
23
|
-
|
|
24
|
-
while (parent) {
|
|
25
|
-
if (hasOverFlow(parent)) {
|
|
26
|
-
return parent;
|
|
27
|
-
}
|
|
28
|
-
|
|
29
|
-
parent = parent.parentElement;
|
|
30
|
-
}
|
|
31
|
-
|
|
32
|
-
return null;
|
|
33
|
-
};
|
|
34
|
-
|
|
35
|
-
const createScrollParent = (
|
|
36
|
-
treeElement: HTMLElement,
|
|
37
|
-
refreshHitAreas: () => void,
|
|
38
|
-
): ScrollParent => {
|
|
39
|
-
const container = getParentWithOverflow(treeElement);
|
|
40
|
-
|
|
41
|
-
if (container && container.tagName !== "HTML") {
|
|
42
|
-
return new ContainerScrollParent({
|
|
43
|
-
container,
|
|
44
|
-
refreshHitAreas,
|
|
45
|
-
});
|
|
46
|
-
} else {
|
|
47
|
-
return new DocumentScrollParent({ refreshHitAreas, treeElement });
|
|
48
|
-
}
|
|
49
|
-
};
|
|
50
|
-
|
|
51
|
-
export default createScrollParent;
|
|
@@ -1,104 +0,0 @@
|
|
|
1
|
-
import type {
|
|
2
|
-
HorizontalScrollDirection,
|
|
3
|
-
VerticalScrollDirection,
|
|
4
|
-
} from "./scrollParent";
|
|
5
|
-
|
|
6
|
-
import { getOffsetTop } from "../positionUtils";
|
|
7
|
-
import { ScrollParent } from "./scrollParent";
|
|
8
|
-
|
|
9
|
-
interface Params {
|
|
10
|
-
refreshHitAreas: () => void;
|
|
11
|
-
treeElement: HTMLElement;
|
|
12
|
-
}
|
|
13
|
-
|
|
14
|
-
export default class DocumentScrollParent extends ScrollParent {
|
|
15
|
-
private _documentScrollHeight?: number;
|
|
16
|
-
private _documentScrollWidth?: number;
|
|
17
|
-
private _treeElement: HTMLElement;
|
|
18
|
-
|
|
19
|
-
constructor({ refreshHitAreas, treeElement }: Params) {
|
|
20
|
-
super({ container: document.documentElement, refreshHitAreas });
|
|
21
|
-
|
|
22
|
-
this._treeElement = treeElement;
|
|
23
|
-
}
|
|
24
|
-
|
|
25
|
-
public scrollToY(top: number): void {
|
|
26
|
-
const treeTop = getOffsetTop(this._treeElement);
|
|
27
|
-
|
|
28
|
-
super.scrollToY(top + treeTop);
|
|
29
|
-
}
|
|
30
|
-
|
|
31
|
-
public stopScrolling() {
|
|
32
|
-
super.stopScrolling();
|
|
33
|
-
|
|
34
|
-
this._documentScrollHeight = undefined;
|
|
35
|
-
this._documentScrollWidth = undefined;
|
|
36
|
-
}
|
|
37
|
-
|
|
38
|
-
protected getNewHorizontalScrollDirection(
|
|
39
|
-
pageX: number,
|
|
40
|
-
): HorizontalScrollDirection | undefined {
|
|
41
|
-
const scrollLeft = this.container.scrollLeft;
|
|
42
|
-
const windowWidth = window.innerWidth;
|
|
43
|
-
|
|
44
|
-
const isNearRightEdge = pageX > windowWidth - 20;
|
|
45
|
-
const isNearLeftEdge = pageX - scrollLeft < 20;
|
|
46
|
-
|
|
47
|
-
if (isNearRightEdge && this._canScrollRight()) {
|
|
48
|
-
return "right";
|
|
49
|
-
}
|
|
50
|
-
|
|
51
|
-
if (isNearLeftEdge) {
|
|
52
|
-
return "left";
|
|
53
|
-
}
|
|
54
|
-
|
|
55
|
-
return undefined;
|
|
56
|
-
}
|
|
57
|
-
|
|
58
|
-
protected getNewVerticalScrollDirection(
|
|
59
|
-
pageY: number,
|
|
60
|
-
): undefined | VerticalScrollDirection {
|
|
61
|
-
const scrollTop = this.container.scrollTop;
|
|
62
|
-
const distanceTop = pageY - scrollTop;
|
|
63
|
-
|
|
64
|
-
if (distanceTop < 20) {
|
|
65
|
-
return "top";
|
|
66
|
-
}
|
|
67
|
-
|
|
68
|
-
const windowHeight = window.innerHeight;
|
|
69
|
-
|
|
70
|
-
if (windowHeight - (pageY - scrollTop) < 20 && this._canScrollDown()) {
|
|
71
|
-
return "bottom";
|
|
72
|
-
}
|
|
73
|
-
|
|
74
|
-
return undefined;
|
|
75
|
-
}
|
|
76
|
-
|
|
77
|
-
private _canScrollDown() {
|
|
78
|
-
return (
|
|
79
|
-
this.container.scrollTop + this.container.clientHeight <
|
|
80
|
-
this._getDocumentScrollHeight()
|
|
81
|
-
);
|
|
82
|
-
}
|
|
83
|
-
|
|
84
|
-
private _canScrollRight() {
|
|
85
|
-
return (
|
|
86
|
-
this.container.scrollLeft + this.container.clientWidth <
|
|
87
|
-
this._getDocumentScrollWidth()
|
|
88
|
-
);
|
|
89
|
-
}
|
|
90
|
-
|
|
91
|
-
private _getDocumentScrollHeight() {
|
|
92
|
-
// Store the original scroll height because the scroll height can increase when the drag element is moved beyond the scroll height.
|
|
93
|
-
this._documentScrollHeight ??= this.container.scrollHeight;
|
|
94
|
-
|
|
95
|
-
return this._documentScrollHeight;
|
|
96
|
-
}
|
|
97
|
-
|
|
98
|
-
private _getDocumentScrollWidth() {
|
|
99
|
-
// Store the original scroll width because the scroll width can increase when the drag element is moved beyond the scroll width.
|
|
100
|
-
this._documentScrollWidth ??= this.container.scrollWidth;
|
|
101
|
-
|
|
102
|
-
return this._documentScrollWidth;
|
|
103
|
-
}
|
|
104
|
-
}
|
|
@@ -1,117 +0,0 @@
|
|
|
1
|
-
export type HorizontalScrollDirection = "left" | "right";
|
|
2
|
-
export type VerticalScrollDirection = "bottom" | "top";
|
|
3
|
-
|
|
4
|
-
interface ConstructorParams {
|
|
5
|
-
container: HTMLElement;
|
|
6
|
-
refreshHitAreas: () => void;
|
|
7
|
-
}
|
|
8
|
-
|
|
9
|
-
export abstract class ScrollParent {
|
|
10
|
-
protected container: HTMLElement;
|
|
11
|
-
protected horizontalScrollDirection?: HorizontalScrollDirection;
|
|
12
|
-
protected horizontalScrollTimeout?: number;
|
|
13
|
-
|
|
14
|
-
protected refreshHitAreas: () => void;
|
|
15
|
-
protected verticalScrollDirection?: VerticalScrollDirection;
|
|
16
|
-
protected verticalScrollTimeout?: number;
|
|
17
|
-
|
|
18
|
-
constructor({ container, refreshHitAreas }: ConstructorParams) {
|
|
19
|
-
this.container = container;
|
|
20
|
-
this.refreshHitAreas = refreshHitAreas;
|
|
21
|
-
}
|
|
22
|
-
|
|
23
|
-
public checkHorizontalScrolling(pageX: number): void {
|
|
24
|
-
const newHorizontalScrollDirection =
|
|
25
|
-
this.getNewHorizontalScrollDirection(pageX);
|
|
26
|
-
|
|
27
|
-
if (this.horizontalScrollDirection !== newHorizontalScrollDirection) {
|
|
28
|
-
this.horizontalScrollDirection = newHorizontalScrollDirection;
|
|
29
|
-
|
|
30
|
-
if (this.horizontalScrollTimeout != null) {
|
|
31
|
-
window.clearTimeout(this.horizontalScrollTimeout);
|
|
32
|
-
}
|
|
33
|
-
|
|
34
|
-
if (newHorizontalScrollDirection) {
|
|
35
|
-
this.horizontalScrollTimeout = window.setTimeout(
|
|
36
|
-
this.scrollHorizontally.bind(this),
|
|
37
|
-
40,
|
|
38
|
-
);
|
|
39
|
-
}
|
|
40
|
-
}
|
|
41
|
-
}
|
|
42
|
-
|
|
43
|
-
public checkVerticalScrolling(pageY: number) {
|
|
44
|
-
const newVerticalScrollDirection =
|
|
45
|
-
this.getNewVerticalScrollDirection(pageY);
|
|
46
|
-
|
|
47
|
-
if (this.verticalScrollDirection !== newVerticalScrollDirection) {
|
|
48
|
-
this.verticalScrollDirection = newVerticalScrollDirection;
|
|
49
|
-
|
|
50
|
-
if (this.verticalScrollTimeout != null) {
|
|
51
|
-
window.clearTimeout(this.verticalScrollTimeout);
|
|
52
|
-
this.verticalScrollTimeout = undefined;
|
|
53
|
-
}
|
|
54
|
-
|
|
55
|
-
if (newVerticalScrollDirection) {
|
|
56
|
-
this.verticalScrollTimeout = window.setTimeout(
|
|
57
|
-
this.scrollVertically.bind(this),
|
|
58
|
-
40,
|
|
59
|
-
);
|
|
60
|
-
}
|
|
61
|
-
}
|
|
62
|
-
}
|
|
63
|
-
|
|
64
|
-
public getScrollLeft(): number {
|
|
65
|
-
return this.container.scrollLeft;
|
|
66
|
-
}
|
|
67
|
-
|
|
68
|
-
public scrollToY(top: number): void {
|
|
69
|
-
this.container.scrollTop = top;
|
|
70
|
-
}
|
|
71
|
-
|
|
72
|
-
public stopScrolling() {
|
|
73
|
-
this.horizontalScrollDirection = undefined;
|
|
74
|
-
this.verticalScrollDirection = undefined;
|
|
75
|
-
}
|
|
76
|
-
|
|
77
|
-
protected abstract getNewHorizontalScrollDirection(
|
|
78
|
-
pageX: number,
|
|
79
|
-
): HorizontalScrollDirection | undefined;
|
|
80
|
-
protected abstract getNewVerticalScrollDirection(
|
|
81
|
-
pageY: number,
|
|
82
|
-
): undefined | VerticalScrollDirection;
|
|
83
|
-
|
|
84
|
-
protected scrollHorizontally() {
|
|
85
|
-
if (!this.horizontalScrollDirection) {
|
|
86
|
-
return;
|
|
87
|
-
}
|
|
88
|
-
|
|
89
|
-
const distance = this.horizontalScrollDirection === "left" ? -20 : 20;
|
|
90
|
-
this.container.scrollBy({
|
|
91
|
-
behavior: "instant",
|
|
92
|
-
left: distance,
|
|
93
|
-
top: 0,
|
|
94
|
-
});
|
|
95
|
-
|
|
96
|
-
this.refreshHitAreas();
|
|
97
|
-
|
|
98
|
-
setTimeout(this.scrollHorizontally.bind(this), 40);
|
|
99
|
-
}
|
|
100
|
-
|
|
101
|
-
protected scrollVertically() {
|
|
102
|
-
if (!this.verticalScrollDirection) {
|
|
103
|
-
return;
|
|
104
|
-
}
|
|
105
|
-
|
|
106
|
-
const distance = this.verticalScrollDirection === "top" ? -20 : 20;
|
|
107
|
-
this.container.scrollBy({
|
|
108
|
-
behavior: "instant",
|
|
109
|
-
left: 0,
|
|
110
|
-
top: distance,
|
|
111
|
-
});
|
|
112
|
-
|
|
113
|
-
this.refreshHitAreas();
|
|
114
|
-
|
|
115
|
-
setTimeout(this.scrollVertically.bind(this), 40);
|
|
116
|
-
}
|
|
117
|
-
}
|
package/src/scrollHandler.ts
DELETED
|
@@ -1,55 +0,0 @@
|
|
|
1
|
-
import type { PositionInfo } from "./mouseUtils";
|
|
2
|
-
import type { ScrollParent } from "./scrollHandler/scrollParent";
|
|
3
|
-
|
|
4
|
-
import createScrollParent from "./scrollHandler/createScrollParent";
|
|
5
|
-
|
|
6
|
-
interface ScrollHandlerParams {
|
|
7
|
-
refreshHitAreas: () => void;
|
|
8
|
-
treeElement: HTMLElement;
|
|
9
|
-
}
|
|
10
|
-
|
|
11
|
-
export default class ScrollHandler {
|
|
12
|
-
private _refreshHitAreas: () => void;
|
|
13
|
-
private _scrollParent?: ScrollParent;
|
|
14
|
-
private _treeElement: HTMLElement;
|
|
15
|
-
|
|
16
|
-
constructor({ refreshHitAreas, treeElement }: ScrollHandlerParams) {
|
|
17
|
-
this._refreshHitAreas = refreshHitAreas;
|
|
18
|
-
this._scrollParent = undefined;
|
|
19
|
-
this._treeElement = treeElement;
|
|
20
|
-
}
|
|
21
|
-
|
|
22
|
-
public checkScrolling(positionInfo: PositionInfo): void {
|
|
23
|
-
this._checkVerticalScrolling(positionInfo);
|
|
24
|
-
this._checkHorizontalScrolling(positionInfo);
|
|
25
|
-
}
|
|
26
|
-
|
|
27
|
-
public getScrollLeft(): number {
|
|
28
|
-
return this._getScrollParent().getScrollLeft();
|
|
29
|
-
}
|
|
30
|
-
|
|
31
|
-
public scrollToY(top: number): void {
|
|
32
|
-
this._getScrollParent().scrollToY(top);
|
|
33
|
-
}
|
|
34
|
-
|
|
35
|
-
public stopScrolling() {
|
|
36
|
-
this._getScrollParent().stopScrolling();
|
|
37
|
-
}
|
|
38
|
-
|
|
39
|
-
private _checkHorizontalScrolling(positionInfo: PositionInfo): void {
|
|
40
|
-
this._getScrollParent().checkHorizontalScrolling(positionInfo.pageX);
|
|
41
|
-
}
|
|
42
|
-
|
|
43
|
-
private _checkVerticalScrolling(positionInfo: PositionInfo): void {
|
|
44
|
-
this._getScrollParent().checkVerticalScrolling(positionInfo.pageY);
|
|
45
|
-
}
|
|
46
|
-
|
|
47
|
-
private _getScrollParent(): ScrollParent {
|
|
48
|
-
this._scrollParent ??= createScrollParent(
|
|
49
|
-
this._treeElement,
|
|
50
|
-
this._refreshHitAreas,
|
|
51
|
-
);
|
|
52
|
-
|
|
53
|
-
return this._scrollParent;
|
|
54
|
-
}
|
|
55
|
-
}
|
package/src/selectNodeHandler.ts
DELETED
|
@@ -1,111 +0,0 @@
|
|
|
1
|
-
import type { GetNodeById } from "./jqtreeMethodTypes";
|
|
2
|
-
import type { Node } from "./node";
|
|
3
|
-
|
|
4
|
-
interface SelectNodeHandlerParameters {
|
|
5
|
-
getNodeById: GetNodeById;
|
|
6
|
-
}
|
|
7
|
-
|
|
8
|
-
export default class SelectNodeHandler {
|
|
9
|
-
private _getNodeById: GetNodeById;
|
|
10
|
-
private _selectedNodes: Set<NodeId>;
|
|
11
|
-
private _selectedSingleNode: Node | null;
|
|
12
|
-
|
|
13
|
-
constructor({ getNodeById }: SelectNodeHandlerParameters) {
|
|
14
|
-
this._getNodeById = getNodeById;
|
|
15
|
-
this._selectedNodes = new Set<NodeId>();
|
|
16
|
-
this.clear();
|
|
17
|
-
}
|
|
18
|
-
|
|
19
|
-
public addToSelection(node: Node): void {
|
|
20
|
-
if (node.id != null) {
|
|
21
|
-
this._selectedNodes.add(node.id);
|
|
22
|
-
} else {
|
|
23
|
-
this._selectedSingleNode = node;
|
|
24
|
-
}
|
|
25
|
-
}
|
|
26
|
-
|
|
27
|
-
public clear(): void {
|
|
28
|
-
this._selectedNodes.clear();
|
|
29
|
-
this._selectedSingleNode = null;
|
|
30
|
-
}
|
|
31
|
-
|
|
32
|
-
public getSelectedNode(): false | Node {
|
|
33
|
-
const selectedNodes = this.getSelectedNodes();
|
|
34
|
-
|
|
35
|
-
if (selectedNodes.length) {
|
|
36
|
-
return selectedNodes[0] ?? false;
|
|
37
|
-
} else {
|
|
38
|
-
return false;
|
|
39
|
-
}
|
|
40
|
-
}
|
|
41
|
-
|
|
42
|
-
public getSelectedNodes(): Node[] {
|
|
43
|
-
if (this._selectedSingleNode) {
|
|
44
|
-
return [this._selectedSingleNode];
|
|
45
|
-
} else {
|
|
46
|
-
const selectedNodes: Node[] = [];
|
|
47
|
-
|
|
48
|
-
this._selectedNodes.forEach((id) => {
|
|
49
|
-
const node = this._getNodeById(id);
|
|
50
|
-
if (node) {
|
|
51
|
-
selectedNodes.push(node);
|
|
52
|
-
}
|
|
53
|
-
});
|
|
54
|
-
|
|
55
|
-
return selectedNodes;
|
|
56
|
-
}
|
|
57
|
-
}
|
|
58
|
-
|
|
59
|
-
public getSelectedNodesUnder(parent: Node): Node[] {
|
|
60
|
-
if (this._selectedSingleNode) {
|
|
61
|
-
if (parent.isParentOf(this._selectedSingleNode)) {
|
|
62
|
-
return [this._selectedSingleNode];
|
|
63
|
-
} else {
|
|
64
|
-
return [];
|
|
65
|
-
}
|
|
66
|
-
} else {
|
|
67
|
-
const selectedNodes: Node[] = [];
|
|
68
|
-
|
|
69
|
-
this._selectedNodes.forEach((id) => {
|
|
70
|
-
const node = this._getNodeById(id);
|
|
71
|
-
if (node && parent.isParentOf(node)) {
|
|
72
|
-
selectedNodes.push(node);
|
|
73
|
-
}
|
|
74
|
-
});
|
|
75
|
-
|
|
76
|
-
return selectedNodes;
|
|
77
|
-
}
|
|
78
|
-
}
|
|
79
|
-
|
|
80
|
-
public isNodeSelected(node: Node): boolean {
|
|
81
|
-
if (node.id != null) {
|
|
82
|
-
return this._selectedNodes.has(node.id);
|
|
83
|
-
} else if (this._selectedSingleNode) {
|
|
84
|
-
return this._selectedSingleNode.element === node.element;
|
|
85
|
-
} else {
|
|
86
|
-
return false;
|
|
87
|
-
}
|
|
88
|
-
}
|
|
89
|
-
|
|
90
|
-
public removeFromSelection(node: Node, includeChildren = false): void {
|
|
91
|
-
if (node.id == null) {
|
|
92
|
-
if (
|
|
93
|
-
this._selectedSingleNode &&
|
|
94
|
-
node.element === this._selectedSingleNode.element
|
|
95
|
-
) {
|
|
96
|
-
this._selectedSingleNode = null;
|
|
97
|
-
}
|
|
98
|
-
} else {
|
|
99
|
-
this._selectedNodes.delete(node.id);
|
|
100
|
-
|
|
101
|
-
if (includeChildren) {
|
|
102
|
-
node.iterate(() => {
|
|
103
|
-
if (node.id != null) {
|
|
104
|
-
this._selectedNodes.delete(node.id);
|
|
105
|
-
}
|
|
106
|
-
return true;
|
|
107
|
-
});
|
|
108
|
-
}
|
|
109
|
-
}
|
|
110
|
-
}
|
|
111
|
-
}
|
package/src/simple.widget.ts
DELETED
|
@@ -1,134 +0,0 @@
|
|
|
1
|
-
const register = (widgetClass: unknown, widgetName: string): void => {
|
|
2
|
-
const getDataKey = (): string => `simple_widget_${widgetName}`;
|
|
3
|
-
|
|
4
|
-
const getWidgetData = (
|
|
5
|
-
el: HTMLElement,
|
|
6
|
-
dataKey: string,
|
|
7
|
-
): null | SimpleWidget<unknown> => {
|
|
8
|
-
const widget = jQuery.data(el, dataKey) as unknown;
|
|
9
|
-
|
|
10
|
-
if (widget && widget instanceof SimpleWidget) {
|
|
11
|
-
return widget;
|
|
12
|
-
} else {
|
|
13
|
-
return null;
|
|
14
|
-
}
|
|
15
|
-
};
|
|
16
|
-
|
|
17
|
-
const createWidget = ($el: JQuery, options: unknown): JQuery => {
|
|
18
|
-
const dataKey = getDataKey();
|
|
19
|
-
|
|
20
|
-
for (const el of $el.get()) {
|
|
21
|
-
const existingWidget = getWidgetData(el, dataKey);
|
|
22
|
-
|
|
23
|
-
if (!existingWidget) {
|
|
24
|
-
const simpleWidgetClass = widgetClass as typeof SimpleWidget;
|
|
25
|
-
const widget = new simpleWidgetClass(el, options);
|
|
26
|
-
|
|
27
|
-
if (!jQuery.data(el, dataKey)) {
|
|
28
|
-
jQuery.data(el, dataKey, widget);
|
|
29
|
-
}
|
|
30
|
-
|
|
31
|
-
// Call init after setting data, so we can call methods
|
|
32
|
-
widget.init();
|
|
33
|
-
}
|
|
34
|
-
}
|
|
35
|
-
|
|
36
|
-
return $el;
|
|
37
|
-
};
|
|
38
|
-
|
|
39
|
-
const destroyWidget = ($el: JQuery): void => {
|
|
40
|
-
const dataKey = getDataKey();
|
|
41
|
-
|
|
42
|
-
for (const el of $el.get()) {
|
|
43
|
-
const widget = getWidgetData(el, dataKey);
|
|
44
|
-
|
|
45
|
-
if (widget) {
|
|
46
|
-
widget.destroy();
|
|
47
|
-
}
|
|
48
|
-
|
|
49
|
-
jQuery.removeData(el, dataKey);
|
|
50
|
-
}
|
|
51
|
-
};
|
|
52
|
-
|
|
53
|
-
const callFunction = (
|
|
54
|
-
$el: JQuery,
|
|
55
|
-
functionName: string,
|
|
56
|
-
args: unknown[],
|
|
57
|
-
): unknown => {
|
|
58
|
-
let result = null;
|
|
59
|
-
|
|
60
|
-
for (const el of $el.get()) {
|
|
61
|
-
const widget = jQuery.data(el, getDataKey()) as unknown;
|
|
62
|
-
|
|
63
|
-
if (widget && widget instanceof SimpleWidget) {
|
|
64
|
-
const simpleWidget = widget as SimpleWidget<unknown>;
|
|
65
|
-
const widgetFunction = simpleWidget[functionName];
|
|
66
|
-
|
|
67
|
-
if (widgetFunction && typeof widgetFunction === "function") {
|
|
68
|
-
result = widgetFunction.apply(widget, args) as unknown;
|
|
69
|
-
}
|
|
70
|
-
}
|
|
71
|
-
}
|
|
72
|
-
|
|
73
|
-
return result;
|
|
74
|
-
};
|
|
75
|
-
|
|
76
|
-
// eslint-disable-next-line @typescript-eslint/no-unsafe-member-access
|
|
77
|
-
(jQuery.fn as any)[widgetName] = function (
|
|
78
|
-
this: JQuery,
|
|
79
|
-
argument1: unknown,
|
|
80
|
-
...args: unknown[]
|
|
81
|
-
) {
|
|
82
|
-
if (!argument1) {
|
|
83
|
-
return createWidget(this, null);
|
|
84
|
-
} else if (typeof argument1 === "object") {
|
|
85
|
-
const options = argument1 as unknown;
|
|
86
|
-
return createWidget(this, options);
|
|
87
|
-
} else if (typeof argument1 === "string" && argument1[0] !== "_") {
|
|
88
|
-
const functionName = argument1;
|
|
89
|
-
|
|
90
|
-
if (argument1 === "destroy") {
|
|
91
|
-
destroyWidget(this);
|
|
92
|
-
return undefined;
|
|
93
|
-
} else {
|
|
94
|
-
return callFunction(this, functionName, args);
|
|
95
|
-
}
|
|
96
|
-
} else {
|
|
97
|
-
return undefined;
|
|
98
|
-
}
|
|
99
|
-
};
|
|
100
|
-
};
|
|
101
|
-
|
|
102
|
-
export default class SimpleWidget<WidgetOptions> {
|
|
103
|
-
[key: string]: unknown;
|
|
104
|
-
|
|
105
|
-
protected static defaults: unknown = {};
|
|
106
|
-
|
|
107
|
-
public $el: JQuery;
|
|
108
|
-
|
|
109
|
-
public options: WidgetOptions;
|
|
110
|
-
|
|
111
|
-
constructor(el: HTMLElement, options: WidgetOptions) {
|
|
112
|
-
this.$el = jQuery(el);
|
|
113
|
-
|
|
114
|
-
// eslint-disable-next-line @typescript-eslint/no-unsafe-member-access
|
|
115
|
-
const defaults = (this.constructor as any).defaults as WidgetOptions;
|
|
116
|
-
this.options = { ...defaults, ...options };
|
|
117
|
-
}
|
|
118
|
-
|
|
119
|
-
public static register(widgetClass: unknown, widgetName: string): void {
|
|
120
|
-
register(widgetClass, widgetName);
|
|
121
|
-
}
|
|
122
|
-
|
|
123
|
-
public deinit(): void {
|
|
124
|
-
//
|
|
125
|
-
}
|
|
126
|
-
|
|
127
|
-
public destroy(): void {
|
|
128
|
-
this.deinit();
|
|
129
|
-
}
|
|
130
|
-
|
|
131
|
-
public init(): void {
|
|
132
|
-
//
|
|
133
|
-
}
|
|
134
|
-
}
|