Skip to content
This repository was archived by the owner on Sep 10, 2019. It is now read-only.

Commit 5e73e6e

Browse files
committed
Corrected iconic sizing issues and added directive attribute to override icon path
1 parent 927e8eb commit 5e73e6e

File tree

1 file changed

+43
-24
lines changed

1 file changed

+43
-24
lines changed

js/angular/components/iconic/iconic.js

+43-24
Original file line numberDiff line numberDiff line change
@@ -33,53 +33,70 @@
3333
dynSrc: '=?',
3434
dynIcon: '=?',
3535
size: '@?',
36-
icon: '@'
36+
icon: '@',
37+
iconDir: '@?'
3738
},
3839
compile: compile
3940
};
4041

4142
return directive;
4243

4344
function compile() {
44-
var contents, assetPath = 'assets/img/iconic/';
45+
var contents, assetPath;
4546

4647
return {
4748
pre: preLink,
4849
post: postLink
4950
};
5051

51-
function preLink(scope, iElement, iAttrs, ctrl, transclude) {
52+
function preLink(scope, element, attrs, ctrl, transclude) {
53+
if (scope.iconDir) {
54+
// make sure ends with /
55+
assetPath = scope.iconDir;
56+
if (assetPath.charAt(assetPath.length - 1) !== '/') {
57+
assetPath += '/';
58+
}
59+
} else {
60+
// default path
61+
assetPath = 'assets/img/iconic/';
62+
}
63+
5264
if(scope.dynSrc) {
53-
iAttrs.$set('data-src', scope.dynSrc);
65+
attrs.$set('data-src', scope.dynSrc);
5466
} else if(scope.dynIcon) {
55-
iAttrs.$set('data-src', assetPath + scope.dynIcon + '.svg');
67+
attrs.$set('data-src', assetPath + scope.dynIcon + '.svg');
5668
} else {
5769
if (scope.icon) {
58-
iAttrs.$set('data-src', assetPath + scope.icon + '.svg');
70+
attrs.$set('data-src', assetPath + scope.icon + '.svg');
5971
} else {
6072
// To support expressions on data-src
61-
iAttrs.$set('data-src', iAttrs.src);
73+
attrs.$set('data-src', attrs.src);
6274
}
6375
}
6476

65-
var iconicClass;
66-
switch (scope.size) {
67-
case 'small':
68-
iconicClass = 'iconic-sm'
69-
break;
70-
case 'medium':
71-
iconicClass = 'iconic-md'
72-
break;
73-
case 'large':
74-
iconicClass = 'iconic-lg'
75-
break;
76-
default:
77-
iconicClass = 'iconic-fluid'
77+
// check if size already added as class
78+
if (!element.hasClass('iconic-sm') &&
79+
!element.hasClass('iconic-md') &&
80+
!element.hasClass('iconic-lg')) {
81+
var iconicClass;
82+
switch (scope.size) {
83+
case 'small':
84+
iconicClass = 'iconic-sm'
85+
break;
86+
case 'medium':
87+
iconicClass = 'iconic-md'
88+
break;
89+
case 'large':
90+
iconicClass = 'iconic-lg'
91+
break;
92+
default:
93+
iconicClass = 'iconic-fluid'
94+
}
95+
element.addClass(iconicClass);
7896
}
79-
angular.element(iElement).addClass(iconicClass);
8097

8198
// save contents of un-inject html, to use for dynamic re-injection
82-
contents = iElement[0].outerHTML;
99+
contents = element[0].outerHTML;
83100
}
84101

85102
function postLink(scope, element, attrs) {
@@ -91,15 +108,16 @@
91108
// only run update on current element
92109
ico.update(element[0]);
93110
});
94-
95-
// handle dynamic updating of icon
111+
112+
// handle dynamic updating of src
96113
if(scope.dynSrc) {
97114
scope.$watch('dynSrc', function(newVal, oldVal) {
98115
if (newVal && newVal != oldVal) {
99116
reinjectSvg(scope.dynSrc);
100117
}
101118
});
102119
}
120+
// handle dynamic updating of icon
103121
if (scope.dynIcon) {
104122
scope.$watch('dynIcon', function(newVal, oldVal) {
105123
if (newVal && newVal != oldVal) {
@@ -125,6 +143,7 @@
125143
function injectSvg(element) {
126144
ico.inject(element, {
127145
each: function(injectedElem) {
146+
// compile injected svg
128147
var angElem = angular.element(injectedElem);
129148
svgElement = $compile(angElem)(angElem.scope());
130149
}

0 commit comments

Comments
 (0)