-
Notifications
You must be signed in to change notification settings - Fork 1.9k
/
Copy pathkendo.html.icon.js
235 lines (198 loc) · 8.08 KB
/
kendo.html.icon.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
import "./kendo.html.base.js";
export const __meta__ = {
id: "html.icon",
name: "Html.Icon",
category: "web",
description: "HTML font icon rendering utility for Kendo UI for jQuery.",
depends: ["html.base"]
};
(function($, undefined) {
var kendo = window.kendo,
extend = $.extend,
HTMLBase = kendo.html.HTMLBase;
var KFONTICON = 'k-icon k-font-icon';
var KI_PREFFIX = 'k-i-';
var KSVGICON = 'k-icon k-svg-icon';
var KSVG_PREFFIX = 'k-svg-i-';
var FLIP_PREFIX = 'k-flip-';
var FLIP_HORIZONTAL = `${FLIP_PREFIX}h`;
var FLIP_VERTICAL = `${FLIP_PREFIX}v`;
var THEME_COLOR_PREFIX = 'k-color-';
var ICON_TYPES = {
'svg': (element, options) => new HTMLSvgIcon(element, options),
'font': (element, options) => new HTMLFontIcon(element, options)
};
var FLIP_CLASSES = {
default: '',
horizontal: FLIP_HORIZONTAL,
vertical: FLIP_VERTICAL,
both: `${FLIP_HORIZONTAL} ${FLIP_VERTICAL}`
};
var renderIcon = function(element, options) {
if (!element || $.isPlainObject(element) || kendo.isString(element)) {
options = element;
element = $("<span></span>");
}
if (kendo.isString(options)) {
options = {
icon: options
};
}
if (!kendo.isPresent(options.type)) {
options.type = kendo.defaults.iconType ? kendo.defaults.iconType : 'svg';
}
if (kendo.isFunction(options.type)) {
return options.type(element, options);
}
if (!kendo.isFunction(ICON_TYPES[options.type])) {
return null;
}
return (ICON_TYPES[options.type](element, options)).html();
};
var HTMLBaseIcon = HTMLBase.extend({
init: function(element, options) {
var that = this;
HTMLBase.fn.init.call(that, element, options);
that._wrapper();
},
options: {
name: 'HTMLIcon',
size: 'none',
themeColor: 'none',
flip: 'default',
iconClass: '',
stylingOptions: [ 'size', 'themeColor', 'fill' ]
},
_wrapper: function() {
var that = this;
that._addClasses();
},
_addClasses: function() {
var that = this,
options = that.options,
stylingOptions = options.stylingOptions,
previouslyAddedClasses = that.wrapper.data("added-classes");
stylingOptions = stylingOptions.map(function(option) {
if (option === 'themeColor') {
return kendo.cssProperties.getValidClass({
widget: options.name,
propName: option,
value: options[option],
prefix: THEME_COLOR_PREFIX
});
}
if (option === 'fill') {
return FLIP_CLASSES[options.flip];
}
return kendo.cssProperties.getValidClass({
widget: options.name,
propName: option,
value: options[option],
fill: options.fillMode
});
});
if (previouslyAddedClasses) {
that.wrapper.removeClass(previouslyAddedClasses.filter(x => x !== that._className).join(" "));
}
that.wrapper.data("added-classes", stylingOptions.concat([that._className]));
that.wrapper.addClass(stylingOptions.join(" "));
}
});
var HTMLFontIcon = HTMLBaseIcon.extend({
init: function(element, options) {
HTMLBaseIcon.fn.init.call(this, element, options);
},
options: extend({}, HTMLBaseIcon.fn.options, {
name: 'HTMLFontIcon',
icon: null
}),
_wrapper: function() {
var that = this,
// Find if there is an existing k-i- class appended to the element.
currentIconClass = that.element[0].className.split(" ").find(x => x.includes(KI_PREFFIX)),
className = that.options.icon ? `${that.options.icon.startsWith(KI_PREFFIX) ? "" : KI_PREFFIX}${that.options.icon}` : "";
that._className = className;
that.wrapper = that.element
.addClass(KFONTICON)
.removeClass(currentIconClass) // Remove any existing icons.
.addClass(className)
.addClass(that.options.iconClass || '');
HTMLBaseIcon.fn._wrapper.call(this);
}
});
var HTMLSvgIcon = HTMLBaseIcon.extend({
init: function(element, options) {
// Ensure that the inner contents of the wrapping span element are always removed for re-rendering purposes.
element.empty();
HTMLBaseIcon.fn.init.call(this, element, options);
},
options: extend({}, HTMLBaseIcon.fn.options, {
name: 'HTMLSVGIcon',
icon: null
}),
_wrapper: function() {
var that = this,
icon = that.options.icon,
iconClass = that.options.iconClass,
// Find if there is an existing k-svg-i- class appended to the element.
currentIconClass = that.element[0].className.split(" ").find(x => x.includes(KSVG_PREFFIX)),
svgElm = $('<svg></svg>'),
className;
if (!icon && iconClass) {
// match k-i-(some-icon-name)
const regex = /k-i-(\w+(?:-\w+)*)/;
let iconNameMatch = iconClass.match(regex);
if (iconNameMatch) {
icon = iconNameMatch[1];
iconClass = iconClass.replace(iconNameMatch[0], "");
}
}
if (kendo.isString(icon)) {
// remove k-i- and convert kebab-case-icon to camelCaseIcon
icon = icon.replace('k-i-', '').replace(/-./g, x=>x[1].toUpperCase());
icon = kendo.ui.svgIcons[icon] || kendo.ui.svgIcons[`${icon}Icon`];
}
className = icon && icon.name ? `${KSVG_PREFFIX}${icon.name}` : '';
that._className = className;
that.wrapper = that.element
.addClass(KSVGICON)
.removeClass(currentIconClass) // Remove any existing icons.
.addClass(className)
.addClass(iconClass || '');
if ($.isPlainObject(icon)) {
svgElm.attr('viewBox', icon.viewBox || '')
.attr({
'viewBox': icon.viewBox || '',
'focusable': 'false',
'xmlns': 'http://www.w3.org/2000/svg'
})
.html(icon.content || '');
that.wrapper.append(svgElm[0].outerHTML);
}
HTMLBaseIcon.fn._wrapper.call(this);
}
});
$.extend(kendo.html, {
renderIcon: renderIcon,
HTMLFontIcon: HTMLFontIcon,
HTMLSvgIcon: HTMLSvgIcon,
getIconRenderer: (type) => ICON_TYPES[type]
});
kendo.cssProperties.registerPrefix("HTMLFontIcon", "k-icon-");
kendo.cssProperties.registerValues("HTMLFontIcon", [{
prop: "size",
values: kendo.cssProperties.sizeValues.concat([['xsmall', 'xs'], ['xlarge', 'xl'], ['xxlarge', 'xxl'], ['xxxlarge', 'xxxl']])
}, {
prop: "themeColor",
values: ['primary', 'secondary', 'tertiary', 'inherit', 'info', 'success', 'warning', 'error', 'dark', 'light', 'inverse']
}]);
kendo.cssProperties.registerPrefix("HTMLSVGIcon", "k-icon-");
kendo.cssProperties.registerValues("HTMLSVGIcon", [{
prop: "size",
values: kendo.cssProperties.sizeValues.concat([['xsmall', 'xs'], ['xlarge', 'xl'], ['xxlarge', 'xxl'], ['xxxlarge', 'xxxl']])
}, {
prop: "themeColor",
values: ['primary', 'secondary', 'tertiary', 'inherit', 'info', 'success', 'warning', 'error', 'dark', 'light', 'inverse']
}]);
})(window.kendo.jQuery);
export default kendo;