Skip to content

Instantly share code, notes, and snippets.

@navad
Last active October 9, 2016 02:25
Show Gist options
  • Save navad/c6da12424ef9bc573700 to your computer and use it in GitHub Desktop.
Save navad/c6da12424ef9bc573700 to your computer and use it in GitHub Desktop.
Grunticon embedder for use in Angular directives/templates
/* globals grunticon: false */
(function() {
'use strict';
angular.module('ngGrunticon', [])
.provider('grunticonEmbedConfig', function() {
this.gruntIcons = undefined;
this.init = function(grunticonCss) {
var css = grunticon.getCSS(grunticonCss);
css = css ? css : window.document.querySelector('[data-grunticon-css]');
if (css) {
this.gruntIcons = {};
var icons = grunticon.getIcons(css);
Object.keys(icons).forEach(function(key) {
var selector = key.slice('grunticon:.'.length);
this.gruntIcons[selector] = icons[key];
}, this);
}
};
this.$get = function() {
return this;
};
})
.directive('grunticonEmbed', function($log, grunticonEmbedConfig) {
return {
restrict: 'A',
scope: {
grunticonEmbed: '@'
},
controller: function($scope, $element) {
$scope.$watch('grunticonEmbed', function(newClass, oldClass) {
if (oldClass) {
$element.removeClass(oldClass);
}
if (newClass) {
$element.addClass(newClass);
}
embedIcon($element);
});
}
};
function embedIcon(element) {
if (grunticonEmbedConfig.gruntIcons) {
var classListArray = element[0].classList.toString().split(' ');
var icons = arrayIntersection(classListArray, Object.keys(grunticonEmbedConfig.gruntIcons));
if (icons.length === 1) {
element[0].innerHTML = grunticonEmbedConfig.gruntIcons[icons[0]];
element[0].style.backgroundImage = 'none';
}
} else {
$log.error('Grunticon CSS not configured! please use ' +
'\'grunticonEmbedConfigProvider.init()\' to configure.');
}
}
});
// Utility
function arrayIntersection(array1, array2) {
return array1.filter(function(n) {
return array2.indexOf(n) !== -1;
});
}
})();
@navad
Copy link
Author

navad commented Oct 26, 2015

Gist updated to support dynamic class name + other improvements.

Dynamic class name:

Ideally, we would like the embedded to work with DOM element which has ng-class in them to select dynamically the name of the icon to show. However, we couldn't make our embedded to work after ng-class has finished injecting the classes to the DOM element. Therefore, we had to do some workaround:

Not working: <div ng-class="myIcon" data-grunticon-embed>
Working: <div data-grunticon-embed="{{ myIcon }}">

If anyone has a better idea we would love to hear

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment