Subversion Repositories Applications.papyrus

Rev

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