-
Notifications
You must be signed in to change notification settings - Fork 1.9k
/
Copy pathkendo.badge.js
324 lines (254 loc) · 9.14 KB
/
kendo.badge.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
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
import "./kendo.core.js";
import "./kendo.icons.js";
export const __meta__ = {
id: "badge",
name: "Badge",
category: "web", // suite
description: "The Badge decorates avatars, navigation menus, or other components in the application when visual notification is needed",
depends: ["core", "icons"] // dependencies
};
(function($, undefined) {
var kendo = window.kendo;
var Widget = kendo.ui.Widget;
var ui = kendo.ui;
var HIDDEN = 'k-hidden';
var iconTemplate = ({ icon }) => kendo.ui.icon($(`<span class='k-badge-icon'></span>`), { icon: icon });
var svgIconTemplate = ({ icon }) => `<span class='k-badge-icon k-svg-icon k-icon'>${icon}</span>`;
var Badge = Widget.extend({
init: function(element, options) {
var that = this;
Widget.fn.init.call(that, element, options);
that._content();
that._appearance();
kendo.notify(that);
},
destroy: function() {
var that = this;
Widget.fn.destroy.call(that);
},
options: {
name: 'Badge',
cutoutBorder: false,
data: {},
fillMode: 'solid',
icon: '',
max: Infinity,
position: 'inline',
align: '',
rounded: 'medium',
roundings: {
'small': 'sm',
'medium': 'md',
'large': 'lg',
'full': 'full'
},
sizes: {
'small': 'sm',
'medium': 'md',
'large': 'lg'
},
size: 'medium',
template: null,
text: '',
themeColor: 'secondary',
visible: true,
_classNames: []
},
_content: function() {
var that = this;
var text = that.options.text;
var template = that.options.template;
var data = that.options.data;
var icon = that.options.icon;
// Order of precedence
// 1) template
// 2) icon
// 3) text
// 4) content
if (template !== null) {
that._text = text;
that._template = kendo.template(template).bind(that);
that.element.html( that._template(data) );
return;
}
if (icon !== '') {
that.icon(icon);
return;
}
if (text !== '') {
that.text(text);
return;
}
that.text(that.element.html());
},
_appearance: function() {
var that = this;
that._themeColor = that.options.themeColor;
that._shape = that.options.shape;
that._sizes = that.options.sizes;
that._size = that.options.size;
that._fillMode = that.options.fillMode;
that._rounded = that.options.rounded;
that._roundings = that.options.roundings;
that._cutoutBorder = that.options.cutoutBorder;
that._align = that.options.align;
that._position = that.options.position;
that._visible = that.options.visible;
that._updateClassNames();
},
_updateClassNames: function() {
var that = this;
var classNames = ['k-badge'];
var keepClassNames = that.options._classNames;
var themeColor = that._themeColor;
var shape = that._shape;
var sizes = that._sizes;
var size = that._size;
var sizeAbbr = sizes[size] === undefined ? size : sizes[size];
var fillMode = that._fillMode;
var rounded = that._rounded;
var roundings = that._roundings;
var roundedAbbr = roundings[rounded] === undefined ? rounded : roundings[rounded];
var cutoutBorder = that._cutoutBorder;
var align = that._align;
var position = that._position;
var visible = that._visible;
// Remove all class names
that.element.removeClass(function(index, className) {
if (className.indexOf('k-') === 0 && keepClassNames.indexOf(className) === -1) {
that.element.removeClass(className);
}
});
// Fill
if (typeof fillMode === 'string' && fillMode !== '') {
classNames.push('k-badge-' + fillMode);
}
// Color
if (typeof themeColor === 'string' && themeColor !== '') {
classNames.push('k-badge-' + fillMode + '-' + themeColor);
}
// Size
if (typeof size === 'string' && size !== '') {
classNames.push('k-badge-' + sizeAbbr);
}
// Rounded
if (typeof rounded === 'string' && rounded !== '') {
classNames.push('k-rounded-' + roundedAbbr);
}
// Shape
if (typeof shape === 'string' && shape !== '') {
classNames.push('k-badge-' + shape);
}
// Cutout border
if (typeof cutoutBorder === 'boolean' && cutoutBorder === true) {
classNames.push('k-badge-border-cutout');
}
// Position
if (typeof position === 'string' && position !== '') {
classNames.push('k-badge-' + position);
}
// Align
if (typeof position === 'string' && position !== '' && position !== 'inline' && typeof align === 'string' && align.split(' ').length == 2) {
classNames.push('k-' + align.replace(' ', '-'));
}
// Visibility
if (visible === false) {
classNames.push(HIDDEN);
}
// Apply classnames
that.element.addClass(classNames.join(' '));
},
setOptions: function(options) {
var that = this;
that.element.removeClass(function(index, className) {
if (className.indexOf('k-') >= 0) {
that.element.removeClass(className);
}
});
Widget.fn.setOptions.call(that, options);
that._content();
that._appearance();
},
text: function(text) {
var that = this;
var max = that.options.max;
// handle badge.text()
if (arguments.length === 0 || text === undefined) {
return that._text;
}
that._text = text;
// handle badge.text(true|false|null)
if (text === true || text === false || text === null) {
that.element.html('');
return;
}
// handle badge.text('string')
if (typeof text === 'string') {
that.element.html(text);
return;
}
// handle badge.text(1)
if (typeof text === 'number') {
if (text > max) {
that.element.html(max + '+');
} else {
that.element.html(text);
}
return;
}
// handle other objects
if (typeof text === 'object' && 'toString' in text) {
that.element.html(text.toString());
return;
}
},
icon: function(icon) {
var that = this;
var iconTemplateFunction;
// handle badge.icon()
if (arguments.length === 0 || icon === undefined) {
return that._icon;
}
that._icon = icon;
// Handle badge.icon(<SVG />)
if (icon.indexOf('<svg') === 0) {
iconTemplateFunction = kendo.template(svgIconTemplate);
that.element.html(iconTemplateFunction({ icon: icon }));
return;
}
// Handle badge.icon(ICON_NAME)
iconTemplateFunction = kendo.template(iconTemplate);
that.element.html(iconTemplateFunction({ icon: icon }));
},
themeColor: function(color) {
var that = this;
// handle badge.color()
if (arguments.length === 0 || color === undefined) {
return that._themeColor;
}
that._themeColor = color;
that._updateClassNames();
},
rounded: function(rounded) {
var that = this;
// handle badge.shape()
if (arguments.length === 0 || rounded === undefined) {
return that._rounded;
}
that._rounded = rounded;
that._updateClassNames();
},
hide: function() {
var that = this;
that._visible = false;
that._updateClassNames();
},
show: function() {
var that = this;
that._visible = true;
that._updateClassNames();
}
});
ui.plugin(Badge);
})(window.kendo.jQuery);
export default kendo;