-
Notifications
You must be signed in to change notification settings - Fork 12k
Refactoring to put browser specific code in a new class #3718
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
Conversation
449a060 to
d837bce
Compare
|
I'm not sure if it's better to have a single |
d837bce to
7d8afe8
Compare
…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
7d8afe8 to
41f56fb
Compare
There was a problem hiding this 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!
src/core/core.platform.js
Outdated
|
|
||
| // Core.Platform abstracts away browser specific APIs from the chart | ||
| module.exports = function(Chart) { | ||
| var platform = Chart.corePlatform = {}; |
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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
src/core/core.platform.js
Outdated
| * @param config {ChartOptions} the chart options | ||
| */ | ||
| /** | ||
| * @method IPlatform#releaseCanvas |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
sounds good.
There was a problem hiding this comment.
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
There was a problem hiding this comment.
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).
src/platforms/platform.browser.js
Outdated
| * @param chartController {Core.Controller} the main chart controller | ||
| */ | ||
| function BrowserPlatform(chartController) { | ||
| this.chartController = chartController; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
this.controller ?
src/core/core.platform.js
Outdated
| * @name Core.platform.events | ||
| * @type Map<String, String> | ||
| */ | ||
| platform.events = { |
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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').
src/platforms/platform.browser.js
Outdated
| BrowserPlatform.prototype.createEvent = function(e) { | ||
| var chart = this.chartController.chart; | ||
| var relativePosition = helpers.getRelativePosition(e, chart); | ||
| var chartEvent = { |
There was a problem hiding this comment.
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 { ... })
test/core.controller.tests.js
Outdated
| }); | ||
| }); | ||
|
|
||
| describe('event handling', function() { |
There was a problem hiding this comment.
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
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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.
src/core/core.platform.js
Outdated
| */ | ||
| /** | ||
| * @method IPlatform#acquireContext | ||
| * @param item {CanvasRenderingContext2D|HTMLCanvasElement} the context or canvas to use |
There was a problem hiding this comment.
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.
| require('./core/core.tooltip')(Chart); | ||
|
|
||
| // By default, we only load the browser platform. | ||
| Chart.platform = require('./platforms/platform.dom')(Chart); |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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
There was a problem hiding this comment.
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).
There was a problem hiding this comment.
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
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
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.Platformis the constructor for the platform object that is attached to the chart instance.Plugins are notified about the event using the
onEventcall. The legend plugin was converted to use onEvent instead of the older privatehandleEventmethod.Wrote test to check that plugins are notified about events