Skip to content

Commit

Permalink
Migrate to TS -- Part I (some of the basic class and utils)
Browse files Browse the repository at this point in the history
Note: lots of the common type are put in `src/util/types.ts`.
  • Loading branch information
100pah committed Feb 16, 2020
1 parent 6b991be commit 3229e88
Show file tree
Hide file tree
Showing 53 changed files with 7,125 additions and 5,861 deletions.
1 change: 1 addition & 0 deletions index.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export * from './echarts.all';
43 changes: 21 additions & 22 deletions src/CoordinateSystem.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,45 +18,44 @@
*/

import * as zrUtil from 'zrender/src/core/util';
import GlobalModel from './model/Global';
import ExtensionAPI from './ExtensionAPI';
import { CoordinateSystemCreator, CoordinateSystem } from './coord/CoordinateSystem';

var coordinateSystemCreators = {};
var coordinateSystemCreators: {[type: string]: CoordinateSystemCreator} = {};

function CoordinateSystemManager() {
class CoordinateSystemManager {

this._coordinateSystems = [];
}

CoordinateSystemManager.prototype = {

constructor: CoordinateSystemManager,
private _coordinateSystems: CoordinateSystem[] = [];

create: function (ecModel, api) {
var coordinateSystems = [];
create(ecModel: GlobalModel, api: ExtensionAPI): void {
var coordinateSystems: CoordinateSystem[] = [];
zrUtil.each(coordinateSystemCreators, function (creater, type) {
var list = creater.create(ecModel, api);
coordinateSystems = coordinateSystems.concat(list || []);
});

this._coordinateSystems = coordinateSystems;
},
}

update: function (ecModel, api) {
update(ecModel: GlobalModel, api: ExtensionAPI): void {
zrUtil.each(this._coordinateSystems, function (coordSys) {
coordSys.update && coordSys.update(ecModel, api);
});
},
}

getCoordinateSystems: function () {
getCoordinateSystems(): CoordinateSystem[] {
return this._coordinateSystems.slice();
}
};

CoordinateSystemManager.register = function (type, coordinateSystemCreator) {
coordinateSystemCreators[type] = coordinateSystemCreator;
};
static register = function (type: string, creator: CoordinateSystemCreator): void {
coordinateSystemCreators[type] = creator;
}

static get = function (type: string): CoordinateSystemCreator {
return coordinateSystemCreators[type];
}

CoordinateSystemManager.get = function (type) {
return coordinateSystemCreators[type];
};
}

export default CoordinateSystemManager;
export default CoordinateSystemManager;
50 changes: 38 additions & 12 deletions src/ExtensionAPI.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,18 +18,44 @@
*/

import * as zrUtil from 'zrender/src/core/util';
import {EChartsType} from './echarts';
import {CoordinateSystem} from './coord/CoordinateSystem';
import Element from 'zrender/src/Element';
import ComponentModel from './model/Component';

var echartsAPIList = [
'getDom', 'getZr', 'getWidth', 'getHeight', 'getDevicePixelRatio', 'dispatchAction', 'isDisposed',
'on', 'off', 'getDataURL', 'getConnectedDataURL', 'getModel', 'getOption',
'getViewOfComponentModel', 'getViewOfSeriesModel'
];
// And `getCoordinateSystems` and `getComponentByElement` will be injected in echarts.js

function ExtensionAPI(chartInstance) {
zrUtil.each(echartsAPIList, function (name) {
this[name] = zrUtil.bind(chartInstance[name], chartInstance);
}, this);
var availableMethods = {
getDom: 1,
getZr: 1,
getWidth: 1,
getHeight: 1,
getDevicePixelRatio: 1,
dispatchAction: 1,
isDisposed: 1,
on: 1,
off: 1,
getDataURL: 1,
getConnectedDataURL: 1,
getModel: 1,
getOption: 1,
getViewOfComponentModel: 1,
getViewOfSeriesModel: 1
};

interface ExtensionAPI extends Pick<EChartsType, keyof typeof availableMethods> {}

abstract class ExtensionAPI {

constructor(ecInstance: EChartsType) {
zrUtil.each(availableMethods, function (v, name: string) {
(this as any)[name] = zrUtil.bind((ecInstance as any)[name], ecInstance);
}, this);
}

// Implemented in echarts.js
abstract getCoordinateSystems(): CoordinateSystem[];

// Implemented in echarts.js
abstract getComponentByElement(el: Element): ComponentModel;
}

export default ExtensionAPI;
export default ExtensionAPI;
8 changes: 7 additions & 1 deletion src/chart/custom.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@
* under the License.
*/

// @ts-nocheck

import {__DEV__} from '../config';
import * as zrUtil from 'zrender/src/core/util';
import * as graphicUtil from '../util/graphic';
Expand All @@ -28,6 +30,8 @@ import SeriesModel from '../model/Series';
import Model from '../model/Model';
import ChartView from '../view/Chart';
import {createClipPath} from './helper/createClipPathFromCoordSys';
import {EventQueryItem, ECEvent} from '../util/types';
import Element from 'zrender/src/Element';

import prepareCartesian2d from '../coord/cartesian/prepareCustom';
import prepareGeo from '../coord/geo/prepareCustom';
Expand Down Expand Up @@ -208,7 +212,9 @@ ChartView.extend({
/**
* @override
*/
filterForExposedEvent: function (eventType, query, targetEl, packedEvent) {
filterForExposedEvent: function (
eventType: string, query: EventQueryItem, targetEl: Element, packedEvent: ECEvent
): boolean {
var elementName = query.element;
if (elementName == null || targetEl.name === elementName) {
return true;
Expand Down
2 changes: 1 addition & 1 deletion src/chart/graph/GraphSeries.ts
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ var GraphSeries = echarts.extendSeriesModel({

mergeDefaultAndTheme: function (option) {
GraphSeries.superApply(this, 'mergeDefaultAndTheme', arguments);
defaultEmphasis(option, ['edgeLabel'], ['show']);
defaultEmphasis(option, 'edgeLabel', ['show']);
},

getInitialData: function (option, ecModel) {
Expand Down
6 changes: 4 additions & 2 deletions src/chart/helper/createListFromArray.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,17 +17,19 @@
* under the License.
*/

// @ts-nocheck

import * as zrUtil from 'zrender/src/core/util';
import List from '../../data/List';
import createDimensions from '../../data/helper/createDimensions';
import {SOURCE_FORMAT_ORIGINAL} from '../../data/helper/sourceType';
import {getDimensionTypeByAxis} from '../../data/helper/dimensionHelper';
import {getDataItemValue} from '../../util/model';
import CoordinateSystem from '../../CoordinateSystem';
import {getCoordSysInfoBySeries} from '../../model/referHelper';
import Source from '../../data/Source';
import {enableDataStack} from '../../data/helper/dataStackHelper';
import {makeSeriesEncodeForAxisCoordSys} from '../../data/helper/sourceHelper';
import { SOURCE_FORMAT_ORIGINAL } from '../../util/types';

/**
* @param {module:echarts/data/Source|Array} source Or raw data.
Expand Down Expand Up @@ -120,7 +122,7 @@ function createListFromArray(source, seriesModel, opt) {
return list;
}

function isNeedCompleteOrdinalData(source) {
function isNeedCompleteOrdinalData(source: Source) {
if (source.sourceFormat === SOURCE_FORMAT_ORIGINAL) {
var sampleItem = firstDataNotNull(source.data || []);
return sampleItem != null
Expand Down
10 changes: 8 additions & 2 deletions src/chart/helper/createRenderPlanner.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,15 +17,19 @@
* under the License.
*/

// @ts-nocheck

import {makeInner} from '../../util/model';
import SeriesModel from '../../model/Series';
import { StageHandlerPlanReturn, StageHandlerPlan } from '../../util/types';

/**
* @return {string} If large mode changed, return string 'reset';
*/
export default function () {
var inner = makeInner();

return function (seriesModel) {
return function (seriesModel: SeriesModel): StageHandlerPlanReturn {
var fields = inner(seriesModel);
var pipelineContext = seriesModel.pipelineContext;

Expand All @@ -38,6 +42,8 @@ export default function () {
var large = fields.large = pipelineContext && pipelineContext.large;
var progressive = fields.progressiveRender = pipelineContext && pipelineContext.progressiveRender;

return !!((originalLarge ^ large) || (originalProgressive ^ progressive)) && 'reset';
return (
!!((originalLarge ^ large as any) || (originalProgressive ^ progressive as any)) && 'reset'
) as StageHandlerPlanReturn;
};
}
5 changes: 4 additions & 1 deletion src/component/dataZoom/dataZoomProcessor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@
* under the License.
*/

// @ts-nocheck

import * as echarts from '../../echarts';
import {createHashMap, each} from 'zrender/src/core/util';

Expand All @@ -40,7 +42,8 @@ echarts.registerProcessor({
return seriesModelMap;
},

modifyOutputEnd: true,
// FIXME:TS never used, so comment it
// modifyOutputEnd: true,

// Consider appendData, where filter should be performed. Because data process is
// in block mode currently, it is not need to worry about that the overallProgress
Expand Down
4 changes: 3 additions & 1 deletion src/component/dataset.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@
* under the License.
*/

// @ts-nocheck

/**
* This module is imported by echarts directly.
*
Expand All @@ -29,7 +31,7 @@
import ComponentModel from '../model/Component';
import ComponentView from '../view/Component';
import {detectSourceFormat} from '../data/helper/sourceHelper';
import {SERIES_LAYOUT_BY_COLUMN} from '../data/helper/sourceType';
import { SERIES_LAYOUT_BY_COLUMN } from '../util/types';

ComponentModel.extend({

Expand Down
4 changes: 3 additions & 1 deletion src/component/helper/BrushController.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,11 @@
* under the License.
*/

// @ts-nocheck

import {__DEV__} from '../../config';
import * as zrUtil from 'zrender/src/core/util';
import Eventful from 'zrender/src/mixin/Eventful';
import Eventful from 'zrender/src/core/Eventful';
import * as graphic from '../../util/graphic';
import * as interactionMutex from './interactionMutex';
import DataDiffer from '../../data/DataDiffer';
Expand Down
4 changes: 3 additions & 1 deletion src/component/helper/RoamController.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,10 @@
* under the License.
*/

// @ts-nocheck

import * as zrUtil from 'zrender/src/core/util';
import Eventful from 'zrender/src/mixin/Eventful';
import Eventful from 'zrender/src/core/Eventful';
import * as eventTool from 'zrender/src/core/event';
import * as interactionMutex from './interactionMutex';

Expand Down
6 changes: 4 additions & 2 deletions src/component/marker/MarkerModel.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,15 @@
* under the License.
*/

// @ts-nocheck

import {__DEV__} from '../../config';
import * as echarts from '../../echarts';
import * as zrUtil from 'zrender/src/core/util';
import env from 'zrender/src/core/env';
import * as modelUtil from '../../util/model';
import * as formatUtil from '../../util/format';
import dataFormatMixin from '../../model/mixin/dataFormat';
import DataFormatMixin from '../../model/mixin/dataFormat';

var addCommas = formatUtil.addCommas;
var encodeHTML = formatUtil.encodeHTML;
Expand Down Expand Up @@ -152,6 +154,6 @@ var MarkerModel = echarts.extendComponentModel({
}
});

zrUtil.mixin(MarkerModel, dataFormatMixin);
zrUtil.mixin(MarkerModel, DataFormatMixin.prototype);

export default MarkerModel;
6 changes: 4 additions & 2 deletions src/component/timeline/SliderTimelineModel.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,11 @@
* under the License.
*/

// @ts-nocheck

import * as zrUtil from 'zrender/src/core/util';
import TimelineModel from './TimelineModel';
import dataFormatMixin from '../../model/mixin/dataFormat';
import DataFormatMixin from '../../model/mixin/dataFormat';

var SliderTimelineModel = TimelineModel.extend({

Expand Down Expand Up @@ -117,6 +119,6 @@ var SliderTimelineModel = TimelineModel.extend({

});

zrUtil.mixin(SliderTimelineModel, dataFormatMixin);
zrUtil.mixin(SliderTimelineModel, DataFormatMixin.prototype);

export default SliderTimelineModel;
4 changes: 2 additions & 2 deletions src/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,11 +27,11 @@ var dev;

// In browser
if (typeof window !== 'undefined') {
dev = window.__DEV__;
dev = (window as any).__DEV__;
}
// In node
else if (typeof global !== 'undefined') {
dev = global.__DEV__;
dev = (global as any).__DEV__;
}

if (typeof dev === 'undefined') {
Expand Down
Loading

0 comments on commit 3229e88

Please sign in to comment.