DCA redraw hacks
This commit is contained in:
@@ -77,6 +77,8 @@ function initChart() {
|
||||
chart.crossHairMoved().subscribe(null, (point)=>setTimeout(()=>handleCrosshairMovement(point),0) )
|
||||
chart.onSymbolChanged().subscribe(null, changeSymbol)
|
||||
chart.onIntervalChanged().subscribe(null, changeInterval)
|
||||
// chart.onHoveredSourceChanged().subscribe(null, ()=>console.log('hovered source changed', arguments))
|
||||
// chart.selection().onChanged().subscribe(null, s => console.log('selection', chart.selection().allSources()));
|
||||
changeSymbol(chart.symbolExt())
|
||||
changeInterval(widget.symbolInterval().interval)
|
||||
useChartOrderStore().chartReady = true
|
||||
@@ -205,6 +207,7 @@ export let draggingShapeIds = []
|
||||
|
||||
|
||||
function handleCrosshairMovement(point) {
|
||||
// console.log('crosshair moved')
|
||||
crosshairPoint = point
|
||||
const co = useChartOrderStore()
|
||||
if (co.drawing)
|
||||
|
||||
@@ -61,12 +61,14 @@ export class Shape {
|
||||
this.id = null // TradingView shapeId, or null if no TV shape created yet (drawing mode)
|
||||
this.type = type // ShapeType
|
||||
this.model = {} // set to nothing at first
|
||||
this.tvCallbacks = null
|
||||
this.ourPoints = null
|
||||
this.tvPoints = null
|
||||
this.creationOptions = {disableSave:true, disableUndo:false, disableSelection:false}
|
||||
this.ourProps = {}
|
||||
if (props !== null)
|
||||
this.ourProps = mixin(props, this.ourProps)
|
||||
this.tvProps = null
|
||||
this.tvProps = {}
|
||||
if (onModel !== null)
|
||||
this.onModel = onModel
|
||||
if (onDelete !== null )
|
||||
@@ -129,7 +131,9 @@ export class Shape {
|
||||
newProps.linecolor = color
|
||||
|
||||
// text label
|
||||
const text = allocationText(this.model.allocation, this.model.amount, this.model.amountSymbol)
|
||||
const text =
|
||||
// `${this.id} ` +
|
||||
allocationText(this.model.allocation, this.model.amount, this.model.amountSymbol)
|
||||
if (!text.length)
|
||||
newProps.showLabel = false
|
||||
else {
|
||||
@@ -197,8 +201,11 @@ export class Shape {
|
||||
|
||||
doCreate() {
|
||||
// createShape(this.type, this.points, {overrides:this.props}, new ShapeTVCallbacks(this))
|
||||
const options = {...this.creationOptions}
|
||||
options['overrides'] = this.ourProps
|
||||
this.tvPoints = [...this.ourPoints]
|
||||
createShape(this.type, this.ourPoints, {overrides:this.ourProps}, new ShapeTVCallbacks(this))
|
||||
this.tvCallbacks = new ShapeTVCallbacks(this);
|
||||
createShape(this.type, this.ourPoints, options, this.tvCallbacks)
|
||||
if (this.debug) console.log('created', this.type.name, this.ourPoints, this.id)
|
||||
}
|
||||
|
||||
@@ -219,7 +226,8 @@ export class Shape {
|
||||
setPoints(points) {
|
||||
// setting points to null will delete the shape from the chart. setting points to a valid value will cause the
|
||||
// shape to be drawn.
|
||||
if (this.debug) console.log('setPoints', points, this.id)
|
||||
if (this.debug) console.log('setPoints', this.id, this.ourPoints, points)
|
||||
const oldPoints = this.ourPoints
|
||||
this.ourPoints = points
|
||||
if (points === null || !points.length)
|
||||
this.delete()
|
||||
@@ -228,8 +236,32 @@ export class Shape {
|
||||
this.doCreate()
|
||||
else if (dirtyPoints(this.tvPoints, points)) {
|
||||
const s = this.tvShape();
|
||||
if (this.debug) console.log('adjusting tv points', s, this.tvPoints, points)
|
||||
if (!dragging) {
|
||||
let sbm = false
|
||||
if( draggingShapeIds.length ) {
|
||||
const dragShape = chart.getShapeById(draggingShapeIds[0])
|
||||
const sbms = [...dragShape._model._sourcesBeingMoved]
|
||||
// console.log('shape setPoints', this.id, dragShape, sbm)
|
||||
sbm = !sbms.length
|
||||
// if (!sbms.length) {
|
||||
// console.log('shape setPoints SBM return')
|
||||
// return
|
||||
// }
|
||||
}
|
||||
if (this.debug) console.log('adjusting tv points', this.tvPoints, points, dragging, this.beingDragged())
|
||||
// if ( dragging && !this.beingDragged())
|
||||
// s.setSelectionEnabled(false)
|
||||
// s.setPoints(points)
|
||||
// if ( dragging && !this.beingDragged())
|
||||
// s.setSelectionEnabled(true)
|
||||
|
||||
// best so far
|
||||
if (sbm && this.type === ShapeType.VLine) {
|
||||
// console.log('sbm: re-create shape')
|
||||
this.delete()
|
||||
this.ourPoints = points
|
||||
this.create()
|
||||
} else
|
||||
if (!dragging || this.type !== ShapeType.HLine) {
|
||||
if (this.debug) console.log('not dragging. use setPoints.')
|
||||
s.setPoints(points)
|
||||
}
|
||||
@@ -276,7 +308,13 @@ export class Shape {
|
||||
delete() {
|
||||
if (this.debug) console.log('shape.delete', this.id)
|
||||
this.ourPoints = null
|
||||
this.tvPoints = null
|
||||
this.tvProps = null
|
||||
if (this.id === null) return
|
||||
if (this.tvCallbacks !== null) {
|
||||
this.tvCallbacks.enabled = false
|
||||
this.tvCallbacks = null
|
||||
}
|
||||
deleteShapeId(this.id)
|
||||
this.id = null
|
||||
}
|
||||
@@ -341,21 +379,25 @@ class ShapeTVCallbacks {
|
||||
constructor(shape) {
|
||||
this.shape = shape
|
||||
this.creating = false
|
||||
this.enabled = true // gets disabled when the shape is deleted, squelching any further callbacks
|
||||
}
|
||||
|
||||
onCreate(shapeId, _tvShape, points, props) {
|
||||
if (!this.enabled) return // possible when shape is deleted and re-created
|
||||
this.creating = true
|
||||
this.shape.id = shapeId
|
||||
invokeCallback(this.shape, 'onCreate', points, props)
|
||||
}
|
||||
|
||||
onPoints(shapeId, _tvShape, points) {
|
||||
if (!this.enabled) return // possible when shape is deleted and re-created
|
||||
if (this.shape.debug) console.log('tvcb onPoints', points)
|
||||
this.shape.tvPoints = points
|
||||
this.shape.onPoints(points)
|
||||
}
|
||||
|
||||
onProps(shapeId, _tvShape, props) {
|
||||
if (!this.enabled) return // possible when shape is deleted and re-created
|
||||
if (this.shape.debug) console.log('tvOnProps', props)
|
||||
if (this.creating) { // todo still useful?
|
||||
this.creating = false
|
||||
@@ -366,43 +408,53 @@ class ShapeTVCallbacks {
|
||||
}
|
||||
|
||||
onDraw() {
|
||||
if (!this.enabled) return // possible when shape is deleted and re-created
|
||||
invokeCallback(this.shape, 'onDraw')
|
||||
}
|
||||
|
||||
onRedraw() {
|
||||
if (!this.enabled) return // possible when shape is deleted and re-created
|
||||
invokeCallback(this.shape, 'onRedraw')
|
||||
}
|
||||
|
||||
onUndraw() {
|
||||
if (!this.enabled) return // possible when shape is deleted and re-created
|
||||
invokeCallback(this.shape, 'onUndraw')
|
||||
}
|
||||
|
||||
onAddPoint() {
|
||||
if (!this.enabled) return // possible when shape is deleted and re-created
|
||||
invokeCallback(this.shape, 'onAddPoint')
|
||||
}
|
||||
|
||||
onMove(_shapeId, _tvShape, points) {
|
||||
onMove(shapeId, _tvShape, points) {
|
||||
if (!this.enabled) return // possible when shape is deleted and re-created
|
||||
invokeCallback(this.shape, 'onMove',points)
|
||||
}
|
||||
|
||||
onDrag(_shapeId, _tvShape, points) {
|
||||
onDrag(shapeId, _tvShape, points) {
|
||||
if (!this.enabled) return // possible when shape is deleted and re-created
|
||||
if (this.shape.debug) console.log('onDrag')
|
||||
invokeCallback(this.shape, 'onDrag', points)
|
||||
}
|
||||
|
||||
onHide(_shapeId, _tvShape, props) {
|
||||
onHide(shapeId, _tvShape, props) {
|
||||
if (!this.enabled) return // possible when shape is deleted and re-created
|
||||
invokeCallback(this.shape, 'onHide',props)
|
||||
}
|
||||
|
||||
onShow(_shapeId, _tvShape, props) {
|
||||
onShow(shapeId, _tvShape, props) {
|
||||
if (!this.enabled) return // possible when shape is deleted and re-created
|
||||
invokeCallback(this.shape, 'onShow',props)
|
||||
}
|
||||
|
||||
onClick(_shapeId, _tvShape) {
|
||||
onClick(shapeId, _tvShape) {
|
||||
if (!this.enabled) return // possible when shape is deleted and re-created
|
||||
invokeCallback(this.shape, 'onClick')
|
||||
}
|
||||
|
||||
onDelete(shapeId) {
|
||||
this.enabled = false
|
||||
this.shape.id = null
|
||||
invokeCallback(this.shape, 'onDelete', shapeId)
|
||||
}
|
||||
@@ -481,7 +533,6 @@ export class VLine extends Line {
|
||||
|
||||
// Model
|
||||
this.model.time = null
|
||||
this.debug=true
|
||||
|
||||
this.setModel(model) // call setModel at the end
|
||||
}
|
||||
@@ -493,25 +544,30 @@ export class VLine extends Line {
|
||||
|
||||
|
||||
setModel(model) {
|
||||
// console.log('VLine setModel', this.id, {...this.model}, model)
|
||||
super.setModel(model)
|
||||
if (model.time === null) {
|
||||
this.model.time = null
|
||||
this.delete()
|
||||
}
|
||||
else {
|
||||
else if (model.time !== this.model.time) {
|
||||
this.model.time = model.time
|
||||
const time = nearestOhlcStart(model.time);
|
||||
this.setPoints([{time, price:0}])
|
||||
}
|
||||
}
|
||||
|
||||
setPoints(points) {
|
||||
// console.error('VLine setPoints', points)
|
||||
super.setPoints(points);
|
||||
}
|
||||
|
||||
onPoints(points) {
|
||||
super.onPoints(points);
|
||||
const orig = this.ourPoints && this.ourPoints.length ? this.ourPoints[0].time : null
|
||||
super.onPoints(points);
|
||||
const time = points[0].time;
|
||||
if (!timeAdjustmentTooSmall(orig, time)) {
|
||||
if (!timeAdjustmentTooSmall(orig, time))
|
||||
this.updateModel({time: time})
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user