|
58 | 58 | --> |
59 | 59 | <dom-module id="google-chart"> |
60 | 60 | <template> |
| 61 | + <div id="styles"></div> |
61 | 62 | <style include="google-chart-styles"></style> |
62 | 63 | <google-chart-loader id="loader" type="[[type]]"></google-chart-loader> |
63 | 64 | <div id="chartdiv"></div> |
|
310 | 311 | _typeChanged: function() { |
311 | 312 | this.$.loader.create(this.type, this.$.chartdiv) |
312 | 313 | .then(function(chart) { |
| 314 | + |
| 315 | + // only add link stylesheet elements if there are none already |
| 316 | + if (!this.$.styles.children.length) { |
| 317 | + this._localizeGlobalStylesheets(); |
| 318 | + } |
| 319 | + |
313 | 320 | var loader = this.$.loader; |
314 | 321 | Object.keys(this.events.concat(['select', 'ready']) |
315 | 322 | .reduce(function(set, eventName) { |
|
469 | 476 | .then(function(dataView) { |
470 | 477 | this._dataView = dataView; |
471 | 478 | }.bind(this)); |
| 479 | + }, |
| 480 | + |
| 481 | + /** |
| 482 | + * Queries global document head for google charts link#load-css-* and clones |
| 483 | + * them into the local root's div#styles element for shadow dom support. |
| 484 | + */ |
| 485 | + _localizeGlobalStylesheets: function() { |
| 486 | + // get all gchart stylesheets |
| 487 | + var stylesheets = Polymer.dom(document.head) |
| 488 | + .querySelectorAll('link[rel="stylesheet"][type="text/css"]'); |
| 489 | + |
| 490 | + var stylesheetsArray = Array.from(stylesheets); |
| 491 | + |
| 492 | + for (var i = 0; i < stylesheetsArray.length; i++) { |
| 493 | + var sheetLinkEl = stylesheetsArray[i]; |
| 494 | + var isGchartStylesheet = sheetLinkEl.id.indexOf('load-css-') == 0; |
| 495 | + |
| 496 | + if (isGchartStylesheet) { |
| 497 | + // clone necessary stylesheet attributes |
| 498 | + var clonedLinkEl = document.createElement('link'); |
| 499 | + clonedLinkEl.setAttribute('rel', 'stylesheet'); |
| 500 | + clonedLinkEl.setAttribute('type', 'text/css'); |
| 501 | + clonedLinkEl.setAttribute('href', sheetLinkEl.getAttribute('href')); |
| 502 | + |
| 503 | + Polymer.dom(this.$.styles).appendChild(clonedLinkEl); |
| 504 | + } |
| 505 | + } |
472 | 506 | } |
473 | 507 | }); |
474 | 508 | })(); |
|
0 commit comments