Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

AngularJS : Transcluding SVG elements in directives

I'm interested in building a composite SVG by composing reusable graphical elements wrapped inside of Angular.js directives. For instance I might have:

<div ng-app="svgApp">
  <canvas>
    <drawing ng-repeat="i in [1,2,3]" cy="{{i * 40}}"></drawing>
  </canvas>
</div>

where I am defining the following directives:

  .directive('canvas', function () {
    return {
      template: '<svg height=200 width=100 ng-transclude></svg>',
      restrict: 'E',
      replace: true,
      transclude: true
    };
  })

  .directive('drawing', function () {
    return {
      template: '<circle cx=50 r=15></circle>',
      restrict: 'E',
      replace: true
    };
  })

The problem is that the SVG elements do not appear to get transcluded properly. A clue seems to be here, in another StackOverflow question that this is mostly because SVG nodes are not created properly within Angular.js.

After poking around further, I found this solution, which involves using a helper function to replace the relevant DOM elements with properly created SVG nodes, a la:

  .value('createSVGNode', function(name, element, settings) {
      var namespace = 'http://www.w3.org/2000/svg';
      var node = document.createElementNS(namespace, name);
      for (var attribute in settings) {
        var value = settings[attribute];
        if (value !== null && !attribute.match(/\$/) && (typeof value !== 'string' || value !== '')) {
            node.setAttribute(attribute, value);
        }
      }
      return node;
  });

However, it seemed undesirable that I would need to use this everywhere, and I wanted to keep the workaround as local to the bug as possible, until it was fixed.

My question is whether the following would be a reasonable workaround:

angular.forEach(['circle', ...], function (svgElem) {

  svgModule

    .directive(svgElem, function (createSVGNode) {
      return {
        restrict: 'E',
        link: function(scope, element, attrs) {
          var node = createSVGNode(svgElem, element, attrs);
          angular.element(node).append(element[0].childNodes);
          element.replaceWith(node);
        }
      };
    });

});

And this works in Plunker!

Is it valid for me to redefine existing SVG element directives in this way?

like image 930
christang Avatar asked Apr 26 '14 00:04

christang


1 Answers

The above is a possible approach if you cannot move your code up to the active development tip of AngularJS 1.3 where the issue of transcluding elements of a different namespace (such as SVG or MathML) is resolved.

The linked Plunker demonstrates how to update your code with the fix. The key is the addition of a new "templateNamespace" key in the directive definition object:

  .directive('svgInternal', function () {
    return {
      templateNamespace: 'svg',
      template: '<g><rect height="25" width="25" /><text x="30" y="20">Hello, World</text></g>',
      restrict: 'E',
      replace: true
    };
  })

where the following markup demonstrates the SVG-type directive in use:

  <svg height="30">
    <svg-internal></svg-internal>
  </svg>

Edit: "type" was changed to "templateNamespace" since 1.3.beta.19.

like image 117
christang Avatar answered Nov 09 '22 20:11

christang