Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add load function #245

Merged
merged 1 commit into from
Jun 25, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
54 changes: 43 additions & 11 deletions google-chart-loader.js
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,44 @@ var promises = {};
/** @type {!Object<string, function(!Object)>} resolves for the package promises */
var resolves = {};

/**
* @typedef {{
* version: (string|undefined),
* packages: (!Array<string>|undefined),
* language: (string|undefined),
* mapsApiKey: (string|undefined),
* }}
*/
var LoadSettings;

/**
* Loads Google Charts API with the selected settings or using defaults.
*
* The following settings are available:
* - version: which version of library to load, default: 'current',
* - packages: which chart packages to load, default: ['corechart'],
* - language: what language to load library in, default: `lang` attribute on
* `<html>` or 'en' if not specified,
* - mapsApiKey: key to use for maps API.
*
* @param {!LoadSettings=} settings
* @return {!Promise}
*/
export async function load(settings = {}) {
await loaderPromise;
const {
version = 'current',
packages = [DEFACTO_CHART_PACKAGE],
language = document.documentElement.lang || 'en',
mapsApiKey,
} = settings;
return google.charts.load(version, {
'packages': packages,
'language': language,
'mapsApiKey': mapsApiKey,
});
}

Polymer({
is: 'google-chart-loader',
properties: {
Expand Down Expand Up @@ -206,18 +244,12 @@ Polymer({
return;
}
packagesToLoad = {};
loaderPromise.then(function() {
google.charts.load('current', {
'packages': packages,
'language': document.documentElement.lang || 'en',
loaderPromise.then(() => load()).then(() => {
packages.forEach((pkg) => {
this.fire('loaded', pkg);
resolves[pkg](google.visualization);
});
google.charts.setOnLoadCallback(function() {
packages.forEach(function(pkg) {
this.fire('loaded', pkg);
resolves[pkg](google.visualization);
}.bind(this));
}.bind(this));
}.bind(this));
});
}, 100);
},

Expand Down
29 changes: 29 additions & 0 deletions test/custom-load-test.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
<!--
@license
Copyright (c) 2014 The Polymer Project Authors. All rights reserved.
This code may only be used under the BSD style license found at https://polymer.github.io/LICENSE.txt
The complete set of authors may be found at https://polymer.github.io/AUTHORS.txt
The complete set of contributors may be found at https://polymer.github.io/CONTRIBUTORS.txt
Code distributed by Google as part of the polymer project is also
subject to an additional IP rights grant found at https://polymer.github.io/PATENTS.txt
-->
<!DOCTYPE html>
<html lang="de">
<script src="../../../@webcomponents/webcomponentsjs/webcomponents-bundle.js"></script>
<script src="../../../wct-browser-legacy/browser.js"></script>
<script type="module">
import {load} from '../google-chart-loader.js';

// This test has to run separately because Google Charts API is loaded only once per document.
suite('custom load test', () => {
test('loads Google Charts API with custom settings', async () => {
await load({version: '45.2'});
// Verify that the library has been loaded with correct settings by
// inspecting scripts added to the document.
assert.isNotNull(document.querySelector('script[src*="charts/45.2"]'));
assert.isNotNull(document.querySelector('script[src*="corechart_module"]'));
assert.isNotNull(document.querySelector('script[src*="__de"]'));
});
});
</script>
</html>
3 changes: 2 additions & 1 deletion test/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,8 @@
// Load and run all tests (.html, .js) as one suite:
WCT.loadSuites([
'basic-tests.html?wc-shadydom=true&wc-ce=true',
'basic-tests.html?dom=shadow'
'basic-tests.html?dom=shadow',
'custom-load-test.html',
]);
</script>
</body>
Expand Down