Skip to content

Instantly share code, notes, and snippets.

@MattFriedman
Last active January 2, 2016 05:19
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save MattFriedman/8256505 to your computer and use it in GitHub Desktop.
Save MattFriedman/8256505 to your computer and use it in GitHub Desktop.
A topcoat-icon angular directive. (relying on Modernizr and the topcoat IcomaticUtils file)
'use strict';
/**
* Usage: <span topcoat-icon="alert"></span>
*/
angular.module('listoutfitterApp').directive('topcoatIcon', function () {
var fontfaceSupported = Modernizr.fontface;
// don't use the IcomaticUtils fallback data structure.
// For performance use a lookup hash instead.
// This will be orders of magnitude faster.
var fallbacks = {};
for (var i = 0; i < IcomaticUtils.fallbacks.length; i++) {
fallbacks[IcomaticUtils.fallbacks[i].from]
= IcomaticUtils.fallbacks[i].to;
}
return {
replace: true,
restrict: 'A',
compile: function (el, attr) {
var topcoatIconText = attr.topcoatIcon;
el.text(topcoatIconText);
el.addClass("icomatic");
// If font-face is not supported then the goal is to end up
// with html that looks like this:
// <span topcoat-icon="next" class="icomatic">
// <span class="icomatic-alt">next</span>
// </span>
if (!fontfaceSupported) {
el.html(
'<span class="icomatic-alt">'
+ topcoatIconText
+ '</span>'
+ fallbacks[topcoatIconText]
);
}
}
};
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment