Skip to content

Instantly share code, notes, and snippets.

@valmirphp
Forked from fergalhanley/vod-svg.js
Last active February 18, 2016 04:38
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 valmirphp/d7edd6fc34fc50f41509 to your computer and use it in GitHub Desktop.
Save valmirphp/d7edd6fc34fc50f41509 to your computer and use it in GitHub Desktop.
This AngularJS directive allows changing the color of dynamically loaded SVG files. Subject to same domain policy.
angular
.module('241App.site.directives', [])
.directive('pmtMap', mapDirective)
.directive('pmtIconSvg', iconSvg2Directive);
function vodSvgDirective() {
return {
restrict: 'E',
replace: 'true',
template: '<object type="image/svg+xml" data=""></object>',
link: function(scope, element, attrs) {
var paintSvg = function (svgElem){
var paths = [];
['path', 'circle', 'rectangle', 'polygon'].forEach(function(shape){
paths.push.apply(paths, svgElem.getElementsByTagName(shape));
});
for(var i = 0; i < paths.length; i++) {
paths[i].style.fill = attrs.color || "#BB2727";
}
};
element.bind('load', function(e) {
paintSvg( element[0].getSVGDocument() );
});
}
};
};
function iconSvg2Directive()
{
var LinkIconDirective = function(scope, element, attrs) {
var paintSvg = function (svgElem){
var paths = [];
['path', 'circle', 'rectangle', 'polygon'].forEach(function(shape){
paths.push.apply(paths, svgElem.getElementsByTagName(shape));
});
for(var i = 0; i < paths.length; i++) {
paths[i].style.fill = attrs.fill || "#BB2727";
}
};
var styles = [
"width : " + attrs.width || '32px',
"height : " + attrs.height || '32px',
];
element.attr('data', attrs.path);
element.attr('style', styles.join(';') );
element.bind('load', function(e) {
paintSvg( element[0].getSVGDocument() );
});
}
return {
restrict: 'E',
replace: 'true',
template: '<object type="image/svg+xml" data=""></object>',
link: LinkIconDirective
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment