-
Notifications
You must be signed in to change notification settings - Fork 31
/
Copy pathdatafiles-action.component.ts
176 lines (152 loc) · 4.33 KB
/
datafiles-action.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
import {
Component,
Input,
OnChanges,
OnInit,
SimpleChanges,
} from "@angular/core";
import { UserApi } from "shared/sdk";
import { ActionConfig, ActionDataset } from "./datafiles-action.interfaces";
import { DataFiles_File } from "datasets/datafiles/datafiles.interfaces";
@Component({
selector: "datafiles-action",
//standalone: true,
//imports: [],
templateUrl: "./datafiles-action.component.html",
styleUrls: ["./datafiles-action.component.scss"],
})
export class DatafilesActionComponent implements OnInit, OnChanges {
@Input({ required: true }) actionConfig: ActionConfig;
@Input({ required: true }) actionDataset: ActionDataset;
@Input({ required: true }) files: DataFiles_File[];
@Input({ required: true }) maxFileSize: number;
jwt = "";
visible = true;
use_mat_icon = false;
use_icon = false;
disabled_condition = "false";
selectedTotalFileSize = 0;
numberOfFileSelected = 0;
form: HTMLFormElement = null;
constructor(private userApi: UserApi) {
this.userApi.jwt().subscribe((jwt) => {
this.jwt = jwt.jwt;
});
}
private evaluate_disabled_condition(condition: string) {
return condition
.replaceAll(
"#SizeLimit",
String(
this.maxFileSize > 0 &&
this.selectedTotalFileSize <= this.maxFileSize,
),
)
.replaceAll("#Selected", String(this.numberOfFileSelected > 0));
}
private prepare_disabled_condition() {
if (this.actionConfig.enabled) {
this.disabled_condition =
"!(" +
this.evaluate_disabled_condition(this.actionConfig.enabled) +
")";
} else if (this.actionConfig.disabled) {
this.disabled_condition = this.evaluate_disabled_condition(
this.actionConfig.disabled,
);
} else {
this.disabled_condition = "false";
}
}
ngOnInit() {
this.use_mat_icon = this.actionConfig.mat_icon !== undefined;
this.use_icon = this.actionConfig.icon !== undefined;
this.prepare_disabled_condition();
this.update_status();
//this.compute_disabled();
}
ngOnChanges(changes: SimpleChanges) {
console.log(changes);
if (changes["files"]) {
this.update_status();
//this.compute_disabled();
}
}
update_status() {
this.selectedTotalFileSize = this.files
.filter((item) => item.selected || this.actionConfig.files === "all")
.reduce((sum, item) => sum + item.size, 0);
this.numberOfFileSelected = this.files.filter(
(item) => item.selected,
).length;
}
get disabled() {
this.update_status();
this.prepare_disabled_condition();
return eval(this.disabled_condition);
}
add_input(name, value) {
const input = document.createElement("input");
input.type = "hidden";
input.name = name;
input.value = value;
return input;
}
perform_action() {
const action_type = this.actionConfig.type || "form";
switch (action_type) {
case "form":
default:
return this.type_form();
}
}
type_form() {
if (this.form !== null) {
document.body.removeChild(this.form);
}
this.form = document.createElement("form");
this.form.target = this.actionConfig.target || "_self";
this.form.method = this.actionConfig.method || "POST";
this.form.action = this.actionConfig.url;
this.form.style.display = "none";
this.form.appendChild(
this.add_input("auth_token", this.userApi.getCurrentToken().id),
);
this.form.appendChild(this.add_input("jwt", this.jwt));
this.form.appendChild(this.add_input("dataset", this.actionDataset.pid));
this.form.appendChild(
this.add_input("directory", this.actionDataset.sourceFolder),
);
let index = 0;
for (const item of this.files) {
if (
this.actionConfig.files === "all" ||
(this.actionConfig.files === "selected" && item.selected)
) {
this.form.appendChild(
this.add_input("files[" + index + "]", item.path),
);
index = index + 1;
}
}
document.body.appendChild(this.form);
this.form.submit();
return true;
}
/*
* future development
*
type_fetch() {
const data = new URLSearchParams();
for (const pair of new FormData(formElement)) {
data.append(pair[0], pair[1]);
}
fetch(url, {
method: 'post',
body: data,
})
.then(…);
}
}
*/
}