Attachment 'embedapi.js'
Download 1 /*
2 function embedded_svg_edit(frame){
3 //initialize communication
4 this.frame = frame;
5 this.stack = []; //callback stack
6
7 var editapi = this;
8
9 window.addEventListener("message", function(e){
10 if(e.data.substr(0,5) == "ERROR"){
11 editapi.stack.splice(0,1)[0](e.data,"error")
12 }else{
13 editapi.stack.splice(0,1)[0](e.data)
14 }
15 }, false)
16 }
17
18 embedded_svg_edit.prototype.call = function(code, callback){
19 this.stack.push(callback);
20 this.frame.contentWindow.postMessage(code,"*");
21 }
22
23 embedded_svg_edit.prototype.getSvgString = function(callback){
24 this.call("svgCanvas.getSvgString()",callback)
25 }
26
27 embedded_svg_edit.prototype.setSvgString = function(svg){
28 this.call("svgCanvas.setSvgString('"+svg.replace(/'/g, "\\'")+"')");
29 }
30 */
31
32
33 /*
34 Embedded SVG-edit API
35
36 General usage:
37 - Have an iframe somewhere pointing to a version of svg-edit > r1000
38 - Initialize the magic with:
39 var svgCanvas = new embedded_svg_edit(window.frames['svgedit']);
40 - Pass functions in this format:
41 svgCanvas.setSvgString("string")
42 - Or if a callback is needed:
43 svgCanvas.setSvgString("string")(function(data, error){
44 if(error){
45 //there was an error
46 }else{
47 //handle data
48 }
49 })
50
51 Everything is done with the same API as the real svg-edit,
52 and all documentation is unchanged. The only difference is
53 when handling returns, the callback notation is used instead.
54
55 var blah = new embedded_svg_edit(window.frames['svgedit']);
56 blah.clearSelection("woot","blah",1337,[1,2,3,4,5,"moo"],-42,{a: "tree",b:6, c: 9})(function(){console.log("GET DATA",arguments)})
57 */
58
59 function embedded_svg_edit(frame){
60 //initialize communication
61 this.frame = frame;
62 //this.stack = [] //callback stack
63 this.callbacks = {}; //successor to stack
64 this.encode = embedded_svg_edit.encode;
65 //List of functions extracted with this:
66 //Run in firebug on http://svg-edit.googlecode.com/svn/trunk/docs/files/svgcanvas-js.html
67
68 //for(var i=0,q=[],f = document.querySelectorAll("div.CFunction h3.CTitle a");i<f.length;i++){q.push(f[i].name)};q
69 //var functions = ["clearSelection", "addToSelection", "removeFromSelection", "open", "save", "getSvgString", "setSvgString", "createLayer", "deleteCurrentLayer", "getNumLayers", "getLayer", "getCurrentLayer", "setCurrentLayer", "renameCurrentLayer", "setCurrentLayerPosition", "getLayerVisibility", "setLayerVisibility", "moveSelectedToLayer", "getLayerOpacity", "setLayerOpacity", "clear"];
70
71
72 //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
73 //var l=[];for(var i in svgCanvas){if(typeof svgCanvas[i] == "function"){l.push(i)}};
74 //run in svgedit itself
75 var functions = ["updateElementFromJson", "embedImage", "fixOperaXML", "clearSelection", "addToSelection", "removeFromSelection", "addNodeToSelection", "open", "save", "getSvgString", "setSvgString", "createLayer", "deleteCurrentLayer", "getNumLayers", "getLayer", "getCurrentLayer", "setCurrentLayer", "renameCurrentLayer", "setCurrentLayerPosition", "getLayerVisibility", "setLayerVisibility", "moveSelectedToLayer", "getLayerOpacity", "setLayerOpacity", "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", "beginUndoableChange", "changeSelectedAttributeNoUndo", "finishUndoableChange", "changeSelectedAttribute", "deleteSelectedElements", "groupSelectedElements", "ungroupSelectedElement", "moveToTopSelectedElement", "moveToBottomSelectedElement", "moveSelectedElements", "getStrokedBBox", "getVisibleElements", "cycleElement", "getUndoStackSize", "getRedoStackSize", "getNextUndoCommandText", "getNextRedoCommandText", "undo", "redo", "cloneSelectedElements", "alignSelectedElements", "getZoom", "getVersion", "setIconSize", "setLang", "setCustomHandlers"]
76
77 //TODO: rewrite the following, it's pretty scary.
78 for(var i = 0; i < functions.length; i++){
79 this[functions[i]] = (function(d){
80 return function(){
81 var t = this //new callback
82 for(var g = 0, args = []; g < arguments.length; g++){
83 args.push(arguments[g]);
84 }
85 var cbid = t.send(d,args, function(){}) //the callback (currently it's nothing, but will be set later
86
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
111 embedded_svg_edit.encode = function(obj){
112 //simple partial JSON encoder implementation
113 if(window.JSON && JSON.stringify) return JSON.stringify(obj);
114 var enc = arguments.callee; //for purposes of recursion
115
116 if(typeof obj == "boolean" || typeof obj == "number"){
117 return obj+'' //should work...
118 }else if(typeof obj == "string"){
119 //a large portion of this is stolen from Douglas Crockford's json2.js
120 return '"'+
121 obj.replace(
122 /[\\\"\x00-\x1f\x7f-\x9f\u00ad\u0600-\u0604\u070f\u17b4\u17b5\u200c-\u200f\u2028-\u202f\u2060-\u206f\ufeff\ufff0-\uffff]/g
123 , function (a) {
124 return '\\u' + ('0000' + a.charCodeAt(0).toString(16)).slice(-4);
125 })
126 +'"'; //note that this isn't quite as purtyful as the usualness
127 }else if(obj.length){ //simple hackish test for arrayish-ness
128 for(var i = 0; i < obj.length; i++){
129 obj[i] = enc(obj[i]); //encode every sub-thingy on top
130 }
131 return "["+obj.join(",")+"]";
132 }else{
133 var pairs = []; //pairs will be stored here
134 for(var k in obj){ //loop through thingys
135 pairs.push(enc(k)+":"+enc(obj[k])); //key: value
136 }
137 return "{"+pairs.join(",")+"}" //wrap in the braces
138 }
139 }
140
141 embedded_svg_edit.prototype.send = function(name, args, callback){
142 var cbid = Math.floor(Math.random()*31776352877+993577).toString();
143 //this.stack.push(callback);
144 this.callbacks[cbid] = callback;
145 for(var argstr = [], i = 0; i < args.length; i++){
146 argstr.push(this.encode(args[i]))
147 }
148 var t = this;
149 setTimeout(function(){//delay for the callback to be set in case its synchronous
150 t.frame.contentWindow.postMessage(cbid+";svgCanvas['"+name+"']("+argstr.join(",")+")","*");
151 }, 0);
152 return cbid;
153 //this.stack.shift()("svgCanvas['"+name+"']("+argstr.join(",")+")")
154 }
Attached Files
To refer to attachments on a page, use attachment:filename, as shown below in the list of files. Do NOT use the URL of the [get] link, since this is subject to change and can break easily.You are not allowed to attach a file to this page.