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
|
import { getClass, makeInstance, register, root } from '../utils/adopter.js'
import { proportionalSize } from '../utils/utils.js'
import { reference } from '../modules/core/regex.js'
import Dom from './Dom.js'
import SVGNumber from '../types/SVGNumber.js'
const Doc = getClass(root)
export default class Element extends Dom {
constructor (node, attrs) {
super(node, attrs)
// initialize data object
this.dom = {}
// create circular reference
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 element by its center
center (x, y) {
return this.cx(x).cy(y)
}
// Move by center over x-axis
cx (x) {
return x == null ? this.x() + this.width() / 2 : this.x(x - this.width() / 2)
}
// Move by center over y-axis
cy (y) {
return y == null
? this.y() + this.height() / 2
: this.y(y - this.height() / 2)
}
// Get defs
defs () {
return this.doc().defs()
}
// Get parent document
doc () {
let p = this.parent(Doc)
return p && p.doc()
}
getEventHolder () {
return this
}
// Set height of element
height (height) {
return this.attr('height', height)
}
// Checks whether the given point inside the bounding box of the element
inside (x, y) {
let box = this.bbox()
return x > box.x &&
y > box.y &&
x < box.x + box.width &&
y < box.y + box.height
}
// Move element to given x and y values
move (x, y) {
return this.x(x).y(y)
}
// return array of all ancestors of given type up to the root svg
parents (type) {
let parents = []
let parent = this
do {
parent = parent.parent(type)
if (!parent || parent instanceof getClass('HtmlNode')) break
parents.push(parent)
} while (parent.parent)
return parents
}
// Get referenced element form attribute value
reference (attr) {
attr = this.attr(attr)
if (!attr) return null
const m = attr.match(reference)
return m ? makeInstance(m[1]) : null
}
// set given data to the elements data property
setData (o) {
this.dom = o
return this
}
// Set element size to given width and height
size (width, height) {
let p = proportionalSize(this, width, height)
return this
.width(new SVGNumber(p.width))
.height(new SVGNumber(p.height))
}
// Set width of element
width (width) {
return this.attr('width', width)
}
// write svgjs data to the dom
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 super.writeDataToDom()
}
// Move over x-axis
x (x) {
return this.attr('x', x)
}
// Move over y-axis
y (y) {
return this.attr('y', y)
}
}
register(Element)
|