-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathhierarchical-tree-view-item.ts
153 lines (134 loc) · 5.28 KB
/
hierarchical-tree-view-item.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
import { ICustomItemExtension, CustomItemViewer, DashboardControl } from 'devexpress-dashboard/common';
import { ICustomItemMetaData } from 'devexpress-dashboard/model/items/custom-item/meta';
import dxTreeView, { Node, Properties } from 'devextreme/ui/tree_view';
const HIERARCIAL_TREE_VIEW_EXTENSION_NAME = 'TreeView';
const svgIcon = `<?xml version="1.0" encoding="utf-8"?>
<svg version="1.1" id="` + HIERARCIAL_TREE_VIEW_EXTENSION_NAME + `" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" x="0px" y="0px" viewBox="0 0 24 24" style="enable-background:new 0 0 24 24;" xml:space="preserve">
<polygon class="dx-dashboard-contrast-icon" points="12,13 12,11 8,11 8,8 6,8 6,21 12,21 12,19 8,19 8,13 "/>
<path class="dx-dashboard-accent-icon" d="M10,7H4C3.5,7,3,6.6,3,6V2c0-0.5,0.5-1,1-1h6c0.6,0,1,0.5,1,1v4C11,6.6,10.6,7,10,7z
M21,14v-4c0-0.6-0.5-1-1-1h-6c-0.6,0-1,0.4-1,1v4c0,0.6,0.4,1,1,1h6C20.5,15,21,14.6,21,14z M21,22v-4c0-0.5-0.5-1-1-1h-6
c-0.6,0-1,0.5-1,1v4c0,0.5,0.4,1,1,1h6C20.5,23,21,22.5,21,22z"/>
</svg>`;
const treeViewMeta: ICustomItemMetaData = {
bindings: [{
propertyName: 'idBinding',
dataItemType: 'Dimension',
array: false,
displayName: 'ID',
emptyPlaceholder: 'Add ID',
selectedPlaceholder: 'Configure ID',
}, {
propertyName: 'parentIdBinding',
dataItemType: 'Dimension',
array: false,
displayName: 'Parent ID',
emptyPlaceholder: 'Add Parent ID',
selectedPlaceholder: 'Configure Parent ID',
}, {
propertyName: 'dimensionsBinding',
dataItemType: 'Dimension',
array: false,
displayName: 'Dimensions',
emptyPlaceholder: 'Add Dimension',
selectedPlaceholder: 'Configure Dimension',
enableInteractivity: true
}],
interactivity: {
filter: true
},
icon: HIERARCIAL_TREE_VIEW_EXTENSION_NAME,
// Uncomment the line below to place this custom item in the "Filter" group:
//groupName: 'filter',
title: 'Hierarchical Tree View'
};
export class TreeViewItemExtension implements ICustomItemExtension {
name = HIERARCIAL_TREE_VIEW_EXTENSION_NAME;
metaData = <ICustomItemMetaData>treeViewMeta;
dashboardControl: DashboardControl;
constructor(dashboardControl: DashboardControl) {
this.dashboardControl = dashboardControl;
dashboardControl.registerIcon(svgIcon);
}
public createViewerItem = (model: any, element: any, content: any) => {
return new TreeViewItem(model, element, content, this.dashboardControl);
}
}
export class TreeViewItem extends CustomItemViewer {
private dxTreeViewWidget?: dxTreeView;
private _requiredBindingsCount: number;
private dashboardControl: DashboardControl;
constructor(model: any, container: any, options: any, dashboardControl: DashboardControl) {
super(model, container, options);
this._requiredBindingsCount = 3;
this.dashboardControl = dashboardControl;
}
override renderContent(element: HTMLElement, changeExisting: boolean, afterRenderCallback?: any) {
let dataSource: any[] = [];
//Check Bindings
let bindings = this.getBindingValue('dimensionsBinding').concat(this.getBindingValue('idBinding')).concat(this.getBindingValue('parentIdBinding'));
if (bindings.length !== this._requiredBindingsCount)
return;
//Get Data Source
this.iterateData(function (dataRow) {
let row = <any>{
ID: dataRow.getDisplayText('idBinding')[0],
ParentID: dataRow.getDisplayText('parentIdBinding')[0] !== '-1' ? dataRow.getDisplayText('parentIdBinding')[0] : null,
DisplayField: dataRow.getDisplayText('dimensionsBinding')[0],
};
row._customData = dataRow;
dataSource.push(row);
});
let treeViewOptions: Properties = {
items: dataSource,
dataStructure: "plain",
parentIdExpr: "ParentID",
keyExpr: "ID",
displayExpr: "DisplayField",
selectionMode: "multiple",
selectNodesRecursive: false,
onItemClick: e => {
if(this.getMasterFilterMode() === 'Multiple' && this.allowMultiselection) {
this.setMasterFilterRecursive(<any>e.node);
}
else {
this.setMasterFilter((<any>e.itemData)._customData);
}
},
onContentReady: e => {
this.updateTreeViewSelection();
}
};
if (!changeExisting) {
while (element.firstChild)
element.removeChild(element.firstChild);
let div = document.createElement('div');
element.appendChild(div);
this.dxTreeViewWidget = new dxTreeView(div, treeViewOptions);
}
else {
this.dxTreeViewWidget?.option(treeViewOptions);
}
}
override clearSelection(): void {
super.clearSelection();
this.updateTreeViewSelection();
}
override setSelection(values: Array<Array<any>>): void {
super.setSelection(values);
this.updateTreeViewSelection();
}
updateTreeViewSelection() {
if (this.dxTreeViewWidget) {
this.dxTreeViewWidget.unselectAll();
let nodes: any = this.dxTreeViewWidget.option('items');
nodes.forEach((item: any) => {
if(this.isSelected(item._customData))
this.dxTreeViewWidget?.selectItem(item.ID);
});
}
}
setMasterFilterRecursive(node: Node) {
this.setMasterFilter((<any>node.itemData)._customData);
node.children?.forEach(x => this.setMasterFilterRecursive(x));
}
}