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",
|
---|
70 | //"createLayer", "deleteCurrentLayer", "setCurrentLayer", "renameCurrentLayer", "setCurrentLayerPosition", "setLayerVisibility",
|
---|
71 | //"moveSelectedToLayer", "clear"];
|
---|
72 |
|
---|
73 |
|
---|
74 | //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
|
---|
75 | //var l=[];for(var i in svgCanvas){if(typeof svgCanvas[i] == "function"){l.push(i)}};
|
---|
76 | //run in svgedit itself
|
---|
77 | var functions = ["updateElementFromJson", "embedImage", "fixOperaXML", "clearSelection", "addToSelection",
|
---|
78 | "removeFromSelection", "addNodeToSelection", "open", "save", "getSvgString", "setSvgString", "createLayer",
|
---|
79 | "deleteCurrentLayer", "getCurrentDrawing", "setCurrentLayer", "renameCurrentLayer", "setCurrentLayerPosition",
|
---|
80 | "setLayerVisibility", "moveSelectedToLayer", "clear", "clearPath", "getNodePoint", "clonePathNode", "deletePathNode",
|
---|
81 | "getResolution", "getImageTitle", "setImageTitle", "setResolution", "setBBoxZoom", "setZoom", "getMode", "setMode",
|
---|
82 | "getStrokeColor", "setStrokeColor", "getFillColor", "setFillColor", "setStrokePaint", "setFillPaint", "getStrokeWidth",
|
---|
83 | "setStrokeWidth", "getStrokeStyle", "setStrokeStyle", "getOpacity", "setOpacity", "getFillOpacity", "setFillOpacity",
|
---|
84 | "getStrokeOpacity", "setStrokeOpacity", "getTransformList", "getBBox", "getRotationAngle", "setRotationAngle", "each",
|
---|
85 | "bind", "setIdPrefix", "getBold", "setBold", "getItalic", "setItalic", "getFontFamily", "setFontFamily", "getFontSize",
|
---|
86 | "setFontSize", "getText", "setTextContent", "setImageURL", "setRectRadius", "setSegType", "quickClone",
|
---|
87 | "changeSelectedAttributeNoUndo", "changeSelectedAttribute", "deleteSelectedElements", "groupSelectedElements",
|
---|
88 | "ungroupSelectedElement", "moveToTopSelectedElement", "moveToBottomSelectedElement", "moveSelectedElements",
|
---|
89 | "getStrokedBBox", "getVisibleElements", "cycleElement", "getUndoStackSize", "getRedoStackSize", "getNextUndoCommandText",
|
---|
90 | "getNextRedoCommandText", "undo", "redo", "cloneSelectedElements", "alignSelectedElements", "getZoom", "getVersion",
|
---|
91 | "setIconSize", "setLang", "setCustomHandlers"];
|
---|
92 |
|
---|
93 | //TODO: rewrite the following, it's pretty scary.
|
---|
94 | for(var i = 0; i < functions.length; i++){
|
---|
95 | this[functions[i]] = (function(d){
|
---|
96 | return function(){
|
---|
97 | var t = this //new callback
|
---|
98 | for(var g = 0, args = []; g < arguments.length; g++){
|
---|
99 | args.push(arguments[g]);
|
---|
100 | }
|
---|
101 | var cbid = t.send(d,args, function(){}) //the callback (currently it's nothing, but will be set later
|
---|
102 |
|
---|
103 | return function(newcallback){
|
---|
104 | t.callbacks[cbid] = newcallback; //set callback
|
---|
105 | }
|
---|
106 | }
|
---|
107 | })(functions[i])
|
---|
108 | }
|
---|
109 | //TODO: use AddEvent for Trident browsers, currently they dont support SVG, but they do support onmessage
|
---|
110 | var t = this;
|
---|
111 | window.addEventListener("message", function(e){
|
---|
112 | if(e.data.substr(0,4)=="SVGe"){ //because svg-edit is too longish
|
---|
113 | var data = e.data.substr(4);
|
---|
114 | var cbid = data.substr(0, data.indexOf(";"));
|
---|
115 | if(t.callbacks[cbid]){
|
---|
116 | if(data.substr(0,6) != "error:"){
|
---|
117 | t.callbacks[cbid](eval("("+data.substr(cbid.length+1)+")"))
|
---|
118 | }else{
|
---|
119 | t.callbacks[cbid](data, "error");
|
---|
120 | }
|
---|
121 | }
|
---|
122 | }
|
---|
123 | //this.stack.shift()[0](e.data,e.data.substr(0,5) == "ERROR"?'error':null) //replace with shift
|
---|
124 | }, false)
|
---|
125 | }
|
---|
126 |
|
---|
127 | embedded_svg_edit.encode = function(obj){
|
---|
128 | //simple partial JSON encoder implementation
|
---|
129 | if(window.JSON && JSON.stringify) return JSON.stringify(obj);
|
---|
130 | var enc = arguments.callee; //for purposes of recursion
|
---|
131 |
|
---|
132 | if(typeof obj == "boolean" || typeof obj == "number"){
|
---|
133 | return obj+'' //should work...
|
---|
134 | }else if(typeof obj == "string"){
|
---|
135 | //a large portion of this is stolen from Douglas Crockford's json2.js
|
---|
136 | return '"'+
|
---|
137 | obj.replace(
|
---|
138 | /[\\\"\x00-\x1f\x7f-\x9f\u00ad\u0600-\u0604\u070f\u17b4\u17b5\u200c-\u200f\u2028-\u202f\u2060-\u206f\ufeff\ufff0-\uffff]/g
|
---|
139 | , function (a) {
|
---|
140 | return '\\u' + ('0000' + a.charCodeAt(0).toString(16)).slice(-4);
|
---|
141 | })
|
---|
142 | +'"'; //note that this isn't quite as purtyful as the usualness
|
---|
143 | }else if(obj.length){ //simple hackish test for arrayish-ness
|
---|
144 | for(var i = 0; i < obj.length; i++){
|
---|
145 | obj[i] = enc(obj[i]); //encode every sub-thingy on top
|
---|
146 | }
|
---|
147 | return "["+obj.join(",")+"]";
|
---|
148 | }else{
|
---|
149 | var pairs = []; //pairs will be stored here
|
---|
150 | for(var k in obj){ //loop through thingys
|
---|
151 | pairs.push(enc(k)+":"+enc(obj[k])); //key: value
|
---|
152 | }
|
---|
153 | return "{"+pairs.join(",")+"}" //wrap in the braces
|
---|
154 | }
|
---|
155 | }
|
---|
156 |
|
---|
157 | embedded_svg_edit.prototype.send = function(name, args, callback){
|
---|
158 | var cbid = Math.floor(Math.random()*31776352877+993577).toString();
|
---|
159 | //this.stack.push(callback);
|
---|
160 | this.callbacks[cbid] = callback;
|
---|
161 | for(var argstr = [], i = 0; i < args.length; i++){
|
---|
162 | argstr.push(this.encode(args[i]))
|
---|
163 | }
|
---|
164 | var t = this;
|
---|
165 | setTimeout(function(){//delay for the callback to be set in case its synchronous
|
---|
166 | t.frame.contentWindow.postMessage(cbid+";svgCanvas['"+name+"']("+argstr.join(",")+")","*");
|
---|
167 | }, 0);
|
---|
168 | return cbid;
|
---|
169 | //this.stack.shift()("svgCanvas['"+name+"']("+argstr.join(",")+")")
|
---|
170 | }
|
---|
171 |
|
---|
172 |
|
---|
173 |
|
---|