removed unneeded json_encode / json_decode for 3 reasons:

- it's available natively since IE8 and it's provided by Chrome Frame which is a requirement for old IE
- it's only used for postMessage exchange which is a newer API than JSON
- if ever we would need it, it should be added in an external lib, not inside the editor code itself

git-svn-id: http://svg-edit.googlecode.com/svn/trunk@2462 eee81c28-f429-11dd-99c0-75d572ba1ddd
master
Bruno Heridet 2013-02-23 17:37:05 +00:00
parent cc2d05f125
commit 9f58a680c3
2 changed files with 19 additions and 82 deletions

View File

@ -3,9 +3,9 @@ function embedded_svg_edit(frame){
//initialize communication //initialize communication
this.frame = frame; this.frame = frame;
this.stack = []; //callback stack this.stack = []; //callback stack
var editapi = this; var editapi = this;
window.addEventListener("message", function(e){ window.addEventListener("message", function(e){
if(e.data.substr(0,5) == "ERROR"){ if(e.data.substr(0,5) == "ERROR"){
editapi.stack.splice(0,1)[0](e.data,"error") editapi.stack.splice(0,1)[0](e.data,"error")
@ -48,9 +48,9 @@ svgCanvas.setSvgString("string")(function(data, error){
} }
}) })
Everything is done with the same API as the real svg-edit, Everything is done with the same API as the real svg-edit,
and all documentation is unchanged. The only difference is and all documentation is unchanged. The only difference is
when handling returns, the callback notation is used instead. when handling returns, the callback notation is used instead.
var blah = new embedded_svg_edit(window.frames['svgedit']); var blah = new embedded_svg_edit(window.frames['svgedit']);
blah.clearSelection("woot","blah",1337,[1,2,3,4,5,"moo"],-42,{a: "tree",b:6, c: 9})(function(){console.log("GET DATA",arguments)}) blah.clearSelection("woot","blah",1337,[1,2,3,4,5,"moo"],-42,{a: "tree",b:6, c: 9})(function(){console.log("GET DATA",arguments)})
@ -61,16 +61,14 @@ function embedded_svg_edit(frame){
this.frame = frame; this.frame = frame;
//this.stack = [] //callback stack //this.stack = [] //callback stack
this.callbacks = {}; //successor to stack this.callbacks = {}; //successor to stack
this.encode = embedded_svg_edit.encode;
//List of functions extracted with this: //List of functions extracted with this:
//Run in firebug on http://svg-edit.googlecode.com/svn/trunk/docs/files/svgcanvas-js.html //Run in firebug on http://svg-edit.googlecode.com/svn/trunk/docs/files/svgcanvas-js.html
//for(var i=0,q=[],f = document.querySelectorAll("div.CFunction h3.CTitle a");i<f.length;i++){q.push(f[i].name)};q //for(var i=0,q=[],f = document.querySelectorAll("div.CFunction h3.CTitle a");i<f.length;i++){q.push(f[i].name)};q
//var functions = ["clearSelection", "addToSelection", "removeFromSelection", "open", "save", "getSvgString", "setSvgString", //var functions = ["clearSelection", "addToSelection", "removeFromSelection", "open", "save", "getSvgString", "setSvgString",
//"createLayer", "deleteCurrentLayer", "setCurrentLayer", "renameCurrentLayer", "setCurrentLayerPosition", "setLayerVisibility", //"createLayer", "deleteCurrentLayer", "setCurrentLayer", "renameCurrentLayer", "setCurrentLayerPosition", "setLayerVisibility",
//"moveSelectedToLayer", "clear"]; //"moveSelectedToLayer", "clear"];
//Newer, well, it extracts things that aren't documented as well. All functions accessible through the normal thingy can now be accessed though the API //Newer, well, it extracts things that aren't documented as well. All functions accessible through the normal thingy can now be accessed though the API
//var l=[];for(var i in svgCanvas){if(typeof svgCanvas[i] == "function"){l.push(i)}}; //var l=[];for(var i in svgCanvas){if(typeof svgCanvas[i] == "function"){l.push(i)}};
//run in svgedit itself //run in svgedit itself
@ -89,69 +87,39 @@ function embedded_svg_edit(frame){
"getStrokedBBox", "getVisibleElements", "cycleElement", "getUndoStackSize", "getRedoStackSize", "getNextUndoCommandText", "getStrokedBBox", "getVisibleElements", "cycleElement", "getUndoStackSize", "getRedoStackSize", "getNextUndoCommandText",
"getNextRedoCommandText", "undo", "redo", "cloneSelectedElements", "alignSelectedElements", "getZoom", "getVersion", "getNextRedoCommandText", "undo", "redo", "cloneSelectedElements", "alignSelectedElements", "getZoom", "getVersion",
"setIconSize", "setLang", "setCustomHandlers"]; "setIconSize", "setLang", "setCustomHandlers"];
//TODO: rewrite the following, it's pretty scary. //TODO: rewrite the following, it's pretty scary.
for(var i = 0; i < functions.length; i++){ for(var i = 0; i < functions.length; i++){
this[functions[i]] = (function(d){ this[functions[i]] = (function(d){
return function(){ return function(){
var t = this //new callback var t = this; //new callback
for(var g = 0, args = []; g < arguments.length; g++){ for(var g = 0, args = []; g < arguments.length; g++){
args.push(arguments[g]); args.push(arguments[g]);
} }
var cbid = t.send(d, args, function(){}) //the callback (currently it's nothing, but will be set later var cbid = t.send(d, args, function(){}); //the callback (currently it's nothing, but will be set later
return function(newcallback){ return function(newcallback){
t.callbacks[cbid] = newcallback; //set callback t.callbacks[cbid] = newcallback; //set callback
} };
} };
})(functions[i]) })(functions[i]);
} }
//TODO: use AddEvent for Trident browsers, currently they dont support SVG, but they do support onmessage //TODO: use AddEvent for Trident browsers, currently they dont support SVG, but they do support onmessage
var t = this; var t = this;
window.addEventListener("message", function(e){ window.addEventListener("message", function(e){
if(e.data.substr(0,4)=="SVGe"){ //because svg-edit is too longish if(e.data.substr(0,4) == "SVGe"){ //because svg-edit is too longish
var data = e.data.substr(4); var data = e.data.substr(4);
var cbid = data.substr(0, data.indexOf(";")); var cbid = data.substr(0, data.indexOf(";"));
if(t.callbacks[cbid]){ if(t.callbacks[cbid]){
if(data.substr(cbid.length + 1,6) != "error:"){ if(data.substr(cbid.length + 1,6) != "error:"){
t.callbacks[cbid](eval("("+data.substr(cbid.length+1)+")")) t.callbacks[cbid](eval("("+data.substr(cbid.length+1)+")"));
}else{ }else{
t.callbacks[cbid](data, "error"); t.callbacks[cbid](data, "error");
} }
} }
} }
//this.stack.shift()[0](e.data,e.data.substr(0,5) == "ERROR"?'error':null) //replace with shift //this.stack.shift()[0](e.data,e.data.substr(0,5) == "ERROR"?'error':null) //replace with shift
}, false) }, false);
}
embedded_svg_edit.encode = function(obj){
//simple partial JSON encoder implementation
if(window.JSON && JSON.stringify) return JSON.stringify(obj);
var enc = arguments.callee; //for purposes of recursion
if(typeof obj == "boolean" || typeof obj == "number"){
return obj+'' //should work...
}else if(typeof obj == "string"){
//a large portion of this is stolen from Douglas Crockford's json2.js
return '"'+
obj.replace(
/[\\\"\x00-\x1f\x7f-\x9f\u00ad\u0600-\u0604\u070f\u17b4\u17b5\u200c-\u200f\u2028-\u202f\u2060-\u206f\ufeff\ufff0-\uffff]/g
, function (a) {
return '\\u' + ('0000' + a.charCodeAt(0).toString(16)).slice(-4);
})
+'"'; //note that this isn't quite as purtyful as the usualness
}else if(obj.length){ //simple hackish test for arrayish-ness
for(var i = 0; i < obj.length; i++){
obj[i] = enc(obj[i]); //encode every sub-thingy on top
}
return "["+obj.join(",")+"]";
}else{
var pairs = []; //pairs will be stored here
for(var k in obj){ //loop through thingys
pairs.push(enc(k)+":"+enc(obj[k])); //key: value
}
return "{"+pairs.join(",")+"}" //wrap in the braces
}
} }
embedded_svg_edit.prototype.send = function(name, args, callback){ embedded_svg_edit.prototype.send = function(name, args, callback){
@ -159,7 +127,7 @@ embedded_svg_edit.prototype.send = function(name, args, callback){
//this.stack.push(callback); //this.stack.push(callback);
this.callbacks[cbid] = callback; this.callbacks[cbid] = callback;
for(var argstr = [], i = 0; i < args.length; i++){ for(var argstr = [], i = 0; i < args.length; i++){
argstr.push(this.encode(args[i])) argstr.push(JSON.stringify(args[i]));
} }
var t = this; var t = this;
setTimeout(function(){//delay for the callback to be set in case its synchronous setTimeout(function(){//delay for the callback to be set in case its synchronous
@ -167,7 +135,4 @@ embedded_svg_edit.prototype.send = function(name, args, callback){
}, 0); }, 0);
return cbid; return cbid;
//this.stack.shift()("svgCanvas['"+name+"']("+argstr.join(",")+")") //this.stack.shift()("svgCanvas['"+name+"']("+argstr.join(",")+")")
} };

View File

@ -4588,38 +4588,10 @@
// Callback handler for embedapi.js // Callback handler for embedapi.js
try { try {
var json_encode = function(obj){
//simple partial JSON encoder implementation
if (window.JSON && JSON.stringify) return JSON.stringify(obj);
var enc = arguments.callee; //for purposes of recursion
if (typeof obj == 'boolean' || typeof obj == 'number'){
return obj+''; //should work...
} else if (typeof obj == 'string') {
//a large portion of this is stolen from Douglas Crockford's json2.js
return '"'+
obj.replace(
/[\\\"\x00-\x1f\x7f-\x9f\u00ad\u0600-\u0604\u070f\u17b4\u17b5\u200c-\u200f\u2028-\u202f\u2060-\u206f\ufeff\ufff0-\uffff]/g
, function (a) {
return '\\u' + ('0000' + a.charCodeAt(0).toString(16)).slice(-4);
})
+'"'; //note that this isn't quite as purtyful as the usualness
} else if (obj.length) { //simple hackish test for arrayish-ness
for (var i = 0; i < obj.length; i++){
obj[i] = enc(obj[i]); //encode every sub-thingy on top
}
return '['+obj.join(',')+']';
} else {
var pairs = []; //pairs will be stored here
for (var k in obj){ //loop through thingys
pairs.push(enc(k)+':'+enc(obj[k])); //key: value
}
return '{'+pairs.join(',')+'}'; //wrap in the braces
}
};
window.addEventListener('message', function(e) { window.addEventListener('message', function(e) {
var cbid = parseInt(e.data.substr(0, e.data.indexOf(';')), 10); var cbid = parseInt(e.data.substr(0, e.data.indexOf(';')), 10);
try { try {
e.source.postMessage('SVGe'+cbid+';'+json_encode(eval(e.data)), '*'); e.source.postMessage('SVGe'+cbid+';'+JSON.stringify(eval(e.data)), '*');
} catch(err) { } catch(err) {
e.source.postMessage('SVGe'+cbid+';error:'+err.message, '*'); e.source.postMessage('SVGe'+cbid+';error:'+err.message, '*');
} }