Search code examples
polymercytoscape.jscytoscape

How to use cytoscape in a polymer element?


This is my polymer element:

<link rel="import" href="../polymer/polymer.html">
<script src="bower_components/cytoscape/dist/cytoscape.js"></script>

<dom-module id="dependency-graph">
    <template>
        <style>    
            #surface {
                width: 200px;
                height: 200px;
            }
        </style>
        <div id="surface"></div>
    </template>
    <script>
        Polymer({    
            is: 'dependency-graph',    
            ready: function () {  
                var surface = this.$.surface;
                var cy = cytoscape({
                    container: surface                       
                });
...

Running this throws an exception in cytoscape

return ( _p.sizeCache = _p.sizeCache || ( container ? (function(){
    var rect = container.getBoundingClientRect();
    var style = window.getComputedStyle( container );
    var val = function( name ){ return parseFloat( style.getPropertyValue( name ) ); };

    return {
      width: rect.width - val('padding-left') - val('padding-right') - val('border-left-width') - val('border-right-width'),
      height: rect.height - val('padding-top') - val('padding-bottom') - val('border-top-width') - val('border-bottom-width')
    };

because style.getPropertyValue returns "". If I use document.body as container it will return a number and cytoscape works. What did I do wrong and how can I fix it?

I found an article about a similar problem but I did not get it (or it did not work?): webcomponentsjs is not compatible with third-party scripts that use window.getComputedStyle


Solution

  • I moved my code to the attached: function () { and now it works without any problem. Thx @maxkfranz for the idea.