-
Notifications
You must be signed in to change notification settings - Fork 38
/
SVGPaper.js
229 lines (217 loc) · 9.67 KB
/
SVGPaper.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
xui.Class("xui.UI.SVGPaper", "xui.UI.Div",{
Initialize:function(){
this.addTemplateKeys(['SVG']);
},
Instance:{
append:function(target, pre, base){
if(xui.isHash(target) || xui.isStr(target))
target=xui.create(target);
if(target['xui.UIProfile'])target=target.boxing();
var ns=this,f=arguments.callee.upper,isSvg,rendersvg;
target.each(function(prf){
isSvg=!!prf.box['xui.svg'];
if(isSvg&&prf.renderId){
prf.clearCache();
prf.$beforeDestroy["svgClear"]();
delete prf.renderId;
delete prf.rendered;
rendersvg=1;
}
f.call(ns, prf, null, pre, base, isSvg, ns.get(0)&&xui(ns.get(0)._canvas));
if(rendersvg){
prf.box._initAttr2UI(prf);
}
});
return this;
},
getPaper:function(){
return xui.get(this.get(0), ["_paper"]);
},
getSVGString:function(){
var paper = xui.get(this.get(0), ["_paper"]);
return paper?paper.toSVG():"";
}
},
Static:{
DataModel:{
iframeAutoLoad:null,
html:null,
width:{
$spaceunit:1,
ini:'32em'
},
height:{
$spaceunit:1,
ini:'25em'
},
scaleChildren:{
ini:false
},
overflow:{
ini:undefined
},
graphicZIndex:{
ini:0,
action:function(v){
this.getSubNode('SVG').css('zIndex',v);
}
}
},
RenderTrigger:function(){
var profile=this,
root=profile.getRootNode(),
prop=profile.properties,
// force to px
w=xui.CSS.$px(prop.width,root,true),h=xui.CSS.$px(prop.height,root,true);
(profile.$beforeDestroy=(profile.$beforeDestroy||{}))["svgClear"]=function(){
if(profile._paper){
profile._paper.clear();
profile._paper.remove();
}
};
profile._paper=Raphael(profile.$domId, w, h);
profile._canvas=profile._paper.canvas;
profile._canvas.id=profile.box.KEY+"-SVG:"+profile.serialId+":";
var s=profile._canvas.style;
s.position='absolute';
s.left=0;
s.top=0;
s.zIndex=prop.graphicZIndex;
// contents
var a=[];
xui.arr.each(profile.children,function(o){
if(o[0].box["xui.svg"])a.push(o[0]);
});
if(a.length){
profile.boxing().append(xui.svg.pack(a));
// for IE
if(!Raphael.svg){
xui.setTimeout(function(){
if(profile && !profile.destroyed){
// read again in IE
profile.boxing().append(xui.svg.pack(a));
}
});
}
}
if(profile._paper){
xui.setTimeout(function(){
if(profile && !profile.destroyed){
// ensure right position
if(profile.$designerRoot)
profile._frame=profile._paper.rect(0,0,1,1,0).attr({"stroke-width":"0px"});
else if(profile.$inDesign)
profile._frame=profile._paper.rect(0,0,w,h,8).attr({"stroke-dasharray": ". ", stroke: "#666"});
if(profile._frame)profile._frame._decoration=1;
}
});
}
},
_onresize:function(profile,width,height){
var paper=profile._paper, scaleChildren=profile.properties.scaleChildren,ow,oh,
prop=profile,properties,
us = xui.$us(profile),
adjustunit = function(v,emRate){return profile.$forceu(v, us>0?'em':'px', emRate)};
// caculate by px
width=width?profile.$px(width, null, true):width;
height=height?profile.$px(height, null, true):height;
if(scaleChildren){
ow=profile.$px(paper.width,null,true);
oh=profile.$px(paper.height,null,true);
}
if(paper){
var pw=profile.$px(paper.width,null,true), ph=profile.$px(paper.height,null,true);
if( (width && pw!=width) || (height && ph!=height) ){
var args={},node=profile.getSubNode("SVG");
paper.setSize(width,height);
if(profile.$inDesign && profile._frame){
if(width||width===0)
profile._frame.attr('width',width);
if(height||height===0)
profile._frame.attr('height',height);
}
if(!(width||width===0)){
width=pw;
}
args.width=width;
if((height||height===0)){
height=ph;
}
args.height=height;
if((!xui.isEmpty(args)) && xui.Dom.$hasEventHandler(node.get(0),'onsize'))
node.onSize(true, args);
if(scaleChildren){
paper.forEach(function(elem){
var wr=width/ow,hr=height/oh,xuiElem,
fun=function(elem, key) {
var xuiElem=xui.UIProfile.getFromDom(elem.node.id);
if(!xuiElem)return;
var attr=elem.attr(),
hash;
switch(elem.type){
// circle is xuiElem, so dont use cirle in this case
case 'circle':
hash={
cx:attr.cx*wr,
cy:attr.cy*hr,
r:attr.r*(wr+hr)/2
};
break;
case 'ellipse':
hash={
cx:attr.cx*wr,
cy:attr.cy*hr,
rx:attr.rx*wr,
ry:attr.ry*hr
};
break;
case 'rect':
case 'image':
hash={
x:attr.x*wr,
y:attr.y*hr,
width:attr.width*wr,
height:attr.height*hr
};
break;
case 'text':
hash={
x:attr.x*wr,
y:attr.y*hr
};
break;
case 'path':
hash={
path:Raphael.transformPath(xui.isArr(attr.path)?attr.path.join(""):attr.path, "s"+wr+","+hr+",0,0")
};
break;
}
if(hash){
xuiElem.boxing().setAttr(key || "KEY",hash,false,true);
//elem.attr(hash);
}
};
// find root node
if(profile._frame!==elem
&& elem.node.$xid
&& elem.node.id
&& !/^[^:]+-/.test(elem.node.id)
){
if(elem._isGroupFirst){
var xuiElem=xui.UIProfile.getFromDom(elem.node.id);
if(xuiElem){
xuiElem._elset.forEach(function(o){
fun(o, xuiElem.getKey(o.node.id, true));
})
}
}else{
fun(elem);
}
}
});
}
}
}
}
}
});