task-list.component.ts
3.31 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
import { Component, Input, Inject } 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";
@Component({
selector: "task-list",
templateUrl: "app/task/task-list/task-list.html",
directives: [TaskAcceptComponent]
})
@Inject(NotificationService, "$scope", "$uibModal", TaskService)
export class TaskListComponent {
@Input() tasks: noosfero.Task[];
private taskTemplates = ["AddFriend", "AddMember", "CreateCommunity", "SuggestArticle", "AbuseComplaint"];
rejectionExplanation: string;
currentTask: noosfero.Task;
private modalInstance: any = null;
constructor(private notificationService: NotificationService, private $scope: ng.IScope, private $uibModal: any, private taskService: TaskService) { }
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.currentTask = task;
if (task.accept_details) {
this.modalInstance = this.$uibModal.open({
templateUrl: "app/task/task-list/accept.html",
controller: TaskListComponent,
controllerAs: 'modal',
bindToController: true,
scope: this.$scope
});
} else {
this.callAccept();
}
}
reject(task: noosfero.Task) {
this.currentTask = task;
if (task.reject_details) {
this.modalInstance = this.$uibModal.open({
templateUrl: "app/task/task-list/reject.html",
controller: TaskListComponent,
controllerAs: 'modal',
bindToController: true,
scope: this.$scope
});
} else {
this.callReject();
}
}
callAccept() {
this.taskService.finishTask(this.currentTask).then(() => {
this.removeTask(this.currentTask);
this.notificationService.success({ title: "tasks.actions.accept.title", message: "tasks.actions.accept.message" });
}).finally(() => {
this.cancel();
});
}
callReject() {
this.taskService.cancelTask(this.currentTask).then(() => {
this.removeTask(this.currentTask);
this.notificationService.success({ title: "tasks.actions.reject.title", message: "tasks.actions.reject.message" });
}).finally(() => {
this.cancel();
});
}
cancel() {
if (this.modalInstance) {
this.modalInstance.close();
this.modalInstance = null;
}
if (this.currentTask) {
this.currentTask = null;
}
}
private getTemplateName(task: noosfero.Task) {
return task.type.replace(/::/, '').replace(/([a-z])([A-Z])/g, '$1-$2').toLowerCase());
}
private removeTask(task: noosfero.Task) {
let index = this.tasks.indexOf(task, 0);
if (index > -1) {
this.tasks.splice(index, 1);
}
}
}