Skip to content

Commit

Permalink
opt: event constants
Browse files Browse the repository at this point in the history
  • Loading branch information
liihuu committed Feb 6, 2023
1 parent efa5877 commit 8416bba
Show file tree
Hide file tree
Showing 6 changed files with 73 additions and 51 deletions.
104 changes: 58 additions & 46 deletions src/ChartEvent.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,14 @@ import Nullable from './common/Nullable'
import SyntheticEvent, { EventHandler, MouseTouchEvent, TOUCH_MIN_RADIUS } from './common/SyntheticEvent'
import Coordinate from './common/Coordinate'
import { UpdateLevel } from './common/Updater'
import Bounding from './common/Bounding'

import { AxisExtremum } from './component/Axis'
import YAxis from './component/YAxis'

import Chart from './Chart'
import Pane, { PaneIdConstants } from './pane/Pane'
import Widget from './widget/Widget'
import Widget, { WidgetNameConstants } from './widget/Widget'
import { REAL_SEPARATOR_HEIGHT } from './widget/SeparatorWidget'

interface EventTriggerWidgetInfo {
Expand Down Expand Up @@ -110,7 +111,7 @@ export default class ChartEvent implements EventHandler {

pinchEvent (e: MouseTouchEvent, scale: number): boolean {
const { pane, widget } = this._findWidgetByEvent(e)
if (pane?.getId() !== PaneIdConstants.XAXIS && widget?.getName() === 'main') {
if (pane?.getId() !== PaneIdConstants.XAXIS && widget?.getName() === WidgetNameConstants.MAIN) {
const event = this._makeWidgetEvent(e, widget)
const zoomScale = (scale - this._pinchScale) * 5
this._pinchScale = scale
Expand All @@ -120,21 +121,32 @@ export default class ChartEvent implements EventHandler {
return false
}

mouseWheelHorizontalEvent (e: MouseTouchEvent, distance: number): boolean {
const { pane, widget } = this._findWidgetByEvent(e)
if (pane?.getId() !== PaneIdConstants.XAXIS && widget?.getName() === 'main') {
const timeScaleStore = this._chart.getChartStore().getTimeScaleStore()
timeScaleStore.startScroll()
timeScaleStore.scroll(distance)
return true
}
return false
mouseWheelHortEvent (e: MouseTouchEvent, distance: number): boolean {
const timeScaleStore = this._chart.getChartStore().getTimeScaleStore()
timeScaleStore.startScroll()
timeScaleStore.scroll(distance)
return true
}

mouseWheelVerticalEvent (e: MouseTouchEvent, scale: number): boolean {
const { pane, widget } = this._findWidgetByEvent(e)
if (pane?.getId() !== PaneIdConstants.XAXIS && widget?.getName() === 'main') {
const event = this._makeWidgetEvent(e, widget)
mouseWheelVertEvent (e: MouseTouchEvent, scale: number): boolean {
const { widget } = this._findWidgetByEvent(e)
const isTouch = e.isTouch ?? false
const event = this._makeWidgetEvent(e, widget)
let zoomCoordinate: Nullable<Coordinate> = null
const name = widget?.getName()
if (isTouch) {
if (name === WidgetNameConstants.MAIN || name === WidgetNameConstants.XAXIS) {
zoomCoordinate = { x: event.x, y: event.y }
} else {
const bounding = this._chart.getPaneById(PaneIdConstants.CANDLE)?.getBounding() as Bounding
zoomCoordinate = { x: bounding.width / 2, y: bounding.height / 2 }
}
} else {
if (name === WidgetNameConstants.MAIN) {
zoomCoordinate = { x: event.x, y: event.y }
}
}
if (zoomCoordinate !== null) {
this._chart.getChartStore().getTimeScaleStore().zoom(scale, { x: event.x, y: event.y })
return true
}
Expand All @@ -148,17 +160,17 @@ export default class ChartEvent implements EventHandler {
const event = this._makeWidgetEvent(e, widget)
const name = widget.getName()
switch (name) {
case 'separator': {
case WidgetNameConstants.SEPARATOR: {
return widget.dispatchEvent('mouseDownEvent', event)
}
case 'main': {
case WidgetNameConstants.MAIN: {
const extremum = pane?.getAxisComponent().getExtremum() ?? null
this._prevYAxisExtremum = extremum === null ? extremum : { ...extremum }
this._startScrollCoordinate = { x: event.x, y: event.y }
this._chart.getChartStore().getTimeScaleStore().startScroll()
return widget.dispatchEvent('mouseDownEvent', event)
}
case 'xAxis': {
case WidgetNameConstants.XAXIS: {
const consumed = widget.dispatchEvent('mouseDownEvent', event)
if (consumed) {
this._chart.updatePane(UpdateLevel.OVERLAY)
Expand All @@ -167,7 +179,7 @@ export default class ChartEvent implements EventHandler {
this._xAxisStartScaleDistance = event.pageX
return consumed
}
case 'yAxis': {
case WidgetNameConstants.YAXIS: {
const consumed = widget.dispatchEvent('mouseDownEvent', event)
if (consumed) {
this._chart.updatePane(UpdateLevel.OVERLAY)
Expand Down Expand Up @@ -196,13 +208,13 @@ export default class ChartEvent implements EventHandler {
if (widget !== null) {
const name = widget.getName()
switch (name) {
case 'main': {
case WidgetNameConstants.MAIN: {
this._chart.getChartStore().getCrosshairStore().set({ x: event.x, y: event.y, paneId: pane?.getId() })
return widget.dispatchEvent('mouseMoveEvent', event)
}
case 'separator':
case 'xAxis':
case 'yAxis': {
case WidgetNameConstants.SEPARATOR:
case WidgetNameConstants.XAXIS:
case WidgetNameConstants.YAXIS: {
const consumed = widget.dispatchEvent('mouseMoveEvent', event)
this._chart.getChartStore().getCrosshairStore().set()
return consumed
Expand All @@ -213,7 +225,7 @@ export default class ChartEvent implements EventHandler {
}

pressedMouseMoveEvent (e: MouseTouchEvent): boolean {
if (this._mouseDownWidget !== null && this._mouseDownWidget.getName() === 'separator') {
if (this._mouseDownWidget !== null && this._mouseDownWidget.getName() === WidgetNameConstants.SEPARATOR) {
return this._mouseDownWidget.dispatchEvent('pressedMouseMoveEvent', e)
}
const { pane, widget } = this._findWidgetByEvent(e)
Expand All @@ -225,7 +237,7 @@ export default class ChartEvent implements EventHandler {
const event = this._makeWidgetEvent(e, widget)
const name = widget.getName()
switch (name) {
case 'main': {
case WidgetNameConstants.MAIN: {
const bounding = widget.getBounding()
const consumed = widget.dispatchEvent('pressedMouseMoveEvent', event)
if (!consumed && this._startScrollCoordinate !== null) {
Expand Down Expand Up @@ -259,7 +271,7 @@ export default class ChartEvent implements EventHandler {
this._chart.getChartStore().getCrosshairStore().set({ x: event.x, y: event.y, paneId: pane?.getId() })
return consumed
}
case 'xAxis': {
case WidgetNameConstants.XAXIS: {
const consumed = widget.dispatchEvent('pressedMouseMoveEvent', event)
if (!consumed) {
const scale = this._xAxisStartScaleDistance / event.pageX
Expand All @@ -271,7 +283,7 @@ export default class ChartEvent implements EventHandler {
}
return consumed
}
case 'yAxis': {
case WidgetNameConstants.YAXIS: {
const consumed = widget.dispatchEvent('pressedMouseMoveEvent', event)
if (!consumed) {
if (this._prevYAxisExtremum !== null) {
Expand Down Expand Up @@ -311,10 +323,10 @@ export default class ChartEvent implements EventHandler {
const event = this._makeWidgetEvent(e, widget)
const name = widget.getName()
switch (name) {
case 'main':
case 'separator':
case 'xAxis':
case 'yAxis': {
case WidgetNameConstants.MAIN:
case WidgetNameConstants.SEPARATOR:
case WidgetNameConstants.XAXIS:
case WidgetNameConstants.YAXIS: {
consumed = widget.dispatchEvent('mouseUpEvent', event)
break
}
Expand All @@ -340,9 +352,9 @@ export default class ChartEvent implements EventHandler {
const event = this._makeWidgetEvent(e, widget)
const name = widget.getName()
switch (name) {
case 'main':
case 'xAxis':
case 'yAxis': {
case WidgetNameConstants.MAIN:
case WidgetNameConstants.XAXIS:
case WidgetNameConstants.YAXIS: {
consumed = widget.dispatchEvent('mouseRightClickEvent', event)
break
}
Expand All @@ -356,7 +368,7 @@ export default class ChartEvent implements EventHandler {

mouseDoubleClickEvent (e: MouseTouchEvent): boolean {
const { pane, widget } = this._findWidgetByEvent(e)
if (widget !== null && widget.getName() === 'yAxis') {
if (widget !== null && widget.getName() === WidgetNameConstants.YAXIS) {
const yAxis = pane?.getAxisComponent() as YAxis
if (!yAxis.getAutoCalcTickFlag()) {
yAxis.setAutoCalcTickFlag(true)
Expand All @@ -378,7 +390,7 @@ export default class ChartEvent implements EventHandler {
const event = this._makeWidgetEvent(e, widget)
const name = widget.getName()
switch (name) {
case 'main': {
case WidgetNameConstants.MAIN: {
const chartStore = this._chart.getChartStore()
const crosshairStore = chartStore.getCrosshairStore()
if (widget.dispatchEvent('mouseDownEvent', event)) {
Expand Down Expand Up @@ -413,8 +425,8 @@ export default class ChartEvent implements EventHandler {
}
return true
}
case 'xAxis':
case 'yAxis': {
case WidgetNameConstants.XAXIS:
case WidgetNameConstants.YAXIS: {
const consumed = widget.dispatchEvent('mouseDownEvent', event)
if (consumed) {
this._chart.updatePane(UpdateLevel.OVERLAY)
Expand All @@ -432,7 +444,7 @@ export default class ChartEvent implements EventHandler {
const event = this._makeWidgetEvent(e, widget)
const name = widget.getName()
switch (name) {
case 'main': {
case WidgetNameConstants.MAIN: {
if (widget.dispatchEvent('pressedMouseMoveEvent', event)) {
event.preventDefault?.()
this._chart.updatePane(UpdateLevel.OVERLAY)
Expand All @@ -453,8 +465,8 @@ export default class ChartEvent implements EventHandler {
}
return true
}
case 'xAxis':
case 'yAxis': {
case WidgetNameConstants.XAXIS:
case WidgetNameConstants.YAXIS: {
const consumed = widget.dispatchEvent('pressedMouseMoveEvent', event)
if (consumed) {
event.preventDefault?.()
Expand All @@ -473,7 +485,7 @@ export default class ChartEvent implements EventHandler {
const event = this._makeWidgetEvent(e, widget)
const name = widget.getName()
switch (name) {
case 'main': {
case WidgetNameConstants.MAIN: {
widget.dispatchEvent('mouseUpEvent', event)
if (this._startScrollCoordinate !== null) {
const time = new Date().getTime() - this._flingStartTime
Expand Down Expand Up @@ -501,8 +513,8 @@ export default class ChartEvent implements EventHandler {
}
return true
}
case 'xAxis':
case 'yAxis': {
case WidgetNameConstants.XAXIS:
case WidgetNameConstants.YAXIS: {
const consumed = widget.dispatchEvent('mouseUpEvent', event)
if (consumed) {
this._chart.updatePane(UpdateLevel.OVERLAY)
Expand All @@ -515,7 +527,7 @@ export default class ChartEvent implements EventHandler {

tapEvent (e: MouseTouchEvent): boolean {
const { pane, widget } = this._findWidgetByEvent(e)
if (widget !== null && widget.getName() === 'main' && this._touchCoordinate === null && !this._touchCancelCrosshair && !this._touchZoomed) {
if (widget !== null && widget.getName() === WidgetNameConstants.MAIN && this._touchCoordinate === null && !this._touchCancelCrosshair && !this._touchZoomed) {
const event = this._makeWidgetEvent(e, widget)
this._touchCoordinate = { x: event.x, y: event.y }
this._chart.getChartStore().getCrosshairStore().set({ x: event.x, y: event.y, paneId: pane?.getId() })
Expand All @@ -526,7 +538,7 @@ export default class ChartEvent implements EventHandler {

longTapEvent (e: MouseTouchEvent): boolean {
const { pane, widget } = this._findWidgetByEvent(e)
if (widget !== null && widget.getName() === 'main') {
if (widget !== null && widget.getName() === WidgetNameConstants.MAIN) {
const event = this._makeWidgetEvent(e, widget)
this._touchCoordinate = { x: event.x, y: event.y }
this._chart.getChartStore().getCrosshairStore().set({ x: event.x, y: event.y, paneId: pane?.getId() })
Expand Down
3 changes: 2 additions & 1 deletion src/widget/IndicatorWidget.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@

import Pane from '../pane/Pane'

import { WidgetNameConstants } from './Widget'
import DrawWidget from './DrawWidget'

import YAxis from '../component/YAxis'
Expand All @@ -38,7 +39,7 @@ export default class IndicatorWidget extends DrawWidget<YAxis> {
}

getName (): string {
return 'main'
return WidgetNameConstants.MAIN
}

protected updateMain (ctx: CanvasRenderingContext2D): void {
Expand Down
4 changes: 2 additions & 2 deletions src/widget/SeparatorWidget.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ import { ActionType } from '../common/Action'
import Axis from '../component/Axis'
import YAxis from '../component/YAxis'

import Widget from './Widget'
import Widget, { WidgetNameConstants } from './Widget'
import Pane from '../pane/Pane'

import { createDom } from '../common/utils/dom'
Expand Down Expand Up @@ -51,7 +51,7 @@ export default class SeparatorWidget extends Widget<YAxis> {
}

getName (): string {
return 'separator'
return WidgetNameConstants.SEPARATOR
}

checkEventOn (): boolean {
Expand Down
7 changes: 7 additions & 0 deletions src/widget/Widget.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,13 @@ import Pane from '../pane/Pane'
import { createDom } from '../common/utils/dom'
import { merge } from '../common/utils/typeChecks'

export const WidgetNameConstants = {
MAIN: 'main',
XAXIS: 'xAxis',
YAXIS: 'yAxis',
SEPARATOR: 'separator'
}

export default abstract class Widget<C extends Axis = Axis> extends Eventful implements Updater {
/**
* Parent pane
Expand Down
3 changes: 2 additions & 1 deletion src/widget/XAxisWidget.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@

import Pane from '../pane/Pane'

import { WidgetNameConstants } from './Widget'
import DrawWidget from './DrawWidget'

import XAxis from '../component/XAxis'
Expand All @@ -34,7 +35,7 @@ export default class XAxisWidget extends DrawWidget<XAxis> {
}

getName (): string {
return 'xAxis'
return WidgetNameConstants.XAXIS
}

protected updateMain (ctx: CanvasRenderingContext2D): void {
Expand Down
3 changes: 2 additions & 1 deletion src/widget/YAxisWidget.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@

import Pane from '../pane/Pane'

import { WidgetNameConstants } from './Widget'
import DrawWidget from './DrawWidget'

import YAxis from '../component/YAxis'
Expand All @@ -38,7 +39,7 @@ export default class YAxisWidget extends DrawWidget<YAxis> {
}

getName (): string {
return 'yAxis'
return WidgetNameConstants.YAXIS
}

protected updateMain (ctx: CanvasRenderingContext2D): void {
Expand Down

0 comments on commit 8416bba

Please sign in to comment.