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
|
import {proportionalSize, matcher} from './helpers.js'
import {makeInstance, adopt, assignNewId, eid, root, getClass} from './adopter.js'
import {delimiter} from './regex.js'
import {ns} from './namespaces.js'
import SVGNumber from './SVGNumber.js'
import {registerMethods} from './methods.js'
import {registerConstructor} from './methods.js'
const Doc = getClass(root)
export const name = 'Element'
export function setup (node) {
// initialize data object
this.dom = {}
// create circular reference
this.node = node
this.type = node.nodeName
this.node.instance = this
if (node.hasAttribute('svgjs:data')) {
// pull svgjs data from the dom (getAttributeNS doesn't work in html5)
this.setData(JSON.parse(node.getAttribute('svgjs:data')) || {})
}
}
// Move over x-axis
export function x (x) {
return this.attr('x', x)
}
// Move over y-axis
export function y (y) {
return this.attr('y', y)
}
// Move by center over x-axis
export function cx (x) {
return x == null ? this.x() + this.width() / 2 : this.x(x - this.width() / 2)
}
// Move by center over y-axis
export function cy (y) {
return y == null
? this.y() + this.height() / 2
: this.y(y - this.height() / 2)
}
// Move element to given x and y values
export function move (x, y) {
return this.x(x).y(y)
}
// Move element by its center
export function center (x, y) {
return this.cx(x).cy(y)
}
// Set width of element
export function width (width) {
return this.attr('width', width)
}
// Set height of element
export function height (height) {
return this.attr('height', height)
}
// Set element size to given width and height
export function size (width, height) {
let p = proportionalSize(this, width, height)
return this
.width(new SVGNumber(p.width))
.height(new SVGNumber(p.height))
}
// Clone element
export function 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)
else this.after(clone)
return clone
}
// Remove element
export function remove () {
if (this.parent()) { this.parent().removeElement(this) }
return this
}
// Replace element
export function replace (element) {
this.after(element).remove()
return element
}
// Add element to given container and return self
export function addTo (parent) {
return makeInstance(parent).put(this)
}
// Add element to given container and return container
export function putIn (parent) {
return makeInstance(parent).add(this)
}
// Get / set id
export function 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)
}
// Checks whether the given point inside the bounding box of the element
export function inside (x, y) {
let box = this.bbox()
return x > box.x &&
y > box.y &&
x < box.x + box.width &&
y < box.y + box.height
}
// Return id on string conversion
export function toString () {
return this.id()
}
// Return array of classes on the node
export function classes () {
var attr = this.attr('class')
return attr == null ? [] : attr.trim().split(delimiter)
}
// Return true if class exists on the node, false otherwise
export function hasClass (name) {
return this.classes().indexOf(name) !== -1
}
// Add class to the node
export function addClass (name) {
if (!this.hasClass(name)) {
var array = this.classes()
array.push(name)
this.attr('class', array.join(' '))
}
return this
}
// Remove class from the node
export function removeClass (name) {
if (this.hasClass(name)) {
this.attr('class', this.classes().filter(function (c) {
return c !== name
}).join(' '))
}
return this
}
// Toggle the presence of a class on the node
export function toggleClass (name) {
return this.hasClass(name) ? this.removeClass(name) : this.addClass(name)
}
// FIXME: getIdFromReference
// Get referenced element form attribute value
export function reference (attr) {
return get(this.attr(attr))
}
// Returns the parent element instance
export function 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)
}
}
// Get parent document
export function doc () {
let p = this.parent(Doc)
return p && p.doc()
}
// Get defs
export function defs () {
return this.doc().defs()
}
// return array of all ancestors of given type up to the root svg
export function parents (type) {
let parents = []
let parent = this
do {
parent = parent.parent(type)
if (!parent || !parent.node) break
parents.push(parent)
} while (parent.parent)
return parents
}
// matches the element vs a css selector
export function matches (selector) {
return matches(this.node, selector)
}
// Returns the svg node to call native svg methods on it
export function native () {
return this.node
}
// Import raw svg
export function svg () {
// write svgjs data to the dom
this.writeDataToDom()
return this.node.outerHTML
}
// write svgjs data to the dom
export function writeDataToDom () {
// remove previously set data
this.node.removeAttribute('svgjs:data')
if (Object.keys(this.dom).length) {
this.node.setAttribute('svgjs:data', JSON.stringify(this.dom)) // see #428
}
return this
}
// set given data to the elements data property
export function setData (o) {
this.dom = o
return this
}
export function getEventTarget () {
return this.node
}
registerMethods('Element', {
x, y, cx, cy, move, center, width, height, size, clone, remove, replace,
putIn, id, inside, toString, classes, hasClass, addClass, removeClass,
toggleClass, reference, doc, defs, parents, matches, native, svg,
writeDataToDom, setData, getEventTarget
})
registerConstructor('Element', setup)
|