2009-09-02 16:27:59 +00:00
|
|
|
var shapetime = {};
|
2009-09-02 23:10:20 +00:00
|
|
|
var nodelete = false;
|
2009-09-02 16:27:59 +00:00
|
|
|
|
|
|
|
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();
|
|
|
|
var keys = state.getKeys();
|
|
|
|
svgCanvas.each(function(e) {
|
|
|
|
// 'this' is the SVG DOM element node (ellipse, rect, etc)
|
|
|
|
// 'e' is an integer describing the position within the document
|
|
|
|
var k = this.id;
|
|
|
|
var v = state.get(k);
|
|
|
|
if (v) {
|
|
|
|
var ob = JSON.parse(v);
|
|
|
|
if (ob) {
|
|
|
|
// do nothing
|
|
|
|
} else {
|
|
|
|
//var node = document.getElementById(k);
|
|
|
|
//if (node) node.parentNode.removeChild(node);
|
|
|
|
}
|
|
|
|
//keys.remove(k);
|
|
|
|
|
2009-09-02 23:10:20 +00:00
|
|
|
} else if(this.id != "selectorParentGroup" && !nodelete){
|
2009-09-02 16:27:59 +00:00
|
|
|
//console.log(this)
|
2009-09-02 23:10:20 +00:00
|
|
|
|
2009-09-02 16:27:59 +00:00
|
|
|
this.parentNode.removeChild(this);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
// New nodes
|
|
|
|
for (var k in keys) {
|
|
|
|
var v = state.get(keys[k]);
|
|
|
|
var ob = JSON.parse(v);
|
|
|
|
if (ob){
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
if(JSON.stringify(attrs) != JSON.stringify(ob.attr)){
|
|
|
|
shapetime[k] = ob.time
|
|
|
|
svgCanvas.updateElementFromJson(ob)
|
|
|
|
}
|
|
|
|
}else{
|
|
|
|
shapetime[k] = ob.time
|
|
|
|
svgCanvas.updateElementFromJson(ob)
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
function getId(canvas, objnum) {
|
|
|
|
return "svg_"+wave.getViewer().getId()+"_"+objnum;
|
|
|
|
}
|
|
|
|
|
|
|
|
function main() {
|
|
|
|
$(document).ready(function(){
|
|
|
|
if (wave && wave.isInWaveContainer()) {
|
|
|
|
wave.setStateCallback(stateUpdated);
|
|
|
|
}
|
2009-09-02 17:38:51 +00:00
|
|
|
|
|
|
|
var oldchanged = svgCanvas.bind("changed", function(canvas, elem){
|
|
|
|
if(oldchanged)oldchanged.apply(this, [canvas,elem]);
|
|
|
|
|
2009-09-02 16:27:59 +00:00
|
|
|
var delta = {}
|
|
|
|
$.each(elem, function(){
|
|
|
|
|
|
|
|
var attrs = {};
|
|
|
|
var a = this.attributes;
|
|
|
|
if(a){
|
|
|
|
for(var i = a.length; i--;){
|
|
|
|
attrs[a.item(i).nodeName] = a.item(i).nodeValue;
|
|
|
|
}
|
|
|
|
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)
|
|
|
|
});
|
2009-09-02 17:38:51 +00:00
|
|
|
var oldselected = svgCanvas.bind("selected", function(canvas, elem){
|
|
|
|
|
|
|
|
if(oldselected)oldselected.apply(this, [canvas,elem]);
|
|
|
|
|
|
|
|
|
|
|
|
var delta = {}
|
|
|
|
var deletions = 0;
|
|
|
|
$.each(elem, function(){
|
2009-09-02 18:29:35 +00:00
|
|
|
if(!this.parentNode){
|
2009-09-02 17:38:51 +00:00
|
|
|
delta[this.id] = null;
|
|
|
|
|
|
|
|
deletions ++
|
|
|
|
}
|
|
|
|
});
|
|
|
|
if(deletions > 0){
|
|
|
|
wave.getState().submitDelta(delta)
|
|
|
|
}
|
|
|
|
});
|
2009-09-02 16:27:59 +00:00
|
|
|
svgCanvas.bind("cleared", function(){
|
|
|
|
//alert("cleared")
|
|
|
|
var state = {}, keys = wave.getState().getKeys()
|
|
|
|
for(var i = 0; i < keys.length; i++){
|
|
|
|
state[keys[i]] = null;
|
|
|
|
}
|
|
|
|
wave.getState().submitDelta(state)
|
|
|
|
});
|
|
|
|
svgCanvas.bind("getid", getId);
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
gadgets.util.registerOnLoadHandler(main);
|