From 7308ade0ec331b667e2e251be63dc34f222edd4b Mon Sep 17 00:00:00 2001 From: Kevin Kwok Date: Mon, 4 Jan 2010 23:21:03 +0000 Subject: [PATCH] SVG edit wave should work now (hopefully) git-svn-id: http://svg-edit.googlecode.com/svn/trunk@1148 eee81c28-f429-11dd-99c0-75d572ba1ddd --- wave/wave.js | 65 +++++++++++++++++++++++++++++++++++++++------------- 1 file changed, 49 insertions(+), 16 deletions(-) diff --git a/wave/wave.js b/wave/wave.js index 46a9d262..f579df18 100644 --- a/wave/wave.js +++ b/wave/wave.js @@ -2,6 +2,7 @@ var shapetime = {}; var nodelete = false; function stateUpdated() { + // 'state' is an object of key-value pairs that map ids to JSON serialization of SVG elements // 'keys' is an array of all the keys in the state var state = wave.getState(); @@ -11,7 +12,9 @@ function stateUpdated() { // 'e' is an integer describing the position within the document var k = this.id; var v = state.get(k); - if (v) { + if(k == "selectorParentGroup" || k == "svgcontent"){ + //meh + }else if (v) { var ob = JSON.parse(v); if (ob) { // do nothing @@ -21,9 +24,8 @@ function stateUpdated() { } //keys.remove(k); - } else if(this.id != "selectorParentGroup" && !nodelete){ - //console.log(this) - + } else if(!nodelete){ + this.parentNode.removeChild(this); } }); @@ -36,10 +38,7 @@ function stateUpdated() { if(!shapetime[k] || ob.time > shapetime[k]){ var a; if(a = document.getElementById(k)){ - var attrs = {}; - for(var i = a.length; i--;){ - attrs[a.item(i).nodeName] = a.item(i).nodeValue; - } + var attrs = get_attrs(a); if(JSON.stringify(attrs) != JSON.stringify(ob.attr)){ shapetime[k] = ob.time svgCanvas.updateElementFromJson(ob) @@ -60,16 +59,27 @@ function getId(canvas, objnum) { var extra = SHA256(wave.getViewer().getId()); //in case the next step kills all the characters for(var i = 0, l = id.length, n = ""; i < l; i++){ if("abcdefghijklmnopqrstuvwxyz0123456789".indexOf(id[i]) != -1){ - n+=i; + n+=id[i]; } } return "svg_"+n+"_"+extra.substr(0,5)+"_"+objnum; } +function get_attrs(a){ + var attrs = {}; + for(var i = a.length; i--;){ + var attr = a.item(i).nodeName; + if(",style,".indexOf(","+attr+",") == -1){ + attrs[attr] = a.item(i).nodeValue; + } + } + return attrs +} + function main() { $(document).ready(function(){ if (wave && wave.isInWaveContainer()) { - wave.setStateCallback(stateUpdated); + wave.setStateCallback(function(){setTimeout(stateUpdated,10)}); } var oldchanged = svgCanvas.bind("changed", function(canvas, elem){ @@ -81,18 +91,20 @@ function main() { var attrs = {}; var a = this.attributes; if(a){ - for(var i = a.length; i--;){ - attrs[a.item(i).nodeName] = a.item(i).nodeValue; - } + var attrs = get_attrs(a) var ob = {element: this.nodeName, attr: attrs}; ob.time = shapetime[this.id] = (new Date).getTime() delta[this.id] = JSON.stringify(ob); } }) + wave.getState().submitDelta(delta) //sendDelta(canvas, elem) + }); + //* + var oldselected = svgCanvas.bind("selected", function(canvas, elem){ if(oldselected)oldselected.apply(this, [canvas,elem]); @@ -101,9 +113,8 @@ function main() { var delta = {} var deletions = 0; $.each(elem, function(){ - if(!this.parentNode){ + if(!this.parentNode && this != window){ delta[this.id] = null; - deletions ++ } }); @@ -111,6 +122,7 @@ function main() { wave.getState().submitDelta(delta) } }); + /// svgCanvas.bind("cleared", function(){ //alert("cleared") var state = {}, keys = wave.getState().getKeys() @@ -119,12 +131,33 @@ function main() { } wave.getState().submitDelta(state) }); + //*/ svgCanvas.bind("getid", getId); }) } -gadgets.util.registerOnLoadHandler(main); +window.wave={ +_state: {}, +callback: function(){}, +state: { +get: function(r){return wave._state[r]}, +set: function(r,v){wave._state[r]=v}, +getKeys: function(){var a=[];for(var i in wave._state)a.push(i);return a}, +submitDelta: function(d){for(var i in d)wave.state.set(i,d[i]);wave.callback()} +}, +viewer: { +id: "local@example.com", +getId: function(){return wave.viewer.id} +}, +getState:function(){return wave.state}, +getViewer:function(){return wave.viewer}, +isInWaveContainer:function(){return true}, +setStateCallback:function(c){wave.callback=c} +} +if(window.gadgets) gadgets.util.registerOnLoadHandler(main); + +$(main) //and why not use my stuff? function SHA256(b){function h(j,k){return(j>>e)+(k>>e)+((p=(j&o)+(k&o))>>e)<>>k|j<<32-k}var g=[],d,c=3,l=[2],p,i,q,a,m=[],n=[];i=b.length*8;for(var e=16,o=65535,r="";c<312;c++){for(d=l.length;d--&&c%l[d]!=0;);d<0&&l.push(c)}b+="\u0080";for(c=0;c<=i;c+=8)n[c>>5]|=(b.charCodeAt(c/8)&255)<<24-c%32;n[(i+64>>9<<4)+15]=i;for(c=8;c--;)m[c]=parseInt(Math.pow(l[c],0.5).toString(e).substr(2,8),e);for(c=0;c>>10,g[b-7]),f(g[b-15],7)^f(g[b-15],18)^g[b-15]>>>3),g[b-e]);i=h(h(h(h(a[7],f(a[4],6)^f(a[4],11)^f(a[4],25)),a[4]&a[5]^~a[4]&a[6]),parseInt(Math.pow(l[b],1/3).toString(e).substr(2,8),e)),g[b]);q=(f(a[0],2)^f(a[0],13)^f(a[0],22))+(a[0]&a[1]^a[0]&a[2]^a[1]&a[2]);for(d=8;--d;)a[d]=d==4?h(a[3],i):a[d-1];a[0]=h(i,q)}for(d=8;d--;)m[d]+=a[d]}for(c=0;c<8;c++)for(b=8;b--;)r+=(m[c]>>>b*4&15).toString(e);return r}