-
Notifications
You must be signed in to change notification settings - Fork 19
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
173 additions
and
153 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,151 +1,171 @@ | ||
'use strict'; | ||
|
||
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'); | ||
_observeStringAttr('stack'); | ||
|
||
/** | ||
* 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; | ||
}); | ||
|
||
/** | ||
* stack can be passed "large" or an integer | ||
*/ | ||
attrs.$observe('stack', function () { | ||
var className; | ||
element.removeClass(currentClasses.stack); | ||
|
||
if (attrs.stack === 'large') { | ||
className = 'fa-stack-lg'; | ||
} else if (!isNaN(parseInt(attrs.stack, 10))) { | ||
className = 'fa-stack-' + attrs.stack + 'x'; | ||
} | ||
|
||
element.addClass(className); | ||
currentClasses.stack = 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 | ||
); | ||
|
||
attrs.$observe('alt', function () { | ||
if (attrs.alt) { | ||
var srText; | ||
srText = attrs.alt; | ||
element.removeAttr('alt') | ||
element.attr('aria-hidden', 'true'); | ||
element.after('<span class="sr-only">' + srText + '</span>'); | ||
} | ||
}); | ||
|
||
} | ||
}; | ||
}) | ||
.directive('faStack', function () { | ||
return { | ||
restrict: 'E', | ||
transclude: true, | ||
template: '<span ng-transclude class="fa-stack fa-lg"></span>', | ||
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('size'); | ||
|
||
/** | ||
* 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; | ||
}); | ||
} | ||
}; | ||
}); | ||
.directive('fa', function () { | ||
return { | ||
restrict: 'E', | ||
template: '<span class="fa" aria-hidden="true"></span>', | ||
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) { | ||
var className; | ||
|
||
attrs.$observe(attr, function () { | ||
baseClass = baseClass || 'fa-' + attr; | ||
element.removeClass(currentClasses[attr]); | ||
if (attrs[attr]) { | ||
className = [baseClass, attrs[attr]].join('-'); | ||
element.addClass(className); | ||
currentClasses[attr] = className; | ||
} | ||
}); | ||
} | ||
|
||
_observeStringAttr('name', 'fa'); | ||
_observeStringAttr('rotate'); | ||
_observeStringAttr('flip'); | ||
_observeStringAttr('stack'); | ||
|
||
/** | ||
* 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; | ||
}); | ||
|
||
/** | ||
* stack can be passed "large" or an integer | ||
*/ | ||
attrs.$observe('stack', function () { | ||
var className; | ||
element.removeClass(currentClasses.stack); | ||
|
||
if (attrs.stack === 'large') { | ||
className = 'fa-stack-lg'; | ||
} else if (!isNaN(parseInt(attrs.stack, 10))) { | ||
className = 'fa-stack-' + attrs.stack + 'x'; | ||
} | ||
|
||
element.addClass(className); | ||
currentClasses.stack = className; | ||
}); | ||
|
||
/*** BOOLEAN ATTRS ***/ | ||
// generic function to bind boolean attrs | ||
function _observeBooleanAttr (attr, className) { | ||
var value; | ||
|
||
attrs.$observe(attr, function () { | ||
className = className || 'fa-' + attr; | ||
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().prop('tagName') === 'LI' && | ||
element.parent().parent() && | ||
element.parent().parent().hasClass('fa-ul') && | ||
element.parent().children()[0] === element[0] && | ||
attrs.list !== 'false' && | ||
attrs.list !== false | ||
); | ||
|
||
attrs.$observe('alt', function () { | ||
var altText = attrs.alt, | ||
altElem = element.next(), | ||
altElemClass = 'fa-alt-text'; | ||
|
||
if (altText) { | ||
element.removeAttr('alt'); | ||
|
||
// create the alt elem if one does not exist | ||
if (!altElem || !altElem.hasClass(altElemClass)) { | ||
element.after('<span class="sr-only fa-alt-text"></span>'); | ||
altElem = element.next(); | ||
} | ||
|
||
altElem.text(altText); | ||
} else if (altElem && altElem.hasClass(altElemClass)) { | ||
// kill the alt elem if we used to have alt text, but now we don't | ||
altElem.remove(); | ||
} | ||
}); | ||
} | ||
}; | ||
}) | ||
.directive('faStack', function () { | ||
return { | ||
restrict: 'E', | ||
transclude: true, | ||
template: '<span ng-transclude class="fa-stack fa-lg"></span>', | ||
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) { | ||
var className; | ||
|
||
attrs.$observe(attr, function () { | ||
baseClass = baseClass || 'fa-' + attr; | ||
element.removeClass(currentClasses[attr]); | ||
if (attrs[attr]) { | ||
className = [baseClass, attrs[attr]].join('-'); | ||
element.addClass(className); | ||
currentClasses[attr] = className; | ||
} | ||
}); | ||
} | ||
|
||
_observeStringAttr('size'); | ||
|
||
/** | ||
* 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; | ||
}); | ||
} | ||
}; | ||
}); |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,6 @@ | ||
{ | ||
"name": "angular-fontawesome", | ||
"version": "0.4.0", | ||
"version": "1.0.0", | ||
"author": "Paul Marbach <[email protected]>", | ||
"description": "Angular directive for FontAwesome", | ||
"scripts": { | ||
|