task-list.component.ts
3.73 KB
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
import { Component, Input, Inject, provide } from "ng-forward";
import { NotificationService } from "../../shared/services/notification.service";
import { TaskService } from "../../../lib/ng-noosfero-api/http/task.service";
import { TaskAcceptComponent } from "./task-accept.component";
import { Arrays } from "../../../lib/util/arrays";
import { EventsHubService } from "../../shared/services/events-hub.service";
import { NoosferoKnownEvents } from "../../known-events";
@Component({
selector: "task-list",
templateUrl: "app/task/task-list/task-list.html",
directives: [TaskAcceptComponent],
providers: [
provide('eventsHubService', { useClass: EventsHubService })
]
})
@Inject(NotificationService, "$scope", "$uibModal", TaskService, EventsHubService)
export class TaskListComponent {
@Input() tasks: noosfero.Task[];
private taskTemplates = ["AddFriend", "AddMember", "CreateCommunity", "SuggestArticle", "AbuseComplaint"];
currentTask: noosfero.Task;
confirmationTask: noosfero.Task;
eventsNames: NoosferoKnownEvents;
private modalInstance: any = null;
constructor(private notificationService: NotificationService,
private $scope: ng.IScope,
private $uibModal: any,
private taskService: TaskService,
private eventsHubService: EventsHubService) {
this.eventsNames = new NoosferoKnownEvents();
}
ngOnInit() {
this.eventsHubService.subscribeToEvent(this.eventsNames.TASK_CLOSED, (task: noosfero.Task) => {
Arrays.remove(this.tasks, task);
});
}
getTaskTemplate(task: noosfero.Task) {
if (this.taskTemplates.indexOf(task.type) >= 0) {
let templateName = this.getTemplateName(task);
return `app/task/types/${templateName}/${templateName}.html`;
} else {
return 'app/task/types/default.html';
}
}
accept(task: noosfero.Task) {
this.closeTask(task, task.accept_details, "app/task/task-list/accept.html", () => { this.callAccept(); });
}
reject(task: noosfero.Task) {
this.closeTask(task, task.reject_details, "app/task/task-list/reject.html", () => { this.callReject(); });
}
private closeTask(task: noosfero.Task, hasDetails: boolean, templateUrl: string, confirmationFunction: Function) {
this.currentTask = task;
this.confirmationTask = <any>{ id: task.id };
if (hasDetails) {
this.modalInstance = this.$uibModal.open({
templateUrl: templateUrl,
controller: TaskListComponent,
controllerAs: 'modal',
bindToController: true,
scope: this.$scope
});
} else {
confirmationFunction();
}
}
callAccept() {
this.callCloseTask("finish", "tasks.actions.accept.title", "tasks.actions.accept.message");
}
callReject() {
this.callCloseTask("cancel", "tasks.actions.reject.title", "tasks.actions.reject.message");
}
private callCloseTask(action: string, title: string, message: string) {
this.taskService.closeTask(this.confirmationTask, action).then(() => {
this.eventsHubService.emitEvent(this.eventsNames.TASK_CLOSED, this.currentTask);
this.notificationService.success({ title: title, message: message });
}).finally(() => {
this.cancel();
});
}
cancel() {
if (this.modalInstance) {
this.modalInstance.close();
this.modalInstance = null;
}
this.currentTask = null;
this.confirmationTask = null;
}
private getTemplateName(task: noosfero.Task) {
return task.type.replace(/::/, '').replace(/([a-z])([A-Z])/g, '$1-$2').toLowerCase();
}
}