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

Refactoring to put browser specific code in a new class #3718

Merged
merged 2 commits into from
Dec 21, 2016
Merged

Conversation

etimberg
Copy link
Member

First revision of the implementation of https://simonbrunel.gitbooks.io/chartjs-proposals/content/Platform.html#event-management

The BrowserPlatform class is attached to each chart to handle browser specific logic. This can be replaced on a different platform. BrowserPlatform implements IPlatform (the general interface that all platform implementations must support). Chart.Platform is the constructor for the platform object that is attached to the chart instance.

Plugins are notified about the event using the onEvent call. The legend plugin was converted to use onEvent instead of the older private handleEvent method.
Wrote test to check that plugins are notified about events

@etimberg etimberg added this to the Version 2.5 milestone Dec 19, 2016
@etimberg
Copy link
Member Author

I'm not sure if it's better to have a single onEvent for plugins or if it's better to have one for each internal event type (ie. onMouseMove, onClick, etc)

…orm.

BrowserPlatform implements IPlatform. Chart.Platform is the constructor for the platform object that is attached to the chart instance.

Plugins are notified about the event using the `onEvent` call. The legend plugin was converted to use onEvent instead of the older private `handleEvent` method.
Wrote test to check that plugins are notified about events
Copy link
Member

@simonbrunel simonbrunel left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks like I can drop my local work on the platform stuff! Your version looks good, the main difference is that Chart.Platform is a static class in my case, no inheritance, simply overrides (requiring platform.dom.js defines Chart.Platform.* implementation for browser). I don't really see advantages to have a platform instance for each charts, e.g. acquiring a context could be Chart.Platform.acquireContext(item, config). Anyway, great job!


// Core.Platform abstracts away browser specific APIs from the chart
module.exports = function(Chart) {
var platform = Chart.corePlatform = {};
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we simply call it Chart.platform?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, that was actually my first thought before I made it more OO like. I'll move to a static class in Chart.Platform

* @param config {ChartOptions} the chart options
*/
/**
* @method IPlatform#releaseCanvas
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should be releaseContext instead, canvas should be totally abstracted from the platform API.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

sounds good.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

should this also be changed to take a CanvasRenderingContext2D as the parameter? Right now it's the canvas dom node

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, releaseContext should take the context2d as parameter (the one that acquireContext returns).

* @param chartController {Core.Controller} the main chart controller
*/
function BrowserPlatform(chartController) {
this.chartController = chartController;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this.controller ?

* @name Core.platform.events
* @type Map<String, String>
*/
platform.events = {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do we really need that map, could simply directly use strings values?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we can directly just use string variables.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What I mean is I don't think we even need variables, just a doc comment with all Chart.js event names, then directly use strings into the code (e.g. if (e.type === 'mouseenter') { ... } and in the BrowserPlatform map: pointerenter: 'mouseenter').

BrowserPlatform.prototype.createEvent = function(e) {
var chart = this.chartController.chart;
var relativePosition = helpers.getRelativePosition(e, chart);
var chartEvent = {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can avoid the local variable (return { ... })

@@ -879,4 +879,59 @@ describe('Chart.Controller', function() {
expect(chart.tooltip._options).toEqual(jasmine.objectContaining(newTooltipConfig));
});
});

describe('event handling', function() {
Copy link
Member

@simonbrunel simonbrunel Dec 20, 2016

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

All platform related tests should be moved to test/platform.browser.tests.js

Edit: wrong file, should be platform.browser.tests.js

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

do you think the responsive tests should move here as well? or just the acquireContext, releaseContext and event tests?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not sure what should be moved exactly, I guess everything that touch the DOM and canvas element.

*/
/**
* @method IPlatform#acquireContext
* @param item {CanvasRenderingContext2D|HTMLCanvasElement} the context or canvas to use
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Item type should be Object since on other platforms it might be anything (e.g. in QML, it's an QML Item). Also missing the doc for the return type: @returns A context2d instance implementing the W3C Canvas 2D Context API standard

…m.dom and move associated tests into platform.dom.tests. Chart.platform is now a static class instead of a constructable object.
@@ -19,6 +19,9 @@ require('./core/core.legend')(Chart);
require('./core/core.interaction')(Chart);
require('./core/core.tooltip')(Chart);

// By default, we only load the browser platform.
Chart.platform = require('./platforms/platform.dom')(Chart);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should it be Chart.Platform or Chart.platform? I thought namespaces was all lowercase and classes with a capital.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I used Chart.platform since it's not a constructor

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Works for me, just thinking that it might have been a good idea to have some kind of naming rules for namespace and class names: I used to see namespaces in lowercase and class capitalized (whatever static or not).

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ok, I'm fine with that. I'll change this to be Platform since it's a class

@etimberg etimberg merged commit ecc35c5 into master Dec 21, 2016
@etimberg etimberg deleted the plugin-events branch December 21, 2016 15:22
exwm pushed a commit to exwm/Chart.js that referenced this pull request Apr 30, 2021
Refactoring to put browser specific code in a new class, BrowserPlatform.
BrowserPlatform implements IPlatform. Chart.Platform is the constructor for the platform object that is attached to the chart instance.

Plugins are notified about the event using the `onEvent` call. The legend plugin was converted to use onEvent instead of the older private `handleEvent` method.
Wrote test to check that plugins are notified about events
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants