Skip to content

Commit 61cf047

Browse files
authored
feat(options): Pass instance arg to callbacks
Pass instance argument for callback options to facilitate the usage. Fix #989
1 parent f9cf2d1 commit 61cf047

File tree

5 files changed

+104
-37
lines changed

5 files changed

+104
-37
lines changed

spec/internals/bb-spec.js

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -279,4 +279,62 @@ describe("Interface & initialization", () => {
279279
});
280280
});
281281
});
282+
283+
describe("check for callbacks if instance param is passed", () => {
284+
let chart;
285+
const spy = sinon.spy();
286+
287+
before(() => {
288+
const args = {
289+
data: {
290+
columns: [
291+
["data1", 300, 350, 300]
292+
]
293+
}
294+
};
295+
296+
["beforeinit", "init", "rendered", "afterinit", "resize", "resized", "over", "out"]
297+
.forEach(v => {
298+
args[`on${v}`] = ctx => spy(v, ctx);
299+
});
300+
301+
chart = util.generate(args);
302+
});
303+
304+
beforeEach(() => spy.resetHistory());
305+
306+
it("check for the init callbacks", () => {
307+
const expected = ["beforeinit", "init", "rendered", "afterinit"];
308+
309+
spy.args.forEach((v, i) => {
310+
expect(v[0]).to.be.equal(expected[i]);
311+
expect(v[1]).to.be.equal(chart);
312+
});
313+
});
314+
315+
it("check for the resize callbacks", () => {
316+
const expected = ["resize", "resized"];
317+
318+
// when
319+
chart.internal.resizeFunction();
320+
321+
spy.args.forEach((v, i) => {
322+
expect(v[0]).to.be.equal(expected[i]);
323+
expect(v[1]).to.be.equal(chart);
324+
});
325+
});
326+
327+
it("check for the onover/out callbacks", () => {
328+
const expected = ["over", "out"];
329+
330+
// when
331+
chart.$.svg.on("mouseenter")();
332+
chart.$.svg.on("mouseleave")();
333+
334+
spy.args.forEach((v, i) => {
335+
expect(v[0]).to.be.equal(expected[i]);
336+
expect(v[1]).to.be.equal(chart);
337+
});
338+
});
339+
});
282340
});

