-
Notifications
You must be signed in to change notification settings - Fork 1
/
angular-fontawesome.js
75 lines (66 loc) · 2.15 KB
/
angular-fontawesome.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
angular.module('picardy.fontawesome', [])
.directive('fa', function () {
return {
restrict: 'E',
template: '<i class="fa"></i>',
replace: true,
link: function (scope, element, attrs) {
/*** STRING ATTRS ***/
// keep a state of the current attrs so that when they change,
// we can remove the old attrs before adding the new ones.
var currentClasses = {};
// generic function to bind string attrs
function _observeStringAttr (attr, baseClass) {
attrs.$observe(attr, function () {
baseClass = baseClass || 'fa-' + attr;
element.removeClass(currentClasses[attr]);
if (attrs[attr]) {
var className = [baseClass, attrs[attr]].join('-');
element.addClass(className);
currentClasses[attr] = className;
}
});
}
_observeStringAttr('name', 'fa');
_observeStringAttr('rotate');
_observeStringAttr('flip');
/**
* size can be passed "large" or an integer
*/
attrs.$observe('size', function () {
var className;
element.removeClass(currentClasses.size);
if (attrs.size === 'large') {
className = 'fa-lg';
} else if (!isNaN(parseInt(attrs.size, 10))) {
className = 'fa-' + attrs.size + 'x';
}
element.addClass(className);
currentClasses.size = className;
});
/*** BOOLEAN ATTRS ***/
// generic function to bind boolean attrs
function _observeBooleanAttr (attr, className) {
attrs.$observe(attr, function () {
className = className || 'fa-' + attr;
var value = attr in attrs && attrs[attr] !== 'false' && attrs[attr] !== false;
element.toggleClass(className, value);
});
}
_observeBooleanAttr('border');
_observeBooleanAttr('fw');
_observeBooleanAttr('inverse');
_observeBooleanAttr('spin');
/*** CONDITIONAL ATTRS ***/
// automatically populate fa-li if DOM structure indicates
element.toggleClass('fa-li', (
element.parent() &&
element.parent().parent() &&
element.parent().parent().hasClass('fa-ul') &&
element.parent().children()[0] === element[0]) &&
attrs.list !== 'false' &&
attrs.list !== false
);
}
};
});