root / trunk / web / dojo / dojox / gfx / utils.js @ 11
History | View | Annotate | Download (4.53 KB)
1 |
/*
|
---|---|
2 |
Copyright (c) 2004-2010, The Dojo Foundation All Rights Reserved.
|
3 |
Available via Academic Free License >= 2.1 OR the modified BSD license.
|
4 |
see: http://dojotoolkit.org/license for details
|
5 |
*/
|
6 |
|
7 |
|
8 |
if(!dojo._hasResource["dojox.gfx.utils"]){ |
9 |
dojo._hasResource["dojox.gfx.utils"]=true; |
10 |
dojo.provide("dojox.gfx.utils");
|
11 |
dojo.require("dojox.gfx");
|
12 |
(function(){
|
13 |
var d=dojo,g=dojox.gfx,gu=g.utils;
|
14 |
dojo.mixin(gu,{forEach:function(_1,f,o){ |
15 |
o=o||d.global; |
16 |
f.call(o,_1); |
17 |
if(_1 instanceof g.Surface||_1 instanceof g.Group){ |
18 |
d.forEach(_1.children,function(_2){
|
19 |
gu.forEach(_2,f,o); |
20 |
}); |
21 |
} |
22 |
},serialize:function(_3){ |
23 |
var t={},v,_4=_3 instanceof g.Surface; |
24 |
if(_4||_3 instanceof g.Group){ |
25 |
t.children=d.map(_3.children,gu.serialize); |
26 |
if(_4){
|
27 |
return t.children;
|
28 |
} |
29 |
}else{
|
30 |
t.shape=_3.getShape(); |
31 |
} |
32 |
if(_3.getTransform){
|
33 |
v=_3.getTransform(); |
34 |
if(v){
|
35 |
t.transform=v; |
36 |
} |
37 |
} |
38 |
if(_3.getStroke){
|
39 |
v=_3.getStroke(); |
40 |
if(v){
|
41 |
t.stroke=v; |
42 |
} |
43 |
} |
44 |
if(_3.getFill){
|
45 |
v=_3.getFill(); |
46 |
if(v){
|
47 |
t.fill=v; |
48 |
} |
49 |
} |
50 |
if(_3.getFont){
|
51 |
v=_3.getFont(); |
52 |
if(v){
|
53 |
t.font=v; |
54 |
} |
55 |
} |
56 |
return t;
|
57 |
},toJson:function(_5,_6){ |
58 |
return d.toJson(gu.serialize(_5),_6);
|
59 |
},deserialize:function(_7,_8){ |
60 |
if(_8 instanceof Array){ |
61 |
return d.map(_8,d.hitch(null,gu.deserialize,_7)); |
62 |
} |
63 |
var _9=("shape" in _8)?_7.createShape(_8.shape):_7.createGroup(); |
64 |
if("transform" in _8){ |
65 |
_9.setTransform(_8.transform); |
66 |
} |
67 |
if("stroke" in _8){ |
68 |
_9.setStroke(_8.stroke); |
69 |
} |
70 |
if("fill" in _8){ |
71 |
_9.setFill(_8.fill); |
72 |
} |
73 |
if("font" in _8){ |
74 |
_9.setFont(_8.font); |
75 |
} |
76 |
if("children" in _8){ |
77 |
d.forEach(_8.children,d.hitch(null,gu.deserialize,_9));
|
78 |
} |
79 |
return _9;
|
80 |
},fromJson:function(_a,_b){ |
81 |
return gu.deserialize(_a,d.fromJson(_b));
|
82 |
},toSvg:function(_c){ |
83 |
var _d=new dojo.Deferred(); |
84 |
if(dojox.gfx.renderer==="svg"){ |
85 |
try{
|
86 |
var _e=gu._cleanSvg(gu._innerXML(_c.rawNode));
|
87 |
_d.callback(_e); |
88 |
} |
89 |
catch(e){
|
90 |
_d.errback(e); |
91 |
} |
92 |
}else{
|
93 |
if(!gu._initSvgSerializerDeferred){
|
94 |
gu._initSvgSerializer(); |
95 |
} |
96 |
var _f=dojox.gfx.utils.toJson(_c);
|
97 |
var _10=function(){ |
98 |
try{
|
99 |
var _11=_c.getDimensions();
|
100 |
var _12=_11.width;
|
101 |
var _13=_11.height;
|
102 |
var _14=gu._gfxSvgProxy.document.createElement("div"); |
103 |
gu._gfxSvgProxy.document.body.appendChild(_14); |
104 |
dojo.withDoc(gu._gfxSvgProxy.document,function(){
|
105 |
dojo.style(_14,"width",_12);
|
106 |
dojo.style(_14,"height",_13);
|
107 |
},this);
|
108 |
var ts=gu._gfxSvgProxy[dojox._scopeName].gfx.createSurface(_14,_12,_13);
|
109 |
var _15=function(_16){ |
110 |
try{
|
111 |
gu._gfxSvgProxy[dojox._scopeName].gfx.utils.fromJson(_16,_f); |
112 |
var svg=gu._cleanSvg(_14.innerHTML);
|
113 |
_16.clear(); |
114 |
_16.destroy(); |
115 |
gu._gfxSvgProxy.document.body.removeChild(_14); |
116 |
_d.callback(svg); |
117 |
} |
118 |
catch(e){
|
119 |
_d.errback(e); |
120 |
} |
121 |
}; |
122 |
ts.whenLoaded(null,_15);
|
123 |
} |
124 |
catch(ex){
|
125 |
_d.errback(ex); |
126 |
} |
127 |
}; |
128 |
if(gu._initSvgSerializerDeferred.fired>0){ |
129 |
_10(); |
130 |
}else{
|
131 |
gu._initSvgSerializerDeferred.addCallback(_10); |
132 |
} |
133 |
} |
134 |
return _d;
|
135 |
},_gfxSvgProxy:null,_initSvgSerializerDeferred:null,_svgSerializerInitialized:function(){ |
136 |
gu._initSvgSerializerDeferred.callback(true);
|
137 |
},_initSvgSerializer:function(){ |
138 |
if(!gu._initSvgSerializerDeferred){
|
139 |
gu._initSvgSerializerDeferred=new dojo.Deferred();
|
140 |
var f=dojo.doc.createElement("iframe"); |
141 |
dojo.style(f,{display:"none",position:"absolute",width:"1em",height:"1em",top:"-10000px"}); |
142 |
var _17;
|
143 |
if(dojo.isIE){
|
144 |
f.onreadystatechange=function(){ |
145 |
if(f.contentWindow.document.readyState=="complete"){ |
146 |
f.onreadystatechange=function(){ |
147 |
}; |
148 |
_17=setInterval(function(){
|
149 |
if(f.contentWindow[dojo._scopeName]&&f.contentWindow[dojox._scopeName].gfx&&f.contentWindow[dojox._scopeName].gfx.utils){
|
150 |
clearInterval(_17); |
151 |
f.contentWindow.parent[dojox._scopeName].gfx.utils._gfxSvgProxy=f.contentWindow; |
152 |
f.contentWindow.parent[dojox._scopeName].gfx.utils._svgSerializerInitialized(); |
153 |
} |
154 |
},50);
|
155 |
} |
156 |
}; |
157 |
}else{
|
158 |
f.onload=function(){ |
159 |
f.onload=function(){ |
160 |
}; |
161 |
_17=setInterval(function(){
|
162 |
if(f.contentWindow[dojo._scopeName]&&f.contentWindow[dojox._scopeName].gfx&&f.contentWindow[dojox._scopeName].gfx.utils){
|
163 |
clearInterval(_17); |
164 |
f.contentWindow.parent[dojox._scopeName].gfx.utils._gfxSvgProxy=f.contentWindow; |
165 |
f.contentWindow.parent[dojox._scopeName].gfx.utils._svgSerializerInitialized(); |
166 |
} |
167 |
},50);
|
168 |
}; |
169 |
} |
170 |
var uri=(dojo.config["dojoxGfxSvgProxyFrameUrl"]||dojo.moduleUrl("dojox","gfx/resources/gfxSvgProxyFrame.html")); |
171 |
f.setAttribute("src",uri);
|
172 |
dojo.body().appendChild(f); |
173 |
} |
174 |
},_innerXML:function(_18){ |
175 |
if(_18.innerXML){
|
176 |
return _18.innerXML;
|
177 |
}else{
|
178 |
if(_18.xml){
|
179 |
return _18.xml;
|
180 |
}else{
|
181 |
if(typeof XMLSerializer!="undefined"){ |
182 |
return (new XMLSerializer()).serializeToString(_18); |
183 |
} |
184 |
} |
185 |
} |
186 |
return null; |
187 |
},_cleanSvg:function(svg){ |
188 |
if(svg){
|
189 |
if(svg.indexOf("xmlns=\"http://www.w3.org/2000/svg\"")==-1){ |
190 |
svg=svg.substring(4,svg.length);
|
191 |
svg="<svg xmlns=\"http://www.w3.org/2000/svg\""+svg;
|
192 |
} |
193 |
svg=svg.replace(/\bdojoGfx\w*\s*=\s*(['"])\w*\1/g,""); |
194 |
} |
195 |
return svg;
|
196 |
}}); |
197 |
})(); |
198 |
} |