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
|
import EventTarget from './EventTarget.js'
import { assignNewId, adopt, makeInstance, eid } from './adopter.js'
import { map } from './utils.js'
import { matcher } from './helpers.js'
import { ns } from './namespaces.js'
import { extend } from './tools.js'
import attr from './attr.js'
export default class Dom extends EventTarget {
constructor (node) {
super(node)
this.node = node
this.type = node.nodeName
}
// Add given element at a position
add (element, i) {
element = makeInstance(element)
if (i == null) {
this.node.appendChild(element.node)
} else if (element.node !== this.node.childNodes[i]) {
this.node.insertBefore(element.node, this.node.childNodes[i])
}
return this
}
// Add element to given container and return self
addTo (parent) {
return makeInstance(parent).put(this)
}
// Returns all child elements
children () {
return map(this.node.children, function (node) {
return adopt(node)
})
}
// Remove all elements in this container
clear () {
// remove children
while (this.node.hasChildNodes()) {
this.node.removeChild(this.node.lastChild)
}
// remove defs reference
delete this._defs
return this
}
// Clone element
clone (parent) {
// write dom data to the dom so the clone can pickup the data
this.writeDataToDom()
// clone element and assign new id
let clone = assignNewId(this.node.cloneNode(true))
// insert the clone in the given parent or after myself
if (parent) parent.add(clone)
// FIXME: after might not be available here
else this.after(clone)
return clone
}
// Iterates over all children and invokes a given block
each (block, deep) {
var children = this.children()
var i, il
for (i = 0, il = children.length; i < il; i++) {
block.apply(children[i], [i, children])
if (deep) {
children[i].each(block, deep)
}
}
return this
}
// Get first child
first () {
return adopt(this.node.firstChild)
}
// Get a element at the given index
get (i) {
return adopt(this.node.childNodes[i])
}
getEventHolder () {
return this.node
}
getEventTarget () {
return this.node
}
// Checks if the given element is a child
has (element) {
return this.index(element) >= 0
}
// Get / set id
id (id) {
// generate new id if no id set
if (typeof id === 'undefined' && !this.node.id) {
this.node.id = eid(this.type)
}
// dont't set directly width this.node.id to make `null` work correctly
return this.attr('id', id)
}
// Gets index of given element
index (element) {
return [].slice.call(this.node.childNodes).indexOf(element.node)
}
// Get the last child
last () {
return adopt(this.node.lastChild)
}
// matches the element vs a css selector
matches (selector) {
return matcher(this.node, selector)
}
// Returns the svg node to call native svg methods on it
native () {
return this.node
}
// Returns the parent element instance
parent (type) {
var parent = this
// check for parent
if (!parent.node.parentNode) return null
// get parent element
parent = adopt(parent.node.parentNode)
if (!type) return parent
// loop trough ancestors if type is given
while (parent && parent.node instanceof window.SVGElement) {
if (typeof type === 'string' ? parent.matches(type) : parent instanceof type) return parent
parent = adopt(parent.node.parentNode)
}
}
// Basically does the same as `add()` but returns the added element instead
put (element, i) {
this.add(element, i)
return element
}
// Add element to given container and return container
putIn (parent) {
return makeInstance(parent).add(this)
}
// Remove element
remove () {
if (this.parent()) {
this.parent().removeElement(this)
}
return this
}
// Remove a given child
removeElement (element) {
this.node.removeChild(element.node)
return this
}
// Replace element
replace (element) {
// FIXME: after() might not be available here
this.after(element).remove()
return element
}
// Return id on string conversion
toString () {
return this.id()
}
// Import raw svg
svg (svg) {
var well, len
// act as a setter if svg is given
if (svg) {
// create temporary holder
well = document.createElementNS(ns, 'svg')
// dump raw svg
well.innerHTML = svg
// transplant nodes
for (len = well.children.length; len--;) {
this.node.appendChild(well.firstElementChild)
}
// otherwise act as a getter
} else {
// write svgjs data to the dom
this.writeDataToDom()
return this.node.outerHTML
}
return this
}
// write svgjs data to the dom
writeDataToDom () {
// dump variables recursively
this.each(function () {
this.writeDataToDom()
})
return this
}
}
extend(Dom, { attr })
|