From bcee77bd714f8fab66157934d5178c6660e59e06 Mon Sep 17 00:00:00 2001 From: EricYang Date: Thu, 15 Oct 2020 12:29:51 +0800 Subject: [PATCH] refactor: use getControlConfigByType() to reduce methods count --- packages/core/src/components/axes/toolbar.ts | 152 +++++++++---------- 1 file changed, 73 insertions(+), 79 deletions(-) diff --git a/packages/core/src/components/axes/toolbar.ts b/packages/core/src/components/axes/toolbar.ts index 5790119c8c..41b7518d86 100644 --- a/packages/core/src/components/axes/toolbar.ts +++ b/packages/core/src/components/axes/toolbar.ts @@ -157,7 +157,7 @@ export class Toolbar extends Component { .attr("viewBox", "0 0 32 32") .attr("role", Roles.IMG); - buttonIcon.html(button.iconSVGContent()); + buttonIcon.html(button.iconSVGContent); if (button.shouldBeDisabled()) { buttonContainer .classed("toolbar-button--disabled", true) @@ -359,10 +359,6 @@ export class Toolbar extends Component { } getControlConfigs() { - const isZoomBarEnabled = - this.services.zoom.isZoomBarEnabled() && - !this.services.zoom.isEmptyState(); - const numberOfIcons = Tools.getProperty( this.model.getOptions(), "toolbar", @@ -375,31 +371,7 @@ export class Toolbar extends Component { ); const controlList = []; controls.forEach((control) => { - let controlConfig; - switch (control.type) { - case ToolbarControlTypes.ZOOM_IN: - if (isZoomBarEnabled) { - controlConfig = this.getZoomInConfig(); - } - break; - case ToolbarControlTypes.ZOOM_OUT: - if (isZoomBarEnabled) { - controlConfig = this.getZoomOutConfig(); - } - break; - case ToolbarControlTypes.RESET_ZOOM: - if (isZoomBarEnabled) { - controlConfig = this.getResetZoomConfig(); - } - break; - - // add more toolbar controls here - - default: - throw Error( - "Not supported toolbar control type: " + control.type - ); - } + const controlConfig = this.getControlConfigByType(control.type); // add to list if config is valid if (controlConfig) { @@ -430,66 +402,88 @@ export class Toolbar extends Component { } } - // add more toolbar control configuration here - - getZoomInConfig() { - return { - id: "toolbar-zoomIn", - shouldBeDisabled: () => this.services.zoom.isMinZoomDomain(), - iconSVGContent: () => this.getZoomInIconSVGContent(), - clickFunction: () => this.services.zoom.zoomIn() - }; - } - - getZoomOutConfig() { - return { - id: "toolbar-zoomOut", - shouldBeDisabled: () => this.services.zoom.isMaxZoomDomain(), - iconSVGContent: () => this.getZoomOutIconSVGContent(), - clickFunction: () => this.services.zoom.zoomOut() - }; - } - - getResetZoomConfig() { - return { - id: "toolbar-resetZoom", - shouldBeDisabled: () => this.services.zoom.isMaxZoomDomain(), - iconSVGContent: () => this.getResetZoomIconSVGContent(), - clickFunction: () => this.services.zoom.resetZoomDomain() - }; - } - + // special button config for overflow button getOverflowButtonConfig() { return { id: "toolbar-overflow-menu", - shouldBeDisabled: () => { - return false; - }, - iconSVGContent: () => this.getOverflowIconSVGContent(), + shouldBeDisabled: () => false, + iconSVGContent: ` + + `, clickFunction: () => this.toggleOverflowMenu() }; } - // add more icons here - // svg icon must be with 32x32 viewBox + getControlConfigByType(controlType: ToolbarControlTypes) { + const isZoomBarEnabled = + this.services.zoom.isZoomBarEnabled() && + !this.services.zoom.isEmptyState(); - getOverflowIconSVGContent() { - return ` - - `; - } + let controlConfig; + switch (controlType) { + case ToolbarControlTypes.ZOOM_IN: + if (isZoomBarEnabled) { + controlConfig = { + id: "toolbar-zoomIn", + shouldBeDisabled: () => + this.services.zoom.isMinZoomDomain(), + iconSVGContent: this.getControlIconByType(controlType), + clickFunction: () => this.services.zoom.zoomIn() + }; + } + break; + case ToolbarControlTypes.ZOOM_OUT: + if (isZoomBarEnabled) { + controlConfig = { + id: "toolbar-zoomOut", + shouldBeDisabled: () => + this.services.zoom.isMaxZoomDomain(), + iconSVGContent: this.getControlIconByType(controlType), + clickFunction: () => this.services.zoom.zoomOut() + }; + } + break; + case ToolbarControlTypes.RESET_ZOOM: + if (isZoomBarEnabled) { + controlConfig = { + id: "toolbar-resetZoom", + shouldBeDisabled: () => + this.services.zoom.isMaxZoomDomain(), + iconSVGContent: this.getControlIconByType(controlType), + clickFunction: () => + this.services.zoom.resetZoomDomain() + }; + } + break; - getZoomInIconSVGContent() { - return ` - `; - } + // add more toolbar control configuration here - getZoomOutIconSVGContent() { - return ` - `; + default: + throw Error( + "Not supported toolbar control type: " + controlType + ); + } + return controlConfig; } - getResetZoomIconSVGContent() { - return ``; + getControlIconByType(controlType: ToolbarControlTypes) { + switch (controlType) { + case ToolbarControlTypes.ZOOM_IN: + return ` + `; + case ToolbarControlTypes.ZOOM_OUT: + return ` + `; + case ToolbarControlTypes.RESET_ZOOM: + return ``; + + // add more icons here + // svg icon must be with 32x32 viewBox + + default: + throw Error( + "Not supported toolbar control type: " + controlType + ); + } } }