diff --git a/index.html b/index.html index eb3af2d..305fe0b 100644 --- a/index.html +++ b/index.html @@ -9,6 +9,7 @@ + @@ -75,22 +76,5 @@

AngularJs Google Chart Tools directive

- - diff --git a/karma.conf.js b/karma.conf.js index c966ded..99b367f 100644 --- a/karma.conf.js +++ b/karma.conf.js @@ -37,7 +37,7 @@ module.exports = function (config) { // possible values: 'dots', 'progress' // available reporters: https://npmjs.org/browse/keyword/karma-reporter reporters: ['dots', 'coverage'], - + coverageReporter:{type:'html', dir:'coverage/'}, // web server port @@ -59,10 +59,10 @@ module.exports = function (config) { // start these browsers // available browser launchers: https://npmjs.org/browse/keyword/karma-launcher - browsers: ['PhantomJS'], + browsers: ['PhantomJS', "Chrome"], // Continuous Integration mode // if true, Karma captures browsers, runs the tests and exits - singleRun: true + singleRun: false }); }; diff --git a/ng-google-chart.js b/ng-google-chart.js index 3f6c9e7..aca2fe8 100755 --- a/ng-google-chart.js +++ b/ng-google-chart.js @@ -1,4 +1,4 @@ -/*! angular-google-chart 2015-11-29 */ +/*! angular-google-chart 2016-09-20 */ /* * @description Google Chart Api Directive Module for AngularJS * @version 0.1.0 @@ -224,9 +224,9 @@ /* global angular */ (function(){ angular.module('googlechart') - .directive('agcBeforeDraw', onReadyDirective); + .directive('agcBeforeDraw', beforeDrawDirective); - function onReadyDirective(){ + function beforeDrawDirective(){ return { restrict: 'A', scope: false, @@ -243,6 +243,59 @@ }; } })(); + +/* global angular */ +(function() { + angular.module('googlechart') + .value('agcGstaticLoaderStrategy', agcGstaticLoaderStrategy); + + agcGstaticLoaderStrategy.$inject = ["$rootScope", "agcScriptTagHelper"]; + function agcGstaticLoaderStrategy($rootScope, agcScriptTagHelper){ + var tagPromise = agcScriptTagHelper(""); + } +})(); + +/* global angular */ +(function() { + angular.module("googlechart") + .factory("agcJsapiLoaderStrategy", agcJsapiLoaderStrategyFactory); + + agcJsapiLoaderStrategyFactory.$inject = ["$rootScope", "$q", "agcScriptTagHelper", "googleChartApiConfig"]; + function agcJsapiLoaderStrategyFactory($rootScope, $q, agcScriptTagHelper, googleChartApiConfig){ + return function agcJsapiLoaderStrategy(){ + + var apiReady = $q.defer(); + // Massage configuration as needed. + googleChartApiConfig.optionalSettings = googleChartApiConfig.optionalSettings || {}; + + var userDefinedCallback = googleChartApiConfig.optionalSettings.callback; + + var settings = { + callback: function() { + if (angular.isFunction(userDefinedCallback)) + userDefinedCallback.call(this); + + $rootScope.$apply(function(){ + apiReady.resolve(google); + }); + } + }; + + settings = angular.extend({}, googleChartApiConfig.optionalSettings, settings); + + agcScriptTagHelper("//www.google.com/jsapi") + .then(function(){ + window.google.load('visualization', googleChartApiConfig.version || '1', settings); + }) + .catch(function(){ + apiReady.reject(); + }); + + return apiReady.promise; + }; + } +})(); + (function(){ angular.module('googlechart') .directive('agcOnClick', onClickDirective); @@ -355,6 +408,37 @@ }; } })(); +/* global angular */ + +(function(){ + angular.module('googlechart') + .directive('agcOnRangeChange', agcOnRangeChangeDirective); + + function agcOnRangeChangeDirective(){ + return { + restrict: 'A', + scope: false, + require: 'googleChart', + link: function(scope, element, attrs, googleChartController){ + callback.$inject = ['args', 'chart', 'chartWrapper']; + function callback(args, chart, chartWrapper){ + var returnParams = { + chartWrapper: chartWrapper, + chart: chart, + args: args, + start: args[0].start, + end: args[0].end + }; + scope.$apply(function () { + scope.$eval(attrs.agcOnRangeChange, returnParams); + }); + } + googleChartController.registerChartListener('rangechange', callback, this); + } + }; + } +})(); + /* global angular */ (function(){ angular.module('googlechart') @@ -404,6 +488,54 @@ }; } })(); +/* global angular */ +(function() { + angular.module("googlechart") + .factory("agcScriptTagHelper", agcScriptTagHelperFactory); + + agcScriptTagHelperFactory.$inject = ["$q"]; + function agcScriptTagHelperFactory($q) + { + /** Add a script tag to the document's head section and return an angular + * promise that resolves when the script has loaded. + */ + function agcScriptTagHelper(url) + { + var deferred = $q.defer(); + var head = document.getElementsByTagName('head')[0]; + var script = document.createElement('script'); + + script.setAttribute('type', 'text/javascript'); + script.src = url; + + if (script.addEventListener) { // Standard browsers (including IE9+) + script.addEventListener('load', onLoad, false); + script.onerror = onError; + } else { // IE8 and below + script.onreadystatechange = function () { + if (script.readyState === 'loaded' || script.readyState === 'complete') { + script.onreadystatechange = null; + onLoad(); + } + }; + } + head.appendChild(script); + + function onLoad() { + deferred.resolve(); + } + + function onError() { + deferred.reject(); + } + + return deferred.promise; + } + + return agcScriptTagHelper; + } +})(); + /* global angular, google */ /* jshint -W072 */ (function(){ @@ -437,49 +569,10 @@ angular.module('googlechart') .factory('googleChartApiPromise', googleChartApiPromiseFactory); - googleChartApiPromiseFactory.$inject = ['$rootScope', '$q', 'googleChartApiConfig', 'googleJsapiUrl']; + googleChartApiPromiseFactory.$inject = ['$rootScope', '$q', 'googleChartApiConfig', 'agcJsapiLoaderStrategy']; - function googleChartApiPromiseFactory($rootScope, $q, apiConfig, googleJsapiUrl) { - apiConfig.optionalSettings = apiConfig.optionalSettings || {}; - var apiReady = $q.defer(); - var onLoad = function () { - // override callback function - var settings = { - callback: function () { - var oldCb = apiConfig.optionalSettings.callback; - $rootScope.$apply(function () { - apiReady.resolve(google); - }); - - if (angular.isFunction(oldCb)) { - oldCb.call(this); - } - } - }; - - settings = angular.extend({}, apiConfig.optionalSettings, settings); - - window.google.load('visualization', apiConfig.version, settings); - }; - var head = document.getElementsByTagName('head')[0]; - var script = document.createElement('script'); - - script.setAttribute('type', 'text/javascript'); - script.src = googleJsapiUrl; - - if (script.addEventListener) { // Standard browsers (including IE9+) - script.addEventListener('load', onLoad, false); - } else { // IE8 and below - script.onreadystatechange = function () { - if (script.readyState === 'loaded' || script.readyState === 'complete') { - script.onreadystatechange = null; - onLoad(); - } - }; - } - head.appendChild(script); - - return apiReady.promise; + function googleChartApiPromiseFactory($rootScope, $q, apiConfig, agcJsapiLoaderStrategy) { + return agcJsapiLoaderStrategy(); } })(); /* global angular */ @@ -795,25 +888,4 @@ return GoogleChartService; } })(); -/* global angular */ -(function(){ - angular.module('googlechart') - .provider('googleJsapiUrl', googleJsapiUrlProvider); - - function googleJsapiUrlProvider() { - var protocol = 'https:'; - var url = '//www.google.com/jsapi'; - - this.setProtocol = function (newProtocol) { - protocol = newProtocol; - }; - - this.setUrl = function (newUrl) { - url = newUrl; - }; - - this.$get = function () { - return (protocol ? protocol : '') + url; - }; - } -})(); \ No newline at end of file +//# sourceMappingURL=ng-google-chart.js.map \ No newline at end of file diff --git a/package.json b/package.json index 613c801..6f316f6 100644 --- a/package.json +++ b/package.json @@ -33,15 +33,16 @@ "grunt-contrib-jshint": "^0.11.2", "grunt-contrib-uglify": "^0.9.1", "grunt-contrib-watch": "^0.6.1", - "grunt-karma": "^0.12.0", - "jasmine-core": "^2.3.4", - "karma": "^0.13.9", - "karma-coverage": "^0.5.2", - "karma-jasmine": "^0.3.6", - "karma-jasmine-matchers": "^2.0.0-beta1", - "karma-phantomjs-launcher": "^0.2.1", - "karma-sinon": "^1.0.4", - "phantomjs": "^1.9.18", - "sinon": "^1.16.1" + "grunt-karma": "latest", + "jasmine-core": "latest", + "karma": "latest", + "karma-chrome-launcher": "^2.0.0", + "karma-coverage": "latest", + "karma-jasmine": "latest", + "karma-jasmine-matchers": "latest", + "karma-phantomjs-launcher": "latest", + "karma-sinon": "latest", + "phantomjs": "latest", + "sinon": "latest" } } diff --git a/sample.js b/sample.js index 06db085..4084dfe 100644 --- a/sample.js +++ b/sample.js @@ -22,10 +22,10 @@ angular.module("google-chart-sample", ["ngRoute", "googlechart"]).config(['$rout otherwise({ redirectTo: '/fat' }); - }]).value('googleChartApiConfig', { + }]);/*.value('googleChartApiConfig', { version: '1', optionalSettings: { packages: ['corechart', 'gauge'], language: 'fr' } - }); + });*/ diff --git a/src/googleChartApiPromise.js b/src/googleChartApiPromise.js index 7269ab0..a44abf9 100644 --- a/src/googleChartApiPromise.js +++ b/src/googleChartApiPromise.js @@ -3,48 +3,9 @@ angular.module('googlechart') .factory('googleChartApiPromise', googleChartApiPromiseFactory); - googleChartApiPromiseFactory.$inject = ['$rootScope', '$q', 'googleChartApiConfig', 'googleJsapiUrl']; + googleChartApiPromiseFactory.$inject = ['$rootScope', '$q', 'googleChartApiConfig', 'agcJsapiLoaderStrategy']; - function googleChartApiPromiseFactory($rootScope, $q, apiConfig, googleJsapiUrl) { - apiConfig.optionalSettings = apiConfig.optionalSettings || {}; - var apiReady = $q.defer(); - var onLoad = function () { - // override callback function - var settings = { - callback: function () { - var oldCb = apiConfig.optionalSettings.callback; - $rootScope.$apply(function () { - apiReady.resolve(google); - }); - - if (angular.isFunction(oldCb)) { - oldCb.call(this); - } - } - }; - - settings = angular.extend({}, apiConfig.optionalSettings, settings); - - window.google.load('visualization', apiConfig.version, settings); - }; - var head = document.getElementsByTagName('head')[0]; - var script = document.createElement('script'); - - script.setAttribute('type', 'text/javascript'); - script.src = googleJsapiUrl; - - if (script.addEventListener) { // Standard browsers (including IE9+) - script.addEventListener('load', onLoad, false); - } else { // IE8 and below - script.onreadystatechange = function () { - if (script.readyState === 'loaded' || script.readyState === 'complete') { - script.onreadystatechange = null; - onLoad(); - } - }; - } - head.appendChild(script); - - return apiReady.promise; + function googleChartApiPromiseFactory($rootScope, $q, apiConfig, agcJsapiLoaderStrategy) { + return agcJsapiLoaderStrategy(); } })(); \ No newline at end of file diff --git a/src/googleJsapiUrl.js b/src/googleJsapiUrl.js deleted file mode 100644 index bbbceae..0000000 --- a/src/googleJsapiUrl.js +++ /dev/null @@ -1,22 +0,0 @@ -/* global angular */ -(function(){ - angular.module('googlechart') - .provider('googleJsapiUrl', googleJsapiUrlProvider); - - function googleJsapiUrlProvider() { - var protocol = 'https:'; - var url = '//www.google.com/jsapi'; - - this.setProtocol = function (newProtocol) { - protocol = newProtocol; - }; - - this.setUrl = function (newUrl) { - url = newUrl; - }; - - this.$get = function () { - return (protocol ? protocol : '') + url; - }; - } -})(); \ No newline at end of file diff --git a/test/googleChartApiPromise.spec.js b/test/googleChartApiPromise.spec.js index 4c653b3..7e3727c 100644 --- a/test/googleChartApiPromise.spec.js +++ b/test/googleChartApiPromise.spec.js @@ -27,6 +27,23 @@ describe('googleChartApiPromise factory', function() { expect(googleChartApiPromise.catch).toBeFunction(); expect(googleChartApiPromise.finally).toBeFunction(); }); +}); + +describe('googleChartApiPromise functionality', function() { + var googleChartApiPromise; + beforeEach(module('googlechart', function($provide) { + $provide.provider('googleJsapiUrl', function() { + return { + $get: function() { + return 'https://www.google.com/jsapi'; + } + }; + }); + })); + + beforeEach(inject(function($injector) { + googleChartApiPromise = $injector.get('googleChartApiPromise'); + })); it('should load the google api', function(done) { @@ -41,6 +58,6 @@ describe('googleChartApiPromise factory', function() { */ expect(true).toBe(false); done(); - }) + },10000) }); }); \ No newline at end of file diff --git a/test/googleJsapiUrl.spec.js b/test/googleJsapiUrl.spec.js deleted file mode 100644 index 449af01..0000000 --- a/test/googleJsapiUrl.spec.js +++ /dev/null @@ -1,36 +0,0 @@ -/* global angular */ -/* eslint-env jasmine */ -describe('googleJsapiUrl provider', function() { - var providerInstance; - - //capture the provider by injecting it into a fake module - beforeEach(function() { - angular.module('fakeModule',[]) - .config(function(googleJsapiUrlProvider) { - providerInstance = googleJsapiUrlProvider; - }); - - module('googlechart', 'fakeModule'); - - //run injector to get the provider instance - inject(function(){}); - }); - - it ('should return default value if nothing changed', function(){ - var result = providerInstance.$get(); - expect(result).toBe('https://www.google.com/jsapi'); - }); - - it ('should return http url if protocol changed', function(){ - providerInstance.setProtocol('http:'); - var result = providerInstance.$get(); - expect(result).toMatch(/^http:/); - expect(result).not.toMatch(/^https:/); - }); - - it ('should return url as set if it has been set', function(){ - providerInstance.setUrl('//www.example.com/api'); - var result = providerInstance.$get(); - expect(result).toMatch(/\/\/www\.example\.com\/api$/); - }); -}); \ No newline at end of file