spec/plugin/stanford/stanford-spec.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import {compareEpochs, getCentroid, getRegionArea, pointInRegion} from "../../..
66
describe("STANFORD", () => {
77
let chart;
88
let stanford = new Stanford({ epochs: [30, 35] });
9-
const args = {
9+
let args = {
1010
data: {
1111
x: "x",
1212
columns: [

src/config/Options.js

Lines changed: 28 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -222,61 +222,66 @@ export default class Options {
222222
* @name onover
223223
* @memberof Options
224224
* @type {Function}
225-
* @default function(){}
225+
* @default undefined
226226
* @example
227-
* onover: function() {
227+
* // @param {Chart} ctx - Instance itself
228+
* onover: function(ctx) {
228229
* ...
229230
* }
230231
*/
231-
onover: () => {},
232+
onover: undefined,
232233

233234
/**
234235
* Set a callback to execute when mouse/touch leaves the chart.
235236
* @name onout
236237
* @memberof Options
237238
* @type {Function}
238-
* @default function(){}
239+
* @default undefined
239240
* @example
240-
* onout: function() {
241+
* // @param {Chart} ctx - Instance itself
242+
* onout: function(ctx) {
241243
* ...
242244
* }
243245
*/
244-
onout: () => {},
246+
onout: undefined,
245247

246248
/**
247249
* Set a callback to execute when user resizes the screen.
248250
* @name onresize
249251
* @memberof Options
250252
* @type {Function}
251-
* @default function(){}
253+
* @default undefined
252254
* @example
253-
* onresize: function() {
255+
* // @param {Chart} ctx - Instance itself
256+
* onresize: function(ctx) {
254257
* ...
255258
* }
256259
*/
257-
onresize: () => {},
260+
onresize: undefined,
258261

259262
/**
260263
* SSet a callback to execute when screen resize finished.
261264
* @name onresized
262265
* @memberof Options
263266
* @type {Function}
264-
* @default function(){}
267+
* @default undefined
265268
* @example
266-
* onresized: function() {
269+
* // @param {Chart} ctx - Instance itself
270+
* onresized: function(ctx) {
267271
* ...
268272
* }
269273
*/
270-
onresized: () => {},
274+
onresized: undefined,
271275

272276
/**
273277
* Set a callback to execute before the chart is initialized
274278
* @name onbeforeinit
275279
* @memberof Options
276280
* @type {Function}
277-
* @default function(){}
281+
* @default undefined
278282
* @example
279-
* onbeforeinit: function() {
283+
* // @param {Chart} ctx - Instance itself
284+
* onbeforeinit: function(ctx) {
280285
* ...
281286
* }
282287
*/
@@ -287,22 +292,24 @@ export default class Options {
287292
* @name oninit
288293
* @memberof Options
289294
* @type {Function}
290-
* @default function(){}
295+
* @default undefined
291296
* @example
292-
* oninit: function() {
297+
* // @param {Chart} ctx - Instance itself
298+
* oninit: function(ctx) {
293299
* ...
294300
* }
295301
*/
296-
oninit: () => {},
302+
oninit: undefined,
297303

298304
/**
299305
* Set a callback to execute after the chart is initialized
300306
* @name onafterinit
301307
* @memberof Options
302308
* @type {Function}
303-
* @default function(){}
309+
* @default undefined
304310
* @example
305-
* onafterinit: function() {
311+
* // @param {Chart} ctx - Instance itself
312+
* onafterinit: function(ctx) {
306313
* ...
307314
* }
308315
*/
@@ -315,7 +322,8 @@ export default class Options {
315322
* @type {Function}
316323
* @default undefined
317324
* @example
318-
* onrendered: function() {
325+
* // @param {Chart} ctx - Instance itself
326+
* onrendered: function(ctx) {
319327
* ...
320328
* }
321329
*/

src/internals/ChartInternal.js

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ export default class ChartInternal {
4343
$$.callPluginHook("$beforeInit");
4444

4545
// can do something
46-
callFn($$.config.onbeforeinit, $$);
46+
callFn($$.config.onbeforeinit, $$, $$.api);
4747
}
4848

4949
afterInit() {
@@ -52,7 +52,7 @@ export default class ChartInternal {
5252
$$.callPluginHook("$afterInit");
5353

5454
// can do something
55-
callFn($$.config.onafterinit, $$);
55+
callFn($$.config.onafterinit, $$, $$.api);
5656
}
5757

5858
init() {
@@ -229,8 +229,8 @@ export default class ChartInternal {
229229
const isTouch = $$.inputType === "touch";
230230

231231
$$.svg
232-
.on(isTouch ? "touchstart" : "mouseenter", () => callFn(config.onover, $$))
233-
.on(isTouch ? "touchend" : "mouseleave", () => callFn(config.onout, $$));
232+
.on(isTouch ? "touchstart" : "mouseenter", () => callFn(config.onover, $$, $$.api))
233+
.on(isTouch ? "touchend" : "mouseleave", () => callFn(config.onout, $$, $$.api));
234234
}
235235

236236
config.svg_classname && $$.svg.attr("class", config.svg_classname);
@@ -312,7 +312,7 @@ export default class ChartInternal {
312312
$$.updateDimension();
313313

314314
// oninit callback
315-
config.oninit.call($$);
315+
callFn(config.oninit, $$, $$.api);
316316

317317
$$.redraw({
318318
withTransition: false,
@@ -782,7 +782,7 @@ export default class ChartInternal {
782782
// callback function after redraw ends
783783
const afterRedraw = flow || config.onrendered ? () => {
784784
flowFn && flowFn();
785-
callFn(config.onrendered, $$);
785+
callFn(config.onrendered, $$, $$.api);
786786
} : null;
787787

788788
if (afterRedraw) {
@@ -1175,7 +1175,7 @@ export default class ChartInternal {
11751175
const config = $$.config;
11761176

11771177
$$.resizeFunction = $$.generateResize();
1178-
$$.resizeFunction.add(config.onresize.bind($$));
1178+
$$.resizeFunction.add(() => callFn(config.onresize, $$, $$.api));
11791179

11801180
if (config.resize_auto) {
11811181
$$.resizeFunction.add(() => {
@@ -1190,7 +1190,7 @@ export default class ChartInternal {
11901190
});
11911191
}
11921192

1193-
$$.resizeFunction.add(config.onresized.bind($$));
1193+
$$.resizeFunction.add(() => callFn(config.onresized, $$, $$.api));
11941194

11951195
// attach resize event
11961196
window.addEventListener("resize", $$.resizeFunction);

types/options.d.ts

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
import { Axis } from "./axis";
66
import { ChartTypes, d3Selection, DataItem, PrimitiveArray } from "./types";
77
import Stanford from "./plugin/stanford/index";
8+
import { Chart } from "./chart";
89

910
export interface ChartOptions {
1011
/**
@@ -513,42 +514,42 @@ export interface ChartOptions {
513514
/**
514515
* Set a callback to execute when the chart is initialized.
515516
*/
516-
oninit?(): void;
517+
oninit?(ctx: Chart): void;
517518

518519
/**
519520
* Set a callback to execute after the chart is initialized
520521
*/
521-
onafterinit?(): void;
522+
onafterinit?(ctx: Chart): void;
522523

523524
/**
524525
* Set a callback to execute before the chart is initialized
525526
*/
526-
onbeforeinit?(): void;
527+
onbeforeinit?(ctx: Chart): void;
527528

528529
/**
529530
* Set a callback which is executed when the chart is rendered. Basically, this callback will be called in each time when the chart is redrawed.
530531
*/
531-
onrendered?(): void;
532+
onrendered?(ctx: Chart): void;
532533

533534
/**
534535
* Set a callback to execute when mouse/touch enters the chart.
535536
*/
536-
onover?(): void;
537+
onover?(ctx: Chart): void;
537538

538539
/**
539540
* Set a callback to execute when mouse/touch leaves the chart.
540541
*/
541-
onout?(): void;
542+
onout?(ctx: Chart): void;
542543

543544
/**
544545
* Set a callback to execute when user resizes the screen.
545546
*/
546-
onresize?(): void;
547+
onresize?(ctx: Chart): void;
547548

548549
/**
549550
* Set a callback to execute when screen resize finished.
550551
*/
551-
onresized?(): void;
552+
onresized?(ctx: Chart): void;
552553

553554
/**
554555
* Set 'clip-path' attribute for chart element.

0 commit comments

Comments
 (0)