Skip to content

Commit

Permalink
Make Chart.helpers importable (#4479)
Browse files Browse the repository at this point in the history
Properly export helpers and remove dependencies to `Chart.helpers`. Helpers can now be accessed from `src/helpers/index.js` (`var helpers = require('path/to/helpers/index')`, instead of `var helpers = Chart.helpers`).
  • Loading branch information
simonbrunel committed Jul 15, 2017
1 parent b03ab1c commit 717e8d9
Show file tree
Hide file tree
Showing 37 changed files with 638 additions and 623 deletions.
18 changes: 14 additions & 4 deletions src/chart.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,10 @@
*/
var Chart = require('./core/core')();

require('./helpers/helpers.core')(Chart);
require('./helpers/helpers.easing')(Chart);
Chart.helpers = require('./helpers/index');

// @todo dispatch these helpers into appropriated helpers/helpers.* file and write unit tests!
require('./core/core.helpers')(Chart);
require('./helpers/helpers.time')(Chart);
require('./helpers/helpers.canvas')(Chart);

require('./platforms/platform')(Chart);
require('./core/core.element')(Chart);
Expand Down Expand Up @@ -66,3 +65,14 @@ module.exports = Chart;
if (typeof window !== 'undefined') {
window.Chart = Chart;
}

// DEPRECATIONS

/**
* Provided for backward compatibility, use Chart.helpers.canvas instead.
* @namespace Chart.canvasHelpers
* @deprecated since version 2.6.0
* @todo remove at version 3
* @private
*/
Chart.canvasHelpers = Chart.helpers.canvas;
4 changes: 2 additions & 2 deletions src/controllers/controller.bar.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
'use strict';

module.exports = function(Chart) {
var helpers = require('../helpers/index');

var helpers = Chart.helpers;
module.exports = function(Chart) {

Chart.defaults.bar = {
hover: {
Expand Down
4 changes: 2 additions & 2 deletions src/controllers/controller.bubble.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
'use strict';

module.exports = function(Chart) {
var helpers = require('../helpers/index');

var helpers = Chart.helpers;
module.exports = function(Chart) {

Chart.defaults.bubble = {
hover: {
Expand Down
5 changes: 3 additions & 2 deletions src/controllers/controller.doughnut.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
'use strict';

var helpers = require('../helpers/index');

module.exports = function(Chart) {

var helpers = Chart.helpers,
defaults = Chart.defaults;
var defaults = Chart.defaults;

defaults.doughnut = {
animation: {
Expand Down
8 changes: 4 additions & 4 deletions src/controllers/controller.line.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
'use strict';

module.exports = function(Chart) {
var helpers = require('../helpers/index');

var helpers = Chart.helpers;
module.exports = function(Chart) {

Chart.defaults.line = {
showLines: true,
Expand Down Expand Up @@ -285,13 +285,13 @@ module.exports = function(Chart) {
var ilen = points.length;
var i = 0;

Chart.helpers.canvas.clipArea(chart.ctx, area);
helpers.canvas.clipArea(chart.ctx, area);

if (lineEnabled(me.getDataset(), chart.options)) {
meta.dataset.draw();
}

Chart.helpers.canvas.unclipArea(chart.ctx);
helpers.canvas.unclipArea(chart.ctx);

// Draw the points
for (; i<ilen; ++i) {
Expand Down
4 changes: 2 additions & 2 deletions src/controllers/controller.polarArea.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
'use strict';

module.exports = function(Chart) {
var helpers = require('../helpers/index');

var helpers = Chart.helpers;
module.exports = function(Chart) {

Chart.defaults.polarArea = {

Expand Down
4 changes: 2 additions & 2 deletions src/controllers/controller.radar.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
'use strict';

module.exports = function(Chart) {
var helpers = require('../helpers/index');

var helpers = Chart.helpers;
module.exports = function(Chart) {

Chart.defaults.radar = {
scale: {
Expand Down
4 changes: 2 additions & 2 deletions src/core/core.animation.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
/* global window: false */
'use strict';

module.exports = function(Chart) {
var helpers = require('../helpers/index');

var helpers = Chart.helpers;
module.exports = function(Chart) {

Chart.defaults.global.animation = {
duration: 1000,
Expand Down
4 changes: 2 additions & 2 deletions src/core/core.controller.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
'use strict';

module.exports = function(Chart) {
var helpers = require('../helpers/index');

var helpers = Chart.helpers;
module.exports = function(Chart) {
var plugins = Chart.plugins;
var platform = Chart.platform;

Expand Down
4 changes: 2 additions & 2 deletions src/core/core.datasetController.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
'use strict';

module.exports = function(Chart) {
var helpers = require('../helpers/index');

var helpers = Chart.helpers;
module.exports = function(Chart) {

var arrayEvents = ['push', 'pop', 'shift', 'splice', 'unshift'];

Expand Down
3 changes: 1 addition & 2 deletions src/core/core.element.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
'use strict';

var color = require('chartjs-color');
var helpers = require('../helpers/index');

module.exports = function(Chart) {

var helpers = Chart.helpers;

function interpolate(start, view, model, ease) {
var keys = Object.keys(model);
var i, ilen, key, actual, origin, target, type, c0, c1;
Expand Down
2 changes: 1 addition & 1 deletion src/core/core.helpers.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@
'use strict';

var color = require('chartjs-color');
var helpers = require('../helpers/index');

module.exports = function(Chart) {
var helpers = Chart.helpers;

// -- Basic js utility methods

Expand Down
3 changes: 2 additions & 1 deletion src/core/core.interaction.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
'use strict';

var helpers = require('../helpers/index');

module.exports = function(Chart) {
var helpers = Chart.helpers;

/**
* Helper function to get relative position for an event
Expand Down
4 changes: 2 additions & 2 deletions src/core/core.layoutService.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
'use strict';

module.exports = function(Chart) {
var helpers = require('../helpers/index');

var helpers = Chart.helpers;
module.exports = function(Chart) {

function filterByPosition(array, position) {
return helpers.where(array, function(v) {
Expand Down
4 changes: 2 additions & 2 deletions src/core/core.plugin.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
'use strict';

module.exports = function(Chart) {
var helpers = require('../helpers/index');

var helpers = Chart.helpers;
module.exports = function(Chart) {

Chart.defaults.global.plugins = {};

Expand Down
4 changes: 2 additions & 2 deletions src/core/core.scale.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
'use strict';

module.exports = function(Chart) {
var helpers = require('../helpers/index');

var helpers = Chart.helpers;
module.exports = function(Chart) {

Chart.defaults.scale = {
display: true,
Expand Down
4 changes: 2 additions & 2 deletions src/core/core.scaleService.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
'use strict';

module.exports = function(Chart) {
var helpers = require('../helpers/index');

var helpers = Chart.helpers;
module.exports = function(Chart) {

Chart.scaleService = {
// Scale registration object. Extensions can register new scale types (such as log or DB scales) and then
Expand Down
4 changes: 2 additions & 2 deletions src/core/core.ticks.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
'use strict';

module.exports = function(Chart) {
var helpers = require('../helpers/index');

var helpers = Chart.helpers;
module.exports = function(Chart) {

/**
* Namespace to hold static tick generation functions
Expand Down
4 changes: 2 additions & 2 deletions src/core/core.tooltip.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
'use strict';

module.exports = function(Chart) {
var helpers = require('../helpers/index');

var helpers = Chart.helpers;
module.exports = function(Chart) {

/**
* Helper method to merge the opacity into a color
Expand Down
5 changes: 3 additions & 2 deletions src/elements/element.arc.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
'use strict';

var helpers = require('../helpers/index');

module.exports = function(Chart) {

var helpers = Chart.helpers,
globalOpts = Chart.defaults.global;
var globalOpts = Chart.defaults.global;

globalOpts.elements.arc = {
backgroundColor: globalOpts.defaultColor,
Expand Down
3 changes: 2 additions & 1 deletion src/elements/element.line.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
'use strict';

var helpers = require('../helpers/index');

module.exports = function(Chart) {

var helpers = Chart.helpers;
var globalDefaults = Chart.defaults.global;

Chart.defaults.global.elements.line = {
Expand Down
9 changes: 5 additions & 4 deletions src/elements/element.point.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
'use strict';

var helpers = require('../helpers/index');

module.exports = function(Chart) {

var helpers = Chart.helpers,
globalOpts = Chart.defaults.global,
var globalOpts = Chart.defaults.global,
defaultColor = globalOpts.defaultColor;

globalOpts.elements.point = {
Expand Down Expand Up @@ -64,7 +65,7 @@ module.exports = function(Chart) {
var radius = vm.radius;
var x = vm.x;
var y = vm.y;
var color = Chart.helpers.color;
var color = helpers.color;
var errMargin = 1.01; // 1.01 is margin for Accumulated error. (Especially Edge, IE.)
var ratio = 0;

Expand Down Expand Up @@ -94,7 +95,7 @@ module.exports = function(Chart) {
ctx.fillStyle = color(ctx.fillStyle).alpha(ratio).rgbString();
}

Chart.helpers.canvas.drawPoint(ctx, pointStyle, radius, x, y);
helpers.canvas.drawPoint(ctx, pointStyle, radius, x, y);
}
});
};
Loading

0 comments on commit 717e8d9

Please sign in to comment.