-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.ts
74 lines (60 loc) · 1.56 KB
/
index.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
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
///<reference types="jquery"/>
export abstract class JQueryModuleBase {
/**
* Place initialization logic here
*/
abstract init(): void;
/**
* Place destruction logic here
*/
abstract destroy(): void;
}
export abstract class JQueryPluginBase extends JQueryModuleBase {
/**
* The element the plugin is attached to
*/
public element: Element;
/**
* A handy jQuery object of the given element
*/
public $element: JQuery;
/**
* The plugins default options extended by given options
*/
public options: any;
/**
* A clone of the given element for automated destroy function
*/
private _$clone: JQuery;
/**
* JQueryPluginBase constructor
*
* @param name - The plugins name
* @param element - The element the plugin is attached to
* @param defaults - The plugins default options
* @param options - The plugins custom options, default options are extended by these options
*/
constructor(name: string, element: Element, defaults: any, options: any, $: JQueryStatic = jQuery) {
super();
this.element = element;
this.$element = $(element);
// clone DOM element for automated destroy
this._$clone = this.$element.clone(true);
// extend default options
this.options = $.extend(true, {}, defaults, options);
// set plugins init event
this.$element.on('init.' + name, () => {
this.init();
});
// set plugins destroy event
this.$element.on('destroy.' + name, () => {
this.destroy();
});
}
/**
* Automated destruction of the given element
*/
destroy(): void {
this.$element.replaceWith(this._$clone);
};
}