Skip to content

Commit 84116c5

Browse files
author
Elliott Marquez
committed
move style localization to function and two loops and only once
1 parent 892aba1 commit 84116c5

File tree

1 file changed

+31
-25
lines changed

1 file changed

+31
-25
lines changed

google-chart.html

Lines changed: 31 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -312,31 +312,10 @@
312312
this.$.loader.create(this.type, this.$.chartdiv)
313313
.then(function(chart) {
314314

315-
// get all gchart stylesheets
316-
var stylesheets =
317-
Polymer.dom(document.head)
318-
.querySelectorAll('link[rel="stylesheet"][type="text/css"]');
319-
var gchartStylesheets = Array.from(stylesheets).filter(
320-
function(element) {
321-
return element.id.indexOf('load-css-') == 0;
322-
});
323-
324-
// clone necessary attributes
325-
var clonedSheets = gchartStylesheets.map(
326-
function(stylesheet) {
327-
var link = document.createElement('link');
328-
link.setAttribute('rel', 'stylesheet');
329-
link.setAttribute('type', 'text/css');
330-
link.setAttribute('href', stylesheet.getAttribute('href'));
331-
return link;
332-
}
333-
);
334-
335-
// append them to this local dom (for sdom support)
336-
clonedSheets.forEach(
337-
function(stylesheet) {
338-
Polymer.dom(this.$.styles).appendChild(stylesheet);
339-
}.bind(this));
315+
// only add link stylesheet elements if there are none already
316+
if (!this.$.styles.children.length) {
317+
this._localizeGlobalStylesheets();
318+
}
340319

341320
var loader = this.$.loader;
342321
Object.keys(this.events.concat(['select', 'ready'])
@@ -497,6 +476,33 @@
497476
.then(function(dataView) {
498477
this._dataView = dataView;
499478
}.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+
}
500506
}
501507
});
502508
})();

0 commit comments

Comments
 (0)