-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathsvg.js
2692 lines (2188 loc) · 66.7 KB
/
svg.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
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/* svg.js v0.30 - svg regex default color array number viewbox bbox rbox element container fx event defs group arrange mask clip gradient use doc shape rect ellipse line poly path plotable image text textpath nested sugar set memory loader - svgjs.com/license */
;(function() {
this.SVG = function(element) {
if (SVG.supported)
return new SVG.Doc(element)
}
// Default namespaces
SVG.ns = 'http://www.w3.org/2000/svg'
SVG.xlink = 'http://www.w3.org/1999/xlink'
// Element id sequence
SVG.did = 1000
// Get next named element id
SVG.eid = function(name) {
return 'Svgjs' + name.charAt(0).toUpperCase() + name.slice(1) + (SVG.did++)
}
// Method for element creation
SVG.create = function(name) {
/* create element */
var element = SVG.document.createElementNS(this.ns, name)
/* apply unique id */
element.setAttribute('id', this.eid(name))
return element
}
// Method for extending objects
SVG.extend = function() {
var modules, methods, key, i
/* get list of modules */
modules = [].slice.call(arguments)
/* get object with extensions */
methods = modules.pop()
for (i = modules.length - 1; i >= 0; i--)
if (modules[i])
for (key in methods)
modules[i].prototype[key] = methods[key]
/* make sure SVG.Set inherits any newly added methods */
if (SVG.Set && SVG.Set.inherit)
SVG.Set.inherit()
}
// Method for getting an eleemnt by id
SVG.get = function(id) {
var node = SVG.document.getElementById(id)
if (node) return node.instance
}
// svg support test
SVG.supported = true;//(function() {
// return !! SVG.document.createElementNS &&
// !! SVG.document.createElementNS(SVG.ns,'svg').createSVGRect
//})()
//if (!SVG.supported) return false
SVG.regex = {
/* test a given value */
test: function(value, test) {
return this[test].test(value)
}
/* parse unit value */
, unit: /^(-?[\d\.]+)([a-z%]{0,2})$/
/* parse hex value */
, hex: /^#?([a-f\d]{2})([a-f\d]{2})([a-f\d]{2})$/i
/* parse rgb value */
, rgb: /rgb\((\d+),(\d+),(\d+)\)/
/* test hex value */
, isHex: /^#[a-f0-9]{3,6}$/i
/* test rgb value */
, isRgb: /^rgb\(/
/* test css declaration */
, isCss: /[^:]+:[^;]+;?/
/* test css property */
, isStyle: /^font|text|leading|cursor/
/* test for blank string */
, isBlank: /^(\s+)?$/
/* test for numeric string */
, isNumber: /^-?[\d\.]+$/
/* test for percent value */
, isPercent: /^-?[\d\.]+%$/
}
SVG.defaults = {
// Default matrix
matrix: '1 0 0 1 0 0'
// Default attribute values
, attrs: {
/* fill and stroke */
'fill-opacity': 1
, 'stroke-opacity': 1
, 'stroke-width': 0
, 'stroke-linejoin': 'miter'
, 'stroke-linecap': 'butt'
, fill: '#000000'
, stroke: '#000000'
, opacity: 1
/* position */
, x: 0
, y: 0
, cx: 0
, cy: 0
/* size */
, width: 0
, height: 0
/* radius */
, r: 0
, rx: 0
, ry: 0
/* gradient */
, offset: 0
, 'stop-opacity': 1
, 'stop-color': '#000000'
}
// Default transformation values
, trans: function() {
return {
/* translate */
x: 0
, y: 0
/* scale */
, scaleX: 1
, scaleY: 1
/* rotate */
, rotation: 0
/* skew */
, skewX: 0
, skewY: 0
/* matrix */
, matrix: this.matrix
, a: 1
, b: 0
, c: 0
, d: 1
, e: 0
, f: 0
}
}
}
SVG.Color = function(color) {
var match
/* initialize defaults */
this.r = 0
this.g = 0
this.b = 0
/* parse color */
if (typeof color == 'string') {
if (SVG.regex.isRgb.test(color)) {
/* get rgb values */
match = SVG.regex.rgb.exec(color.replace(/\s/g,''))
/* parse numeric values */
this.r = parseInt(match[1])
this.g = parseInt(match[2])
this.b = parseInt(match[3])
} else if (SVG.regex.isHex.test(color)) {
/* get hex values */
match = SVG.regex.hex.exec(this._fullHex(color))
/* parse numeric values */
this.r = parseInt(match[1], 16)
this.g = parseInt(match[2], 16)
this.b = parseInt(match[3], 16)
}
} else if (typeof color == 'object') {
this.r = color.r
this.g = color.g
this.b = color.b
}
}
SVG.extend(SVG.Color, {
// Default to hex conversion
toString: function() {
return this.toHex()
}
// Build hex value
, toHex: function() {
return '#'
+ this._compToHex(this.r)
+ this._compToHex(this.g)
+ this._compToHex(this.b)
}
// Build rgb value
, toRgb: function() {
return 'rgb(' + [this.r, this.g, this.b].join() + ')'
}
// Calculate true brightness
, brightness: function() {
return (this.r / 255 * 0.30)
+ (this.g / 255 * 0.59)
+ (this.b / 255 * 0.11)
}
// Private: ensure to six-based hex
, _fullHex: function(hex) {
return hex.length == 4 ?
[ '#',
hex.substring(1, 2), hex.substring(1, 2)
, hex.substring(2, 3), hex.substring(2, 3)
, hex.substring(3, 4), hex.substring(3, 4)
].join('') : hex
}
// Private: component to hex value
, _compToHex: function(comp) {
var hex = comp.toString(16)
return hex.length == 1 ? '0' + hex : hex
}
})
// Test if given value is a color string
SVG.Color.test = function(color) {
color += ''
return SVG.regex.isHex.test(color)
|| SVG.regex.isRgb.test(color)
}
// Test if given value is a rgb object
SVG.Color.isRgb = function(color) {
return color && typeof color.r == 'number'
}
SVG.Array = function(array, fallback) {
this.value = array || []
if (this.value.length == 0 && fallback)
this.value = fallback
}
SVG.extend(SVG.Array, {
// Convert array to string
toString: function() {
var array = []
/* detect array type */
if (Array.isArray(this.value[0])) {
/* it is a poly point string */
for (var i = 0, il = this.value.length; i < il; i++)
array.push(this.value[i].join(','))
} else {
/* it's a regular array */
array = this.value
}
return array.join(' ')
}
// Real value
, valueOf: function() {
return this.value
}
})
SVG.Number = function(value) {
/* initialize defaults */
this.value = 0
this.unit = ''
/* parse value */
switch(typeof value) {
case 'number':
/* ensure a valid numeric value */
this.value = isNaN(value) ? 0 : !isFinite(value) ? (value < 0 ? Number.MIN_VALUE : Number.MAX_VALUE) : value
break
case 'string':
var match = value.match(SVG.regex.unit)
if (match) {
/* make value numeric */
this.value = parseFloat(match[1])
/* normalize percent value */
if (match[2] == '%')
this.value /= 100
/* store unit */
this.unit = match[2]
}
break
default:
if (value instanceof SVG.Number) {
this.value = value.value
this.unit = value.unit
}
break
}
}
SVG.extend(SVG.Number, {
// Stringalize
toString: function() {
return (this.unit == '%' ? ~~(this.value * 1e8) / 1e6 : this.value) + this.unit
}
, // Convert to primitive
valueOf: function() {
return this.value
}
// Convert to different unit
, to: function(unit) {
if (typeof unit === 'string')
this.unit = unit
return this
}
// Add number
, plus: function(number) {
this.value = this + new SVG.Number(number)
return this
}
// Subtract number
, minus: function(number) {
return this.plus(-new SVG.Number(number))
}
// Multiply number
, times: function(number) {
this.value = this * new SVG.Number(number)
return this
}
// Divide number
, divide: function(number) {
this.value = this / new SVG.Number(number)
return this
}
})
SVG.ViewBox = function(element) {
var x, y, width, height
, box = element.bbox()
, view = (element.attr('viewBox') || '').match(/-?[\d\.]+/g)
/* clone attributes */
this.x = box.x
this.y = box.y
this.width = element.node.offsetWidth || element.attr('width')
this.height = element.node.offsetHeight || element.attr('height')
if (view) {
/* get width and height from viewbox */
x = parseFloat(view[0])
y = parseFloat(view[1])
width = parseFloat(view[2])
height = parseFloat(view[3])
/* calculate zoom accoring to viewbox */
this.zoom = ((this.width / this.height) > (width / height)) ?
this.height / height :
this.width / width
/* calculate real pixel dimensions on parent SVG.Doc element */
this.x = x
this.y = y
this.width = width
this.height = height
}
/* ensure a default zoom value */
this.zoom = this.zoom || 1
}
//
SVG.extend(SVG.ViewBox, {
// Parse viewbox to string
toString: function() {
return this.x + ' ' + this.y + ' ' + this.width + ' ' + this.height
}
})
SVG.BBox = function(element) {
var box
/* initialize zero box */
this.x = 0
this.y = 0
this.width = 0
this.height = 0
/* get values if element is given */
if (element) {
try {
/* actual, native bounding box */
box = element.node.getBBox()
} catch(e) {
/* fallback for some browsers */
box = {
x: element.node.clientLeft
, y: element.node.clientTop
, width: element.node.clientWidth
, height: element.node.clientHeight
}
}
/* include translations on x an y */
this.x = box.x + element.trans.x
this.y = box.y + element.trans.y
/* plain width and height */
this.width = box.width * element.trans.scaleX
this.height = box.height * element.trans.scaleY
}
/* add the center */
this.cx = this.x + this.width / 2
this.cy = this.y + this.height / 2
}
//
SVG.extend(SVG.BBox, {
// merge bounding box with another, return a new instance
merge: function(box) {
var b = new SVG.BBox()
/* merge box */
b.x = Math.min(this.x, box.x)
b.y = Math.min(this.y, box.y)
b.width = Math.max(this.x + this.width, box.x + box.width) - b.x
b.height = Math.max(this.y + this.height, box.y + box.height) - b.y
/* add the center */
b.cx = b.x + b.width / 2
b.cy = b.y + b.height / 2
return b
}
})
SVG.RBox = function(element) {
var e, zoom
, box = {}
/* initialize zero box */
this.x = 0
this.y = 0
this.width = 0
this.height = 0
if (element) {
e = element.doc().parent
zoom = element.doc().viewbox().zoom
/* actual, native bounding box */
box = element.node.getBoundingClientRect()
/* get screen offset */
this.x = box.left
this.y = box.top
/* subtract parent offset */
this.x -= e.offsetLeft
this.y -= e.offsetTop
while (e = e.offsetParent) {
this.x -= e.offsetLeft
this.y -= e.offsetTop
}
/* calculate cumulative zoom from svg SVG.documents */
e = element
while (e = e.parent) {
if (e.type == 'svg' && e.viewbox) {
zoom *= e.viewbox().zoom
this.x -= e.x() || 0
this.y -= e.y() || 0
}
}
}
/* recalculate viewbox distortion */
this.x /= zoom
this.y /= zoom
this.width = box.width /= zoom
this.height = box.height /= zoom
/* add the center */
this.cx = this.x + this.width / 2
this.cy = this.y + this.height / 2
}
SVG.Element = function(node) {
/* make stroke value accessible dynamically */
this._stroke = SVG.defaults.attrs.stroke
/* initialize style store */
this.styles = {}
/* initialize transformation store with defaults */
this.trans = SVG.defaults.trans()
/* keep reference to the element node */
if (this.node = node) {
this.type = node.nodeName
this.node.instance = this
}
}
//
SVG.extend(SVG.Element, {
// Move over x-axis
x: function(x) {
if (x) {
x = new SVG.Number(x)
x.value /= this.trans.scaleX
}
return this.attr('x', x)
}
// Move over y-axis
, y: function(y) {
if (y) {
y = new SVG.Number(y)
y.value /= this.trans.scaleY
}
return this.attr('y', y)
}
// Move by center over x-axis
, cx: function(x) {
return x == null ? this.bbox().cx : this.x(x - this.bbox().width / 2)
}
// Move by center over y-axis
, cy: function(y) {
return y == null ? this.bbox().cy : this.y(y - this.bbox().height / 2)
}
// Move element to given x and y values
, move: function(x, y) {
return this.x(x).y(y)
}
// Move element by its center
, center: function(x, y) {
return this.cx(x).cy(y)
}
// Set element size to given width and height
, size: function(width, height) {
return this.attr({
width: new SVG.Number(width)
, height: new SVG.Number(height)
})
}
// Clone element
, clone: function() {
var clone , attr
, type = this.type
/* invoke shape method with shape-specific arguments */
clone = type == 'rect' || type == 'ellipse' ?
this.parent[type](0,0) :
type == 'line' ?
this.parent[type](0,0,0,0) :
type == 'image' ?
this.parent[type](this.src) :
type == 'text' ?
this.parent[type](this.content) :
type == 'path' ?
this.parent[type](this.attr('d')) :
type == 'polyline' || type == 'polygon' ?
this.parent[type](this.attr('points')) :
type == 'g' ?
this.parent.group() :
this.parent[type]()
/* apply attributes attributes */
attr = this.attr()
delete attr.id
clone.attr(attr)
/* copy transformations */
clone.trans = this.trans
/* apply attributes and translations */
return clone.transform({})
}
// Remove element
, remove: function() {
if (this.parent)
this.parent.removeElement(this)
return this
}
// Get parent SVG.document
, doc: function(type) {
return this._parent(type || SVG.Doc)
}
// Set svg element attribute
, attr: function(a, v, n) {
if (a == null) {
/* get an object of attributes */
a = {}
v = this.node.attributes
for (n = v.length - 1; n >= 0; n--)
a[v[n].nodeName] = SVG.regex.test(v[n].nodeValue, 'isNumber') ? parseFloat(v[n].nodeValue) : v[n].nodeValue
return a
} else if (typeof a == 'object') {
/* apply every attribute individually if an object is passed */
for (v in a) this.attr(v, a[v])
} else if (v === null) {
/* remove value */
this.node.removeAttribute(a)
} else if (v == null) {
/* act as a getter for style attributes */
if (this._isStyle(a)) {
return a == 'text' ?
this.content :
a == 'leading' && this.leading ?
this.leading() :
this.style(a)
/* act as a getter if the first and only argument is not an object */
} else {
v = this.node.getAttribute(a)
return v == null ?
SVG.defaults.attrs[a] :
SVG.regex.test(v, 'isNumber') ?
parseFloat(v) : v
}
} else if (a == 'style') {
/* redirect to the style method */
return this.style(v)
} else {
/* treat x differently on text elements */
if (a == 'x' && Array.isArray(this.lines))
for (n = this.lines.length - 1; n >= 0; n--)
this.lines[n].attr(a, v)
/* BUG FIX: some browsers will render a stroke if a color is given even though stroke width is 0 */
if (a == 'stroke-width')
this.attr('stroke', parseFloat(v) > 0 ? this._stroke : null)
else if (a == 'stroke')
this._stroke = v
/* ensure full hex color */
if (SVG.Color.test(v) || SVG.Color.isRgb(v))
v = new SVG.Color(v)
/* ensure correct numeric values */
else if (typeof v === 'number')
v = new SVG.Number(v)
/* parse array values */
else if (Array.isArray(v))
v = new SVG.Array(v)
/* set give attribute on node */
n != null ?
this.node.setAttributeNS(n, a, v.toString()) :
this.node.setAttribute(a, v.toString())
/* if the passed argument belongs in the style as well, add it there */
if (this._isStyle(a)) {
a == 'text' ?
this.text(v) :
a == 'leading' && this.leading ?
this.leading(v) :
this.style(a, v)
/* rebuild if required */
if (this.rebuild)
this.rebuild(a, v)
}
}
return this
}
// Manage transformations
, transform: function(o, v) {
if (arguments.length == 0) {
/* act as a getter if no argument is given */
return this.trans
} else if (typeof o === 'string') {
/* act as a getter if only one string argument is given */
if (arguments.length < 2)
return this.trans[o]
/* apply transformations as object if key value arguments are given*/
var transform = {}
transform[o] = v
return this.transform(transform)
}
/* ... otherwise continue as a setter */
var transform = []
/* parse matrix */
o = this._parseMatrix(o)
/* merge values */
for (v in o)
if (o[v] != null)
this.trans[v] = o[v]
/* compile matrix */
this.trans.matrix = this.trans.a
+ ' ' + this.trans.b
+ ' ' + this.trans.c
+ ' ' + this.trans.d
+ ' ' + this.trans.e
+ ' ' + this.trans.f
/* alias current transformations */
o = this.trans
/* add matrix */
if (o.matrix != SVG.defaults.matrix)
transform.push('matrix(' + o.matrix + ')')
/* add rotation */
if (o.rotation != 0)
transform.push('rotate(' + o.rotation + ' ' + (o.cx == null ? this.bbox().cx : o.cx) + ' ' + (o.cy == null ? this.bbox().cy : o.cy) + ')')
/* add scale */
if (o.scaleX != 1 || o.scaleY != 1)
transform.push('scale(' + o.scaleX + ' ' + o.scaleY + ')')
/* add skew on x axis */
if (o.skewX != 0)
transform.push('skewX(' + o.skewX + ')')
/* add skew on y axis */
if (o.skewY != 0)
transform.push('skewY(' + o.skewY + ')')
/* add translation */
if (o.x != 0 || o.y != 0)
transform.push('translate(' + new SVG.Number(o.x / o.scaleX) + ' ' + new SVG.Number(o.y / o.scaleY) + ')')
/* add offset translation */
if (this._offset && this._offset.x != 0 && this._offset.y != 0)
transform.push('translate(' + (-this._offset.x) + ' ' + (-this._offset.y) + ')')
/* update transformations, even if there are none */
if (transform.length == 0)
this.node.removeAttribute('transform')
else
this.node.setAttribute('transform', transform.join(' '))
return this
}
// Dynamic style generator
, style: function(s, v) {
if (arguments.length == 0) {
/* get full style */
return this.attr('style') || ''
} else if (arguments.length < 2) {
/* apply every style individually if an object is passed */
if (typeof s == 'object') {
for (v in s) this.style(v, s[v])
} else if (SVG.regex.isCss.test(s)) {
/* parse css string */
s = s.split(';')
/* apply every definition individually */
for (var i = 0; i < s.length; i++) {
v = s[i].split(':')
if (v.length == 2)
this.style(v[0].replace(/\s+/g, ''), v[1].replace(/^\s+/,'').replace(/\s+$/,''))
}
} else {
/* act as a getter if the first and only argument is not an object */
return this.styles[s]
}
} else if (v === null || SVG.regex.test(v, 'isBlank')) {
/* remove value */
delete this.styles[s]
} else {
/* store value */
this.styles[s] = v
}
/* rebuild style string */
s = ''
for (v in this.styles)
s += v + ':' + this.styles[v] + ';'
/* apply style */
if (s == '')
this.node.removeAttribute('style')
else
this.node.setAttribute('style', s)
return this
}
// Store data values on svg nodes
, data: function(a, v, r) {
if (arguments.length < 2) {
try {
return JSON.parse(this.attr('data-' + a))
} catch(e) {
return this.attr('data-' + a)
}
} else {
this.attr(
'data-' + a
, v === null ?
null :
r === true || typeof v === 'string' || typeof v === 'number' ?
v :
JSON.stringify(v)
)
}
return this
}
// Get bounding box
, bbox: function() {
return new SVG.BBox(this)
}
// Get rect box
, rbox: function() {
return new SVG.RBox(this)
}
// Checks whether the given point inside the bounding box of the element
, inside: function(x, y) {
var box = this.bbox()
return x > box.x
&& y > box.y
&& x < box.x + box.width
&& y < box.y + box.height
}
// Show element
, show: function() {
return this.style('display', '')
}
// Hide element
, hide: function() {
return this.style('display', 'none')
}
// Is element visible?
, visible: function() {
return this.style('display') != 'none'
}
// Return id on string conversion
, toString: function() {
return this.attr('id')
}
// Private: find svg parent by instance
, _parent: function(parent) {
var element = this
while (element != null && !(element instanceof parent))
element = element.parent
return element
}
// Private: tester method for style detection
, _isStyle: function(a) {
return typeof a == 'string' ? SVG.regex.test(a, 'isStyle') : false
}
// Private: parse a matrix string
, _parseMatrix: function(o) {
if (o.matrix) {
/* split matrix string */
var m = o.matrix.replace(/\s/g, '').split(',')
/* pasrse values */
if (m.length == 6) {
o.a = parseFloat(m[0])
o.b = parseFloat(m[1])
o.c = parseFloat(m[2])
o.d = parseFloat(m[3])
o.e = parseFloat(m[4])
o.f = parseFloat(m[5])
}
}
return o
}
})
SVG.Container = function(element) {
this.constructor.call(this, element)
}
// Inherit from SVG.Element
SVG.Container.prototype = new SVG.Element
//
SVG.extend(SVG.Container, {
// Returns all child elements
children: function() {
return this._children || (this._children = [])
}
// Add given element at a position
, add: function(element, i) {
if (!this.has(element)) {
/* define insertion index if none given */
i = i == null ? this.children().length : i
/* remove references from previous parent */
if (element.parent) {
var index = element.parent.children().indexOf(element)
element.parent.children().splice(index, 1)
}
/* add element references */
this.children().splice(i, 0, element)
this.node.insertBefore(element.node, this.node.childNodes[i] || null)
element.parent = this
}
/* reposition defs */
if (this._defs) {
this.node.removeChild(this._defs.node)
this.node.appendChild(this._defs.node)
}
return this
}
// Basically does the same as `add()` but returns the added element instead
, put: function(element, i) {
this.add(element, i)
return element
}
// Checks if the given element is a child
, has: function(element) {
return this.children().indexOf(element) >= 0
}
// Get a element at the given index
, get: function(i) {
return this.children()[i]
}
// Iterates over all children and invokes a given block
, each: function(block, deep) {
var i, il
, children = this.children()
for (i = 0, il = children.length; i < il; i++) {
if (children[i] instanceof SVG.Element)
block.apply(children[i], [i, children])
if (deep && (children[i] instanceof SVG.Container))
children[i].each(block, deep)
}
return this
}
// Remove a child element at a position
, removeElement: function(element) {
var i = this.children().indexOf(element)
this.children().splice(i, 1)
this.node.removeChild(element.node)
element.parent = null