Re: [XBL] Accessing flattened tree parents of a shadow tree

Cameron McCormack:
> > This won't work if the svg:svg ancestor element is more than one 
> > shadow scope away.

Ian Hickson:
> Ok:
> 
>   get ownerSVGElement() {
>     var owner = this.boundElement.parentNode;
>     if (owner && !(owner instanceof SVGSVGElement))
>       owner = owner.ownerSVGElement;
>     return owner;
>   }

My example before wasn’t the best, I think.  Here’s a more concrete one:
I want to write a function that will center an element in its viewport.
I want this to work for any element that has implemented
SVGTransformable.  For this, the function will need to use the
.nearestViewportElement property to find the closest svg:svg element
that created the viewport.

  <?xbl href="http://someone.elses.bindings.org/containers.xbl"?>
  <svg width="400" height="400" viewBox="0 0 100 100">
    <binding element="ex|something">
      <implementation>
        ...
      </implementation>
    </binding>
  
    <script>
      function centerInViewport(e) {
        // Find the SVG element that created the viewport.
        var svg = e.nearestViewportElement;
  
        // Determine the center point of the viewport.
        var vb = svg.viewBox.baseVal;
        var vbMidX = vb.x + vb.width / 2;
        var vbMidY = vb.y + vb.height / 2;
  
        // Determine the center point of the element.
        var bb = e.getBBox();
        var bbMidX = bb.x + bb.width / 2;
        var bbMidY = bb.y + bb.height / 2;
  
        // Determine the amount to translate by.
        var dx = vbMidX - bbMidX;
        var dy = vbMidY - bbMidY;
  
        // Translate the element.
        var translation = svg.createSVGTransform();
        translation.setTranslate(dx, dy);
        e.transform.baseVal.appendItem(translation);
      }
    </script>
  
    ...

    <svg x="0" y="0" width="100" height="50" viewBox="0 0 50 50">
      <other:container>
        <ex:something id="x"/>
      </other:container>
    </svg>
  
    <script>
      centerInViewport(document.getElementById('x'));
    </script>
  </svg>

I don’t know how other:container is implemented; it may not have a
.nearesetViewportElement property on its implementation object.

> But could you elaborate on why an author would ever actually want to do 
> this though? I really don't understand what the use case is.

Hopefully the above should explain.  Any time the binding needs to know
about its context it’ll need to have some way of getting out to the
enclosing shadow scopes.

-- 
Cameron McCormack, http://mcc.id.au/
 xmpp:heycam@jabber.org  ▪  ICQ 26955922  ▪  MSN cam@mcc.id.au

Received on Thursday, 15 February 2007 00:29:44 UTC