From 20d89f55745108c1c47ab69a36053b976407b224 Mon Sep 17 00:00:00 2001 From: dangreen Date: Fri, 29 Jul 2022 14:42:42 +0400 Subject: [PATCH 1/2] feat: pass some chart options to DateAdapter --- src/core/core.adapters.js | 3 ++- src/scales/scale.time.js | 5 ++++- test/specs/scale.time.tests.js | 33 +++++++++++++++++++++++++++++++++ types/adapters.d.ts | 3 +++ 4 files changed, 42 insertions(+), 2 deletions(-) diff --git a/src/core/core.adapters.js b/src/core/core.adapters.js index 254091602fc..7fae18998f4 100644 --- a/src/core/core.adapters.js +++ b/src/core/core.adapters.js @@ -26,8 +26,9 @@ function abstract() { export class DateAdapter { - constructor(options) { + constructor(options, chartOptions) { this.options = options || {}; + this.chartOptions = chartOptions || {}; } /** diff --git a/src/scales/scale.time.js b/src/scales/scale.time.js index 6b8078a7514..23e883a02de 100644 --- a/src/scales/scale.time.js +++ b/src/scales/scale.time.js @@ -221,7 +221,10 @@ export default class TimeScale extends Scale { init(scaleOpts, opts) { const time = scaleOpts.time || (scaleOpts.time = {}); - const adapter = this._adapter = new adapters._date(scaleOpts.adapters.date); + const adapterChartOpts = { + locale: opts.locale + }; + const adapter = this._adapter = new adapters._date(scaleOpts.adapters.date, adapterChartOpts); // Backward compatibility: before introducing adapter, `displayFormats` was // supposed to contain *all* unit/string pairs but this can't be resolved diff --git a/test/specs/scale.time.tests.js b/test/specs/scale.time.tests.js index 4daa7d750db..1acddb6b4b8 100644 --- a/test/specs/scale.time.tests.js +++ b/test/specs/scale.time.tests.js @@ -1235,4 +1235,37 @@ describe('Time scale tests', function() { }); }); }); + + it('should pass default locale to date adapter', function() { + var chart = window.acquireChart({ + type: 'line', + data: {}, + options: { + scales: { + x: { + type: 'time' + }, + } + } + }); + + expect(chart.scales.x._adapter.chartOptions.locale).toBe('en-US'); + }); + + it('should pass locale to date adapter from options', function() { + var chart = window.acquireChart({ + type: 'line', + data: {}, + options: { + locale: 'es', + scales: { + x: { + type: 'time' + }, + } + } + }); + + expect(chart.scales.x._adapter.chartOptions.locale).toBe('es'); + }); }); diff --git a/types/adapters.d.ts b/types/adapters.d.ts index f06c41b6851..a6ffdc48e4d 100644 --- a/types/adapters.d.ts +++ b/types/adapters.d.ts @@ -4,6 +4,9 @@ export interface DateAdapter { // Override one or multiple of the methods to adjust to the logic of the current date library. override(members: Partial): void; readonly options: unknown; + readonly chartOptions: { + locale?: string; + }; /** * Returns a map of time formats for the supported formatting units defined From 9352dc91d94e5d38322bc1b719b089e2b5b9ce4f Mon Sep 17 00:00:00 2001 From: dangreen Date: Fri, 29 Jul 2022 20:32:58 +0400 Subject: [PATCH 2/2] feat: pass some chart options to DateAdapter - review fixes --- src/core/core.adapters.js | 14 ++++++++++++-- src/scales/scale.time.js | 7 +++---- test/specs/scale.time.tests.js | 22 +++++++--------------- types/adapters.d.ts | 10 +++++++--- 4 files changed, 29 insertions(+), 24 deletions(-) diff --git a/src/core/core.adapters.js b/src/core/core.adapters.js index 7fae18998f4..064afff809d 100644 --- a/src/core/core.adapters.js +++ b/src/core/core.adapters.js @@ -4,6 +4,10 @@ * @private */ +/** + * @typedef { import("../../types/index.esm").ChartOptions } ChartOptions + */ + /** * @return {*} */ @@ -26,11 +30,17 @@ function abstract() { export class DateAdapter { - constructor(options, chartOptions) { + constructor(options) { this.options = options || {}; - this.chartOptions = chartOptions || {}; } + /** + * Will called with chart options after adapter creation. + * @param {ChartOptions} chartOptions + */ + // eslint-disable-next-line no-unused-vars + init(chartOptions) {} + /** * Returns a map of time formats for the supported formatting units defined * in Unit as well as 'datetime' representing a detailed date/time string. diff --git a/src/scales/scale.time.js b/src/scales/scale.time.js index 23e883a02de..8f57c1b5974 100644 --- a/src/scales/scale.time.js +++ b/src/scales/scale.time.js @@ -221,10 +221,9 @@ export default class TimeScale extends Scale { init(scaleOpts, opts) { const time = scaleOpts.time || (scaleOpts.time = {}); - const adapterChartOpts = { - locale: opts.locale - }; - const adapter = this._adapter = new adapters._date(scaleOpts.adapters.date, adapterChartOpts); + const adapter = this._adapter = new adapters._date(scaleOpts.adapters.date); + + adapter.init(opts); // Backward compatibility: before introducing adapter, `displayFormats` was // supposed to contain *all* unit/string pairs but this can't be resolved diff --git a/test/specs/scale.time.tests.js b/test/specs/scale.time.tests.js index 1acddb6b4b8..70abc9bbced 100644 --- a/test/specs/scale.time.tests.js +++ b/test/specs/scale.time.tests.js @@ -1236,23 +1236,15 @@ describe('Time scale tests', function() { }); }); - it('should pass default locale to date adapter', function() { - var chart = window.acquireChart({ - type: 'line', - data: {}, - options: { - scales: { - x: { - type: 'time' - }, - } + it('should pass chart options to date adapter', function() { + let chartOptions; + + Chart._adapters._date.override({ + init(options) { + chartOptions = options; } }); - expect(chart.scales.x._adapter.chartOptions.locale).toBe('en-US'); - }); - - it('should pass locale to date adapter from options', function() { var chart = window.acquireChart({ type: 'line', data: {}, @@ -1266,6 +1258,6 @@ describe('Time scale tests', function() { } }); - expect(chart.scales.x._adapter.chartOptions.locale).toBe('es'); + expect(chartOptions).toEqual(chart.options); }); }); diff --git a/types/adapters.d.ts b/types/adapters.d.ts index a6ffdc48e4d..cae40966ba0 100644 --- a/types/adapters.d.ts +++ b/types/adapters.d.ts @@ -1,13 +1,17 @@ +import type { ChartOptions } from './index.esm'; + export type TimeUnit = 'millisecond' | 'second' | 'minute' | 'hour' | 'day' | 'week' | 'month' | 'quarter' | 'year'; export interface DateAdapter { // Override one or multiple of the methods to adjust to the logic of the current date library. override(members: Partial): void; readonly options: unknown; - readonly chartOptions: { - locale?: string; - }; + /** + * Will called with chart options after adapter creation. + * @param {ChartOptions} chartOptions + */ + init(chartOptions: ChartOptions): void; /** * Returns a map of time formats for the supported formatting units defined * in Unit as well as 'datetime' representing a detailed date/time string.