-
Notifications
You must be signed in to change notification settings - Fork 0
/
utils-lib.js
116 lines (112 loc) · 3.9 KB
/
utils-lib.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
/**
* PolymerVis is a suite of Polymer elements for visualizations.
* @name PolymerVis
* @module PolymerVis
*/
(function(PolymerVis) {
/**
* Convenience method for dynamically loading a script.
*
* This method creates a new `<script>` element with the provided URL and
* appends it to the document to start loading. In the onload callback, the
* import property of the link element will contain the imported document
* contents.
*
* @alias module:loadScript
* @param {string} src The url to the script to load.
* @param {Function} onload callback when script is loaded.
* @param {Function} onerror callback when error loading script.
* @param {boolean} optAsync whether to execute the script asynchronously.
* @return {HTMLScriptElement}
*/
PolymerVis.loadScript = function(src, onload, onerror, optAsync = true) {
var ele = document.createElement('script');
ele.src = src;
ele.async = optAsync;
if (onload) ele.onload = onload;
if (onerror) ele.onerror = onerror;
document.body.appendChild(ele);
return ele;
};
/**
* Convenience method for dynamically loading a stylesheet.
*
* This method creates a new `<link rel="stylesheet">` element with the
* provided URL and appends it to the document to start loading. In the onload
* callback, the import property of the link element will contain the
* imported document contents.
*
* @alias module:loadStylesheet
* @param {string} href The url to the script to load.
* @param {Function} onload callback when script is loaded.
* @param {Function} onerror callback when error loading script.
* @param {boolean} optAsync whether to execute the script asynchronously.
* @return {HTMLLinkElement}
*/
PolymerVis.loadStylesheet = function(href, onload, onerror, optAsync = true) {
var ele = document.createElement('link');
ele.href = href;
ele.rel = 'stylesheet';
ele.async = optAsync;
if (onload) ele.onload = onload;
if (onerror) ele.onerror = onerror;
document.body.appendChild(ele);
return ele;
};
/**
* Load an external CSS file, and insert a `style` element
* into the shadowRoot.
* @alias module:insertCssIntoShadowRoot
* @param {String} cssSrc url to the css file
* @param {String} shadowRoot Node to insert the `style` element
* @param {Function} onload callback when CSS is inserted
* @param {String} id id for style element
* @example
* PolymerVis.insertCssIntoShadowRoot('https://some.css', ele.shadowRoot, 'custom');
*/
PolymerVis.insertCssIntoShadowRoot = function insertCssIntoShadowRoot(
cssSrc,
shadowRoot,
onload,
id = 'custom'
) {
var httpRequest = new XMLHttpRequest();
if (!httpRequest) {
console.warn(`cannot create XMLHttpRequest to load ${cssSrc}`);
return;
}
httpRequest.onreadystatechange = () => {
if (httpRequest.readyState === XMLHttpRequest.DONE) {
if (httpRequest.status === 200) {
var style =
shadowRoot.querySelector(`#${id}`) ||
document.createElement('style');
style.id = id;
style.textContent = httpRequest.responseText;
shadowRoot.appendChild(style);
if (onload) onload(style);
} else {
console.warn(`cannot load ${cssSrc}`);
}
}
};
httpRequest.open('GET', cssSrc);
httpRequest.send();
};
/**
* Check if the node is inside a [`shadowRoot`](https://developer.mozilla.org/en-US/docs/Web/API/shadowRoot) or not.
* Return the `shadowRoot` otherwise return `false`.
* @alias module:isInShadowRoot
* @param {Node} node the node to check
* @return {Node}
*/
PolymerVis.isInShadowRoot = function isInShadowRoot(node) {
while (node) {
if (node.toString() === '[object ShadowRoot]') {
return node;
}
node = node.parentNode;
}
return false;
};
})((window.PolymerVis = window.PolymerVis || {}));