-
Notifications
You must be signed in to change notification settings - Fork 1.1k
/
Copy pathFormio.js
135 lines (122 loc) · 4.45 KB
/
Formio.js
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
import { Formio as FormioCore } from '@formio/core/sdk';
import { Formio as FormioEmbed } from './Embed';
import CDN from './CDN';
import Providers from './providers';
FormioCore.cdn = new CDN();
FormioCore.Providers = Providers;
FormioCore.version = 'FORMIO_VERSION';
CDN.defaultCDN = FormioCore.version.includes('rc') ? 'https://cdn.test-form.io' : 'https://cdn.form.io';
const isNil = (val) => val === null || val === undefined;
FormioCore.prototype.uploadFile = function(storage, file, fileName, dir, progressCallback, url, options, fileKey, groupPermissions, groupId, uploadStartCallback, abortCallback, multipartOptions) {
const requestArgs = {
provider: storage,
method: 'upload',
file: file,
fileName: fileName,
dir: dir
};
fileKey = fileKey || 'file';
const request = FormioCore.pluginWait('preRequest', requestArgs)
.then(() => {
return FormioCore.pluginGet('fileRequest', requestArgs)
.then((result) => {
if (storage && isNil(result)) {
const Provider = Providers.getProvider('storage', storage);
if (Provider) {
const provider = new Provider(this);
if (uploadStartCallback) {
uploadStartCallback();
}
return provider.uploadFile(file, fileName, dir, progressCallback, url, options, fileKey, groupPermissions, groupId, abortCallback, multipartOptions);
}
else {
throw ('Storage provider not found');
}
}
return result || { url: '' };
});
});
return FormioCore.pluginAlter('wrapFileRequestPromise', request, requestArgs);
};
FormioCore.prototype.downloadFile = function(file, options) {
const requestArgs = {
method: 'download',
file: file
};
const request = FormioCore.pluginWait('preRequest', requestArgs)
.then(() => {
return FormioCore.pluginGet('fileRequest', requestArgs)
.then((result) => {
if (file.storage && isNil(result)) {
const Provider = Providers.getProvider('storage', file.storage);
if (Provider) {
const provider = new Provider(this);
return provider.downloadFile(file, options);
}
else {
throw ('Storage provider not found');
}
}
return result || { url: '' };
});
});
return FormioCore.pluginAlter('wrapFileRequestPromise', request, requestArgs);
};
FormioCore.prototype.deleteFile = function(file, options) {
const requestArgs = {
method: 'delete',
file: file
};
const request = FormioCore.pluginWait('preRequest', requestArgs)
.then(() => {
return FormioCore.pluginGet('fileRequest', requestArgs)
.then((result) => {
if (file.storage && isNil(result)) {
const Provider = Providers.getProvider('storage', file.storage);
if (Provider) {
const provider = new Provider(this);
return provider.deleteFile(file, options);
}
else {
throw ('Storage provider not found');
}
}
return result || { url: '' };
});
});
return FormioCore.pluginAlter('wrapFileRequestPromise', request, requestArgs);
};
// Esnure we proxy the following methods to the FormioEmbed class.
['setBaseUrl', 'setApiUrl', 'setAppUrl', 'setProjectUrl', 'setPathType', 'setLicense'].forEach((fn) => {
const baseFn = FormioCore[fn];
FormioCore[fn] = function(arg) {
const retVal = FormioEmbed[fn](arg, true);
return baseFn ? baseFn.call(this, arg) : retVal;
};
});
// For reverse compatability.
FormioCore.Promise = Promise;
FormioCore.formioReady = FormioEmbed.formioReady;
FormioCore.config = FormioEmbed.config;
FormioCore.builder = FormioEmbed.builder;
FormioCore.Report = FormioEmbed.Report;
FormioCore.Form = FormioEmbed.Form;
FormioCore.FormBuilder = FormioEmbed.FormBuilder;
FormioCore.use = FormioEmbed.use;
FormioCore.createForm = FormioEmbed.createForm;
FormioCore.submitDone = FormioEmbed.submitDone;
FormioCore.addLibrary = FormioEmbed.addLibrary;
FormioCore.addLoader = FormioEmbed.addLoader;
FormioCore.addToGlobal = (global) => {
if (typeof global === 'object' && !global.Formio) {
global.Formio = FormioCore;
}
};
if (typeof global !== 'undefined') {
FormioCore.addToGlobal(global);
}
if (typeof window !== 'undefined') {
FormioCore.addToGlobal(window);
}
FormioEmbed._formioReady(FormioCore);
export { FormioCore as Formio };