UNPKG

backbone-ng2-google-charts

Version:
99 lines (98 loc) 3.81 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); var core_1 = require("@angular/core"); var GoogleChartsLoaderService = (function () { function GoogleChartsLoaderService() { this.chartPackage = { AnnotationChart: 'annotationchart', AreaChart: 'corechart', Bar: 'bar', BarChart: 'corechart', BubbleChart: 'corechart', Calendar: 'calendar', CandlestickChart: 'corechart', ColumnChart: 'corechart', ComboChart: 'corechart', PieChart: 'corechart', Gantt: 'gantt', Gauge: 'gauge', GeoChart: 'geochart', Histogram: 'corechart', Line: 'line', LineChart: 'corechart', Map: 'map', OrgChart: 'orgchart', Sankey: 'sankey', Scatter: 'scatter', ScatterChart: 'corechart', SteppedAreaChart: 'corechart', Table: 'table', Timeline: 'timeline', TreeMap: 'treemap', WordTree: 'wordtree' }; this.googleScriptLoadingNotifier = new core_1.EventEmitter(); this.googleScriptIsLoading = false; } GoogleChartsLoaderService.prototype.load = function (chartType) { var _this = this; return new Promise(function (resolve, reject) { if (resolve === void 0) { resolve = Function.prototype; } if (reject === void 0) { reject = Function.prototype; } _this.loadGoogleChartsScript().then(function () { google.charts.load('45', { packages: [_this.chartPackage[chartType]], callback: resolve }); }).catch(function () { console.error('Google charts script could not be loaded'); }); }); }; GoogleChartsLoaderService.prototype.loadGoogleChartsScript = function () { var _this = this; return new Promise(function (resolve, reject) { if (resolve === void 0) { resolve = Function.prototype; } if (reject === void 0) { reject = Function.prototype; } if (typeof google !== 'undefined' && google.charts) { resolve(); } else if (!_this.googleScriptIsLoading) { _this.googleScriptIsLoading = true; var script = document.createElement('script'); script.type = 'text/javascript'; script.src = 'https://www.gstatic.com/charts/loader.js'; script.async = true; script.defer = true; script.onload = function () { _this.googleScriptIsLoading = false; _this.googleScriptLoadingNotifier.emit(true); resolve(); }; script.onerror = function () { _this.googleScriptIsLoading = false; _this.googleScriptLoadingNotifier.emit(false); reject(); }; document.getElementsByTagName('head')[0].appendChild(script); } else { _this.googleScriptLoadingNotifier.subscribe(function (loaded) { if (loaded) { resolve(); } else { reject(); } }); } }); }; return GoogleChartsLoaderService; }()); GoogleChartsLoaderService.decorators = [ { type: core_1.Injectable }, ]; /** @nocollapse */ GoogleChartsLoaderService.ctorParameters = function () { return []; }; exports.GoogleChartsLoaderService = GoogleChartsLoaderService;