-
Notifications
You must be signed in to change notification settings - Fork 1.9k
/
Copy pathkendo.label.js
133 lines (103 loc) · 3.37 KB
/
kendo.label.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
import './kendo.core.js';
import './kendo.floatinglabel.js';
export const __meta__ = {
id: 'label',
name: 'Label',
category: 'framework',
description: 'Abstraction of label rendering for inputs',
depends: ['core', 'floatinglabel'],
hidden: true
};
const kendo = window.kendo;
const $ = kendo.jQuery;
const ui = kendo.ui;
const Widget = ui.Widget;
const isFunction = kendo.isFunction;
const LABELCLASSES = "k-label k-input-label";
const FLOATINGLABELCLASS = "k-floating-label";
var Label = Widget.extend({
options: {
name: "Label",
widget: null
},
init: function(element, options) {
var that = this;
element = element || $("<label></label>");
Widget.fn.init.call(that, element, options);
options = $.extend(true, {}, options);
that.widget = options.widget;
if (options.floating) {
that._floatingLabel();
}
that._label();
},
destroy: function() {
if (this.floatingLabel) {
this.floatingLabel.destroy();
}
Widget.fn.destroy.call(this);
},
_unwrapFloating: function() {
var that = this;
if (that.floatingLabel) {
that.floatingLabel.destroy();
that.widget.wrapper.unwrap(that._floatingLabelContainer);
}
},
setOptions: function(options) {
var that = this;
var removeFloating = false;
if (typeof(options) === 'string' || ($.isPlainObject(options) && options.floating === false )) {
removeFloating = true;
}
options = $.isPlainObject(options) ? options : { content: options };
Widget.fn.setOptions.call(that, options);
if (removeFloating && that.floatingLabel) {
that._unwrapFloating();
that.floatingLabel.destroy();
delete that.floatingLabel;
} else if (options.floating === true && !that.floatingLabel) {
that.element.remove();
that._floatingLabel();
}
that._label();
},
_label: function() {
var that = this;
var element = that.widget.element;
var options = that.options;
var id = element.attr("id");
var labelText = options.content;
var floating = options.floating || false;
if (isFunction(labelText)) {
labelText = labelText.call(that);
}
if (!labelText) {
labelText = "";
}
if (!id) {
id = options.name + "_" + kendo.guid();
element.attr("id", id);
}
that.element.addClass(floating ? FLOATINGLABELCLASS : LABELCLASSES)
.attr("for", id)
.text(labelText)
[floating ? "insertAfter" : "insertBefore"](that.options.beforeElm || that.widget.wrapper);
if (that.floatingLabel) {
that.floatingLabel.refresh();
}
},
_floatingLabel: function() {
var that = this;
var options = $.extend({}, that.options);
var floating;
delete options.name;
floating = options.floating || false;
if (floating) {
that._floatingLabelContainer = that.widget.wrapper.wrap("<span></span>").parent();
that.floatingLabel = new kendo.ui.FloatingLabel(that._floatingLabelContainer, $.extend({}, options));
}
}
});
kendo.ui.plugin(Label);
export default kendo;