Subversion Repositories Applications.papyrus

Rev

Rev 1422 | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
1318 alexandre_ 1
/*
2
	Copyright (c) 2004-2006, The Dojo Foundation
3
	All Rights Reserved.
4
 
5
	Licensed under the Academic Free License version 2.1 or above OR the
6
	modified BSD license. For more information on Dojo licensing, see:
7
 
8
		http://dojotoolkit.org/community/licensing.shtml
9
*/
10
 
1422 alexandre_ 11
 
12
 
1318 alexandre_ 13
dojo.provide("dojo.svg");
14
dojo.require("dojo.lang.common");
15
dojo.require("dojo.dom");
16
dojo.mixin(dojo.svg, dojo.dom);
17
dojo.svg.graphics = dojo.svg.g = new function (d) {
18
	this.suspend = function () {
19
		try {
20
			d.documentElement.suspendRedraw(0);
21
		}
22
		catch (e) {
23
		}
24
	};
25
	this.resume = function () {
26
		try {
27
			d.documentElement.unsuspendRedraw(0);
28
		}
29
		catch (e) {
30
		}
31
	};
32
	this.force = function () {
33
		try {
34
			d.documentElement.forceRedraw();
35
		}
36
		catch (e) {
37
		}
38
	};
39
}(document);
40
dojo.svg.animations = dojo.svg.anim = new function (d) {
41
	this.arePaused = function () {
42
		try {
43
			return d.documentElement.animationsPaused();
44
		}
45
		catch (e) {
46
			return false;
47
		}
48
	};
49
	this.pause = function () {
50
		try {
51
			d.documentElement.pauseAnimations();
52
		}
53
		catch (e) {
54
		}
55
	};
56
	this.resume = function () {
57
		try {
58
			d.documentElement.unpauseAnimations();
59
		}
60
		catch (e) {
61
		}
62
	};
63
}(document);
64
dojo.svg.toCamelCase = function (selector) {
65
	var arr = selector.split("-"), cc = arr[0];
66
	for (var i = 1; i < arr.length; i++) {
67
		cc += arr[i].charAt(0).toUpperCase() + arr[i].substring(1);
68
	}
69
	return cc;
70
};
71
dojo.svg.toSelectorCase = function (selector) {
72
	return selector.replace(/([A-Z])/g, "-$1").toLowerCase();
73
};
74
dojo.svg.getStyle = function (node, cssSelector) {
75
	return document.defaultView.getComputedStyle(node, cssSelector);
76
};
77
dojo.svg.getNumericStyle = function (node, cssSelector) {
78
	return parseFloat(dojo.svg.getStyle(node, cssSelector));
79
};
80
dojo.svg.getOpacity = function (node) {
81
	return Math.min(1, dojo.svg.getNumericStyle(node, "fill-opacity"));
82
};
83
dojo.svg.setOpacity = function (node, opacity) {
84
	node.setAttributeNS(this.xmlns.svg, "fill-opacity", opacity);
85
	node.setAttributeNS(this.xmlns.svg, "stroke-opacity", opacity);
86
};
87
dojo.svg.clearOpacity = function (node) {
88
	node.setAttributeNS(this.xmlns.svg, "fill-opacity", "1.0");
89
	node.setAttributeNS(this.xmlns.svg, "stroke-opacity", "1.0");
90
};
91
dojo.svg.getCoords = function (node) {
92
	if (node.getBBox) {
93
		var box = node.getBBox();
94
		return {x:box.x, y:box.y};
95
	}
96
	return null;
97
};
98
dojo.svg.setCoords = function (node, coords) {
99
	var p = dojo.svg.getCoords();
100
	if (!p) {
101
		return;
102
	}
103
	var dx = p.x - coords.x;
104
	var dy = p.y - coords.y;
105
	dojo.svg.translate(node, dx, dy);
106
};
107
dojo.svg.getDimensions = function (node) {
108
	if (node.getBBox) {
109
		var box = node.getBBox();
110
		return {width:box.width, height:box.height};
111
	}
112
	return null;
113
};
114
dojo.svg.setDimensions = function (node, dim) {
115
	if (node.width) {
116
		node.width.baseVal.value = dim.width;
117
		node.height.baseVal.value = dim.height;
118
	} else {
119
		if (node.r) {
120
			node.r.baseVal.value = Math.min(dim.width, dim.height) / 2;
121
		} else {
122
			if (node.rx) {
123
				node.rx.baseVal.value = dim.width / 2;
124
				node.ry.baseVal.value = dim.height / 2;
125
			}
126
		}
127
	}
128
};
129
dojo.svg.translate = function (node, dx, dy) {
130
	if (node.transform && node.ownerSVGElement && node.ownerSVGElement.createSVGTransform) {
131
		var t = node.ownerSVGElement.createSVGTransform();
132
		t.setTranslate(dx, dy);
133
		node.transform.baseVal.appendItem(t);
134
	}
135
};
136
dojo.svg.scale = function (node, scaleX, scaleY) {
137
	if (!scaleY) {
138
		var scaleY = scaleX;
139
	}
140
	if (node.transform && node.ownerSVGElement && node.ownerSVGElement.createSVGTransform) {
141
		var t = node.ownerSVGElement.createSVGTransform();
142
		t.setScale(scaleX, scaleY);
143
		node.transform.baseVal.appendItem(t);
144
	}
145
};
146
dojo.svg.rotate = function (node, ang, cx, cy) {
147
	if (node.transform && node.ownerSVGElement && node.ownerSVGElement.createSVGTransform) {
148
		var t = node.ownerSVGElement.createSVGTransform();
149
		if (cx == null) {
150
			t.setMatrix(t.matrix.rotate(ang));
151
		} else {
152
			t.setRotate(ang, cx, cy);
153
		}
154
		node.transform.baseVal.appendItem(t);
155
	}
156
};
157
dojo.svg.skew = function (node, ang, axis) {
158
	var dir = axis || "x";
159
	if (node.transform && node.ownerSVGElement && node.ownerSVGElement.createSVGTransform) {
160
		var t = node.ownerSVGElement.createSVGTransform();
161
		if (dir != "x") {
162
			t.setSkewY(ang);
163
		} else {
164
			t.setSkewX(ang);
165
		}
166
		node.transform.baseVal.appendItem(t);
167
	}
168
};
169
dojo.svg.flip = function (node, axis) {
170
	var dir = axis || "x";
171
	if (node.transform && node.ownerSVGElement && node.ownerSVGElement.createSVGTransform) {
172
		var t = node.ownerSVGElement.createSVGTransform();
173
		t.setMatrix((dir != "x") ? t.matrix.flipY() : t.matrix.flipX());
174
		node.transform.baseVal.appendItem(t);
175
	}
176
};
177
dojo.svg.invert = function (node) {
178
	if (node.transform && node.ownerSVGElement && node.ownerSVGElement.createSVGTransform) {
179
		var t = node.ownerSVGElement.createSVGTransform();
180
		t.setMatrix(t.matrix.inverse());
181
		node.transform.baseVal.appendItem(t);
182
	}
183
};
184
dojo.svg.applyMatrix = function (node, a, b, c, d, e, f) {
185
	if (node.transform && node.ownerSVGElement && node.ownerSVGElement.createSVGTransform) {
186
		var m;
187
		if (b) {
188
			var m = node.ownerSVGElement.createSVGMatrix();
189
			m.a = a;
190
			m.b = b;
191
			m.c = c;
192
			m.d = d;
193
			m.e = e;
194
			m.f = f;
195
		} else {
196
			m = a;
197
		}
198
		var t = node.ownerSVGElement.createSVGTransform();
199
		t.setMatrix(m);
200
		node.transform.baseVal.appendItem(t);
201
	}
202
};
203
dojo.svg.group = function (nodes) {
204
	var p = nodes.item(0).parentNode;
205
	var g = document.createElementNS(this.xmlns.svg, "g");
206
	for (var i = 0; i < nodes.length; i++) {
207
		g.appendChild(nodes.item(i));
208
	}
209
	p.appendChild(g);
210
	return g;
211
};
212
dojo.svg.ungroup = function (g) {
213
	var p = g.parentNode;
214
	while (g.childNodes.length > 0) {
215
		p.appendChild(g.childNodes.item(0));
216
	}
217
	p.removeChild(g);
218
};
219
dojo.svg.getGroup = function (node) {
220
	var a = this.getAncestors(node);
221
	for (var i = 0; i < a.length; i++) {
222
		if (a[i].nodeType == this.ELEMENT_NODE && a[i].nodeName.toLowerCase() == "g") {
223
			return a[i];
224
		}
225
	}
226
	return null;
227
};
228
dojo.svg.bringToFront = function (node) {
229
	var n = this.getGroup(node) || node;
230
	n.ownerSVGElement.appendChild(n);
231
};
232
dojo.svg.sendToBack = function (node) {
233
	var n = this.getGroup(node) || node;
234
	n.ownerSVGElement.insertBefore(n, n.ownerSVGElement.firstChild);
235
};
236
dojo.svg.bringForward = function (node) {
237
	var n = this.getGroup(node) || node;
238
	if (this.getLastChildElement(n.parentNode) != n) {
239
		this.insertAfter(n, this.getNextSiblingElement(n), true);
240
	}
241
};
242
dojo.svg.sendBackward = function (node) {
243
	var n = this.getGroup(node) || node;
244
	if (this.getFirstChildElement(n.parentNode) != n) {
245
		this.insertBefore(n, this.getPreviousSiblingElement(n), true);
246
	}
247
};
248
dojo.svg.createNodesFromText = function (txt, wrap) {
249
	var docFrag = (new DOMParser()).parseFromString(txt, "text/xml").normalize();
250
	if (wrap) {
251
		return [docFrag.firstChild.cloneNode(true)];
252
	}
253
	var nodes = [];
254
	for (var x = 0; x < docFrag.childNodes.length; x++) {
255
		nodes.push(docFrag.childNodes.item(x).cloneNode(true));
256
	}
257
	return nodes;
258
};
259