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
|
/* global createElement */
SVG.Parent = SVG.invent({
// Initialize node
create: function (node) {
this.constructor(node)
},
// Inherit from
inherit: SVG.Element,
// Add class methods
extend: {
// Returns all child elements
children: function () {
return SVG.utils.map(this.node.children, function (node) {
return SVG.adopt(node)
})
},
// Add given element at a position
add: function (element, i) {
element = createElement(element)
if (element.node !== this.node.children[i]) {
this.node.insertBefore(element.node, this.node.children[i] || null)
}
return this
},
// Basically does the same as `add()` but returns the added element instead
put: function (element, i) {
this.add(element, i)
return element.instance || element
},
// Checks if the given element is a child
has: function (element) {
return this.index(element) >= 0
},
// Gets index of given element
index: function (element) {
return [].slice.call(this.node.children).indexOf(element.node)
},
// Get a element at the given index
get: function (i) {
return SVG.adopt(this.node.children[i])
},
// Get first child
first: function () {
return this.get(0)
},
// Get the last child
last: function () {
return this.get(this.node.children.length - 1)
},
// Iterates over all children and invokes a given block
each: function (block, deep) {
var children = this.children()
var i, il
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.Parent)) {
children[i].each(block, deep)
}
}
return this
},
// Remove a given child
removeElement: function (element) {
this.node.removeChild(element.node)
return this
},
// Remove all elements in this container
clear: function () {
// remove children
while (this.node.hasChildNodes()) {
this.node.removeChild(this.node.lastChild)
}
// remove defs reference
delete this._defs
return this
}
}
})
|