-
Notifications
You must be signed in to change notification settings - Fork 1.9k
/
Copy pathkendo.floatinglabel.js
131 lines (107 loc) · 4.39 KB
/
kendo.floatinglabel.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
import "./kendo.core.js";
export const __meta__ = {
id: "floatinglabel",
name: "FloatingLabel",
category: "framework",
depends: ["core"],
hidden: true
};
(function($, undefined) {
var kendo = window.kendo,
Widget = kendo.ui.Widget,
ui = kendo.ui,
NS = ".kendoFloatingLabel",
FLOATINGLABELCONTAINER = "k-floating-label-container",
EMPTY = "k-empty",
FOCUSED = "k-focus",
STATEDISABLED = "k-disabled",
STATEREADONLY = "k-readonly",
FLOATINGLABEL_OFFSET_Y = "--kendo-floating-label-offset-y",
FLOATINGLABEL_OFFSET_X = "--kendo-floating-label-offset-x",
FLOATINGLABEL_FOCUS_OFFSET_Y = "--kendo-floating-label-focus-offset-y",
FLOATINGLABEL_FOCUS_OFFSET_X = "--kendo-floating-label-focus-offset-x";
var FloatingLabel = Widget.extend({
init: function(element, options) {
var that = this;
Widget.fn.init.call(that, element, options);
options = $.extend(true, {}, options);
that.widget = that.options.widget;
that.widgetWrapper = that.widget.wrapper[0];
that.refresh();
that._editable({
readonly: that.options.widget.options.readonly !== undefined ? that.options.widget.options.readonly : false,
disable: that.options.widget.options.enable !== undefined ? !(that.options.widget.options.enable) : false
});
if (that.widgetWrapper.style.width) {
that.element.css("width", that.widgetWrapper.style.width);
that.widgetWrapper.style.width = "100%";
}
that.element.addClass(FLOATINGLABELCONTAINER);
kendo.notify(that);
},
options: {
name: 'FloatingLabel',
widget: null,
floatCheck: ({ element }) => !element.val()
},
readonly: function(readonly) {
this._editable({
readonly: readonly === undefined ? true : readonly,
disable: false
});
},
enable: function(enable) {
this._editable({
readonly: false,
disable: !(enable = enable === undefined ? true : enable)
});
},
refresh: function() {
var that = this;
var element = that.element;
var inputInner = that.options.widget.wrapper.find(".k-input-inner");
if (inputInner.length > 0) {
var labelHeight = that.element.find("> .k-floating-label").height();
var offsetX = inputInner.position().left + Number.parseInt(inputInner.css('padding-left'));
var offsetY = inputInner.position().top + Number.parseInt(inputInner.css('padding-top')) + labelHeight;
element[0].style.setProperty(FLOATINGLABEL_OFFSET_X, offsetX + "px");
element[0].style.setProperty(FLOATINGLABEL_OFFSET_Y, offsetY + "px");
}
element
.removeClass(EMPTY)
.removeClass(FOCUSED);
if (that.options.floatCheck({ element: that.options.widget.element, floating: that.element })) {
element.addClass(EMPTY);
}
if (document.activeElement === that.options.widget.element[0]
|| (that.options.widget.input && document.activeElement === that.options.widget.input[0])) {
element.addClass(FOCUSED);
}
},
destroy: function() {
var that = this;
that.element.off(NS);
Widget.fn.destroy.call(that);
},
_editable: function(options) {
var that = this;
var element = that.element;
var disable = options.disable;
var readonly = options.readonly;
element.off(NS);
if (!readonly && !disable) {
element
.removeClass(STATEDISABLED)
.removeClass(STATEREADONLY);
element.on("focusin" + NS, that.refresh.bind(that));
element.on("focusout" + NS, that.refresh.bind(that));
} else {
element
.toggleClass(STATEDISABLED, disable)
.toggleClass(STATEREADONLY, readonly);
}
}
});
ui.plugin(FloatingLabel);
})(window.kendo.jQuery);
export default kendo;