Newer
Older
import { NestedTreeControl } from '@angular/cdk/tree';
import { Component, Input, OnInit, HostListener, Output, EventEmitter } from '@angular/core';
import { MatTreeNestedDataSource } from '@angular/material/tree';
import { SortPipe } from '../../plugins/sorting.pipe';
import { FormControl } from '@angular/forms';
import { tap, debounceTime } from 'rxjs/operators';
import { LatinisePipe } from 'ngx-pipes';
/** Flat node with expandable and level information */
interface ExampleFlatNode {
expandable: boolean;
item: string;
parent_id: string;
level: number;
}
/**
* @title Tree with flat nodes
*/
@Component({
selector: 'app-maarch-flat-tree',
templateUrl: 'maarch-flat-tree.component.html',
styleUrls: ['maarch-flat-tree.component.scss'],
providers: [SortPipe],
})
export class MaarchFlatTreeComponent implements OnInit {
@Input() rawData: any = [];
@Input() selectionPropagation: boolean = true;
@Input() openState: string = '';
@Output() afterSelectNode = new EventEmitter<any>();
@Output() afterDeselectNode = new EventEmitter<any>();
holdShift: boolean = false;
defaultOpenedNodes: any[] = [];
treeControl = new NestedTreeControl<any>(node => node.children);
dataSource = new MatTreeNestedDataSource<any>();
searchMode: boolean = false;
searchTerm: FormControl = new FormControl('');
lastSelectedNodeIds: any[] = [];
pendingChildOf: any = {};
temp: any = {};
@HostListener('document:keydown.Shift', ['$event']) onKeydownHandler(event: KeyboardEvent) {
if (this.selectionPropagation) {
this.holdShift = true;
}
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
}
@HostListener('document:keyup.Shift', ['$event']) onKeyupHandler(event: KeyboardEvent) {
this.holdShift = false;
}
constructor(
private sortPipe: SortPipe,
private latinisePipe: LatinisePipe,
) { }
ngOnInit(): void {
// SAMPLE
/* this.rawData = [
{
id: '46',
text: 'bonjour',
parent_id: null,
icon: 'fa fa-building',
state: {
selected: true,
}
},
{
id: '42',
text: 'coucou',
parent_id: '46',
icon: 'fa fa-building',
state: {
selected: true,
}
},
{
id: '41',
text: 'coucou',
parent_id: '42',
icon: 'fa fa-building',
state: {
selected: true,
}
},
{
id: '1',
text: 'Compétences fonctionnelles',
parent_id: null,
icon: 'fa fa-building',
state: {
selected: true,
}
},
{
id: '232',
text: 'Compétences technique',
parent_id: null,
icon: 'fa fa-building',
state: {
selected: true,
}
}
]; */
if (this.rawData.length > 0) {
this.initData();
}
}
initData(data: any = this.rawData) {
this.rawData = data;
// Catch all nodes Ids to open tree nodes in root level
if (this.openState !== 'all') {
this.setDefaultOpened();
}
this.rawData = data.map((item: any) => {
return {
...item,
parent_id: item.parent_id === '#' || item.parent_id === '' ? null : item.parent_id,
state: item.state !== undefined ? {
selected: item.state.selected,
opened: item.state.opened || this.defaultOpenedNodes.indexOf(item.id) > -1 || this.openState === 'all',
disabled: item.state.disabled,
} : {
selected: false,
opened: this.defaultOpenedNodes.indexOf(item.id) > -1 || this.openState === 'all',
disabled: false,
}
// order data
this.rawData = this.sortPipe.transform(this.rawData, 'text');
// Convert flat data to nested data
let FlatToNested, flatToNested;
FlatToNested = require('flat-to-nested');
flatToNested = new FlatToNested({
id: 'id',
parent: 'parent_id',
children: 'children',
options : { deleteParent: false }
});
let nestedData = flatToNested.convert(this.rawData);
nestedData = nestedData.children;
// Set last child of children nodes to fix css tree lines
this.initLastNodes(nestedData);
// Set data to tree
this.dataSource.data = nestedData;
this.treeControl.dataNodes = nestedData;
// Set search filter
this.searchTerm.valueChanges
.pipe(
debounceTime(300),
// filter(value => value.length > 2),
tap((filterValue: any) => {
filterValue = filterValue.trim(); // Remove whitespace
filterValue = filterValue.toLowerCase(); // MatTableDataSource defaults to lowercase matches
this.searchNode(this.dataSource.data, filterValue);
}),
).subscribe();
}
setDefaultOpened() {
this.rawData.filter((item: any) => item.state !== undefined && item.state.opened).forEach((item: any) => {
this.defaultOpenedNodes = this.defaultOpenedNodes.concat(this.getParents([item]));
});
this.defaultOpenedNodes = this.defaultOpenedNodes.map((node: any) => node.id);
}
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
getData(id: any) {
return this.rawData.filter((elem: any) => elem.id === id)[0];
}
getIteration(it: number) {
return Array(it).fill(0).map((x, i) => i);
}
hasChild = (_: number, node: any) => !!node.children && node.children.length > 0;
selectNode(node: any) {
if (!node.state.disabled) {
if (this.searchMode) {
this.searchMode = false;
this.searchTerm.setValue('');
}
this.lastSelectedNodeIds = [];
if (this.holdShift) {
this.toggleNode(
this.dataSource.data,
{
selected: !node.state.selected,
opened: true
},
[node.id]
);
} else {
node.state.selected = !node.state.selected;
this.lastSelectedNodeIds = [node];
}
if (node.state.selected) {
this.afterSelectNode.emit(this.lastSelectedNodeIds);
} else {
this.afterDeselectNode.emit(this.lastSelectedNodeIds);
}
}
}
toggleNode(data, state, nodeIds) {
// traverse throuh each node
if (Array.isArray(data)) { // if data is an array
data.forEach((d) => {
if (nodeIds.indexOf(d.id) > -1 || (this.holdShift && nodeIds.indexOf(d.parent_id) > -1)) {
Object.keys(state).forEach(key => {
if (d.state.disabled && key === 'opened') {
d.state[key] = state[key];
} else if (!d.state.disabled) {
d.state[key] = state[key];
if (key === 'selected') {
this.lastSelectedNodeIds.push(d);
}
}
});
}
if (this.holdShift && nodeIds.indexOf(d.parent_id) > -1) {
nodeIds.push(d.id);
}
this.toggleNode(d, state, nodeIds);
}); // call the function on each item
} else if (data instanceof Object) { // otherwise, if data is an object
(data.children || []).forEach((f) => {
if (nodeIds.indexOf(f.id) > -1 || (this.holdShift && nodeIds.indexOf(f.parent_id) > -1)) {
Object.keys(state).forEach(key => {
if (f.state.disabled && key === 'opened') {
f.state[key] = state[key];
} else if (!f.state.disabled) {
f.state[key] = state[key];
if (key === 'selected') {
this.lastSelectedNodeIds.push(f);
}
}
});
}
if (this.holdShift && nodeIds.indexOf(f.parent_id) > -1) {
nodeIds.push(f.id);
}
this.toggleNode(f, state, nodeIds);
}); // and call function on each child
}
}
getParents(node: any[]) {
const res = this.rawData.filter((data: any) => data.id === node[node.length - 1].parent_id);
if (res.length > 0) {
node.push(res[0]);
return this.getParents(node);
} else {
return node;
}
}
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
searchNode(data, term) {
this.searchMode = term !== '';
// traverse throuh each node
if (Array.isArray(data)) { // if data is an array
data.forEach((d) => {
d.state.opened = true;
if (this.latinisePipe.transform(d.text.toLowerCase()).indexOf(this.latinisePipe.transform(term)) > -1) {
d.state.search = true;
} else if (term === '') {
delete d.state.search;
} else {
d.state.search = false;
}
this.searchNode(d, term);
}); // call the function on each item
} else if (data instanceof Object) { // otherwise, if data is an object
(data.children || []).forEach((f) => {
f.state.opened = true;
if (this.latinisePipe.transform(f.text.toLowerCase()).indexOf(this.latinisePipe.transform(term)) > -1) {
f.state.search = true;
} else if (term === '') {
delete f.state.search;
} else {
f.state.search = false;
}
this.searchNode(f, term);
}); // and call function on each child
}
}
initLastNodes(data) {
// traverse throuh each node
if (Array.isArray(data)) { // if data is an array
data.forEach((d, index) => {
if (index === data.length - 1) {
d.last = true;
}
this.initLastNodes(d);
}); // call the function on each item
} else if (data instanceof Object) { // otherwise, if data is an object
(data.children || []).forEach((f, index) => {
if (index === data.children.length - 1) {
f.last = true;
}
this.initLastNodes(f);
}); // and call function on each child
}
}
getSelectedNodes() {
return this.rawData.filter((data: any) => data.state.selected);
}
}