-
Notifications
You must be signed in to change notification settings - Fork 43
/
filepond.jquery.js
109 lines (85 loc) · 2.8 KB
/
filepond.jquery.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
/*
* jQuery FilePond 1.0.0
* Licensed under MIT, https://opensource.org/licenses/MIT
* Please visit https://pqina.nl/filepond for details.
*/
(function ($, FilePond) {
'use strict';
// No jQuery No Go
if (!$ || !FilePond) {
return;
}
// Test if FilePond is supported
if (!FilePond.supported()) {
// add stub
$.fn.filepond = function () { };
return;
}
// Helpers
function argsToArray(args) {
return Array.prototype.slice.call(args);
}
function isFactory(args) {
return !args.length || typeof args[0] === 'object';
}
function isGetter(obj, key) {
var descriptor = Object.getOwnPropertyDescriptor(obj, key);
return descriptor ? typeof descriptor.get !== 'undefined' : false;
}
function isSetter(obj, key) {
var descriptor = Object.getOwnPropertyDescriptor(obj, key);
return descriptor ? typeof descriptor.set !== 'undefined' : false;
}
function isMethod(obj, key) {
return typeof obj[key] === 'function';
}
// Setup plugin
$.fn.filepond = function () {
// get arguments as array
var args = argsToArray(arguments);
// method results array
var results = [];
// Execute for every item in the list
var items = this.each(function () {
// test if is create call
if (isFactory(args)) {
FilePond.create(this, args[0])
return;
}
// get a reference to the pond instance based on the element
var pond = FilePond.find(this);
// if no pond found, exit here
if (!pond) {
return;
}
// get property name or method name
var key = args[0];
// get params to pass
var params = args.concat().slice(1);
// run method
if (isMethod(pond, key)) {
results.push(pond[key].apply(pond, params));
return;
}
// set setter
if (isSetter(pond, key) && params.length) {
pond[key] = params[0];
return;
}
// get getter
if (isGetter(pond, key)) {
results.push(pond[key]);
return;
}
console.warn('$().filepond("' + key + '") is an unknown property or method.');
});
// returns a jQuery object if no results returned
return results.length ? this.length === 1 ? results[0] : results : items;
};
// Static API
Object.keys(FilePond).forEach(function (key) {
$.fn.filepond[key] = FilePond[key];
});
// Redirect setDefaults to setOptions
$.fn.filepond.setDefaults = FilePond.setOptions;
}(jQuery, FilePond));