-
Notifications
You must be signed in to change notification settings - Fork 1.9k
/
Copy pathkendo.toggleinputbase.js
139 lines (103 loc) · 3.4 KB
/
kendo.toggleinputbase.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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
import "./kendo.core.js";
export const __meta__ = {
id: "toggleinputbase",
name: "ToggleInputBase",
category: "web",
description: "The ToggleInputBase component.",
depends: [ "core" ]
};
(function($, undefined) {
var kendo = window.kendo,
ui = kendo.ui,
Widget = ui.Widget,
CHANGE = "change",
DISABLED = "disabled",
CHECKED = "checked";
var ToggleInputBase = Widget.extend({
init: function(element, options) {
var that = this;
Widget.fn.init.call(that, element, options);
that._wrapper();
that._initSettings();
that._attachEvents();
kendo.notify(that, kendo.ui);
},
events: [
CHANGE
],
options: {
name: "ToggleInputBase"
},
NS: ".kendoToggleInputBase",
RENDER_INPUT: $.noop,
check: function(checked) {
var that = this,
element = that.element[0];
if (checked === undefined) {
return element.checked;
}
if (element.checked !== checked) {
that.options.checked = element.checked = checked;
}
if (checked) {
that.element.attr(CHECKED, CHECKED);
} else {
that.element.prop(CHECKED, false);
}
},
destroy: function() {
Widget.fn.destroy.call(this);
this.wrapper.off(this.NS);
},
enable: function(enable) {
var element = this.element;
if (typeof enable == "undefined") {
enable = true;
}
this.options.enabled = enable;
if (enable) {
element.prop(DISABLED, false);
} else {
element.attr(DISABLED, DISABLED);
}
},
toggle: function() {
var that = this;
that.check(!that.element[0].checked);
},
_attachEvents: function() {
this.element.on(CHANGE + this.NS, this._change.bind(this));
},
_change: function() {
var checked = this.element[0].checked;
this.trigger(CHANGE, { checked: checked });
},
_initSettings: function() {
var that = this,
element = that.element[0],
options = that.options;
if (options.checked === null) {
options.checked = element.checked;
}
that.check(options.checked);
options.enabled = options.enabled && !that.element.attr(DISABLED);
that.enable(options.enabled);
},
_wrapper: function() {
var that = this,
options = that.options,
inputMethod = that.RENDER_INPUT;
inputMethod(that.element, $.extend({}, options));
that.element.removeClass('input-validation-error');
that.wrapper = that.element.wrap(`<span class="${options.wrapperClass}"></span>`).parent();
},
setOptions: function(options) {
this._clearCssClasses(options, this.element);
this._setEvents(options);
$.extend(this.options, options);
this._applyCssClasses(this.element);
},
});
ui.plugin(ToggleInputBase);
})(window.kendo.jQuery);
export default kendo;