pure-upload
Advanced tools
Comparing version 3.0.1 to 4.0.0
77
index.js
@@ -48,2 +48,12 @@ "use strict"; | ||
exports.decorateSimpleFunction = decorateSimpleFunction; | ||
function applyDefaults(target, source) { | ||
var to = Object(target); | ||
for (var nextKey in source) { | ||
if (Object.prototype.hasOwnProperty.call(source, nextKey) && (to[nextKey] === undefined || to[nextKey] === null)) { | ||
to[nextKey] = source[nextKey]; | ||
} | ||
} | ||
return to; | ||
} | ||
; | ||
function getUploadCore(options, callbacks) { | ||
@@ -78,2 +88,12 @@ return new UploadCore(options, callbacks); | ||
; | ||
function getDefaultLocalizer() { | ||
return { | ||
fileSizeInvalid: function (maxFileSize) { return 'The selected file exceeds the allowed size of ' + maxFileSize | ||
+ ' or its size is 0 MB. Please choose another file.'; }, | ||
fileTypeInvalid: function (accept) { return 'File format is not allowed. Only ' + (accept | ||
? accept.split('.').join(' ') | ||
: '') + ' files are allowed.'; }, | ||
invalidResponseFromServer: function () { return 'Invalid response from server'; } | ||
}; | ||
} | ||
function removeEventHandler(el, event, handler) { | ||
@@ -97,6 +117,5 @@ if (el.removeEventListener) { | ||
this.targetElement = targetElement; | ||
this.options = options; | ||
this.options = applyDefaults(options, this.defaultOptions()); | ||
this.uploader = uploader; | ||
this.uploadCore = getUploadCore(this.options, this.uploader.queue.callbacks); | ||
this.setFullOptions(options); | ||
if (exports.isFileApi) { | ||
@@ -133,11 +152,12 @@ this.setupFileApiElements(); | ||
}; | ||
UploadArea.prototype.setFullOptions = function (options) { | ||
this.options.maxFileSize = options.maxFileSize || 1024; | ||
this.options.allowDragDrop = exports.isFileApi && | ||
(options.allowDragDrop === undefined || options.allowDragDrop === null ? true : options.allowDragDrop); | ||
this.options.clickable = options.clickable === undefined || options.clickable === null ? true : options.clickable; | ||
this.options.accept = options.accept || '*.*'; | ||
this.options.validateExtension = !!options.validateExtension; | ||
this.options.multiple = exports.isFileApi && | ||
(options.multiple === undefined || options.multiple === null ? true : options.multiple); | ||
UploadArea.prototype.defaultOptions = function () { | ||
return { | ||
localizer: getDefaultLocalizer(), | ||
maxFileSize: 1024, | ||
allowDragDrop: true, | ||
clickable: true, | ||
accept: '*.*', | ||
validateExtension: false, | ||
multiple: true, | ||
}; | ||
}; | ||
@@ -183,6 +203,3 @@ UploadArea.prototype.selectFiles = function (fileList) { | ||
file.uploadStatus = UploadStatus.failed; | ||
file.responseText = !!this.options.localizer | ||
? this.options.localizer('The selected file exceeds the allowed size of { maxFileSize } MB or its size is 0 MB. Please choose another file.', this.options) | ||
: 'The selected file exceeds the allowed size of ' + this.options.maxFileSize | ||
+ ' or its size is 0 MB. Please choose another file.'; | ||
file.responseText = this.options.localizer.fileSizeInvalid(this.options.maxFileSize); | ||
return false; | ||
@@ -192,7 +209,3 @@ } | ||
file.uploadStatus = UploadStatus.failed; | ||
file.responseText = !!this.options.localizer | ||
? this.options.localizer('File format is not allowed. Only { accept } files are allowed.', this.options) | ||
: 'File format is not allowed. Only ' + (this.options.accept | ||
? this.options.accept.split('.').join(' ') | ||
: '') + ' files are allowed.'; | ||
file.responseText = this.options.localizer.fileTypeInvalid(this.options.accept); | ||
return false; | ||
@@ -371,5 +384,4 @@ } | ||
if (callbacks === void 0) { callbacks = {}; } | ||
this.options = options; | ||
this.callbacks = callbacks; | ||
this.setFullOptions(options); | ||
this.options = applyDefaults(options, this.getDefaultOptions()); | ||
this.setFullCallbacks(callbacks); | ||
@@ -517,16 +529,13 @@ } | ||
file.responseCode = xhr.status; | ||
var response = xhr.responseText || xhr.statusText || (xhr.status | ||
file.responseText = xhr.responseText || xhr.statusText || (xhr.status | ||
? xhr.status.toString() | ||
: '' || 'Invalid response from server'); | ||
file.responseText = !!this.options.localizer | ||
? this.options.localizer(response, {}) | ||
: response; | ||
: '' || this.options.localizer.invalidResponseFromServer()); | ||
}; | ||
UploadCore.prototype.setFullOptions = function (options) { | ||
this.options.url = options.url; | ||
this.options.method = options.method; | ||
this.options.headers = options.headers || {}; | ||
this.options.params = options.params || {}; | ||
this.options.withCredentials = options.withCredentials || false; | ||
this.options.localizer = options.localizer; | ||
UploadCore.prototype.getDefaultOptions = function () { | ||
return { | ||
headers: {}, | ||
params: {}, | ||
withCredentials: false, | ||
localizer: getDefaultLocalizer() | ||
}; | ||
}; | ||
@@ -533,0 +542,0 @@ UploadCore.prototype.setFullCallbacks = function (callbacks) { |
112
index.ts
@@ -55,2 +55,12 @@ export function addEventHandler(el: Element | HTMLElement, event: string, handler: (ev: UIEvent) => void) { | ||
function applyDefaults<T, S>(target: T, source: S): T & S { | ||
let to = Object(target); | ||
for (var nextKey in source) { | ||
if (Object.prototype.hasOwnProperty.call(source, nextKey) && (to[nextKey] === undefined || to[nextKey] === null)) { | ||
to[nextKey] = source[nextKey]; | ||
} | ||
} | ||
return to; | ||
}; | ||
export function getUploadCore(options: IUploadOptions, callbacks: IUploadCallbacks): UploadCore { | ||
@@ -93,2 +103,18 @@ return new UploadCore(options, callbacks); | ||
export interface ILocalizer { | ||
fileSizeInvalid: (maxFileSize: number) => string; | ||
fileTypeInvalid: (accept: string) => string; | ||
invalidResponseFromServer: () => string; | ||
} | ||
function getDefaultLocalizer(): ILocalizer { | ||
return { | ||
fileSizeInvalid: (maxFileSize) => 'The selected file exceeds the allowed size of ' + maxFileSize | ||
+ ' or its size is 0 MB. Please choose another file.', | ||
fileTypeInvalid: accept => 'File format is not allowed. Only ' + (accept | ||
? accept.split('.').join(' ') | ||
: '') + ' files are allowed.', | ||
invalidResponseFromServer: () => 'Invalid response from server' | ||
}; | ||
} | ||
export interface IOffsetInfo { | ||
@@ -113,2 +139,12 @@ running: boolean; | ||
interface IFullUploadAreaOptions extends IUploadAreaOptions { | ||
maxFileSize: number; | ||
allowDragDrop: boolean | (() => boolean); | ||
clickable: boolean | (() => boolean); | ||
accept: string; | ||
multiple: boolean; | ||
validateExtension: boolean; | ||
localizer: ILocalizer; | ||
} | ||
export interface IUploadCallbacks { | ||
@@ -149,5 +185,12 @@ onProgressCallback?: (file: IUploadFile) => void; | ||
params?: { [key: string]: string | number | boolean }; | ||
localizer?: (message: string, params?: Object) => string; | ||
localizer?: ILocalizer; | ||
} | ||
interface IFullUploadOptions extends IUploadOptions { | ||
withCredentials: boolean; | ||
headers: { [key: string]: string | number | boolean }; | ||
params: { [key: string]: string | number | boolean }; | ||
localizer: ILocalizer; | ||
} | ||
export interface IUploadQueueCallbacks extends IUploadCallbacks { | ||
@@ -189,7 +232,7 @@ onFileAddedCallback?: (file: IUploadFile) => void; | ||
public targetElement: HTMLElement; | ||
public options: IUploadAreaOptions; | ||
public uploader: Uploader; | ||
private options: IFullUploadAreaOptions; | ||
private uploadCore: UploadCore; | ||
private fileInput: HTMLInputElement; | ||
private fileList: IUploadFile[] | null | undefined; | ||
private fileList?: IUploadFile[] | null; | ||
private unregisterOnClick: () => void; | ||
@@ -202,6 +245,5 @@ private unregisterOnDrop: () => void; | ||
this.targetElement = targetElement; | ||
this.options = options; | ||
this.options = applyDefaults(options, this.defaultOptions()); | ||
this.uploader = uploader; | ||
this.uploadCore = getUploadCore(this.options, this.uploader.queue.callbacks); | ||
this.setFullOptions(options); | ||
if (isFileApi) { | ||
@@ -245,11 +287,12 @@ this.setupFileApiElements(); | ||
private setFullOptions(options: IUploadAreaOptions): void { | ||
this.options.maxFileSize = options.maxFileSize || 1024; | ||
this.options.allowDragDrop = isFileApi && | ||
(options.allowDragDrop === undefined || options.allowDragDrop === null ? true : options.allowDragDrop); | ||
this.options.clickable = options.clickable === undefined || options.clickable === null ? true : options.clickable; | ||
this.options.accept = options.accept || '*.*'; | ||
this.options.validateExtension = !!options.validateExtension; | ||
this.options.multiple = isFileApi && | ||
(options.multiple === undefined || options.multiple === null ? true : options.multiple); | ||
private defaultOptions() { | ||
return { | ||
localizer: getDefaultLocalizer(), | ||
maxFileSize: 1024, | ||
allowDragDrop: true, | ||
clickable: true, | ||
accept: '*.*', | ||
validateExtension: false, | ||
multiple: true, | ||
}; | ||
} | ||
@@ -299,8 +342,3 @@ | ||
file.uploadStatus = UploadStatus.failed; | ||
file.responseText = !!this.options.localizer | ||
? this.options.localizer( | ||
'The selected file exceeds the allowed size of { maxFileSize } MB or its size is 0 MB. Please choose another file.', | ||
this.options) | ||
: 'The selected file exceeds the allowed size of ' + this.options.maxFileSize | ||
+ ' or its size is 0 MB. Please choose another file.'; | ||
file.responseText = this.options.localizer.fileSizeInvalid(this.options.maxFileSize); | ||
return false; | ||
@@ -310,7 +348,3 @@ } | ||
file.uploadStatus = UploadStatus.failed; | ||
file.responseText = !!this.options.localizer | ||
? this.options.localizer('File format is not allowed. Only { accept } files are allowed.', this.options) | ||
: 'File format is not allowed. Only ' + (this.options.accept | ||
? this.options.accept.split('.').join(' ') | ||
: '') + ' files are allowed.'; | ||
file.responseText = this.options.localizer.fileTypeInvalid(this.options.accept); | ||
return false; | ||
@@ -494,9 +528,8 @@ } | ||
export class UploadCore { | ||
public options: IUploadOptions; | ||
public callbacks: IUploadCallbacksExt; | ||
private options: IFullUploadOptions; | ||
private callbacks: IUploadCallbacksExt; | ||
constructor(options: IUploadOptions, callbacks: IUploadCallbacksExt = {}) { | ||
this.options = options; | ||
this.callbacks = callbacks; | ||
this.setFullOptions(options); | ||
this.options = applyDefaults(options, this.getDefaultOptions()); | ||
this.setFullCallbacks(callbacks); | ||
@@ -664,17 +697,14 @@ } | ||
file.responseCode = xhr.status; | ||
let response = xhr.responseText || xhr.statusText || (xhr.status | ||
file.responseText = xhr.responseText || xhr.statusText || (xhr.status | ||
? xhr.status.toString() | ||
: '' || 'Invalid response from server'); | ||
file.responseText = !!this.options.localizer | ||
? this.options.localizer(response, {}) | ||
: response; | ||
: '' || this.options.localizer.invalidResponseFromServer()); | ||
} | ||
private setFullOptions(options: IUploadOptions): void { | ||
this.options.url = options.url; | ||
this.options.method = options.method; | ||
this.options.headers = options.headers || {}; | ||
this.options.params = options.params || {}; | ||
this.options.withCredentials = options.withCredentials || false; | ||
this.options.localizer = options.localizer; | ||
private getDefaultOptions() { | ||
return { | ||
headers: {}, | ||
params: {}, | ||
withCredentials: false, | ||
localizer: getDefaultLocalizer() | ||
}; | ||
} | ||
@@ -681,0 +711,0 @@ |
{ | ||
"name": "pure-upload", | ||
"version": "3.0.1", | ||
"version": "4.0.0", | ||
"description": "The pure upload library without dependencies", | ||
@@ -5,0 +5,0 @@ "author": { |
@@ -20,2 +20,7 @@ declare module "pure-upload" { | ||
} | ||
export interface ILocalizer { | ||
fileSizeInvalid: (maxFileSize: number) => string; | ||
fileTypeInvalid: (accept: string) => string; | ||
invalidResponseFromServer: () => string; | ||
} | ||
export interface IOffsetInfo { | ||
@@ -73,3 +78,3 @@ running: boolean; | ||
}; | ||
localizer?: (message: string, params?: Object) => string; | ||
localizer?: ILocalizer; | ||
} | ||
@@ -93,7 +98,7 @@ export interface IUploadQueueCallbacks extends IUploadCallbacks { | ||
targetElement: HTMLElement; | ||
options: IUploadAreaOptions; | ||
uploader: Uploader; | ||
private options; | ||
private uploadCore; | ||
private fileInput; | ||
private fileList; | ||
private fileList?; | ||
private unregisterOnClick; | ||
@@ -107,3 +112,3 @@ private unregisterOnDrop; | ||
destroy(): void; | ||
private setFullOptions(options); | ||
private defaultOptions(); | ||
private selectFiles(fileList); | ||
@@ -126,4 +131,4 @@ private putFilesToQueue(); | ||
export class UploadCore { | ||
options: IUploadOptions; | ||
callbacks: IUploadCallbacksExt; | ||
private options; | ||
private callbacks; | ||
constructor(options: IUploadOptions, callbacks?: IUploadCallbacksExt); | ||
@@ -143,3 +148,3 @@ upload(fileList: File[] | Object): void; | ||
private setResponse(file, xhr); | ||
private setFullOptions(options); | ||
private getDefaultOptions(); | ||
private setFullCallbacks(callbacks); | ||
@@ -146,0 +151,0 @@ } |
@@ -71,3 +71,3 @@ # Pure-upload | ||
params?: { [key: string]: string; }; | ||
localizer?: (message: string, params?: Object) => string; | ||
localizer?: ILocalizer; | ||
maxFileSize?: number; | ||
@@ -89,4 +89,8 @@ allowDragDrop?: boolean | (() => boolean); | ||
clear(): void; | ||
``` | ||
### Localizer | ||
```typescript | ||
fileSizeInvalid: (maxFileSize: number) => string; | ||
fileTypeInvalid: (accept: string) => string; | ||
``` | ||
### Upload file | ||
@@ -93,0 +97,0 @@ Standard *File* object extended with additional informations and methods to manage a file in queue. |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
1674
116
0
74349