This repository has been archived by the owner on Nov 22, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 15
/
activities.component.ts
209 lines (180 loc) · 6.57 KB
/
activities.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
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
/**
* @license
* Copyright 2017 JBoss Inc
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
import { ActivitiesConstants } from "@activities/shared/activities-constants";
import { Activity } from "@activities/shared/activity.model";
import { ActivityService } from "@activities/shared/activity.service";
import { NewActivity } from "@activities/shared/new-activity.model";
import { Component } from "@angular/core";
import { ViewChild } from "@angular/core";
import { ActivatedRoute } from "@angular/router";
import { Router } from "@angular/router";
import { ArrayUtils } from "@core/utils/array-utils";
import { AbstractPageComponent } from "@shared/abstract-page.component";
import { ConfirmDeleteComponent } from "@shared/confirm-delete/confirm-delete.component";
import { IdFilter } from "@shared/id-filter";
import { LayoutType } from "@shared/layout-type.enum";
import { SortDirection } from "@shared/sort-direction.enum";
@Component({
moduleId: module.id,
selector: "app-activities",
templateUrl: "./activities.component.html",
styleUrls: ["./activities.component.css"],
providers: [ ActivityService ]
})
export class ActivitiesComponent extends AbstractPageComponent {
public readonly addActivityLink = ActivitiesConstants.addActivityPath;
private allActivities: Activity[] = [];
private filteredActivities: Activity[] = [];
private selectedActivities: Activity[] = [];
private activityNameForDelete: string;
private router: Router;
private activityService: ActivityService;
private filter: IdFilter = new IdFilter();
private layout: LayoutType = LayoutType.CARD;
private sortDirection: SortDirection;
@ViewChild(ConfirmDeleteComponent) private confirmDeleteDialog: ConfirmDeleteComponent;
constructor(router: Router, route: ActivatedRoute, activityService: ActivityService) {
super(route);
this.router = router;
this.activityService = activityService;
}
public loadAsyncPageData(): void {
this.allActivities = this.activityService.getAllActivities();
this.filteredActivities = this.filterActivities();
this.loaded("activities");
}
/**
* Filters and sorts the list of activities based on the user input
*/
private filterActivities(): Activity[] {
// Clear the array first.
this.filteredActivities.splice(0, this.filteredActivities.length);
for (const activity of this.allActivities) {
if (this.filter.accepts(activity)) {
this.filteredActivities.push(activity);
}
}
this.filteredActivities.sort( (a1: Activity, a2: Activity) => {
let rval: number = a1.getId().localeCompare(a2.getId());
if (this.sortDirection === SortDirection.DESC) {
rval *= -1;
}
return rval;
});
this.selectedActivities = ArrayUtils.intersect(this.selectedActivities, this.filteredActivities);
return this.filteredActivities;
}
public onSelected(activity: Activity): void {
// Only allow one item to be selected
this.selectedActivities.shift();
this.selectedActivities.push(activity);
}
public onDeselected(activity: Activity): void {
// Only one item is selected at a time
this.selectedActivities.shift();
// this.selectedConnections.splice(this.selectedConnections.indexOf(connection), 1);
}
public onEdit(activityName: string): void {
const link: string[] = [ ActivitiesConstants.editActivityPath ];
this.router.navigate(link);
}
public onDelete(activityName: string): void {
this.activityNameForDelete = activityName;
this.confirmDeleteDialog.open();
}
public onStart(activityName: string): void {
alert("Start activity " + activityName);
}
public isFiltered(): boolean {
return this.allActivities.length !== this.filteredActivities.length;
}
public toggleSortDirection(): void {
if (this.sortDirection === SortDirection.ASC) {
this.sortDirection = SortDirection.DESC;
} else {
this.sortDirection = SortDirection.ASC;
}
this.filterActivities();
}
public clearFilters(): void {
this.filter.reset();
this.filterActivities();
}
/**
* @returns {boolean} true if activities are being represented by cards
*/
public get isCardLayout(): boolean {
return this.layout === LayoutType.CARD;
}
/**
* @returns {boolean} true if activities are being represented by items in a list
*/
public get isListLayout(): boolean {
return this.layout === LayoutType.LIST;
}
/**
* @returns {boolean} true if sorting activity names in ascending order
*/
public get isSortAscending(): boolean {
return this.sortDirection === SortDirection.ASC;
}
/**
* @returns {boolean} true if sorting activity names in descending order
*/
public get isSortDescending(): boolean {
return this.sortDirection === SortDirection.DESC;
}
/**
* @returns {string} the pattern the activity names are being matched to (can be null or empty)
*/
public get nameFilter(): string {
return this.filter.getPattern();
}
public onListLayout(): void {
this.layout = LayoutType.LIST;
}
public onCardLayout(): void {
this.layout = LayoutType.CARD;
}
/**
* Called to doDelete all selected APIs.
*/
public deleteActivity(): void {
const selectedActivity = this.filterActivities().find((x) => x.getId() === this.activityNameForDelete);
const activityToDelete: NewActivity = new NewActivity();
activityToDelete.setName(selectedActivity.getId());
// Note: we can only doDelete selected items that we can see in the UI.
console.log("[ActivitiesPageComponent] Deleting selected Activity.");
this.activityService.deleteActivity(activityToDelete);
/*
this.apiService
.deleteActivity(activityToDelete)
.subscribe(
() => {
this.removeActivityFromList(selectedActiv);
const link: string[] = [ '/activities' ];
console.log('[CreateApiPageComponent] Navigating to: %o', link);
this.router.navigate(link);
}
);
*/
}
private removeActivityFromList(activity: Activity): void {
this.allActivities.splice(this.allActivities.indexOf(activity), 1);
this.filterActivities();
}
}