embedapi.js 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  1. /*
  2. function embedded_svg_edit(frame){
  3. //initialize communication
  4. this.frame = frame;
  5. this.stack = []; //callback stack
  6. var editapi = this;
  7. window.addEventListener("message", function(e){
  8. if(e.data.substr(0,5) == "ERROR"){
  9. editapi.stack.splice(0,1)[0](e.data,"error")
  10. }else{
  11. editapi.stack.splice(0,1)[0](e.data)
  12. }
  13. }, false)
  14. }
  15. embedded_svg_edit.prototype.call = function(code, callback){
  16. this.stack.push(callback);
  17. this.frame.contentWindow.postMessage(code,"*");
  18. }
  19. embedded_svg_edit.prototype.getSvgString = function(callback){
  20. this.call("svgCanvas.getSvgString()",callback)
  21. }
  22. embedded_svg_edit.prototype.setSvgString = function(svg){
  23. this.call("svgCanvas.setSvgString('"+svg.replace(/'/g, "\\'")+"')");
  24. }
  25. */
  26. /*
  27. Embedded SVG-edit API
  28. General usage:
  29. - Have an iframe somewhere pointing to a version of svg-edit > r1000
  30. - Initialize the magic with:
  31. var svgCanvas = new embedded_svg_edit(window.frames['svgedit']);
  32. - Pass functions in this format:
  33. svgCanvas.setSvgString("string")
  34. - Or if a callback is needed:
  35. svgCanvas.setSvgString("string")(function(data, error){
  36. if(error){
  37. //there was an error
  38. }else{
  39. //handle data
  40. }
  41. })
  42. Everything is done with the same API as the real svg-edit,
  43. and all documentation is unchanged. The only difference is
  44. when handling returns, the callback notation is used instead.
  45. var blah = new embedded_svg_edit(window.frames['svgedit']);
  46. blah.clearSelection("woot","blah",1337,[1,2,3,4,5,"moo"],-42,{a: "tree",b:6, c: 9})(function(){console.log("GET DATA",arguments)})
  47. */
  48. function embedded_svg_edit(frame){
  49. //initialize communication
  50. this.frame = frame;
  51. //this.stack = [] //callback stack
  52. this.callbacks = {}; //successor to stack
  53. this.encode = embedded_svg_edit.encode;
  54. //List of functions extracted with this:
  55. //Run in firebug on http://svg-edit.googlecode.com/svn/trunk/docs/files/svgcanvas-js.html
  56. //for(var i=0,q=[],f = document.querySelectorAll("div.CFunction h3.CTitle a");i<f.length;i++){q.push(f[i].name)};q
  57. //var functions = ["clearSelection", "addToSelection", "removeFromSelection", "open", "save", "getSvgString", "setSvgString",
  58. //"createLayer", "deleteCurrentLayer", "setCurrentLayer", "renameCurrentLayer", "setCurrentLayerPosition", "setLayerVisibility",
  59. //"moveSelectedToLayer", "clear"];
  60. //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
  61. //var l=[];for(var i in svgCanvas){if(typeof svgCanvas[i] == "function"){l.push(i)}};
  62. //run in svgedit itself
  63. var functions = ["updateElementFromJson", "embedImage", "fixOperaXML", "clearSelection", "addToSelection",
  64. "removeFromSelection", "addNodeToSelection", "open", "save", "getSvgString", "setSvgString", "createLayer",
  65. "deleteCurrentLayer", "getCurrentDrawing", "setCurrentLayer", "renameCurrentLayer", "setCurrentLayerPosition",
  66. "setLayerVisibility", "moveSelectedToLayer", "clear", "clearPath", "getNodePoint", "clonePathNode", "deletePathNode",
  67. "getResolution", "getImageTitle", "setImageTitle", "setResolution", "setBBoxZoom", "setZoom", "getMode", "setMode",
  68. "getStrokeColor", "setStrokeColor", "getFillColor", "setFillColor", "setStrokePaint", "setFillPaint", "getStrokeWidth",
  69. "setStrokeWidth", "getStrokeStyle", "setStrokeStyle", "getOpacity", "setOpacity", "getFillOpacity", "setFillOpacity",
  70. "getStrokeOpacity", "setStrokeOpacity", "getTransformList", "getBBox", "getRotationAngle", "setRotationAngle", "each",
  71. "bind", "setIdPrefix", "getBold", "setBold", "getItalic", "setItalic", "getFontFamily", "setFontFamily", "getFontSize",
  72. "setFontSize", "getText", "setTextContent", "setImageURL", "setRectRadius", "setSegType", "quickClone",
  73. "changeSelectedAttributeNoUndo", "changeSelectedAttribute", "deleteSelectedElements", "groupSelectedElements",
  74. "ungroupSelectedElement", "moveToTopSelectedElement", "moveToBottomSelectedElement", "moveSelectedElements",
  75. "getStrokedBBox", "getVisibleElements", "cycleElement", "getUndoStackSize", "getRedoStackSize", "getNextUndoCommandText",
  76. "getNextRedoCommandText", "undo", "redo", "cloneSelectedElements", "alignSelectedElements", "getZoom", "getVersion",
  77. "setIconSize", "setLang", "setCustomHandlers"];
  78. //TODO: rewrite the following, it's pretty scary.
  79. for(var i = 0; i < functions.length; i++){
  80. this[functions[i]] = (function(d){
  81. return function(){
  82. var t = this //new callback
  83. for(var g = 0, args = []; g < arguments.length; g++){
  84. args.push(arguments[g]);
  85. }
  86. var cbid = t.send(d,args, function(){}) //the callback (currently it's nothing, but will be set later
  87. return function(newcallback){
  88. t.callbacks[cbid] = newcallback; //set callback
  89. }
  90. }
  91. })(functions[i])
  92. }
  93. //TODO: use AddEvent for Trident browsers, currently they dont support SVG, but they do support onmessage
  94. var t = this;
  95. window.addEventListener("message", function(e){
  96. if(e.data.substr(0,4)=="SVGe"){ //because svg-edit is too longish
  97. var data = e.data.substr(4);
  98. var cbid = data.substr(0, data.indexOf(";"));
  99. if(t.callbacks[cbid]){
  100. if(data.substr(0,6) != "error:"){
  101. t.callbacks[cbid](eval("("+data.substr(cbid.length+1)+")"))
  102. }else{
  103. t.callbacks[cbid](data, "error");
  104. }
  105. }
  106. }
  107. //this.stack.shift()[0](e.data,e.data.substr(0,5) == "ERROR"?'error':null) //replace with shift
  108. }, false)
  109. }
  110. embedded_svg_edit.encode = function(obj){
  111. //simple partial JSON encoder implementation
  112. if(window.JSON && JSON.stringify) return JSON.stringify(obj);
  113. var enc = arguments.callee; //for purposes of recursion
  114. if(typeof obj == "boolean" || typeof obj == "number"){
  115. return obj+'' //should work...
  116. }else if(typeof obj == "string"){
  117. //a large portion of this is stolen from Douglas Crockford's json2.js
  118. return '"'+
  119. obj.replace(
  120. /[\\\"\x00-\x1f\x7f-\x9f\u00ad\u0600-\u0604\u070f\u17b4\u17b5\u200c-\u200f\u2028-\u202f\u2060-\u206f\ufeff\ufff0-\uffff]/g
  121. , function (a) {
  122. return '\\u' + ('0000' + a.charCodeAt(0).toString(16)).slice(-4);
  123. })
  124. +'"'; //note that this isn't quite as purtyful as the usualness
  125. }else if(obj.length){ //simple hackish test for arrayish-ness
  126. for(var i = 0; i < obj.length; i++){
  127. obj[i] = enc(obj[i]); //encode every sub-thingy on top
  128. }
  129. return "["+obj.join(",")+"]";
  130. }else{
  131. var pairs = []; //pairs will be stored here
  132. for(var k in obj){ //loop through thingys
  133. pairs.push(enc(k)+":"+enc(obj[k])); //key: value
  134. }
  135. return "{"+pairs.join(",")+"}" //wrap in the braces
  136. }
  137. }
  138. embedded_svg_edit.prototype.send = function(name, args, callback){
  139. var cbid = Math.floor(Math.random()*31776352877+993577).toString();
  140. //this.stack.push(callback);
  141. this.callbacks[cbid] = callback;
  142. for(var argstr = [], i = 0; i < args.length; i++){
  143. argstr.push(this.encode(args[i]))
  144. }
  145. var t = this;
  146. setTimeout(function(){//delay for the callback to be set in case its synchronous
  147. t.frame.contentWindow.postMessage(cbid+";svgCanvas['"+name+"']("+argstr.join(",")+")","*");
  148. }, 0);
  149. return cbid;
  150. //this.stack.shift()("svgCanvas['"+name+"']("+argstr.join(",")+")")
  151. }