2010-01-12 20:39:03 +00:00
/ *
2009-11-22 20:33:12 +00:00
function embedded _svg _edit ( frame ) {
//initialize communication
this . frame = frame ;
this . stack = [ ] ; //callback stack
2013-02-23 17:37:05 +00:00
2009-11-22 20:33:12 +00:00
var editapi = this ;
2013-02-23 17:37:05 +00:00
2009-11-22 20:33:12 +00:00
window . addEventListener ( "message" , function ( e ) {
if ( e . data . substr ( 0 , 5 ) == "ERROR" ) {
editapi . stack . splice ( 0 , 1 ) [ 0 ] ( e . data , "error" )
} else {
editapi . stack . splice ( 0 , 1 ) [ 0 ] ( e . data )
}
} , false )
}
embedded _svg _edit . prototype . call = function ( code , callback ) {
this . stack . push ( callback ) ;
this . frame . contentWindow . postMessage ( code , "*" ) ;
}
embedded _svg _edit . prototype . getSvgString = function ( callback ) {
2013-02-15 16:51:48 +00:00
this . call ( "svgCanvas.getSvgString()" , callback )
2009-11-22 20:33:12 +00:00
}
embedded _svg _edit . prototype . setSvgString = function ( svg ) {
this . call ( "svgCanvas.setSvgString('" + svg . replace ( /'/g , "\\'" ) + "')" ) ;
}
* /
2009-11-22 20:13:06 +00:00
/ *
Embedded SVG - edit API
General usage :
- Have an iframe somewhere pointing to a version of svg - edit > r1000
- Initialize the magic with :
var svgCanvas = new embedded _svg _edit ( window . frames [ 'svgedit' ] ) ;
- Pass functions in this format :
svgCanvas . setSvgString ( "string" )
- Or if a callback is needed :
svgCanvas . setSvgString ( "string" ) ( function ( data , error ) {
if ( error ) {
//there was an error
} else {
//handle data
}
} )
2013-02-23 17:37:05 +00:00
Everything is done with the same API as the real svg - edit ,
2009-11-22 20:13:06 +00:00
and all documentation is unchanged . The only difference is
2013-02-23 17:37:05 +00:00
when handling returns , the callback notation is used instead .
2009-11-22 20:13:06 +00:00
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 ) } )
* /
function embedded _svg _edit ( frame ) {
//initialize communication
this . frame = frame ;
//this.stack = [] //callback stack
this . callbacks = { } ; //successor to stack
//List of functions extracted with this:
//Run in firebug on http://svg-edit.googlecode.com/svn/trunk/docs/files/svgcanvas-js.html
2013-02-23 17:37:05 +00:00
2009-11-22 20:13:06 +00:00
//for(var i=0,q=[],f = document.querySelectorAll("div.CFunction h3.CTitle a");i<f.length;i++){q.push(f[i].name)};q
2011-01-30 17:11:56 +00:00
//var functions = ["clearSelection", "addToSelection", "removeFromSelection", "open", "save", "getSvgString", "setSvgString",
//"createLayer", "deleteCurrentLayer", "setCurrentLayer", "renameCurrentLayer", "setCurrentLayerPosition", "setLayerVisibility",
//"moveSelectedToLayer", "clear"];
2013-02-23 17:37:05 +00:00
2009-11-22 20:13:06 +00:00
//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)}};
//run in svgedit itself
2011-01-30 17:11:56 +00:00
var functions = [ "updateElementFromJson" , "embedImage" , "fixOperaXML" , "clearSelection" , "addToSelection" ,
"removeFromSelection" , "addNodeToSelection" , "open" , "save" , "getSvgString" , "setSvgString" , "createLayer" ,
"deleteCurrentLayer" , "getCurrentDrawing" , "setCurrentLayer" , "renameCurrentLayer" , "setCurrentLayerPosition" ,
"setLayerVisibility" , "moveSelectedToLayer" , "clear" , "clearPath" , "getNodePoint" , "clonePathNode" , "deletePathNode" ,
"getResolution" , "getImageTitle" , "setImageTitle" , "setResolution" , "setBBoxZoom" , "setZoom" , "getMode" , "setMode" ,
"getStrokeColor" , "setStrokeColor" , "getFillColor" , "setFillColor" , "setStrokePaint" , "setFillPaint" , "getStrokeWidth" ,
"setStrokeWidth" , "getStrokeStyle" , "setStrokeStyle" , "getOpacity" , "setOpacity" , "getFillOpacity" , "setFillOpacity" ,
"getStrokeOpacity" , "setStrokeOpacity" , "getTransformList" , "getBBox" , "getRotationAngle" , "setRotationAngle" , "each" ,
"bind" , "setIdPrefix" , "getBold" , "setBold" , "getItalic" , "setItalic" , "getFontFamily" , "setFontFamily" , "getFontSize" ,
"setFontSize" , "getText" , "setTextContent" , "setImageURL" , "setRectRadius" , "setSegType" , "quickClone" ,
2012-11-28 04:32:59 +00:00
"changeSelectedAttributeNoUndo" , "changeSelectedAttribute" , "deleteSelectedElements" , "groupSelectedElements" , "zoomChanged" ,
2011-01-30 17:11:56 +00:00
"ungroupSelectedElement" , "moveToTopSelectedElement" , "moveToBottomSelectedElement" , "moveSelectedElements" ,
"getStrokedBBox" , "getVisibleElements" , "cycleElement" , "getUndoStackSize" , "getRedoStackSize" , "getNextUndoCommandText" ,
"getNextRedoCommandText" , "undo" , "redo" , "cloneSelectedElements" , "alignSelectedElements" , "getZoom" , "getVersion" ,
"setIconSize" , "setLang" , "setCustomHandlers" ] ;
2013-02-23 17:37:05 +00:00
2009-11-22 20:13:06 +00:00
//TODO: rewrite the following, it's pretty scary.
for ( var i = 0 ; i < functions . length ; i ++ ) {
this [ functions [ i ] ] = ( function ( d ) {
return function ( ) {
2013-02-23 17:37:05 +00:00
var t = this ; //new callback
2009-11-22 20:13:06 +00:00
for ( var g = 0 , args = [ ] ; g < arguments . length ; g ++ ) {
args . push ( arguments [ g ] ) ;
}
2013-02-23 17:37:05 +00:00
var cbid = t . send ( d , args , function ( ) { } ) ; //the callback (currently it's nothing, but will be set later
2009-11-22 20:13:06 +00:00
return function ( newcallback ) {
2009-11-22 20:33:12 +00:00
t . callbacks [ cbid ] = newcallback ; //set callback
2013-02-23 17:37:05 +00:00
} ;
} ;
} ) ( functions [ i ] ) ;
2009-11-22 20:13:06 +00:00
}
//TODO: use AddEvent for Trident browsers, currently they dont support SVG, but they do support onmessage
var t = this ;
window . addEventListener ( "message" , function ( e ) {
2013-02-23 17:37:05 +00:00
if ( e . data . substr ( 0 , 4 ) == "SVGe" ) { //because svg-edit is too longish
2009-11-22 20:13:06 +00:00
var data = e . data . substr ( 4 ) ;
2009-11-22 20:33:12 +00:00
var cbid = data . substr ( 0 , data . indexOf ( ";" ) ) ;
2009-11-22 20:13:06 +00:00
if ( t . callbacks [ cbid ] ) {
2012-11-28 04:32:59 +00:00
if ( data . substr ( cbid . length + 1 , 6 ) != "error:" ) {
2013-02-23 17:37:05 +00:00
t . callbacks [ cbid ] ( eval ( "(" + data . substr ( cbid . length + 1 ) + ")" ) ) ;
2009-11-22 20:13:06 +00:00
} else {
t . callbacks [ cbid ] ( data , "error" ) ;
}
}
}
//this.stack.shift()[0](e.data,e.data.substr(0,5) == "ERROR"?'error':null) //replace with shift
2013-02-23 17:37:05 +00:00
} , false ) ;
2009-11-22 20:13:06 +00:00
}
embedded _svg _edit . prototype . send = function ( name , args , callback ) {
var cbid = Math . floor ( Math . random ( ) * 31776352877 + 993577 ) . toString ( ) ;
//this.stack.push(callback);
this . callbacks [ cbid ] = callback ;
for ( var argstr = [ ] , i = 0 ; i < args . length ; i ++ ) {
2013-02-23 17:37:05 +00:00
argstr . push ( JSON . stringify ( args [ i ] ) ) ;
2009-11-22 20:13:06 +00:00
}
2009-11-22 20:33:12 +00:00
var t = this ;
setTimeout ( function ( ) { //delay for the callback to be set in case its synchronous
t . frame . contentWindow . postMessage ( cbid + ";svgCanvas['" + name + "'](" + argstr . join ( "," ) + ")" , "*" ) ;
} , 0 ) ;
return cbid ;
2009-11-22 20:13:06 +00:00
//this.stack.shift()("svgCanvas['"+name+"']("+argstr.join(",")+")")
2013-02-23 17:37:05 +00:00
} ;