-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathsizeme-selectric.js
65 lines (53 loc) · 1.87 KB
/
sizeme-selectric.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
(function ($) {
const pluginName = "sizemeSelectric";
const sizemeChange = "sizemeChange";
const currIndex = "currIndex";
class SizemeSelectric {
constructor (element, $element) {
this.element = element;
this.$element = $element;
this.currIndex = "";
this.element.addEventListener(sizemeChange, this.sizemeListener);
this.$element
.on("selectric-change", this.selectricListener)
.data(pluginName, this);
}
selectricListener = () => {
this.$element.data(currIndex, this.element.value);
this.element.dispatchEvent(new Event("change"));
};
sizemeListener = () => {
if (this.currIndex !== this.element.value) {
this.$element.selectric("refresh");
}
};
destroy = () => {
this.$element
.off("selectric-change", this.selectricListener)
.removeData(pluginName);
this.element.removeEventListener(sizemeChange, this.sizemeListener);
};
}
$.fn[pluginName] = function (op = "init") {
this.each(function() {
const element = this;
const $element = $(this);
const selectric = $element.data("selectric");
const sizemeSelectric = $element.data(pluginName);
switch (op) {
case "init":
if (selectric && !sizemeSelectric) {
new SizemeSelectric(element, $element);
}
break;
case "destroy":
if (sizemeSelectric) {
sizemeSelectric.destroy();
}
break;
default:
}
});
return this;
}
})(jQuery);