-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathiconic.js
More file actions
313 lines (268 loc) · 8.73 KB
/
iconic.js
File metadata and controls
313 lines (268 loc) · 8.73 KB
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
(function () {
'use strict';
angular.module('angularIcons.iconic', [])
.provider('Iconic', Iconic)
.directive('baIconic', baIconic)
.config(config)
;
config.$inject = ['$sceDelegateProvider'];
function config($sceDelegateProvider) {
var whitelist = $sceDelegateProvider.resourceUrlWhitelist();
$sceDelegateProvider.resourceUrlWhitelist(whitelist.concat([
'https://unpkg.com/angular-icons@0.0.0/dist/icons/iconic/**',
'https://cdn.jsdelivr.net/angular-icons/0.0.0/icons/iconic/**',
'https://unpkg.com/angular-icons@0.0.0/dist/icons/iconic/**'
]));
}
// iconic wrapper
function Iconic() {
// default path
var assetPath = '';
var assetCdn = 'unpkg';
/**
* Sets the path used to locate the iconic SVG files
* @param {string} path - the base path used to locate the iconic SVG files
*/
this.setAssetPath = function (path) {
assetPath = angular.isString(path) ? path : assetPath;
// make sure ends with /
if (assetPath.charAt(assetPath.length - 1) !== '/') {
assetPath += '/';
}
assetCdn = '';
};
/**
* Configures which CDN to use
* @param {string} cdn - options are 'jsdelivr', 'unpkg' (default 'unpkg')
*/
this.setCdn = function (cdn) {
switch (cdn) {
case 'jsdelivr':
case 'unpkg':
case 'unpkg':
assetCdn = cdn;
break;
default:
assetCdn = 'unpkg';
break;
}
};
/**
* Service implementation
* @returns {{}}
*/
this.$get = function () {
var iconicObject = new IconicJS();
var service = {
getAccess: getAccess,
getAssetPath: getAssetPath
};
return service;
/**
*
* @returns {Window.IconicJS}
*/
function getAccess() {
return iconicObject;
}
/**
*
* @returns {string}
*/
function getAssetPath() {
switch (assetCdn) {
case 'unpkg':
return 'https://unpkg.com/angular-icons@0.0.0/dist/icons/iconic/';
case 'unpkg':
return 'https://unpkg.com/angular-icons@0.0.0/dist/icons/iconic/';
case 'jsdelivr':
return 'https://cdn.jsdelivr.net/angular-icons/0.0.0/icons/iconic/';
default:
return assetPath;
}
}
};
}
baIconic.$inject = ['Iconic', '$compile', '$window'];
function baIconic(iconic, $compile, $window) {
var directive = {
restrict: 'EA',
template: '<img ng-transclude>',
transclude: true,
replace: true,
scope: {
dynSrc: '=?',
dynIcon: '=?',
dynIconAttrs: '=?',
size: '@?',
icon: '@',
iconDir: '@?',
iconAttrs: '=?'
},
compile: compile
};
return directive;
function compile() {
var contents, origAttrs, lastIconAttrs, assetPath;
return {
pre: preLink,
post: postLink
};
function preLink(scope, element, attrs) {
var iconAttrsObj, iconAttr;
if (scope.iconDir) {
// path set via attribute
assetPath = scope.iconDir;
} else {
// default path
assetPath = iconic.getAssetPath();
}
// make sure ends with /
if (assetPath.charAt(assetPath.length - 1) !== '/') {
assetPath += '/';
}
if (scope.dynSrc) {
attrs.$set('data-src', scope.dynSrc);
} else if (scope.dynIcon) {
attrs.$set('data-src', assetPath + scope.dynIcon + '.svg');
} else {
if (scope.icon) {
attrs.$set('data-src', assetPath + scope.icon + '.svg');
} else {
// To support expressions on data-src
attrs.$set('data-src', attrs.src);
}
}
// check if size already added as class
if (!element.hasClass('iconic-sm') && !element.hasClass('iconic-md') && !element.hasClass('iconic-lg')) {
var iconicClass;
switch (scope.size) {
case 'small':
iconicClass = 'iconic-sm';
break;
case 'medium':
iconicClass = 'iconic-md';
break;
case 'large':
iconicClass = 'iconic-lg';
break;
default:
iconicClass = 'iconic-fluid';
}
element.addClass(iconicClass);
}
// add static icon attributes to iconic element
if (scope.iconAttrs) {
iconAttrsObj = angular.fromJson(scope.iconAttrs);
for (iconAttr in iconAttrsObj) {
// add data- to attribute name if not already present
attrs.$set(addDataDash(iconAttr), iconAttrsObj[iconAttr]);
}
}
// save contents and attributes of un-inject html, to use for dynamic re-injection
contents = element[0].outerHTML;
origAttrs = element[0].attributes;
}
function postLink(scope, element, attrs) {
var svgElement, ico = iconic.getAccess();
injectSvg(element[0]);
// subscribe for resize events
angular.element($window).on('resize', resize);
scope.$on("$destroy", function() {
angular.element($window).off('resize', resize);
});
// handle dynamic updating of src
if (scope.dynSrc) {
scope.$watch('dynSrc', function (newVal, oldVal) {
if (newVal && newVal !== oldVal) {
reinjectSvg(scope.dynSrc, scope.dynIconAttrs);
}
});
}
// handle dynamic updating of icon
if (scope.dynIcon) {
scope.$watch('dynIcon', function (newVal, oldVal) {
if (newVal && newVal !== oldVal) {
reinjectSvg(assetPath + scope.dynIcon + '.svg', scope.dynIconAttrs);
}
});
}
// handle dynamic updating of icon attrs
scope.$watch('dynIconAttrs', function (newVal, oldVal) {
if (newVal && newVal !== oldVal) {
if (scope.dynSrc) {
reinjectSvg(scope.dynSrc, scope.dynIconAttrs);
} else {
reinjectSvg(assetPath + scope.dynIcon + '.svg', scope.dynIconAttrs);
}
}
});
function reinjectSvg(newSrc, newAttrs) {
var iconAttr;
if (svgElement) {
// set html
svgElement.empty();
svgElement.append(angular.element(contents));
// remove 'data-icon' attribute added by injector as it
// will cause issues with reinjection when changing icons
svgElement.removeAttr('data-icon');
// set new source
svgElement.attr('data-src', newSrc);
// add additional icon attributes to iconic element
if (newAttrs) {
// remove previously added attributes
if (lastIconAttrs) {
for (iconAttr in lastIconAttrs) {
svgElement.removeAttr(addDataDash(iconAttr));
}
}
// add newly added attributes
for (iconAttr in newAttrs) {
// add data- to attribute name if not already present
svgElement.attr(addDataDash(iconAttr), newAttrs[iconAttr]);
}
}
// store current attrs
lastIconAttrs = newAttrs;
// reinject
injectSvg(svgElement[0]);
}
}
function injectSvg(element) {
ico.inject(element, {
each: function (injectedElem) {
var i, angElem, elemScope;
// wrap raw element
angElem = angular.element(injectedElem);
for(i = 0; i < origAttrs.length; i++) {
// check if attribute should be ignored
if (origAttrs[i].name !== 'ba-iconic' &&
origAttrs[i].name !== 'ng-transclude' &&
origAttrs[i].name !== 'icon' &&
origAttrs[i].name !== 'src') {
// check if attribute already exists on svg
if (angular.isUndefined(angElem.attr(origAttrs[i].name))) {
// add attribute to svg
angElem.attr(origAttrs[i].name, origAttrs[i].value);
}
}
}
// compile
elemScope = angElem.scope();
if (elemScope) {
svgElement = $compile(angElem)(elemScope);
}
}
});
}
function resize() {
// run update on current element
ico.update(element[0]);
}
}
function addDataDash(attr) {
return attr.indexOf('data-') !== 0 ? 'data-' + attr : attr;
}
}
}
})();