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

Cleanup and reorganize core and canvas helpers #4419

Merged
merged 1 commit into from
Jun 24, 2017
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
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
/dist
/docs/index.md
/gh-pages
/jsdoc
/node_modules
.DS_Store
.idea
Expand Down
3 changes: 2 additions & 1 deletion src/chart.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,12 @@
*/
var Chart = require('./core/core.js')();

require('./helpers/helpers.core')(Chart);
require('./core/core.helpers')(Chart);
require('./helpers/helpers.time')(Chart);
require('./helpers/helpers.canvas')(Chart);

require('./platforms/platform.js')(Chart);
require('./core/core.canvasHelpers')(Chart);
require('./core/core.element')(Chart);
require('./core/core.plugin.js')(Chart);
require('./core/core.animation')(Chart);
Expand Down
4 changes: 2 additions & 2 deletions src/controllers/controller.line.js
Original file line number Diff line number Diff line change
Expand Up @@ -285,13 +285,13 @@ module.exports = function(Chart) {
var ilen = points.length;
var i = 0;

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

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

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

// Draw the points
for (; i<ilen; ++i) {
Expand Down
6 changes: 3 additions & 3 deletions src/core/core.controller.js
Original file line number Diff line number Diff line change
Expand Up @@ -151,7 +151,7 @@ module.exports = function(Chart) {
},

clear: function() {
helpers.clear(this);
helpers.canvas.clear(this);
return this;
},

Expand Down Expand Up @@ -511,7 +511,7 @@ module.exports = function(Chart) {

me.clear();

if (easingValue === undefined || easingValue === null) {
if (helpers.isNullOrUndef(easingValue)) {
easingValue = 1;
}

Expand Down Expand Up @@ -688,7 +688,7 @@ module.exports = function(Chart) {

if (canvas) {
me.unbindEvents();
helpers.clear(me);
helpers.canvas.clear(me);
platform.releaseContext(me.ctx);
me.canvas = null;
me.ctx = null;
Expand Down
121 changes: 3 additions & 118 deletions src/core/core.helpers.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,32 +5,9 @@
var color = require('chartjs-color');

module.exports = function(Chart) {
// Global Chart helpers object for utility methods and classes
var helpers = Chart.helpers = {};
var helpers = Chart.helpers;

// -- Basic js utility methods
helpers.each = function(loopable, callback, self, reverse) {
// Check to see if null or undefined firstly.
var i, len;
if (helpers.isArray(loopable)) {
len = loopable.length;
if (reverse) {
for (i = len - 1; i >= 0; i--) {
callback.call(self, loopable[i], i);
}
} else {
for (i = 0; i < len; i++) {
callback.call(self, loopable[i], i);
}
}
} else if (typeof loopable === 'object') {
var keys = Object.keys(loopable);
len = keys.length;
for (i = 0; i < len; i++) {
callback.call(self, loopable[keys[i]], keys[i]);
}
}
};
helpers.clone = function(obj) {
var objClone = {};
helpers.each(obj, function(value, key) {
Expand Down Expand Up @@ -123,32 +100,7 @@ module.exports = function(Chart) {

return base;
};
helpers.getValueAtIndexOrDefault = function(value, index, defaultValue) {
if (value === undefined || value === null) {
return defaultValue;
}

if (helpers.isArray(value)) {
return index < value.length ? value[index] : defaultValue;
}

return value;
};
helpers.getValueOrDefault = function(value, defaultValue) {
return value === undefined ? defaultValue : value;
};
helpers.indexOf = Array.prototype.indexOf?
function(array, item) {
return array.indexOf(item);
}:
function(array, item) {
for (var i = 0, ilen = array.length; i < ilen; ++i) {
if (array[i] === item) {
return i;
}
}
return -1;
};
helpers.where = function(collection, filterCallback) {
if (helpers.isArray(collection) && Array.prototype.filter) {
return collection.filter(filterCallback);
Expand Down Expand Up @@ -178,7 +130,7 @@ module.exports = function(Chart) {
};
helpers.findNextWhere = function(arrayToSearch, filterCallback, startIndex) {
// Default to start of the array
if (startIndex === undefined || startIndex === null) {
if (helpers.isNullOrUndef(startIndex)) {
startIndex = -1;
}
for (var i = startIndex + 1; i < arrayToSearch.length; i++) {
Expand All @@ -190,7 +142,7 @@ module.exports = function(Chart) {
};
helpers.findPreviousWhere = function(arrayToSearch, filterCallback, startIndex) {
// Default to end of the array
if (startIndex === undefined || startIndex === null) {
if (helpers.isNullOrUndef(startIndex)) {
startIndex = arrayToSearch.length;
}
for (var i = startIndex - 1; i >= 0; i--) {
Expand Down Expand Up @@ -223,13 +175,6 @@ module.exports = function(Chart) {

return ChartElement;
};
helpers.noop = function() {};
helpers.uid = (function() {
var id = 0;
return function() {
return id++;
};
}());
// -- Math methods
helpers.isNumber = function(n) {
return !isNaN(parseFloat(n)) && isFinite(n);
Expand Down Expand Up @@ -837,9 +782,6 @@ module.exports = function(Chart) {
canvas.style.width = width + 'px';
};
// -- Canvas methods
helpers.clear = function(chart) {
chart.ctx.clearRect(0, 0, chart.width, chart.height);
};
helpers.fontString = function(pixelSize, fontStyle, fontFamily) {
return fontStyle + ' ' + pixelSize + 'px ' + fontFamily;
};
Expand Down Expand Up @@ -903,19 +845,6 @@ module.exports = function(Chart) {
});
return numberOfLines;
};
helpers.drawRoundedRectangle = function(ctx, x, y, width, height, radius) {
ctx.beginPath();
ctx.moveTo(x + radius, y);
ctx.lineTo(x + width - radius, y);
ctx.quadraticCurveTo(x + width, y, x + width, y + radius);
ctx.lineTo(x + width, y + height - radius);
ctx.quadraticCurveTo(x + width, y + height, x + width - radius, y + height);
ctx.lineTo(x + radius, y + height);
ctx.quadraticCurveTo(x, y + height, x, y + height - radius);
ctx.lineTo(x, y + radius);
ctx.quadraticCurveTo(x, y, x + radius, y);
ctx.closePath();
};

helpers.color = !color?
function(value) {
Expand All @@ -931,54 +860,10 @@ module.exports = function(Chart) {
return color(value);
};

helpers.isArray = Array.isArray?
function(obj) {
return Array.isArray(obj);
} :
function(obj) {
return Object.prototype.toString.call(obj) === '[object Array]';
};
// ! @see http://stackoverflow.com/a/14853974
helpers.arrayEquals = function(a0, a1) {
var i, ilen, v0, v1;

if (!a0 || !a1 || a0.length !== a1.length) {
return false;
}

for (i = 0, ilen=a0.length; i < ilen; ++i) {
v0 = a0[i];
v1 = a1[i];

if (v0 instanceof Array && v1 instanceof Array) {
if (!helpers.arrayEquals(v0, v1)) {
return false;
}
} else if (v0 !== v1) {
// NOTE: two different object instances will never be equal: {x:20} != {x:20}
return false;
}
}

return true;
};
helpers.callback = function(fn, args, thisArg) {
if (fn && typeof fn.call === 'function') {
return fn.apply(thisArg, args);
}
};
helpers.getHoverColor = function(colorValue) {
/* global CanvasPattern */
return (colorValue instanceof CanvasPattern) ?
colorValue :
helpers.color(colorValue).saturate(0.5).darken(0.1).rgbString();
};

/**
* Provided for backward compatibility, use Chart.helpers#callback instead.
* @function Chart.helpers#callCallback
* @deprecated since version 2.6.0
* @todo remove at version 3
*/
helpers.callCallback = helpers.callback;
};
6 changes: 3 additions & 3 deletions src/core/core.scale.js
Original file line number Diff line number Diff line change
Expand Up @@ -422,7 +422,7 @@ module.exports = function(Chart) {
// Get the correct value. NaN bad inputs, If the value type is object get the x or y based on whether we are horizontal or not
getRightValue: function(rawValue) {
// Null and undefined values first
if (rawValue === null || typeof(rawValue) === 'undefined') {
if (helpers.isNullOrUndef(rawValue)) {
return NaN;
}
// isNaN(object) returns true, so make sure NaN is checking for a number; Discard Infinite values
Expand Down Expand Up @@ -575,15 +575,15 @@ module.exports = function(Chart) {
helpers.each(me.ticks, function(tick, index) {
var label = (tick && tick.value) || tick;
// If the callback returned a null or undefined value, do not draw this line
if (label === undefined || label === null) {
if (helpers.isNullOrUndef(label)) {
return;
}

var isLastTick = me.ticks.length === index + 1;

// Since we always show the last tick,we need may need to hide the last shown one before
var shouldSkip = (skipRatio > 1 && index % skipRatio > 0) || (index % skipRatio === 0 && index + skipRatio >= me.ticks.length);
if (shouldSkip && !isLastTick || (label === undefined || label === null)) {
if (shouldSkip && !isLastTick || helpers.isNullOrUndef(label)) {
return;
}

Expand Down
2 changes: 1 addition & 1 deletion src/elements/element.point.js
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ module.exports = function(Chart) {
ctx.fillStyle = color(ctx.fillStyle).alpha(ratio).rgbString();
}

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