diff --git a/dist/highcharts-ng.js b/dist/highcharts-ng.js index 588787b..135a3fb 100644 --- a/dist/highcharts-ng.js +++ b/dist/highcharts-ng.js @@ -1,6 +1,6 @@ /** * highcharts-ng - * @version v1.0.2-dev - 2017-03-02 + * @version v1.0.2-dev - 2017-03-26 * @link /~https://github.com/pablojim/highcharts-ng * @author Barry Fitzgerald <> * @license MIT License, http://www.opensource.org/licenses/MIT @@ -12,7 +12,15 @@ if (typeof module !== 'undefined' && typeof exports !== 'undefined' && module.ex (function () { 'use strict'; - /*global angular: false, Highcharts: false */ + /*global angular: false*/ + var Highcharts = null; + + if (window && window.Highcharts) { + Highcharts = window.Highcharts; + } + if (module && module.exports === 'highcharts-ng') { + Highcharts = require('highcharts'); + } angular.module('highcharts-ng', []) diff --git a/dist/highcharts-ng.min.js b/dist/highcharts-ng.min.js index 875b5c3..99a5d89 100644 --- a/dist/highcharts-ng.min.js +++ b/dist/highcharts-ng.min.js @@ -1,8 +1,8 @@ /** * highcharts-ng - * @version v1.0.2-dev - 2017-03-02 + * @version v1.0.2-dev - 2017-03-26 * @link /~https://github.com/pablojim/highcharts-ng * @author Barry Fitzgerald <> * @license MIT License, http://www.opensource.org/licenses/MIT */ -"undefined"!=typeof module&&"undefined"!=typeof exports&&module.exports===exports&&(module.exports="highcharts-ng"),function(){"use strict";function a(a,e){var f=0,g=this,h={},i={},j=g.changeDetection||angular.equals;this.$onInit=function(){g.config.getChartObj=function(){return g.chart},h=angular.merge({},g.config),i=b(a,g.config,f),g.chart=new(Highcharts[c(i)])(i);var d=a[0].clientWidth,j=a[0].clientHeight;e(function(){a[0].clientWidth===d&&a[0].clientHeight===j||g.chart.reflow()},0,!1)},this.$doCheck=function(){if(!j(g.config,h)){h=angular.merge({},g.config),i=b(a,g.config,f);var c=d(i.series,f);if(i.series){for(var e=g.chart.series.length-1;e>=0;e--){var k=g.chart.series[e];"highcharts-navigator-series"!==k.options.id&&c.indexOf(k.options.id)<0&&k.remove(!1)}angular.forEach(g.config.series,function(a){g.chart.get(a.id)||g.chart.addSeries(a)})}g.chart.update(i,!0)}},this.$onDestroy=function(){if(g.chart){try{g.chart.destroy()}catch(b){}e(function(){a.remove()},0)}}}function b(a,b,c){var e={},f={chart:{events:{}},title:{},subtitle:{},series:[],credits:{},plotOptions:{},navigator:{}};return b?(b.series&&d(b.series,c),e=angular.merge(f,b)):e=f,e.chart.renderTo=a[0],e}function c(a){return void 0===a||void 0===a.chartType?"Chart":e[(""+a.chartType).toLowerCase()]}function d(a,b){var c=[];return angular.forEach(a,function(a){angular.isDefined(a.id)||(a.id="series-"+b++),c.push(a.id)}),c}angular.module("highcharts-ng",[]).component("highchart",{bindings:{config:"<",changeDetection:"<"},controller:a}),a.$inject=["$element","$timeout"];var e={stock:"StockChart",map:"Map",chart:"Chart"}}(); \ No newline at end of file +"undefined"!=typeof module&&"undefined"!=typeof exports&&module.exports===exports&&(module.exports="highcharts-ng"),function(){"use strict";function a(a,f){var g=0,h=this,i={},j={},k=h.changeDetection||angular.equals;this.$onInit=function(){h.config.getChartObj=function(){return h.chart},i=angular.merge({},h.config),j=b(a,h.config,g),h.chart=new(e[c(j)])(j);var d=a[0].clientWidth,k=a[0].clientHeight;f(function(){a[0].clientWidth===d&&a[0].clientHeight===k||h.chart.reflow()},0,!1)},this.$doCheck=function(){if(!k(h.config,i)){i=angular.merge({},h.config),j=b(a,h.config,g);var c=d(j.series,g);if(j.series){for(var e=h.chart.series.length-1;e>=0;e--){var f=h.chart.series[e];"highcharts-navigator-series"!==f.options.id&&c.indexOf(f.options.id)<0&&f.remove(!1)}angular.forEach(h.config.series,function(a){h.chart.get(a.id)||h.chart.addSeries(a)})}h.chart.update(j,!0)}},this.$onDestroy=function(){if(h.chart){try{h.chart.destroy()}catch(b){}f(function(){a.remove()},0)}}}function b(a,b,c){var e={},f={chart:{events:{}},title:{},subtitle:{},series:[],credits:{},plotOptions:{},navigator:{}};return b?(b.series&&d(b.series,c),e=angular.merge(f,b)):e=f,e.chart.renderTo=a[0],e}function c(a){return void 0===a||void 0===a.chartType?"Chart":f[(""+a.chartType).toLowerCase()]}function d(a,b){var c=[];return angular.forEach(a,function(a){angular.isDefined(a.id)||(a.id="series-"+b++),c.push(a.id)}),c}var e=null;window&&window.Highcharts&&(e=window.Highcharts),module&&"highcharts-ng"===module.exports&&(e=require("highcharts")),angular.module("highcharts-ng",[]).component("highchart",{bindings:{config:"<",changeDetection:"<"},controller:a}),a.$inject=["$element","$timeout"];var f={stock:"StockChart",map:"Map",chart:"Chart"}}(); \ No newline at end of file diff --git a/karma.conf.js b/karma.conf.js index 3c16c21..090431c 100644 --- a/karma.conf.js +++ b/karma.conf.js @@ -16,6 +16,7 @@ module.exports = function(config) { 'node_modules/angular/angular.js', 'node_modules/jquery/dist/jquery.js', 'node_modules/angular-mocks/angular-mocks.js', + 'test/highcharts-mock.js', 'src/*.js', 'test/spec/*.js' ], diff --git a/package.json b/package.json index 6c2126d..8245e04 100644 --- a/package.json +++ b/package.json @@ -47,5 +47,7 @@ "test": "grunt test" }, "main": "./dist/highcharts-ng", - "dependencies": {} + "dependencies": { + "highcharts": "^5.0.9" + } } diff --git a/src/highcharts-ng.js b/src/highcharts-ng.js index 9248e24..23f8143 100644 --- a/src/highcharts-ng.js +++ b/src/highcharts-ng.js @@ -4,7 +4,15 @@ if (typeof module !== 'undefined' && typeof exports !== 'undefined' && module.ex (function () { 'use strict'; - /*global angular: false, Highcharts: false */ + /*global angular: false*/ + var Highcharts = null; + + if (window && window.Highcharts) { + Highcharts = window.Highcharts; + } + if (module && module.exports === 'highcharts-ng') { + Highcharts = require('highcharts'); + } angular.module('highcharts-ng', []) diff --git a/test/highcharts-mock.js b/test/highcharts-mock.js new file mode 100644 index 0000000..61d6f83 --- /dev/null +++ b/test/highcharts-mock.js @@ -0,0 +1,38 @@ +'use strict'; + +window.Highcharts = (function () { + var ns = {}; + + ns.reset = function () { + ns.chart = jasmine.createSpyObj('chart', [ + 'redraw', + 'setTitle', + 'hideLoading', + 'destroy', + 'get', + 'addSeries', + 'update']); + ns.chart.series = []; + ns.usedChartConstructor = null; + ns.options = null; + }; + + ns.Chart = function (opt) { + ns.options = opt; + ns.usedChartConstructor = 'Chart'; + return ns.chart; + }; + ns.StockChart = function (opt) { + ns.options = opt; + ns.usedChartConstructor = 'StockChart'; + return this.chart; + }; + ns.Map = function (opt) { + ns.options = opt; + ns.usedChartConstructor = 'Map'; + return ns.chart; + }; + return ns; +}()); + + diff --git a/test/spec/highcharts-ng.js b/test/spec/highcharts-ng.js index 5a2adf6..bb62cc2 100644 --- a/test/spec/highcharts-ng.js +++ b/test/spec/highcharts-ng.js @@ -11,45 +11,18 @@ describe('Module: highchartsNg', function () { usedChartConstructor, chart; + // load the controller's module beforeEach(module('highcharts-ng')); beforeEach(inject(function ($injector, $rootScope, _$compile_, _$timeout_) { title = {}; destroyed = false; - usedChartConstructor = ''; - - chart = jasmine.createSpyObj('chart', [ - 'redraw', - 'setTitle', - 'hideLoading', - 'destroy', - 'get', - 'addSeries', - 'update']); - chart.series = []; - - window.Highcharts = { - Chart: function (opt) { - options = opt; - usedChartConstructor = 'Chart'; - - return chart; - }, - StockChart: function (opt) { - options = opt; - usedChartConstructor = 'StockChart'; + scope = $rootScope; - return chart; - }, - Map: function (opt) { - options = opt; - usedChartConstructor = 'Map'; + window.Highcharts.reset(); + chart = window.Highcharts.chart; - return chart; - } - }; - scope = $rootScope; $compile = _$compile_; $timeout = _$timeout_; @@ -102,7 +75,7 @@ describe('Module: highchartsNg', function () { it('passes options to highcharts', function () { compileDirective('simpleChartConfig'); - + options = window.Highcharts.options; expect(options.chart.type).toBe('bar'); expect(options.chart.series).toBe(templates.simpleChartConfig.series); }); @@ -113,6 +86,7 @@ describe('Module: highchartsNg', function () { }); it('uses highstocks', function () { + usedChartConstructor = window.Highcharts.usedChartConstructor; expect(usedChartConstructor).toBe('StockChart'); }); });