Use ES6 classes for elements (#6702)

* Use ES6 classes for elements

* Add additional elements
This commit is contained in:
Ben McCann
2019-11-07 16:51:26 -08:00
committed by Evert Timberg
parent 94afa63450
commit 4a8a7ee824
10 changed files with 349 additions and 310 deletions

View File

@@ -1,13 +1,12 @@
'use strict';
var defaults = require('../core/core.defaults');
var Element = require('../core/core.element');
var helpers = require('../helpers/index');
var layouts = require('../core/core.layouts');
const defaults = require('../core/core.defaults');
const Element = require('../core/core.element');
const helpers = require('../helpers/index');
const layouts = require('../core/core.layouts');
var getRtlHelper = helpers.rtl.getRtlAdapter;
var noop = helpers.noop;
var valueOrDefault = helpers.valueOrDefault;
const getRtlHelper = helpers.rtl.getRtlAdapter;
const valueOrDefault = helpers.valueOrDefault;
defaults._set('global', {
legend: {
@@ -112,9 +111,9 @@ function getBoxWidth(labelOpts, fontSize) {
/**
* IMPORTANT: this class is exposed publicly as Chart.Legend, backward compatibility required!
*/
var Legend = Element.extend({
class Legend extends Element {
initialize: function(config) {
initialize(config) {
var me = this;
helpers.extend(me, config);
@@ -128,14 +127,15 @@ var Legend = Element.extend({
// Are we in doughnut mode which has a different data type
me.doughnutMode = false;
},
}
// These methods are ordered by lifecycle. Utilities then follow.
// Any function defined here is inherited by all legend types.
// Any function can be extended by the legend type
beforeUpdate: noop,
update: function(maxWidth, maxHeight, margins) {
beforeUpdate() {}
update(maxWidth, maxHeight, margins) {
var me = this;
// Update Lifecycle - Probably don't want to ever extend or overwrite this function ;)
@@ -163,13 +163,15 @@ var Legend = Element.extend({
me.afterUpdate();
return me.minSize;
},
afterUpdate: noop,
}
afterUpdate() {}
//
beforeSetDimensions: noop,
setDimensions: function() {
beforeSetDimensions() {}
setDimensions() {
var me = this;
// Set the unconstrained dimension before label rotation
if (me.isHorizontal()) {
@@ -196,13 +198,15 @@ var Legend = Element.extend({
width: 0,
height: 0
};
},
afterSetDimensions: noop,
}
afterSetDimensions() {}
//
beforeBuildLabels: noop,
buildLabels: function() {
beforeBuildLabels() {}
buildLabels() {
var me = this;
var labelOpts = me.options.labels || {};
var legendItems = helpers.callback(labelOpts.generateLabels, [me.chart], me) || [];
@@ -218,13 +222,15 @@ var Legend = Element.extend({
}
me.legendItems = legendItems;
},
afterBuildLabels: noop,
}
afterBuildLabels() {}
//
beforeFit: noop,
fit: function() {
beforeFit() {}
fit() {
var me = this;
var opts = me.options;
var labelOpts = opts.labels;
@@ -330,16 +336,17 @@ var Legend = Element.extend({
me.width = minSize.width;
me.height = minSize.height;
},
afterFit: noop,
}
afterFit() {}
// Shared Methods
isHorizontal: function() {
isHorizontal() {
return this.options.position === 'top' || this.options.position === 'bottom';
},
}
// Actually draw the legend on the canvas
draw: function() {
draw() {
var me = this;
var opts = me.options;
var labelOpts = opts.labels;
@@ -503,12 +510,12 @@ var Legend = Element.extend({
});
helpers.rtl.restoreTextDirection(me.ctx, opts.textDirection);
},
}
/**
* @private
*/
_getLegendItemAt: function(x, y) {
_getLegendItemAt(x, y) {
var me = this;
var i, hitBox, lh;
@@ -526,14 +533,14 @@ var Legend = Element.extend({
}
return null;
},
}
/**
* Handle an event
* @private
* @param {IEvent} event - The event to handle
*/
handleEvent: function(e) {
handleEvent(e) {
var me = this;
var opts = me.options;
var type = e.type === 'mouseup' ? 'click' : e.type;
@@ -573,7 +580,7 @@ var Legend = Element.extend({
}
}
}
});
}
function createNewLegendAndAttach(chart, legendOpts) {
var legend = new Legend({

View File

@@ -1,11 +1,9 @@
'use strict';
var defaults = require('../core/core.defaults');
var Element = require('../core/core.element');
var helpers = require('../helpers/index');
var layouts = require('../core/core.layouts');
var noop = helpers.noop;
const defaults = require('../core/core.defaults');
const Element = require('../core/core.element');
const helpers = require('../helpers/index');
const layouts = require('../core/core.layouts');
defaults._set('global', {
title: {
@@ -22,19 +20,19 @@ defaults._set('global', {
/**
* IMPORTANT: this class is exposed publicly as Chart.Legend, backward compatibility required!
*/
var Title = Element.extend({
initialize: function(config) {
class Title extends Element {
initialize(config) {
var me = this;
helpers.extend(me, config);
// Contains hit boxes for each dataset (in dataset order)
me.legendHitBoxes = [];
},
}
// These methods are ordered by lifecycle. Utilities then follow.
beforeUpdate: noop,
update: function(maxWidth, maxHeight, margins) {
beforeUpdate() {}
update(maxWidth, maxHeight, margins) {
var me = this;
// Update Lifecycle - Probably don't want to ever extend or overwrite this function ;)
@@ -63,13 +61,13 @@ var Title = Element.extend({
return me.minSize;
},
afterUpdate: noop,
}
afterUpdate() {}
//
beforeSetDimensions: noop,
setDimensions: function() {
beforeSetDimensions() {}
setDimensions() {
var me = this;
// Set the unconstrained dimension before label rotation
if (me.isHorizontal()) {
@@ -96,19 +94,19 @@ var Title = Element.extend({
width: 0,
height: 0
};
},
afterSetDimensions: noop,
}
afterSetDimensions() {}
//
beforeBuildLabels: noop,
buildLabels: noop,
afterBuildLabels: noop,
beforeBuildLabels() {}
buildLabels() {}
afterBuildLabels() {}
//
beforeFit: noop,
fit: function() {
beforeFit() {}
fit() {
var me = this;
var opts = me.options;
var minSize = me.minSize = {};
@@ -125,17 +123,17 @@ var Title = Element.extend({
me.width = minSize.width = isHorizontal ? me.maxWidth : textSize;
me.height = minSize.height = isHorizontal ? textSize : me.maxHeight;
},
afterFit: noop,
}
afterFit() {}
// Shared Methods
isHorizontal: function() {
isHorizontal() {
var pos = this.options.position;
return pos === 'top' || pos === 'bottom';
},
}
// Actually draw the title block on the canvas
draw: function() {
draw() {
var me = this;
var ctx = me.ctx;
var opts = me.options;
@@ -188,7 +186,7 @@ var Title = Element.extend({
ctx.restore();
}
});
}
function createNewTitleBlockAndAttach(chart, titleOpts) {
var title = new Title({