-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
1 changed file
with
93 additions
and
112 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,125 +1,106 @@ | ||
import { Formio as CoreFormio } from '@formio/core/sdk'; | ||
import { Formio } from '@formio/core/sdk'; | ||
import { Formio as FormioEmbed } from './Embed'; | ||
import CDN from './CDN'; | ||
import Providers from './providers'; | ||
Formio.cdn = new CDN(); | ||
Formio.Providers = Providers; | ||
Formio.version = 'FORMIO_VERSION'; | ||
|
||
const isNil = (val) => val === null || val === undefined; | ||
Formio.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 = Formio.pluginWait('preRequest', requestArgs) | ||
.then(() => { | ||
return Formio.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: '' }; | ||
}); | ||
}); | ||
|
||
export class Formio extends CoreFormio { | ||
static cdn = new CDN(); | ||
static Providers = Providers; | ||
static version = 'FORMIO_VERSION'; | ||
static Promise = Promise; | ||
static formioReady = FormioEmbed.formioReady; | ||
static config = FormioEmbed.config; | ||
static builder = FormioEmbed.builder; | ||
static Form = FormioEmbed.Form; | ||
static FormBuilder = FormioEmbed.FormBuilder; | ||
static use = FormioEmbed.use; | ||
static createForm = FormioEmbed.createForm; | ||
static setBaseUrl(url) { | ||
CoreFormio.setBaseUrl(url); | ||
FormioEmbed.setBaseUrl(url, true); | ||
} | ||
return Formio.pluginAlter('wrapFileRequestPromise', request, requestArgs); | ||
}; | ||
|
||
static setApiUrl(url) { | ||
CoreFormio.setApiUrl(url); | ||
FormioEmbed.setApiUrl(url, true); | ||
} | ||
Formio.prototype.downloadFile = function(file, options) { | ||
const requestArgs = { | ||
method: 'download', | ||
file: file | ||
}; | ||
|
||
static setAppUrl(url) { | ||
CoreFormio.setAppUrl(url); | ||
FormioEmbed.setAppUrl(url, true); | ||
} | ||
const request = Formio.pluginWait('preRequest', requestArgs) | ||
.then(() => { | ||
return Formio.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: '' }; | ||
}); | ||
}); | ||
|
||
static setProjectUrl(url) { | ||
CoreFormio.setProjectUrl(url); | ||
FormioEmbed.setProjectUrl(url, true); | ||
} | ||
return Formio.pluginAlter('wrapFileRequestPromise', request, requestArgs); | ||
}; | ||
|
||
static setPathType(type) { | ||
CoreFormio.setPathType(type); | ||
FormioEmbed.setPathType(type, true); | ||
} | ||
Formio.prototype.deleteFile = function(file, options) { | ||
const requestArgs = { | ||
method: 'delete', | ||
file: file | ||
}; | ||
|
||
uploadFile(storage, file, fileName, dir, progressCallback, url, options, fileKey, groupPermissions, groupId, uploadStartCallback, abortCallback) { | ||
const requestArgs = { | ||
provider: storage, | ||
method: 'upload', | ||
file: file, | ||
fileName: fileName, | ||
dir: dir | ||
}; | ||
fileKey = fileKey || 'file'; | ||
const request = Formio.pluginWait('preRequest', requestArgs) | ||
.then(() => { | ||
return Formio.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); | ||
} | ||
else { | ||
throw ('Storage provider not found'); | ||
} | ||
const request = Formio.pluginWait('preRequest', requestArgs) | ||
.then(() => { | ||
return Formio.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); | ||
} | ||
return result || { url: '' }; | ||
}); | ||
}); | ||
return Formio.pluginAlter('wrapFileRequestPromise', request, requestArgs); | ||
} | ||
|
||
downloadFile(file, options) { | ||
const requestArgs = { | ||
method: 'download', | ||
file: file | ||
}; | ||
const request = Formio.pluginWait('preRequest', requestArgs) | ||
.then(() => { | ||
return Formio.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'); | ||
} | ||
else { | ||
throw ('Storage provider not found'); | ||
} | ||
return result || { url: '' }; | ||
}); | ||
}); | ||
return Formio.pluginAlter('wrapFileRequestPromise', request, requestArgs); | ||
} | ||
} | ||
return result || { url: '' }; | ||
}); | ||
}); | ||
|
||
deleteFile(file, options) { | ||
const requestArgs = { | ||
method: 'delete', | ||
file: file | ||
}; | ||
const request = Formio.pluginWait('preRequest', requestArgs) | ||
.then(() => { | ||
return Formio.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 Formio.pluginAlter('wrapFileRequestPromise', request, requestArgs); | ||
} | ||
} | ||
return Formio.pluginAlter('wrapFileRequestPromise', request, requestArgs); | ||
}; | ||
|
||
// For reverse compatability. | ||
Formio.Promise = Promise; | ||
Formio.formioReady = FormioEmbed.formioReady; | ||
Formio.config = FormioEmbed.config; | ||
Formio.builder = FormioEmbed.builder; | ||
Formio.Form = FormioEmbed.Form; | ||
Formio.FormBuilder = FormioEmbed.FormBuilder; | ||
Formio.use = FormioEmbed.use; | ||
Formio.createForm = FormioEmbed.createForm; | ||
export { Formio }; |