From 32966477902791d73a5d5ad246916a9a92fca117 Mon Sep 17 00:00:00 2001 From: jean-emmanuel Date: Tue, 13 Feb 2018 10:04:04 +0100 Subject: [PATCH] svg: add svg widget file --- src/browser/js/app/widgets/plots/svg.js | 111 ++++++++++++++++++++++++ 1 file changed, 111 insertions(+) create mode 100644 src/browser/js/app/widgets/plots/svg.js diff --git a/src/browser/js/app/widgets/plots/svg.js b/src/browser/js/app/widgets/plots/svg.js new file mode 100644 index 00000000..5362cf49 --- /dev/null +++ b/src/browser/js/app/widgets/plots/svg.js @@ -0,0 +1,111 @@ +var Widget = require('../common/widget') +var morph = require('nanomorph') + +class Svg extends Widget { + + static defaults() { + + return { + type:'svg', + id:'auto', + linkId:'', + + _geometry:'geometry', + + left:'auto', + top:'auto', + width:'auto', + height:'auto', + + _style:'style', + + label:'auto', + color:'auto', + css:'', + + _svg:'svg', + + svg:'', + noborder: false, + + _osc:'osc', + + preArgs:[], + address:'auto' + + } + + } + + constructor(options) { + + var html = ` +
+ +
+ ` + + super({...options, html: html}) + + this.height = undefined + this.width = undefined + + this.on('resize', this.resizeHandleProxy.bind(this), {element: this.widget}) + + } + + updateSvg(){ + + if (!this.width || !this.height) return + + var svg = this.getProp('svg') + + svg = svg.replace(/([0-9\.]+%)[\s\n]*,/g, m => (parseFloat(m) * this.width / 100).toFixed(2) + ',') + svg = svg.replace(/,[\s\n]*([0-9\.]+%)/g, m => ',' + (parseFloat(m.slice(1)) * this.height / 100).toFixed(2)) + + + morph(this.widget, DOM.create('
' + svg + '
')) + + } + + resizeHandleProxy() { + + this.resizeHandle(...arguments) + + } + + resizeHandle(event){ + + var {width, height, style} = event, + ratio = CANVAS_SCALING * this.scaling + + this.height = height + this.width = width + + this.updateSvg() + + } + + + onPropChanged(propName, options, oldPropValue) { + + if (super.onPropChanged(...arguments)) return true + + switch (propName) { + + case 'svg': + this.updateSvg() + return + + } + + } + + +} + +Svg.dynamicProps = Svg.prototype.constructor.dynamicProps.concat( + 'svg' +) + +module.exports = Svg