-
Notifications
You must be signed in to change notification settings - Fork 4
/
table-container.component.ts
186 lines (172 loc) · 5.91 KB
/
table-container.component.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
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
import { Component, ViewChild, AfterViewInit } from '@angular/core';
import { ActivatedRoute, Params, Router } from "@angular/router";
import { MdDialogRef, MdDialog, MdDialogConfig } from "@angular/material";
import { Pagination, TableFilter, SearchString, PriorityKeysPipe, HeaderItem } from "@nodeart/ngfb_sortable_table";
import { Observable } from "rxjs";
import { EmployerItemComponent } from "../employer-item/employer-item.component";
import { EmployeeItemComponent } from "../employee-item/employee-item.component";
import { NewPersonComponent } from "../new-person/new-person.component";
import { AlertComponent } from "../alert/alert.component";
import { EmployeePriority, EmployersPriority } from "./enums";
@Component({
selector: 'app-table-container',
templateUrl: './table-container.component.html',
styleUrls: ['./table-container.component.scss'],
providers : [ PriorityKeysPipe ]
})
export class TableContainerComponent implements AfterViewInit {
@ViewChild('sidenav') public sidenav;
@ViewChild('toggler') public toggler;
public isSidenavOpen = true;
public toFetch: string;
public sideNavItems: Array<{name: string, toFetch: string}>;
public itemComponent: Component = EmployeeItemComponent;
public pagination: Pagination = {
placeholder: 'Upload length',
defaultOption: 20,
options: [20, 50, 100]
};
public filterBySelect: TableFilter | null;
public filterByInputValue: SearchString | null;
public addNew: Component | null;
public afterPopupClose: Function | null;
constructor(
public router: Router,
public route: ActivatedRoute,
public keysPipe: PriorityKeysPipe,
public dialog: MdDialog
) {
this.setHeaders = this.setHeaders.bind(this);
for (let i = 0, l = this.router.config.length; i < l; i++) {
if (this.router.config[i].path === 'app/:param') {
this.sideNavItems = this.router.config[i].data['tables'];
}
}
route.params.subscribe((params : Params) => {
this.toFetch = params['param'];
if (this.toFetch === 'employees') {
this.itemComponent = EmployeeItemComponent;
this.filterBySelect = {
field: 'favoriteFramework',
defaultOption: null,
resetTo: null,
placeholder: 'Filter by yor favorite framework',
options: [{
value: null,
text: "all"
},{
value: "AngularJS",
text: "AngularJS"
}, {
value: "Angular2+",
text: "Angular2+"
}, {
value: "React.JS",
text: "React.JS"
}, {
value: "Vue.JS",
text: "Vue.JS"
}]
};
this.filterByInputValue = {
defaultField : 'name',
inputPlaceholder: 'Filter by name (case sensitive)',
fields: [{
value: 'name',
text: 'name'
}, {
value: 'cv',
text: 'cv'
}]
};
this.addNew = NewPersonComponent;
//called it sortable table context, so used arrow function
this.afterPopupClose = data => {
if (data) {
const config: MdDialogConfig = {
disableClose: true
};
const ref: MdDialogRef<AlertComponent> = this.dialog.open(
AlertComponent,
config
);
ref.componentInstance['data'] = {
title: 'Add person',
msg: `${data['name']} will be added to list`,
ok: 'Ok',
err: 'Close'
};
}
};
} else if (this.toFetch === 'employers') {
this.itemComponent = EmployerItemComponent;
this.filterBySelect = null;
this.filterByInputValue = {
defaultField : 'name',
inputPlaceholder: 'Filter by name (case sensitive)',
fields: [{
value: 'name',
text: 'name'
}, {
value: 'address',
text: 'address'
}, {
value: 'email',
text: 'email'
}]
};
this.addNew = null;
//called it sortable table context, so used arrow function
this.afterPopupClose = data => {
const config: MdDialogConfig = {
disableClose: true
};
const ref: MdDialogRef<AlertComponent> = dialog.open(
AlertComponent,
config
);
ref.componentInstance['data'] = {
title: 'Remove employer',
msg: `${data['name']} will be removed from list`,
ok: 'Ok',
err: 'Close'
};
}
}
});
}
public ngAfterViewInit() {
Observable
.fromEvent(this.toggler['_elementRef']['nativeElement'], 'click')
.subscribe(() => {
this.sidenav.toggle(!this.isSidenavOpen);
this.isSidenavOpen = !this.isSidenavOpen;
})
}
public toDocs() {
window.location.href = 'https://nodeart.github.io/Angular-Firebase-Sortable-Table/modules/NgFbSortableTableModule.html';
}
public setHeaders (data: Observable<{key: string, value : Array<any>}>) : Observable<Array<HeaderItem>> {
let priority;
if (this.toFetch === 'employees') {
priority = EmployeePriority;
} else if (this.toFetch === 'employers') {
priority = EmployersPriority;
}
return data.map(({value}): Array<HeaderItem> => {
const arr = Object.keys(value);
if (arr.length) {
const data = Object.assign({}, value[Object.keys(value)[0]]);
const notSortable = ['cv', 'address', 'phone'];
const arr = this.keysPipe
.transform(data, priority)
.map((elem) => ({name : elem, sortable: notSortable.indexOf(elem) === -1}));
if (this.toFetch === 'employers') {
arr.push({name: 'delete', sortable: false});
}
arr.unshift({name : 'image', sortable: false});
return arr;
}
})
}
}