Debian Codegraph @

architecture/httpdlib/html/js/svg/jquerysvg/jquery.svg.js

raw
1 /* http://keith-wood.name/svg.html
2 SVG for jQuery v1.4.5.
3 Written by Keith Wood (kbwood{at}iinet.com.au) August 2007.
4 Dual licensed under the GPL (http://dev.jquery.com/browser/trunk/jquery/GPL-LICENSE.txt) and
5 MIT (http://dev.jquery.com/browser/trunk/jquery/MIT-LICENSE.txt) licenses.
6 Please attribute the author if you use it. */
7
8 (function($) { // Hide scope, no $ conflict
9
10 /* SVG manager.
11 Use the singleton instance of this class, $.svg,
12 to interact with the SVG functionality. */
13 function SVGManager() {
14 this._settings = []; // Settings to be remembered per SVG object
15 this._extensions = []; // List of SVG extensions added to SVGWrapper
16 // for each entry [0] is extension name, [1] is extension class (function)
17 // the function takes one parameter - the SVGWrapper instance
18 this.regional = []; // Localisations, indexed by language, '' for default (English)
19 this.regional[''] = {errorLoadingText: 'Error loading',
20 notSupportedText: 'This browser does not support SVG'};
21 this.local = this.regional['']; // Current localisation
22 this._uuid = new Date().getTime();
23 this._renesis = detectActiveX('RenesisX.RenesisCtrl');
24 }
25
26 /* Determine whether a given ActiveX control is available.
27 @param classId (string) the ID for the ActiveX control
28 @return (boolean) true if found, false if not */
29 function detectActiveX(classId) {
30 try {
31 return !!(window.ActiveXObject && new ActiveXObject(classId));
32 }
33 catch (e) {
34 return false;
35 }
36 }
37
38 var PROP_NAME = 'svgwrapper';
39
40 $.extend(SVGManager.prototype, {
41 /* Class name added to elements to indicate already configured with SVG. */
42 markerClassName: 'hasSVG',
43
44 /* SVG namespace. */
45 svgNS: 'http://www.w3.org/2000/svg',
46 /* XLink namespace. */
47 xlinkNS: 'http://www.w3.org/1999/xlink',
48
49 /* SVG wrapper class. */
50 _wrapperClass: SVGWrapper,
51
52 /* Camel-case versions of attribute names containing dashes or are reserved words. */
53 _attrNames: {class_: 'class', in_: 'in',
54 alignmentBaseline: 'alignment-baseline', baselineShift: 'baseline-shift',
55 clipPath: 'clip-path', clipRule: 'clip-rule',
56 colorInterpolation: 'color-interpolation',
57 colorInterpolationFilters: 'color-interpolation-filters',
58 colorRendering: 'color-rendering', dominantBaseline: 'dominant-baseline',
59 enableBackground: 'enable-background', fillOpacity: 'fill-opacity',
60 fillRule: 'fill-rule', floodColor: 'flood-color',
61 floodOpacity: 'flood-opacity', fontFamily: 'font-family',
62 fontSize: 'font-size', fontSizeAdjust: 'font-size-adjust',
63 fontStretch: 'font-stretch', fontStyle: 'font-style',
64 fontVariant: 'font-variant', fontWeight: 'font-weight',
65 glyphOrientationHorizontal: 'glyph-orientation-horizontal',
66 glyphOrientationVertical: 'glyph-orientation-vertical',
67 horizAdvX: 'horiz-adv-x', horizOriginX: 'horiz-origin-x',
68 imageRendering: 'image-rendering', letterSpacing: 'letter-spacing',
69 lightingColor: 'lighting-color', markerEnd: 'marker-end',
70 markerMid: 'marker-mid', markerStart: 'marker-start',
71 stopColor: 'stop-color', stopOpacity: 'stop-opacity',
72 strikethroughPosition: 'strikethrough-position',
73 strikethroughThickness: 'strikethrough-thickness',
74 strokeDashArray: 'stroke-dasharray', strokeDashOffset: 'stroke-dashoffset',
75 strokeLineCap: 'stroke-linecap', strokeLineJoin: 'stroke-linejoin',
76 strokeMiterLimit: 'stroke-miterlimit', strokeOpacity: 'stroke-opacity',
77 strokeWidth: 'stroke-width', textAnchor: 'text-anchor',
78 textDecoration: 'text-decoration', textRendering: 'text-rendering',
79 underlinePosition: 'underline-position', underlineThickness: 'underline-thickness',
80 vertAdvY: 'vert-adv-y', vertOriginY: 'vert-origin-y',
81 wordSpacing: 'word-spacing', writingMode: 'writing-mode'},
82
83 /* Add the SVG object to its container. */
84 _attachSVG: function(container, settings) {
85 var svg = (container.namespaceURI == this.svgNS ? container : null);
86 var container = (svg ? null : container);
87 if ($(container || svg).hasClass(this.markerClassName)) {
88 return;
89 }
90 if (typeof settings == 'string') {
91 settings = {loadURL: settings};
92 }
93 else if (typeof settings == 'function') {
94 settings = {onLoad: settings};
95 }
96 $(container || svg).addClass(this.markerClassName);
97 try {
98 if (!svg) {
99 svg = document.createElementNS(this.svgNS, 'svg');
100 svg.setAttribute('version', '1.1');
101 if (container.clientWidth > 0) {
102 svg.setAttribute('width', container.clientWidth);
103 }
104 if (container.clientHeight > 0) {
105 svg.setAttribute('height', container.clientHeight);
106 }
107 container.appendChild(svg);
108 }
109 this._afterLoad(container, svg, settings || {});
110 }
111 catch (e) {
112 if ($.browser.msie) {
113 if (!container.id) {
114 container.id = 'svg' + (this._uuid++);
115 }
116 this._settings[container.id] = settings;
117 container.innerHTML = '<embed type="image/svg+xml" width="100%" ' +
118 'height="100%" src="' + (settings.initPath || '') + 'blank.svg" ' +
119 'pluginspage="http://www.adobe.com/svg/viewer/install/main.html"/>';
120 }
121 else {
122 container.innerHTML = '<p class="svg_error">' +
123 this.local.notSupportedText + '</p>';
124 }
125 }
126 },
127
128 /* SVG callback after loading - register SVG root. */
129 _registerSVG: function() {
130 for (var i = 0; i < document.embeds.length; i++) { // Check all
131 var container = document.embeds[i].parentNode;
132 if (!$(container).hasClass($.svg.markerClassName) || // Not SVG
133 $.data(container, PROP_NAME)) { // Already done
134 continue;
135 }
136 var svg = null;
137 try {
138 svg = document.embeds[i].getSVGDocument();
139 }
140 catch(e) {
141 setTimeout($.svg._registerSVG, 250); // Renesis takes longer to load
142 return;
143 }
144 svg = (svg ? svg.documentElement : null);
145 if (svg) {
146 $.svg._afterLoad(container, svg);
147 }
148 }
149 },
150
151 /* Post-processing once loaded. */
152 _afterLoad: function(container, svg, settings) {
153 var settings = settings || this._settings[container.id];
154 this._settings[container ? container.id : ''] = null;
155 var wrapper = new this._wrapperClass(svg, container);
156 $.data(container || svg, PROP_NAME, wrapper);
157 try {
158 if (settings.loadURL) { // Load URL
159 wrapper.load(settings.loadURL, settings);
160 }
161 if (settings.settings) { // Additional settings
162 wrapper.configure(settings.settings);
163 }
164 if (settings.onLoad && !settings.loadURL) { // Onload callback
165 settings.onLoad.apply(container || svg, [wrapper]);
166 }
167 }
168 catch (e) {
169 alert(e);
170 }
171 },
172
173 /* Return the SVG wrapper created for a given container.
174 @param container (string) selector for the container or
175 (element) the container for the SVG object or
176 jQuery collection - first entry is the container
177 @return (SVGWrapper) the corresponding SVG wrapper element, or null if not attached */
178 _getSVG: function(container) {
179 container = (typeof container == 'string' ? $(container)[0] :
180 (container.jquery ? container[0] : container));
181 return $.data(container, PROP_NAME);
182 },
183
184 /* Remove the SVG functionality from a div.
185 @param container (element) the container for the SVG object */
186 _destroySVG: function(container) {
187 var $container = $(container);
188 if (!$container.hasClass(this.markerClassName)) {
189 return;
190 }
191 $container.removeClass(this.markerClassName);
192 if (container.namespaceURI != this.svgNS) {
193 $container.empty();
194 }
195 $.removeData(container, PROP_NAME);
196 },
197
198 /* Extend the SVGWrapper object with an embedded class.
199 The constructor function must take a single parameter that is
200 a reference to the owning SVG root object. This allows the
201 extension to access the basic SVG functionality.
202 @param name (string) the name of the SVGWrapper attribute to access the new class
203 @param extClass (function) the extension class constructor */
204 addExtension: function(name, extClass) {
205 this._extensions.push([name, extClass]);
206 },
207
208 /* Does this node belong to SVG?
209 @param node (element) the node to be tested
210 @return (boolean) true if an SVG node, false if not */
211 isSVGElem: function(node) {
212 return (node.nodeType == 1 && node.namespaceURI == $.svg.svgNS);
213 }
214 });
215
216 /* The main SVG interface, which encapsulates the SVG element.
217 Obtain a reference from $().svg('get') */
218 function SVGWrapper(svg, container) {
219 this._svg = svg; // The SVG root node
220 this._container = container; // The containing div
221 for (var i = 0; i < $.svg._extensions.length; i++) {
222 var extension = $.svg._extensions[i];
223 this[extension[0]] = new extension[1](this);
224 }
225 }
226
227 $.extend(SVGWrapper.prototype, {
228
229 /* Retrieve the width of the SVG object. */
230 _width: function() {
231 return (this._container ? this._container.clientWidth : this._svg.width);
232 },
233
234 /* Retrieve the height of the SVG object. */
235 _height: function() {
236 return (this._container ? this._container.clientHeight : this._svg.height);
237 },
238
239 /* Retrieve the root SVG element.
240 @return the top-level SVG element */
241 root: function() {
242 return this._svg;
243 },
244
245 /* Configure a SVG node.
246 @param node (element, optional) the node to configure
247 @param settings (object) additional settings for the root
248 @param clear (boolean) true to remove existing attributes first,
249 false to add to what is already there (optional)
250 @return (SVGWrapper) this root */
251 configure: function(node, settings, clear) {
252 if (!node.nodeName) {
253 clear = settings;
254 settings = node;
255 node = this._svg;
256 }
257 if (clear) {
258 for (var i = node.attributes.length - 1; i >= 0; i--) {
259 var attr = node.attributes.item(i);
260 if (!(attr.nodeName == 'onload' || attr.nodeName == 'version' ||
261 attr.nodeName.substring(0, 5) == 'xmlns')) {
262 node.attributes.removeNamedItem(attr.nodeName);
263 }
264 }
265 }
266 for (var attrName in settings) {
267 node.setAttribute($.svg._attrNames[attrName] || attrName, settings[attrName]);
268 }
269 return this;
270 },
271
272 /* Locate a specific element in the SVG document.
273 @param id (string) the element's identifier
274 @return (element) the element reference, or null if not found */
275 getElementById: function(id) {
276 return this._svg.ownerDocument.getElementById(id);
277 },
278
279 /* Change the attributes for a SVG node.
280 @param element (SVG element) the node to change
281 @param settings (object) the new settings
282 @return (SVGWrapper) this root */
283 change: function(element, settings) {
284 if (element) {
285 for (var name in settings) {
286 if (settings[name] == null) {
287 element.removeAttribute($.svg._attrNames[name] || name);
288 }
289 else {
290 element.setAttribute($.svg._attrNames[name] || name, settings[name]);
291 }
292 }
293 }
294 return this;
295 },
296
297 /* Check for parent being absent and adjust arguments accordingly. */
298 _args: function(values, names, optSettings) {
299 names.splice(0, 0, 'parent');
300 names.splice(names.length, 0, 'settings');
301 var args = {};
302 var offset = 0;
303 if (values[0] != null && values[0].jquery) {
304 values[0] = values[0][0];
305 }
306 if (values[0] != null && !(typeof values[0] == 'object' && values[0].nodeName)) {
307 args['parent'] = null;
308 offset = 1;
309 }
310 for (var i = 0; i < values.length; i++) {
311 args[names[i + offset]] = values[i];
312 }
313 if (optSettings) {
314 $.each(optSettings, function(i, value) {
315 if (typeof args[value] == 'object') {
316 args.settings = args[value];
317 args[value] = null;
318 }
319 });
320 }
321 return args;
322 },
323
324 /* Add a title.
325 @param parent (element or jQuery) the parent node for the new title (optional)
326 @param text (string) the text of the title
327 @param settings (object) additional settings for the title (optional)
328 @return (element) the new title node */
329 /*
330 //COMMENTED OUT BY Mike Solomon
331 //this creates unwanted tooltip behavior
332 title: function(parent, text, settings) {
333 var args = this._args(arguments, ['text']);
334 var node = this._makeNode(args.parent, 'title', args.settings || {});
335 node.appendChild(this._svg.ownerDocument.createTextNode(args.text));
336 return node;
337 },
338 */
339 /* Add a description.
340 @param parent (element or jQuery) the parent node for the new description (optional)
341 @param text (string) the text of the description
342 @param settings (object) additional settings for the description (optional)
343 @return (element) the new description node */
344 describe: function(parent, text, settings) {
345 var args = this._args(arguments, ['text']);
346 var node = this._makeNode(args.parent, 'desc', args.settings || {});
347 node.appendChild(this._svg.ownerDocument.createTextNode(args.text));
348 return node;
349 },
350
351 /* Add a definitions node.
352 @param parent (element or jQuery) the parent node for the new definitions (optional)
353 @param id (string) the ID of this definitions (optional)
354 @param settings (object) additional settings for the definitions (optional)
355 @return (element) the new definitions node */
356 defs: function(parent, id, settings) {
357 var args = this._args(arguments, ['id'], ['id']);
358 return this._makeNode(args.parent, 'defs', $.extend(
359 (args.id ? {id: args.id} : {}), args.settings || {}));
360 },
361
362 /* Add a symbol definition.
363 @param parent (element or jQuery) the parent node for the new symbol (optional)
364 @param id (string) the ID of this symbol
365 @param x1 (number) the left coordinate for this symbol
366 @param y1 (number) the top coordinate for this symbol
367 @param width (number) the width of this symbol
368 @param height (number) the height of this symbol
369 @param settings (object) additional settings for the symbol (optional)
370 @return (element) the new symbol node */
371 symbol: function(parent, id, x1, y1, width, height, settings) {
372 var args = this._args(arguments, ['id', 'x1', 'y1', 'width', 'height']);
373 return this._makeNode(args.parent, 'symbol', $.extend({id: args.id,
374 viewBox: args.x1 + ' ' + args.y1 + ' ' + args.width + ' ' + args.height},
375 args.settings || {}));
376 },
377
378 /* Add a marker definition.
379 @param parent (element or jQuery) the parent node for the new marker (optional)
380 @param id (string) the ID of this marker
381 @param refX (number) the x-coordinate for the reference point
382 @param refY (number) the y-coordinate for the reference point
383 @param mWidth (number) the marker viewport width
384 @param mHeight (number) the marker viewport height
385 @param orient (string or int) 'auto' or angle (degrees) (optional)
386 @param settings (object) additional settings for the marker (optional)
387 @return (element) the new marker node */
388 marker: function(parent, id, refX, refY, mWidth, mHeight, orient, settings) {
389 var args = this._args(arguments, ['id', 'refX', 'refY',
390 'mWidth', 'mHeight', 'orient'], ['orient']);
391 return this._makeNode(args.parent, 'marker', $.extend(
392 {id: args.id, refX: args.refX, refY: args.refY, markerWidth: args.mWidth,
393 markerHeight: args.mHeight, orient: args.orient || 'auto'}, args.settings || {}));
394 },
395
396 /* Add a style node.
397 @param parent (element or jQuery) the parent node for the new node (optional)
398 @param styles (string) the CSS styles
399 @param settings (object) additional settings for the node (optional)
400 @return (element) the new style node */
401 style: function(parent, styles, settings) {
402 var args = this._args(arguments, ['styles']);
403 var node = this._makeNode(args.parent, 'style', $.extend(
404 {type: 'text/css'}, args.settings || {}));
405 node.appendChild(this._svg.ownerDocument.createTextNode(args.styles));
406 if ($.browser.opera) {
407 $('head').append('<style type="text/css">' + args.styles + '</style>');
408 }
409 return node;
410 },
411
412 /* Add a script node.
413 @param parent (element or jQuery) the parent node for the new node (optional)
414 @param script (string) the JavaScript code
415 @param type (string) the MIME type for the code (optional, default 'text/javascript')
416 @param settings (object) additional settings for the node (optional)
417 @return (element) the new script node */
418 script: function(parent, script, type, settings) {
419 var args = this._args(arguments, ['script', 'type'], ['type']);
420 var node = this._makeNode(args.parent, 'script', $.extend(
421 {type: args.type || 'text/javascript'}, args.settings || {}));
422 node.appendChild(this._svg.ownerDocument.createTextNode(args.script));
423 if (!$.browser.mozilla) {
424 $.globalEval(args.script);
425 }
426 return node;
427 },
428
429 /* Add a linear gradient definition.
430 Specify all of x1, y1, x2, y2 or none of them.
431 @param parent (element or jQuery) the parent node for the new gradient (optional)
432 @param id (string) the ID for this gradient
433 @param stops (string[][]) the gradient stops, each entry is
434 [0] is offset (0.0-1.0 or 0%-100%), [1] is colour,
435 [2] is opacity (optional)
436 @param x1 (number) the x-coordinate of the gradient start (optional)
437 @param y1 (number) the y-coordinate of the gradient start (optional)
438 @param x2 (number) the x-coordinate of the gradient end (optional)
439 @param y2 (number) the y-coordinate of the gradient end (optional)
440 @param settings (object) additional settings for the gradient (optional)
441 @return (element) the new gradient node */
442 linearGradient: function(parent, id, stops, x1, y1, x2, y2, settings) {
443 var args = this._args(arguments,
444 ['id', 'stops', 'x1', 'y1', 'x2', 'y2'], ['x1']);
445 var sets = $.extend({id: args.id},
446 (args.x1 != null ? {x1: args.x1, y1: args.y1, x2: args.x2, y2: args.y2} : {}));
447 return this._gradient(args.parent, 'linearGradient',
448 $.extend(sets, args.settings || {}), args.stops);
449 },
450
451 /* Add a radial gradient definition.
452 Specify all of cx, cy, r, fx, fy or none of them.
453 @param parent (element or jQuery) the parent node for the new gradient (optional)
454 @param id (string) the ID for this gradient
455 @param stops (string[][]) the gradient stops, each entry
456 [0] is offset, [1] is colour, [2] is opacity (optional)
457 @param cx (number) the x-coordinate of the largest circle centre (optional)
458 @param cy (number) the y-coordinate of the largest circle centre (optional)
459 @param r (number) the radius of the largest circle (optional)
460 @param fx (number) the x-coordinate of the gradient focus (optional)
461 @param fy (number) the y-coordinate of the gradient focus (optional)
462 @param settings (object) additional settings for the gradient (optional)
463 @return (element) the new gradient node */
464 radialGradient: function(parent, id, stops, cx, cy, r, fx, fy, settings) {
465 var args = this._args(arguments,
466 ['id', 'stops', 'cx', 'cy', 'r', 'fx', 'fy'], ['cx']);
467 var sets = $.extend({id: args.id}, (args.cx != null ?
468 {cx: args.cx, cy: args.cy, r: args.r, fx: args.fx, fy: args.fy} : {}));
469 return this._gradient(args.parent, 'radialGradient',
470 $.extend(sets, args.settings || {}), args.stops);
471 },
472
473 /* Add a gradient node. */
474 _gradient: function(parent, name, settings, stops) {
475 var node = this._makeNode(parent, name, settings);
476 for (var i = 0; i < stops.length; i++) {
477 var stop = stops[i];
478 this._makeNode(node, 'stop', $.extend(
479 {offset: stop[0], stopColor: stop[1]},
480 (stop[2] != null ? {stopOpacity: stop[2]} : {})));
481 }
482 return node;
483 },
484
485 /* Add a pattern definition.
486 Specify all of vx, vy, xwidth, vheight or none of them.
487 @param parent (element or jQuery) the parent node for the new pattern (optional)
488 @param id (string) the ID for this pattern
489 @param x (number) the x-coordinate for the left edge of the pattern
490 @param y (number) the y-coordinate for the top edge of the pattern
491 @param width (number) the width of the pattern
492 @param height (number) the height of the pattern
493 @param vx (number) the minimum x-coordinate for view box (optional)
494 @param vy (number) the minimum y-coordinate for the view box (optional)
495 @param vwidth (number) the width of the view box (optional)
496 @param vheight (number) the height of the view box (optional)
497 @param settings (object) additional settings for the pattern (optional)
498 @return (element) the new pattern node */
499 pattern: function(parent, id, x, y, width, height, vx, vy, vwidth, vheight, settings) {
500 var args = this._args(arguments, ['id', 'x', 'y', 'width', 'height',
501 'vx', 'vy', 'vwidth', 'vheight'], ['vx']);
502 var sets = $.extend({id: args.id, x: args.x, y: args.y,
503 width: args.width, height: args.height}, (args.vx != null ?
504 {viewBox: args.vx + ' ' + args.vy + ' ' + args.vwidth + ' ' + args.vheight} : {}));
505 return this._makeNode(args.parent, 'pattern', $.extend(sets, args.settings || {}));
506 },
507
508 /* Add a clip path definition.
509 @param parent (element) the parent node for the new element (optional)
510 @param id (string) the ID for this path
511 @param units (string) either 'userSpaceOnUse' (default) or 'objectBoundingBox' (optional)
512 @return (element) the new clipPath node */
513 clipPath: function(parent, id, units, settings) {
514 var args = this._args(arguments, ['id', 'units']);
515 args.units = args.units || 'userSpaceOnUse';
516 return this._makeNode(args.parent, 'clipPath', $.extend(
517 {id: args.id, clipPathUnits: args.units}, args.settings || {}));
518 },
519
520 /* Add a mask definition.
521 @param parent (element or jQuery) the parent node for the new mask (optional)
522 @param id (string) the ID for this mask
523 @param x (number) the x-coordinate for the left edge of the mask
524 @param y (number) the y-coordinate for the top edge of the mask
525 @param width (number) the width of the mask
526 @param height (number) the height of the mask
527 @param settings (object) additional settings for the mask (optional)
528 @return (element) the new mask node */
529 mask: function(parent, id, x, y, width, height, settings) {
530 var args = this._args(arguments, ['id', 'x', 'y', 'width', 'height']);
531 return this._makeNode(args.parent, 'mask', $.extend(
532 {id: args.id, x: args.x, y: args.y, width: args.width, height: args.height},
533 args.settings || {}));
534 },
535
536 /* Create a new path object.
537 @return (SVGPath) a new path object */
538 createPath: function() {
539 return new SVGPath();
540 },
541
542 /* Create a new text object.
543 @return (SVGText) a new text object */
544 createText: function() {
545 return new SVGText();
546 },
547
548 /* Add an embedded SVG element.
549 Specify all of vx, vy, vwidth, vheight or none of them.
550 @param parent (element or jQuery) the parent node for the new node (optional)
551 @param x (number) the x-coordinate for the left edge of the node
552 @param y (number) the y-coordinate for the top edge of the node
553 @param width (number) the width of the node
554 @param height (number) the height of the node
555 @param vx (number) the minimum x-coordinate for view box (optional)
556 @param vy (number) the minimum y-coordinate for the view box (optional)
557 @param vwidth (number) the width of the view box (optional)
558 @param vheight (number) the height of the view box (optional)
559 @param settings (object) additional settings for the node (optional)
560 @return (element) the new node */
561 svg: function(parent, x, y, width, height, vx, vy, vwidth, vheight, settings) {
562 var args = this._args(arguments, ['x', 'y', 'width', 'height',
563 'vx', 'vy', 'vwidth', 'vheight'], ['vx']);
564 var sets = $.extend({x: args.x, y: args.y, width: args.width, height: args.height},
565 (args.vx != null ? {viewBox: args.vx + ' ' + args.vy + ' ' +
566 args.vwidth + ' ' + args.vheight} : {}));
567 return this._makeNode(args.parent, 'svg', $.extend(sets, args.settings || {}));
568 },
569
570 /* Create a group.
571 @param parent (element or jQuery) the parent node for the new group (optional)
572 @param id (string) the ID of this group (optional)
573 @param settings (object) additional settings for the group (optional)
574 @return (element) the new group node */
575 group: function(parent, id, settings) {
576 var args = this._args(arguments, ['id'], ['id']);
577 return this._makeNode(args.parent, 'g', $.extend({id: args.id}, args.settings || {}));
578 },
579
580 /* Add a usage reference.
581 Specify all of x, y, width, height or none of them.
582 @param parent (element or jQuery) the parent node for the new node (optional)
583 @param x (number) the x-coordinate for the left edge of the node (optional)
584 @param y (number) the y-coordinate for the top edge of the node (optional)
585 @param width (number) the width of the node (optional)
586 @param height (number) the height of the node (optional)
587 @param ref (string) the ID of the definition node
588 @param settings (object) additional settings for the node (optional)
589 @return (element) the new node */
590 use: function(parent, x, y, width, height, ref, settings) {
591 var args = this._args(arguments, ['x', 'y', 'width', 'height', 'ref']);
592 if (typeof args.x == 'string') {
593 args.ref = args.x;
594 args.settings = args.y;
595 args.x = args.y = args.width = args.height = null;
596 }
597 var node = this._makeNode(args.parent, 'use', $.extend(
598 {x: args.x, y: args.y, width: args.width, height: args.height},
599 args.settings || {}));
600 node.setAttributeNS($.svg.xlinkNS, 'href', args.ref);
601 return node;
602 },
603
604 /* Add a link, which applies to all child elements.
605 @param parent (element or jQuery) the parent node for the new link (optional)
606 @param ref (string) the target URL
607 @param settings (object) additional settings for the link (optional)
608 @return (element) the new link node */
609 link: function(parent, ref, settings) {
610 var args = this._args(arguments, ['ref']);
611 var node = this._makeNode(args.parent, 'a', args.settings);
612 node.setAttributeNS($.svg.xlinkNS, 'href', args.ref);
613 return node;
614 },
615
616 /* Add an image.
617 @param parent (element or jQuery) the parent node for the new image (optional)
618 @param x (number) the x-coordinate for the left edge of the image
619 @param y (number) the y-coordinate for the top edge of the image
620 @param width (number) the width of the image
621 @param height (number) the height of the image
622 @param ref (string) the path to the image
623 @param settings (object) additional settings for the image (optional)
624 @return (element) the new image node */
625 image: function(parent, x, y, width, height, ref, settings) {
626 var args = this._args(arguments, ['x', 'y', 'width', 'height', 'ref']);
627 var node = this._makeNode(args.parent, 'image', $.extend(
628 {x: args.x, y: args.y, width: args.width, height: args.height},
629 args.settings || {}));
630 node.setAttributeNS($.svg.xlinkNS, 'href', args.ref);
631 return node;
632 },
633
634 /* Draw a path.
635 @param parent (element or jQuery) the parent node for the new shape (optional)
636 @param path (string or SVGPath) the path to draw
637 @param settings (object) additional settings for the shape (optional)
638 @return (element) the new shape node */
639 path: function(parent, path, settings) {
640 var args = this._args(arguments, ['path']);
641 return this._makeNode(args.parent, 'path', $.extend(
642 {d: (args.path.path ? args.path.path() : args.path)}, args.settings || {}));
643 },
644
645 /* Draw a rectangle.
646 Specify both of rx and ry or neither.
647 @param parent (element or jQuery) the parent node for the new shape (optional)
648 @param x (number) the x-coordinate for the left edge of the rectangle
649 @param y (number) the y-coordinate for the top edge of the rectangle
650 @param width (number) the width of the rectangle
651 @param height (number) the height of the rectangle
652 @param rx (number) the x-radius of the ellipse for the rounded corners (optional)
653 @param ry (number) the y-radius of the ellipse for the rounded corners (optional)
654 @param settings (object) additional settings for the shape (optional)
655 @return (element) the new shape node */
656 rect: function(parent, x, y, width, height, rx, ry, settings) {
657 var args = this._args(arguments, ['x', 'y', 'width', 'height', 'rx', 'ry'], ['rx']);
658 return this._makeNode(args.parent, 'rect', $.extend(
659 {x: args.x, y: args.y, width: args.width, height: args.height},
660 (args.rx ? {rx: args.rx, ry: args.ry} : {}), args.settings || {}));
661 },
662
663 /* Draw a circle.
664 @param parent (element or jQuery) the parent node for the new shape (optional)
665 @param cx (number) the x-coordinate for the centre of the circle
666 @param cy (number) the y-coordinate for the centre of the circle
667 @param r (number) the radius of the circle
668 @param settings (object) additional settings for the shape (optional)
669 @return (element) the new shape node */
670 circle: function(parent, cx, cy, r, settings) {
671 var args = this._args(arguments, ['cx', 'cy', 'r']);
672 return this._makeNode(args.parent, 'circle', $.extend(
673 {cx: args.cx, cy: args.cy, r: args.r}, args.settings || {}));
674 },
675
676 /* Draw an ellipse.
677 @param parent (element or jQuery) the parent node for the new shape (optional)
678 @param cx (number) the x-coordinate for the centre of the ellipse
679 @param cy (number) the y-coordinate for the centre of the ellipse
680 @param rx (number) the x-radius of the ellipse
681 @param ry (number) the y-radius of the ellipse
682 @param settings (object) additional settings for the shape (optional)
683 @return (element) the new shape node */
684 ellipse: function(parent, cx, cy, rx, ry, settings) {
685 var args = this._args(arguments, ['cx', 'cy', 'rx', 'ry']);
686 return this._makeNode(args.parent, 'ellipse', $.extend(
687 {cx: args.cx, cy: args.cy, rx: args.rx, ry: args.ry}, args.settings || {}));
688 },
689
690 /* Draw a line.
691 @param parent (element or jQuery) the parent node for the new shape (optional)
692 @param x1 (number) the x-coordinate for the start of the line
693 @param y1 (number) the y-coordinate for the start of the line
694 @param x2 (number) the x-coordinate for the end of the line
695 @param y2 (number) the y-coordinate for the end of the line
696 @param settings (object) additional settings for the shape (optional)
697 @return (element) the new shape node */
698 line: function(parent, x1, y1, x2, y2, settings) {
699 var args = this._args(arguments, ['x1', 'y1', 'x2', 'y2']);
700 return this._makeNode(args.parent, 'line', $.extend(
701 {x1: args.x1, y1: args.y1, x2: args.x2, y2: args.y2}, args.settings || {}));
702 },
703
704 /* Draw a polygonal line.
705 @param parent (element or jQuery) the parent node for the new shape (optional)
706 @param points (number[][]) the x-/y-coordinates for the points on the line
707 @param settings (object) additional settings for the shape (optional)
708 @return (element) the new shape node */
709 polyline: function(parent, points, settings) {
710 var args = this._args(arguments, ['points']);
711 return this._poly(args.parent, 'polyline', args.points, args.settings);
712 },
713
714 /* Draw a polygonal shape.
715 @param parent (element or jQuery) the parent node for the new shape (optional)
716 @param points (number[][]) the x-/y-coordinates for the points on the shape
717 @param settings (object) additional settings for the shape (optional)
718 @return (element) the new shape node */
719 polygon: function(parent, points, settings) {
720 var args = this._args(arguments, ['points']);
721 return this._poly(args.parent, 'polygon', args.points, args.settings);
722 },
723
724 /* Draw a polygonal line or shape. */
725 _poly: function(parent, name, points, settings) {
726 var ps = '';
727 for (var i = 0; i < points.length; i++) {
728 ps += points[i].join() + ' ';
729 }
730 return this._makeNode(parent, name, $.extend(
731 {points: $.trim(ps)}, settings || {}));
732 },
733
734 /* Draw text.
735 Specify both of x and y or neither of them.
736 @param parent (element or jQuery) the parent node for the text (optional)
737 @param x (number or number[]) the x-coordinate(s) for the text (optional)
738 @param y (number or number[]) the y-coordinate(s) for the text (optional)
739 @param value (string) the text content or
740 (SVGText) text with spans and references
741 @param settings (object) additional settings for the text (optional)
742 @return (element) the new text node */
743 text: function(parent, x, y, value, settings) {
744 var args = this._args(arguments, ['x', 'y', 'value']);
745 if (typeof args.x == 'string' && arguments.length < 4) {
746 args.value = args.x;
747 args.settings = args.y;
748 args.x = args.y = null;
749 }
750 return this._text(args.parent, 'text', args.value, $.extend(
751 {x: (args.x && isArray(args.x) ? args.x.join(' ') : args.x),
752 y: (args.y && isArray(args.y) ? args.y.join(' ') : args.y)},
753 args.settings || {}));
754 },
755
756 /* Draw text along a path.
757 @param parent (element or jQuery) the parent node for the text (optional)
758 @param path (string) the ID of the path
759 @param value (string) the text content or
760 (SVGText) text with spans and references
761 @param settings (object) additional settings for the text (optional)
762 @return (element) the new text node */
763 textpath: function(parent, path, value, settings) {
764 var args = this._args(arguments, ['path', 'value']);
765 var node = this._text(args.parent, 'textPath', args.value, args.settings || {});
766 node.setAttributeNS($.svg.xlinkNS, 'href', args.path);
767 return node;
768 },
769
770 /* Draw text. */
771 _text: function(parent, name, value, settings) {
772 var node = this._makeNode(parent, name, settings);
773 if (typeof value == 'string') {
774 node.appendChild(node.ownerDocument.createTextNode(value));
775 }
776 else {
777 for (var i = 0; i < value._parts.length; i++) {
778 var part = value._parts[i];
779 if (part[0] == 'tspan') {
780 var child = this._makeNode(node, part[0], part[2]);
781 child.appendChild(node.ownerDocument.createTextNode(part[1]));
782 node.appendChild(child);
783 }
784 else if (part[0] == 'tref') {
785 var child = this._makeNode(node, part[0], part[2]);
786 child.setAttributeNS($.svg.xlinkNS, 'href', part[1]);
787 node.appendChild(child);
788 }
789 else if (part[0] == 'textpath') {
790 var set = $.extend({}, part[2]);
791 set.href = null;
792 var child = this._makeNode(node, part[0], set);
793 child.setAttributeNS($.svg.xlinkNS, 'href', part[2].href);
794 child.appendChild(node.ownerDocument.createTextNode(part[1]));
795 node.appendChild(child);
796 }
797 else { // straight text
798 node.appendChild(node.ownerDocument.createTextNode(part[1]));
799 }
800 }
801 }
802 return node;
803 },
804
805 /* Add a custom SVG element.
806 @param parent (element or jQuery) the parent node for the new element (optional)
807 @param name (string) the name of the element
808 @param settings (object) additional settings for the element (optional)
809 @return (element) the new custom node */
810 other: function(parent, name, settings) {
811 var args = this._args(arguments, ['name']);
812 return this._makeNode(args.parent, args.name, args.settings || {});
813 },
814
815 /* Create a shape node with the given settings. */
816 _makeNode: function(parent, name, settings) {
817 parent = parent || this._svg;
818 var node = this._svg.ownerDocument.createElementNS($.svg.svgNS, name);
819 for (var name in settings) {
820 var value = settings[name];
821 if (value != null && value != null &&
822 (typeof value != 'string' || value != '')) {
823 node.setAttribute($.svg._attrNames[name] || name, value);
824 }
825 }
826 parent.appendChild(node);
827 return node;
828 },
829
830 /* Add an existing SVG node to the diagram.
831 @param parent (element or jQuery) the parent node for the new node (optional)
832 @param node (element) the new node to add or
833 (string) the jQuery selector for the node or
834 (jQuery collection) set of nodes to add
835 @return (SVGWrapper) this wrapper */
836 add: function(parent, node) {
837 var args = this._args((arguments.length == 1 ? [null, parent] : arguments), ['node']);
838 var svg = this;
839 args.parent = args.parent || this._svg;
840 args.node = (args.node.jquery ? args.node : $(args.node));
841 try {
842 if ($.svg._renesis) {
843 throw 'Force traversal';
844 }
845 args.parent.appendChild(args.node.cloneNode(true));
846 }
847 catch (e) {
848 args.node.each(function() {
849 var child = svg._cloneAsSVG(this);
850 if (child) {
851 args.parent.appendChild(child);
852 }
853 });
854 }
855 return this;
856 },
857
858 /* Clone an existing SVG node and add it to the diagram.
859 @param parent (element or jQuery) the parent node for the new node (optional)
860 @param node (element) the new node to add or
861 (string) the jQuery selector for the node or
862 (jQuery collection) set of nodes to add
863 @return (element[]) collection of new nodes */
864 clone: function(parent, node) {
865 var svg = this;
866 var args = this._args((arguments.length == 1 ? [null, parent] : arguments), ['node']);
867 args.parent = args.parent || this._svg;
868 args.node = (args.node.jquery ? args.node : $(args.node));
869 var newNodes = [];
870 args.node.each(function() {
871 var child = svg._cloneAsSVG(this);
872 if (child) {
873 child.id = '';
874 args.parent.appendChild(child);
875 newNodes.push(child);
876 }
877 });
878 return newNodes;
879 },
880
881 /* SVG nodes must belong to the SVG namespace, so clone and ensure this is so.
882 @param node (element) the SVG node to clone
883 @return (element) the cloned node */
884 _cloneAsSVG: function(node) {
885 var newNode = null;
886 if (node.nodeType == 1) { // element
887 newNode = this._svg.ownerDocument.createElementNS(
888 $.svg.svgNS, this._checkName(node.nodeName));
889 for (var i = 0; i < node.attributes.length; i++) {
890 var attr = node.attributes.item(i);
891 if (attr.nodeName != 'xmlns' && attr.nodeValue) {
892 if (attr.prefix == 'xlink') {
893 newNode.setAttributeNS($.svg.xlinkNS,
894 attr.localName || attr.baseName, attr.nodeValue);
895 }
896 else {
897 newNode.setAttribute(this._checkName(attr.nodeName), attr.nodeValue);
898 }
899 }
900 }
901 for (var i = 0; i < node.childNodes.length; i++) {
902 var child = this._cloneAsSVG(node.childNodes[i]);
903 if (child) {
904 newNode.appendChild(child);
905 }
906 }
907 }
908 else if (node.nodeType == 3) { // text
909 if ($.trim(node.nodeValue)) {
910 newNode = this._svg.ownerDocument.createTextNode(node.nodeValue);
911 }
912 }
913 else if (node.nodeType == 4) { // CDATA
914 if ($.trim(node.nodeValue)) {
915 try {
916 newNode = this._svg.ownerDocument.createCDATASection(node.nodeValue);
917 }
918 catch (e) {
919 newNode = this._svg.ownerDocument.createTextNode(
920 node.nodeValue.replace(/&/g, '&amp;').
921 replace(/</g, '&lt;').replace(/>/g, '&gt;'));
922 }
923 }
924 }
925 return newNode;
926 },
927
928 /* Node names must be lower case and without SVG namespace prefix. */
929 _checkName: function(name) {
930 name = (name.substring(0, 1) >= 'A' && name.substring(0, 1) <= 'Z' ?
931 name.toLowerCase() : name);
932 return (name.substring(0, 4) == 'svg:' ? name.substring(4) : name);
933 },
934
935 /* Load an external SVG document.
936 @param url (string) the location of the SVG document or
937 the actual SVG content
938 @param settings (boolean) see addTo below or
939 (function) see onLoad below or
940 (object) additional settings for the load with attributes below:
941 addTo (boolean) true to add to what's already there,
942 or false to clear the canvas first
943 changeSize (boolean) true to allow the canvas size to change,
944 or false to retain the original
945 onLoad (function) callback after the document has loaded,
946 'this' is the container, receives SVG object and
947 optional error message as a parameter
948 parent (string or element or jQuery) the parent to load
949 into, defaults to top-level svg element
950 @return (SVGWrapper) this root */
951 load: function(url, settings) {
952 settings = (typeof settings == 'boolean' ? {addTo: settings} :
953 (typeof settings == 'function' ? {onLoad: settings} :
954 (typeof settings == 'string' ? {parent: settings} :
955 (typeof settings == 'object' && settings.nodeName ? {parent: settings} :
956 (typeof settings == 'object' && settings.jquery ? {parent: settings} :
957 settings || {})))));
958 if (!settings.parent && !settings.addTo) {
959 this.clear(false);
960 }
961 var size = [this._svg.getAttribute('width'), this._svg.getAttribute('height')];
962 var wrapper = this;
963 // Report a problem with the load
964 var reportError = function(message) {
965 message = $.svg.local.errorLoadingText + ': ' + message;
966 if (settings.onLoad) {
967 settings.onLoad.apply(wrapper._container || wrapper._svg, [wrapper, message]);
968 }
969 else {
970 wrapper.text(null, 10, 20, message);
971 }
972 };
973 // Create a DOM from SVG content
974 var loadXML4IE = function(data) {
975 var xml = new ActiveXObject('Microsoft.XMLDOM');
976 xml.validateOnParse = false;
977 xml.resolveExternals = false;
978 xml.async = false;
979 xml.loadXML(data);
980 if (xml.parseError.errorCode != 0) {
981 reportError(xml.parseError.reason);
982 return null;
983 }
984 return xml;
985 };
986 // Load the SVG DOM
987 var loadSVG = function(data) {
988 if (!data) {
989 return;
990 }
991 if (data.documentElement.nodeName != 'svg') {
992 var errors = data.getElementsByTagName('parsererror');
993 var messages = (errors.length ? errors[0].getElementsByTagName('div') : []); // Safari
994 reportError(!errors.length ? '???' :
995 (messages.length ? messages[0] : errors[0]).firstChild.nodeValue);
996 return;
997 }
998 var parent = (settings.parent ? $(settings.parent)[0] : wrapper._svg);
999 var attrs = {};
1000 for (var i = 0; i < data.documentElement.attributes.length; i++) {
1001 var attr = data.documentElement.attributes.item(i);
1002 if (!(attr.nodeName == 'version' || attr.nodeName.substring(0, 5) == 'xmlns')) {
1003 attrs[attr.nodeName] = attr.nodeValue;
1004 }
1005 }
1006 wrapper.configure(parent, attrs, !settings.parent);
1007 var nodes = data.documentElement.childNodes;
1008 for (var i = 0; i < nodes.length; i++) {
1009 try {
1010 if ($.svg._renesis) {
1011 throw 'Force traversal';
1012 }
1013 parent.appendChild(wrapper._svg.ownerDocument.importNode(nodes[i], true));
1014 if (nodes[i].nodeName == 'script') {
1015 $.globalEval(nodes[i].textContent);
1016 }
1017 }
1018 catch (e) {
1019 wrapper.add(parent, nodes[i]);
1020 }
1021 }
1022 if (!settings.changeSize) {
1023 wrapper.configure(parent, {width: size[0], height: size[1]});
1024 }
1025 if (settings.onLoad) {
1026 settings.onLoad.apply(wrapper._container || wrapper._svg, [wrapper]);
1027 }
1028 };
1029 if (url.match('<svg')) { // Inline SVG
1030 loadSVG($.browser.msie ? loadXML4IE(url) :
1031 new DOMParser().parseFromString(url, 'text/xml'));
1032 }
1033 else { // Remote SVG
1034 $.ajax({url: url, dataType: ($.browser.msie ? 'text' : 'xml'),
1035 success: function(xml) {
1036 loadSVG($.browser.msie ? loadXML4IE(xml) : xml);
1037 }, error: function(http, message, exc) {
1038 reportError(message + (exc ? ' ' + exc.message : ''));
1039 }});
1040 }
1041 return this;
1042 },
1043
1044 /* Delete a specified node.
1045 @param node (element or jQuery) the drawing node to remove
1046 @return (SVGWrapper) this root */
1047 remove: function(node) {
1048 node = (node.jquery ? node[0] : node);
1049 node.parentNode.removeChild(node);
1050 return this;
1051 },
1052
1053 /* Delete everything in the current document.
1054 @param attrsToo (boolean) true to clear any root attributes as well,
1055 false to leave them (optional)
1056 @return (SVGWrapper) this root */
1057 clear: function(attrsToo) {
1058 if (attrsToo) {
1059 this.configure({}, true);
1060 }
1061 while (this._svg.firstChild) {
1062 this._svg.removeChild(this._svg.firstChild);
1063 }
1064 return this;
1065 },
1066
1067 /* Serialise the current diagram into an SVG text document.
1068 @param node (SVG element) the starting node (optional)
1069 @return (string) the SVG as text */
1070 toSVG: function(node) {
1071 node = node || this._svg;
1072 return (typeof XMLSerializer == 'undefined' ? this._toSVG(node) :
1073 new XMLSerializer().serializeToString(node));
1074 },
1075
1076 /* Serialise one node in the SVG hierarchy. */
1077 _toSVG: function(node) {
1078 var svgDoc = '';
1079 if (!node) {
1080 return svgDoc;
1081 }
1082 if (node.nodeType == 3) { // Text
1083 svgDoc = node.nodeValue;
1084 }
1085 else if (node.nodeType == 4) { // CDATA
1086 svgDoc = '<![CDATA[' + node.nodeValue + ']]>';
1087 }
1088 else { // Element
1089 svgDoc = '<' + node.nodeName;
1090 if (node.attributes) {
1091 for (var i = 0; i < node.attributes.length; i++) {
1092 var attr = node.attributes.item(i);
1093 if (!($.trim(attr.nodeValue) == '' || attr.nodeValue.match(/^\[object/) ||
1094 attr.nodeValue.match(/^function/))) {
1095 svgDoc += ' ' + (attr.namespaceURI == $.svg.xlinkNS ? 'xlink:' : '') +
1096 attr.nodeName + '="' + attr.nodeValue + '"';
1097 }
1098 }
1099 }
1100 if (node.firstChild) {
1101 svgDoc += '>';
1102 var child = node.firstChild;
1103 while (child) {
1104 svgDoc += this._toSVG(child);
1105 child = child.nextSibling;
1106 }
1107 svgDoc += '</' + node.nodeName + '>';
1108 }
1109 else {
1110 svgDoc += '/>';
1111 }
1112 }
1113 return svgDoc;
1114 }
1115 });
1116
1117 /* Helper to generate an SVG path.
1118 Obtain an instance from the SVGWrapper object.
1119 String calls together to generate the path and use its value:
1120 var path = root.createPath();
1121 root.path(null, path.move(100, 100).line(300, 100).line(200, 300).close(), {fill: 'red'});
1122 or
1123 root.path(null, path.move(100, 100).line([[300, 100], [200, 300]]).close(), {fill: 'red'}); */
1124 function SVGPath() {
1125 this._path = '';
1126 }
1127
1128 $.extend(SVGPath.prototype, {
1129 /* Prepare to create a new path.
1130 @return (SVGPath) this path */
1131 reset: function() {
1132 this._path = '';
1133 return this;
1134 },
1135
1136 /* Move the pointer to a position.
1137 @param x (number) x-coordinate to move to or
1138 (number[][]) x-/y-coordinates to move to
1139 @param y (number) y-coordinate to move to (omitted if x is array)
1140 @param relative (boolean) true for coordinates relative to the current point,
1141 false for coordinates being absolute
1142 @return (SVGPath) this path */
1143 move: function(x, y, relative) {
1144 relative = (isArray(x) ? y : relative);
1145 return this._coords((relative ? 'm' : 'M'), x, y);
1146 },
1147
1148 /* Draw a line to a position.
1149 @param x (number) x-coordinate to move to or
1150 (number[][]) x-/y-coordinates to move to
1151 @param y (number) y-coordinate to move to (omitted if x is array)
1152 @param relative (boolean) true for coordinates relative to the current point,
1153 false for coordinates being absolute
1154 @return (SVGPath) this path */
1155 line: function(x, y, relative) {
1156 relative = (isArray(x) ? y : relative);
1157 return this._coords((relative ? 'l' : 'L'), x, y);
1158 },
1159
1160 /* Draw a horizontal line to a position.
1161 @param x (number) x-coordinate to draw to or
1162 (number[]) x-coordinates to draw to
1163 @param relative (boolean) true for coordinates relative to the current point,
1164 false for coordinates being absolute
1165 @return (SVGPath) this path */
1166 horiz: function(x, relative) {
1167 this._path += (relative ? 'h' : 'H') + (isArray(x) ? x.join(' ') : x);
1168 return this;
1169 },
1170
1171 /* Draw a vertical line to a position.
1172 @param y (number) y-coordinate to draw to or
1173 (number[]) y-coordinates to draw to
1174 @param relative (boolean) true for coordinates relative to the current point,
1175 false for coordinates being absolute
1176 @return (SVGPath) this path */
1177 vert: function(y, relative) {
1178 this._path += (relative ? 'v' : 'V') + (isArray(y) ? y.join(' ') : y);
1179 return this;
1180 },
1181
1182 /* Draw a cubic Bézier curve.
1183 @param x1 (number) x-coordinate of beginning control point or
1184 (number[][]) x-/y-coordinates of control and end points to draw to
1185 @param y1 (number) y-coordinate of beginning control point (omitted if x1 is array)
1186 @param x2 (number) x-coordinate of ending control point (omitted if x1 is array)
1187 @param y2 (number) y-coordinate of ending control point (omitted if x1 is array)
1188 @param x (number) x-coordinate of curve end (omitted if x1 is array)
1189 @param y (number) y-coordinate of curve end (omitted if x1 is array)
1190 @param relative (boolean) true for coordinates relative to the current point,
1191 false for coordinates being absolute
1192 @return (SVGPath) this path */
1193 curveC: function(x1, y1, x2, y2, x, y, relative) {
1194 relative = (isArray(x1) ? y1 : relative);
1195 return this._coords((relative ? 'c' : 'C'), x1, y1, x2, y2, x, y);
1196 },
1197
1198 /* Continue a cubic Bézier curve.
1199 Starting control point is the reflection of the previous end control point.
1200 @param x2 (number) x-coordinate of ending control point or
1201 (number[][]) x-/y-coordinates of control and end points to draw to
1202 @param y2 (number) y-coordinate of ending control point (omitted if x2 is array)
1203 @param x (number) x-coordinate of curve end (omitted if x2 is array)
1204 @param y (number) y-coordinate of curve end (omitted if x2 is array)
1205 @param relative (boolean) true for coordinates relative to the current point,
1206 false for coordinates being absolute
1207 @return (SVGPath) this path */
1208 smoothC: function(x2, y2, x, y, relative) {
1209 relative = (isArray(x2) ? y2 : relative);
1210 return this._coords((relative ? 's' : 'S'), x2, y2, x, y);
1211 },
1212
1213 /* Draw a quadratic Bézier curve.
1214 @param x1 (number) x-coordinate of control point or
1215 (number[][]) x-/y-coordinates of control and end points to draw to
1216 @param y1 (number) y-coordinate of control point (omitted if x1 is array)
1217 @param x (number) x-coordinate of curve end (omitted if x1 is array)
1218 @param y (number) y-coordinate of curve end (omitted if x1 is array)
1219 @param relative (boolean) true for coordinates relative to the current point,
1220 false for coordinates being absolute
1221 @return (SVGPath) this path */
1222 curveQ: function(x1, y1, x, y, relative) {
1223 relative = (isArray(x1) ? y1 : relative);
1224 return this._coords((relative ? 'q' : 'Q'), x1, y1, x, y);
1225 },
1226
1227 /* Continue a quadratic Bézier curve.
1228 Control point is the reflection of the previous control point.
1229 @param x (number) x-coordinate of curve end or
1230 (number[][]) x-/y-coordinates of points to draw to
1231 @param y (number) y-coordinate of curve end (omitted if x is array)
1232 @param relative (boolean) true for coordinates relative to the current point,
1233 false for coordinates being absolute
1234 @return (SVGPath) this path */
1235 smoothQ: function(x, y, relative) {
1236 relative = (isArray(x) ? y : relative);
1237 return this._coords((relative ? 't' : 'T'), x, y);
1238 },
1239
1240 /* Generate a path command with (a list of) coordinates. */
1241 _coords: function(cmd, x1, y1, x2, y2, x3, y3) {
1242 if (isArray(x1)) {
1243 for (var i = 0; i < x1.length; i++) {
1244 var cs = x1[i];
1245 this._path += (i == 0 ? cmd : ' ') + cs[0] + ',' + cs[1] +
1246 (cs.length < 4 ? '' : ' ' + cs[2] + ',' + cs[3] +
1247 (cs.length < 6 ? '': ' ' + cs[4] + ',' + cs[5]));
1248 }
1249 }
1250 else {
1251 this._path += cmd + x1 + ',' + y1 +
1252 (x2 == null ? '' : ' ' + x2 + ',' + y2 +
1253 (x3 == null ? '' : ' ' + x3 + ',' + y3));
1254 }
1255 return this;
1256 },
1257
1258 /* Draw an arc to a position.
1259 @param rx (number) x-radius of arc or
1260 (number/boolean[][]) x-/y-coordinates and flags for points to draw to
1261 @param ry (number) y-radius of arc (omitted if rx is array)
1262 @param xRotate (number) x-axis rotation (degrees, clockwise) (omitted if rx is array)
1263 @param large (boolean) true to draw the large part of the arc,
1264 false to draw the small part (omitted if rx is array)
1265 @param clockwise (boolean) true to draw the clockwise arc,
1266 false to draw the anti-clockwise arc (omitted if rx is array)
1267 @param x (number) x-coordinate of arc end (omitted if rx is array)
1268 @param y (number) y-coordinate of arc end (omitted if rx is array)
1269 @param relative (boolean) true for coordinates relative to the current point,
1270 false for coordinates being absolute
1271 @return (SVGPath) this path */
1272 arc: function(rx, ry, xRotate, large, clockwise, x, y, relative) {
1273 relative = (isArray(rx) ? ry : relative);
1274 this._path += (relative ? 'a' : 'A');
1275 if (isArray(rx)) {
1276 for (var i = 0; i < rx.length; i++) {
1277 var cs = rx[i];
1278 this._path += (i == 0 ? '' : ' ') + cs[0] + ',' + cs[1] + ' ' +
1279 cs[2] + ' ' + (cs[3] ? '1' : '0') + ',' +
1280 (cs[4] ? '1' : '0') + ' ' + cs[5] + ',' + cs[6];
1281 }
1282 }
1283 else {
1284 this._path += rx + ',' + ry + ' ' + xRotate + ' ' +
1285 (large ? '1' : '0') + ',' + (clockwise ? '1' : '0') + ' ' + x + ',' + y;
1286 }
1287 return this;
1288 },
1289
1290 /* Close the current path.
1291 @return (SVGPath) this path */
1292 close: function() {
1293 this._path += 'z';
1294 return this;
1295 },
1296
1297 /* Return the string rendering of the specified path.
1298 @return (string) stringified path */
1299 path: function() {
1300 return this._path;
1301 }
1302 });
1303
1304 SVGPath.prototype.moveTo = SVGPath.prototype.move;
1305 SVGPath.prototype.lineTo = SVGPath.prototype.line;
1306 SVGPath.prototype.horizTo = SVGPath.prototype.horiz;
1307 SVGPath.prototype.vertTo = SVGPath.prototype.vert;
1308 SVGPath.prototype.curveCTo = SVGPath.prototype.curveC;
1309 SVGPath.prototype.smoothCTo = SVGPath.prototype.smoothC;
1310 SVGPath.prototype.curveQTo = SVGPath.prototype.curveQ;
1311 SVGPath.prototype.smoothQTo = SVGPath.prototype.smoothQ;
1312 SVGPath.prototype.arcTo = SVGPath.prototype.arc;
1313
1314 /* Helper to generate an SVG text object.
1315 Obtain an instance from the SVGWrapper object.
1316 String calls together to generate the text and use its value:
1317 var text = root.createText();
1318 root.text(null, x, y, text.string('This is ').
1319 span('red', {fill: 'red'}).string('!'), {fill: 'blue'}); */
1320 function SVGText() {
1321 this._parts = []; // The components of the text object
1322 }
1323
1324 $.extend(SVGText.prototype, {
1325 /* Prepare to create a new text object.
1326 @return (SVGText) this text */
1327 reset: function() {
1328 this._parts = [];
1329 return this;
1330 },
1331
1332 /* Add a straight string value.
1333 @param value (string) the actual text
1334 @return (SVGText) this text object */
1335 string: function(value) {
1336 this._parts[this._parts.length] = ['text', value];
1337 return this;
1338 },
1339
1340 /* Add a separate text span that has its own settings.
1341 @param value (string) the actual text
1342 @param settings (object) the settings for this text
1343 @return (SVGText) this text object */
1344 span: function(value, settings) {
1345 this._parts[this._parts.length] = ['tspan', value, settings];
1346 return this;
1347 },
1348
1349 /* Add a reference to a previously defined text string.
1350 @param id (string) the ID of the actual text
1351 @param settings (object) the settings for this text
1352 @return (SVGText) this text object */
1353 ref: function(id, settings) {
1354 this._parts[this._parts.length] = ['tref', id, settings];
1355 return this;
1356 },
1357
1358 /* Add text drawn along a path.
1359 @param id (string) the ID of the path
1360 @param value (string) the actual text
1361 @param settings (object) the settings for this text
1362 @return (SVGText) this text object */
1363 path: function(id, value, settings) {
1364 this._parts[this._parts.length] = ['textpath', value,
1365 $.extend({href: id}, settings || {})];
1366 return this;
1367 }
1368 });
1369
1370 /* Attach the SVG functionality to a jQuery selection.
1371 @param command (string) the command to run (optional, default 'attach')
1372 @param options (object) the new settings to use for these SVG instances
1373 @return jQuery (object) for chaining further calls */
1374 $.fn.svg = function(options) {
1375 var otherArgs = Array.prototype.slice.call(arguments, 1);
1376 if (typeof options == 'string' && options == 'get') {
1377 return $.svg['_' + options + 'SVG'].apply($.svg, [this[0]].concat(otherArgs));
1378 }
1379 return this.each(function() {
1380 if (typeof options == 'string') {
1381 $.svg['_' + options + 'SVG'].apply($.svg, [this].concat(otherArgs));
1382 }
1383 else {
1384 $.svg._attachSVG(this, options || {});
1385 }
1386 });
1387 };
1388
1389 /* Determine whether an object is an array. */
1390 function isArray(a) {
1391 return (a && a.constructor == Array);
1392 }
1393
1394 // Singleton primary SVG interface
1395 $.svg = new SVGManager();
1396
1397 })(jQuery);