widgets: plots: improve oop writing, use prototype function defs

This commit is contained in:
jean-emmanuel 2016-09-15 20:52:44 +02:00
parent 7e88f8fe43
commit 3a312a93d4
3 changed files with 242 additions and 222 deletions

View file

@ -1,6 +1,8 @@
var {mapToScale} = require('../utils')
module.exports = function(widgetData){
var _plots_base = module.exports = function(widgetData){
this.widgetData = widgetData
this.widget = $(`
<div class="plot">
@ -15,8 +17,8 @@ module.exports = function(widgetData){
this.linkedWidgets = []
this.visible = false
this.textColor = getComputedStyle(document.documentElement).getPropertyValue('--color-text-fade')
this.rangeX = widgetData.rangeX || {min:0,max:1}
this.rangeY = widgetData.rangeY || {min:0,max:1}
this.rangeX = this.widgetData.rangeX || {min:0,max:1}
this.rangeY = this.widgetData.rangeY || {min:0,max:1}
this.pips = {
x : {
min: Math.abs(this.rangeX.min)>=1000?this.rangeY.min/1000+'k':this.rangeX.min,
@ -29,115 +31,123 @@ module.exports = function(widgetData){
}
this.canvas.resize(function(){
var width = this.canvas.width(),
height = this.canvas.height()
if (height==100 && width==100) return
if (!self.visible) {
this.visible = true
this.lineColor = getComputedStyle(this.widget[0]).getPropertyValue('--color-custom')
}
this.height=height
this.width=width
this.canvas[0].setAttribute('width',width)
this.canvas[0].setAttribute('height',height)
this.updateData()
requestAnimationFrame(this._draw)
}.bind(this))
this._draw = function() {
this.ctx.clearRect(0,0,this.width,this.height)
this.ctx.beginPath()
this.draw()
this.ctx.lineWidth = 1*PXSCALE
this.ctx.strokeStyle = this.lineColor
this.ctx.stroke()
this.ctx.font = PXSCALE * 10 + 'px sans-serif'
this.ctx.fillStyle = this.textColor
this.ctx.textBaseline = "bottom"
this.ctx.textAlign = 'left'
this.ctx.fillText(this.pips.x.min,12*PXSCALE,this.height)
this.ctx.textAlign = 'right'
this.ctx.fillText(this.pips.x.max,this.width-10,this.height)
this.ctx.save()
this.ctx.translate(0, this.height)
this.ctx.rotate(-Math.PI/2)
this.ctx.textBaseline = "top"
this.ctx.textAlign = 'left'
this.ctx.fillText(this.pips.y.min,12*PXSCALE,2*PXSCALE)
this.ctx.textAlign = 'right'
this.ctx.fillText(this.pips.y.max,this.height-10*PXSCALE,2*PXSCALE)
this.ctx.rotate(Math.PI/2)
this.ctx.restore()
}.bind(this)
this.draw = function() {
var first = true
var point = []
for (i in this.data) {
if (this.data[i][1]>this.rangeY.max || this.data[i][1]<this.rangeY.min ||
this.data[i][0]>this.rangeX.max || this.data[i][0]<this.rangeX.min) continue
var newpoint = this.data[i].length?
[
mapToScale(this.data[i][0],[this.rangeX.min,this.rangeX.max],[15*PXSCALE,this.width-15*PXSCALE],0,widgetData.logScaleX,true),
mapToScale(this.data[i][1],[this.rangeY.min,this.rangeY.max],[this.height-15*PXSCALE,15*PXSCALE],0,widgetData.logScaleY,true),
]
:
[
mapToScale(i,[0,this.data.length-1],[15*PXSCALE,this.width-15*PXSCALE],0,widgetData.logScaleX,true),
mapToScale(this.data[i],[this.rangeY.min,this.rangeY.max],[this.height-15*PXSCALE,15*PXSCALE],0,widgetData.logScaleY,true),
]
if (first) {
this.ctx.moveTo(newpoint[0],newpoint[1])
first = false
} else {
this.ctx.moveTo(point[0],point[1])
this.ctx.lineTo(newpoint[0],newpoint[1])
}
point = newpoint
}
}.bind(this)
this._handleSync = function(e,id,w){
this.handleSync(e,id,w)
}.bind(this)
this.handleSync = function(e,id,w) {
if (this.linkedWidgets.indexOf(id)==-1 || !WIDGETS[id]) return
this.updateData()
requestAnimationFrame(this._draw)
}
$('body').on('sync',this._handleSync)
this.updateData = function(){
// update the coordinates to draw
}
this.setValue = function(v) {
this.data = v
requestAnimationFrame(this._draw)
}
this.canvas.resize(this.resizeHandleProxy.bind(this))
$('body').on('sync',this.syncHandleProxy.bind(this))
}
_plots_base.prototype.syncHandleProxy = function() {
this.syncHandle.apply(this,arguments)
}
_plots_base.prototype.resizeHandleProxy = function() {
this.resizeHandle.apply(this,arguments)
}
_plots_base.prototype.syncHandle = function(e,id,w) {
if (this.linkedWidgets.indexOf(id)==-1 || !WIDGETS[id]) return
this.updateData()
requestAnimationFrame(this._draw.bind(this))
}
_plots_base.prototype.resizeHandle = function(){
console.log('rg')
var width = this.canvas.width(),
height = this.canvas.height()
if (height==100 && width==100) return
if (!self.visible) {
this.visible = true
this.lineColor = getComputedStyle(this.widget[0]).getPropertyValue('--color-custom')
}
this.height=height
this.width=width
this.canvas[0].setAttribute('width',width)
this.canvas[0].setAttribute('height',height)
this.updateData()
requestAnimationFrame(this._draw.bind(this))
}
_plots_base.prototype._draw = function() {
this.ctx.clearRect(0,0,this.width,this.height)
this.ctx.beginPath()
this.draw()
this.ctx.lineWidth = 1*PXSCALE
this.ctx.strokeStyle = this.lineColor
this.ctx.stroke()
this.ctx.font = PXSCALE * 10 + 'px sans-serif'
this.ctx.fillStyle = this.textColor
this.ctx.textBaseline = "bottom"
this.ctx.textAlign = 'left'
this.ctx.fillText(this.pips.x.min,12*PXSCALE,this.height)
this.ctx.textAlign = 'right'
this.ctx.fillText(this.pips.x.max,this.width-10,this.height)
this.ctx.save()
this.ctx.translate(0, this.height)
this.ctx.rotate(-Math.PI/2)
this.ctx.textBaseline = "top"
this.ctx.textAlign = 'left'
this.ctx.fillText(this.pips.y.min,12*PXSCALE,2*PXSCALE)
this.ctx.textAlign = 'right'
this.ctx.fillText(this.pips.y.max,this.height-10*PXSCALE,2*PXSCALE)
this.ctx.rotate(Math.PI/2)
this.ctx.restore()
}
_plots_base.prototype.draw = function() {
var first = true
var point = []
for (i in this.data) {
if (this.data[i][1]>this.rangeY.max || this.data[i][1]<this.rangeY.min ||
this.data[i][0]>this.rangeX.max || this.data[i][0]<this.rangeX.min) continue
var newpoint = this.data[i].length?
[
mapToScale(this.data[i][0],[this.rangeX.min,this.rangeX.max],[15*PXSCALE,this.width-15*PXSCALE],0,this.widgetData.logScaleX,true),
mapToScale(this.data[i][1],[this.rangeY.min,this.rangeY.max],[this.height-15*PXSCALE,15*PXSCALE],0,this.widgetData.logScaleY,true),
]
:
[
mapToScale(i,[0,this.data.length-1],[15*PXSCALE,this.width-15*PXSCALE],0,this.widgetData.logScaleX,true),
mapToScale(this.data[i],[this.rangeY.min,this.rangeY.max],[this.height-15*PXSCALE,15*PXSCALE],0,this.widgetData.logScaleY,true),
]
if (first) {
this.ctx.moveTo(newpoint[0],newpoint[1])
first = false
} else {
this.ctx.moveTo(point[0],point[1])
this.ctx.lineTo(newpoint[0],newpoint[1])
}
point = newpoint
}
}
_plots_base.prototype.setValue = function(v) {
this.data = v
requestAnimationFrame(this._draw.bind(this))
}
_plots_base.prototype.updateData = function(){
// update the coordinates to draw
}

View file

@ -44,7 +44,7 @@ module.exports.create = function(widgetData,container) {
visualizer.handleSync = function(e,id,w) {
visualizer.syncHandle = function(e,id,w) {
if (widgetData.widgetId!=id || !WIDGETS[id]) return
visualizer.startLoop()
}

View file

@ -2696,7 +2696,9 @@ module.exports = function(options,linear,resolution) {
},{}],22:[function(require,module,exports){
var {mapToScale} = require('../utils')
module.exports = function(widgetData){
var _plots_base = module.exports = function(widgetData){
this.widgetData = widgetData
this.widget = $(`
<div class="plot">
@ -2711,8 +2713,8 @@ module.exports = function(widgetData){
this.linkedWidgets = []
this.visible = false
this.textColor = getComputedStyle(document.documentElement).getPropertyValue('--color-text-fade')
this.rangeX = widgetData.rangeX || {min:0,max:1}
this.rangeY = widgetData.rangeY || {min:0,max:1}
this.rangeX = this.widgetData.rangeX || {min:0,max:1}
this.rangeY = this.widgetData.rangeY || {min:0,max:1}
this.pips = {
x : {
min: Math.abs(this.rangeX.min)>=1000?this.rangeY.min/1000+'k':this.rangeX.min,
@ -2725,117 +2727,125 @@ module.exports = function(widgetData){
}
this.canvas.resize(function(){
var width = this.canvas.width(),
height = this.canvas.height()
this.canvas.resize(this.resizeHandleProxy.bind(this))
$('body').on('sync',this.syncHandleProxy.bind(this))
if (height==100 && width==100) return
}
if (!self.visible) {
this.visible = true
this.lineColor = getComputedStyle(this.widget[0]).getPropertyValue('--color-custom')
_plots_base.prototype.syncHandleProxy = function() {
this.syncHandle.apply(this,arguments)
}
_plots_base.prototype.resizeHandleProxy = function() {
this.resizeHandle.apply(this,arguments)
}
_plots_base.prototype.syncHandle = function(e,id,w) {
if (this.linkedWidgets.indexOf(id)==-1 || !WIDGETS[id]) return
this.updateData()
requestAnimationFrame(this._draw.bind(this))
}
_plots_base.prototype.resizeHandle = function(){
console.log('rg')
var width = this.canvas.width(),
height = this.canvas.height()
if (height==100 && width==100) return
if (!self.visible) {
this.visible = true
this.lineColor = getComputedStyle(this.widget[0]).getPropertyValue('--color-custom')
}
this.height=height
this.width=width
this.canvas[0].setAttribute('width',width)
this.canvas[0].setAttribute('height',height)
this.updateData()
requestAnimationFrame(this._draw.bind(this))
}
_plots_base.prototype._draw = function() {
this.ctx.clearRect(0,0,this.width,this.height)
this.ctx.beginPath()
this.draw()
this.ctx.lineWidth = 1*PXSCALE
this.ctx.strokeStyle = this.lineColor
this.ctx.stroke()
this.ctx.font = PXSCALE * 10 + 'px sans-serif'
this.ctx.fillStyle = this.textColor
this.ctx.textBaseline = "bottom"
this.ctx.textAlign = 'left'
this.ctx.fillText(this.pips.x.min,12*PXSCALE,this.height)
this.ctx.textAlign = 'right'
this.ctx.fillText(this.pips.x.max,this.width-10,this.height)
this.ctx.save()
this.ctx.translate(0, this.height)
this.ctx.rotate(-Math.PI/2)
this.ctx.textBaseline = "top"
this.ctx.textAlign = 'left'
this.ctx.fillText(this.pips.y.min,12*PXSCALE,2*PXSCALE)
this.ctx.textAlign = 'right'
this.ctx.fillText(this.pips.y.max,this.height-10*PXSCALE,2*PXSCALE)
this.ctx.rotate(Math.PI/2)
this.ctx.restore()
}
_plots_base.prototype.draw = function() {
var first = true
var point = []
for (i in this.data) {
if (this.data[i][1]>this.rangeY.max || this.data[i][1]<this.rangeY.min ||
this.data[i][0]>this.rangeX.max || this.data[i][0]<this.rangeX.min) continue
var newpoint = this.data[i].length?
[
mapToScale(this.data[i][0],[this.rangeX.min,this.rangeX.max],[15*PXSCALE,this.width-15*PXSCALE],0,this.widgetData.logScaleX,true),
mapToScale(this.data[i][1],[this.rangeY.min,this.rangeY.max],[this.height-15*PXSCALE,15*PXSCALE],0,this.widgetData.logScaleY,true),
]
:
[
mapToScale(i,[0,this.data.length-1],[15*PXSCALE,this.width-15*PXSCALE],0,this.widgetData.logScaleX,true),
mapToScale(this.data[i],[this.rangeY.min,this.rangeY.max],[this.height-15*PXSCALE,15*PXSCALE],0,this.widgetData.logScaleY,true),
]
if (first) {
this.ctx.moveTo(newpoint[0],newpoint[1])
first = false
} else {
this.ctx.moveTo(point[0],point[1])
this.ctx.lineTo(newpoint[0],newpoint[1])
}
this.height=height
this.width=width
this.canvas[0].setAttribute('width',width)
this.canvas[0].setAttribute('height',height)
this.updateData()
requestAnimationFrame(this._draw)
}.bind(this))
this._draw = function() {
this.ctx.clearRect(0,0,this.width,this.height)
this.ctx.beginPath()
this.draw()
this.ctx.lineWidth = 1*PXSCALE
this.ctx.strokeStyle = this.lineColor
this.ctx.stroke()
this.ctx.font = PXSCALE * 10 + 'px sans-serif'
this.ctx.fillStyle = this.textColor
this.ctx.textBaseline = "bottom"
this.ctx.textAlign = 'left'
this.ctx.fillText(this.pips.x.min,12*PXSCALE,this.height)
this.ctx.textAlign = 'right'
this.ctx.fillText(this.pips.x.max,this.width-10,this.height)
this.ctx.save()
this.ctx.translate(0, this.height)
this.ctx.rotate(-Math.PI/2)
this.ctx.textBaseline = "top"
this.ctx.textAlign = 'left'
this.ctx.fillText(this.pips.y.min,12*PXSCALE,2*PXSCALE)
this.ctx.textAlign = 'right'
this.ctx.fillText(this.pips.y.max,this.height-10*PXSCALE,2*PXSCALE)
this.ctx.rotate(Math.PI/2)
this.ctx.restore()
}.bind(this)
this.draw = function() {
var first = true
var point = []
for (i in this.data) {
if (this.data[i][1]>this.rangeY.max || this.data[i][1]<this.rangeY.min ||
this.data[i][0]>this.rangeX.max || this.data[i][0]<this.rangeX.min) continue
var newpoint = this.data[i].length?
[
mapToScale(this.data[i][0],[this.rangeX.min,this.rangeX.max],[15*PXSCALE,this.width-15*PXSCALE],0,widgetData.logScaleX,true),
mapToScale(this.data[i][1],[this.rangeY.min,this.rangeY.max],[this.height-15*PXSCALE,15*PXSCALE],0,widgetData.logScaleY,true),
]
:
[
mapToScale(i,[0,this.data.length-1],[15*PXSCALE,this.width-15*PXSCALE],0,widgetData.logScaleX,true),
mapToScale(this.data[i],[this.rangeY.min,this.rangeY.max],[this.height-15*PXSCALE,15*PXSCALE],0,widgetData.logScaleY,true),
]
if (first) {
this.ctx.moveTo(newpoint[0],newpoint[1])
first = false
} else {
this.ctx.moveTo(point[0],point[1])
this.ctx.lineTo(newpoint[0],newpoint[1])
}
point = newpoint
}
}.bind(this)
this._handleSync = function(e,id,w){
this.handleSync(e,id,w)
}.bind(this)
this.handleSync = function(e,id,w) {
if (this.linkedWidgets.indexOf(id)==-1 || !WIDGETS[id]) return
this.updateData()
requestAnimationFrame(this._draw)
point = newpoint
}
}
$('body').on('sync',this._handleSync)
_plots_base.prototype.setValue = function(v) {
this.data = v
requestAnimationFrame(this._draw.bind(this))
}
this.updateData = function(){
// update the coordinates to draw
}
this.setValue = function(v) {
this.data = v
requestAnimationFrame(this._draw)
}
_plots_base.prototype.updateData = function(){
// update the coordinates to draw
}
},{"../utils":34}],23:[function(require,module,exports){
@ -3258,7 +3268,7 @@ module.exports.create = function(widgetData,container) {
visualizer.handleSync = function(e,id,w) {
visualizer.syncHandle = function(e,id,w) {
if (widgetData.widgetId!=id || !WIDGETS[id]) return
visualizer.startLoop()
}