-
Notifications
You must be signed in to change notification settings - Fork 463
/
resource.ts
80 lines (76 loc) · 2.9 KB
/
resource.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
import { OnInit } from "@angular/core";
import { BaseComponent, BaseElement, BaseOptions } from '../base';
import { FormioComponents } from '../components';
import { FormioTemplate } from '../../formio.template';
let Formio = require('formiojs');
export interface ResourceOptions extends BaseOptions<any> {
placeholder?: string;
template: string;
resource: string;
searchFields?: Array<string> | string;
selectFields?: Array<string> | string;
}
interface IdTextPair {
id: string;
text: string;
}
export class ResourceComponent extends BaseComponent<ResourceOptions> {
allowMultiple(): boolean {
return false;
}
}
export class ResourceElement extends BaseElement<ResourceComponent> implements OnInit {
private value: any = {};
public refreshValue(value: any): void {
this.value = value;
}
public submitArray: Array<any> = [];
public selected(selectedValue: any): void {
if (this.component.settings.multiple) {
this.submitArray.push(selectedValue.id);
this.component.setValue(this.submitArray);
}
else {
this.component.setValue(selectedValue.id);
}
}
public removed(removedValue: any): void {
if (this.component.settings.multiple) {
this.submitArray.splice(this.submitArray.indexOf(removedValue.id),1);
this.component.setValue(this.submitArray);
}
}
public searchData(text: any): void {
let selectItems: IdTextPair[] = [];
let templates = this.component.settings.template.split(' ')[1].split('.');
let data: string = templates[1];
let key: string = templates[2];
let baseUrl: string = Formio.getBaseUrl() +'/project/'+ this.component.settings.project +'/form/'+ this.component.settings.resource;
let params: any = {};
if (this.component.settings.selectFields) {
params.select = this.component.settings.selectFields;
}
if (this.component.settings.searchFields && text) {
this.component.settings.searchFields.forEach((item: any) => {
params[item] = text;
});
}
(new Formio(baseUrl)).loadSubmissions({params: params}).then((submission: any) => {
for(let i=0; i < submission.length; i++) {
if (templates.length == 2) {
selectItems.push({id: submission[i], text: JSON.stringify(submission[i][data])});
} else {
selectItems.push({id: submission[i], text: submission[i][data][key]});
}
}
this.component.settings.defaultValue = selectItems.slice(0);
});
}
ngOnInit() {
this.searchData(null);
}
}
export function ResourceField(template:FormioTemplate) {
FormioComponents.register('resource', ResourceComponent, ResourceElement, template.components.resource);
return ResourceElement;
}