/
formulize.plugin.ts
34 lines (31 loc) · 1.2 KB
/
formulize.plugin.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
import { defaultOptions } from './option.value';
import { FormulizeFunction, FormulizeOptions } from './formulize.interface';
import { UI } from './ui/ui';
import { FormulizePlugin } from './formulize.jquery';
import { MethodBase, methodBinder } from './formulize.plugin.method';
export function pluginBinder() {
const reflectedMethod = new MethodBase(null);
const reflectedMethodNames = Object.getOwnPropertyNames(Object.getPrototypeOf(reflectedMethod));
$.fn.formulize = <FormulizePlugin>Object.assign<FormulizeFunction, FormulizeOptions>(
function (this: JQuery, options: FormulizeOptions): JQuery {
this
.toArray()
.forEach(elem => {
$(elem).data('$formulize', new UI(elem, options));
});
return this;
},
<FormulizeOptions>{ ...defaultOptions }
);
reflectedMethodNames
.filter(name => name !== 'constructor')
.map(name => ({
name,
func: function (...args: any[]) {
methodBinder.call(this, name, ...args);
}
}))
.forEach(binder => {
(<any>$.fn)[binder.name] = binder.func;
});
}