From 3a312a93d4b69f1679f00c90ef3124cf4fb0f6f8 Mon Sep 17 00:00:00 2001 From: jean-emmanuel Date: Thu, 15 Sep 2016 20:52:44 +0200 Subject: [PATCH] widgets: plots: improve oop writing, use prototype function defs --- browser/js/app/widgets/plots/_plots_base.js | 236 ++++++++++---------- browser/js/app/widgets/plots/visualizer.js | 2 +- browser/js/browser-headless.bundle.js | 226 ++++++++++--------- 3 files changed, 242 insertions(+), 222 deletions(-) diff --git a/browser/js/app/widgets/plots/_plots_base.js b/browser/js/app/widgets/plots/_plots_base.js index d9fe0b13..0db65f64 100644 --- a/browser/js/app/widgets/plots/_plots_base.js +++ b/browser/js/app/widgets/plots/_plots_base.js @@ -1,6 +1,8 @@ var {mapToScale} = require('../utils') -module.exports = function(widgetData){ +var _plots_base = module.exports = function(widgetData){ + + this.widgetData = widgetData this.widget = $(`
@@ -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.rangeX.max || this.data[i][0]this.rangeY.max || this.data[i][1]this.rangeX.max || this.data[i][0] @@ -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.rangeX.max || this.data[i][0]this.rangeY.max || this.data[i][1]this.rangeX.max || this.data[i][0]