You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

Element.js 3.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  1. import { bbox, rbox, inside } from '../types/Box.js'
  2. import { ctm, screenCTM } from '../types/Matrix.js'
  3. import {
  4. extend,
  5. getClass,
  6. makeInstance,
  7. register,
  8. root
  9. } from '../utils/adopter.js'
  10. import { globals } from '../utils/window.js'
  11. import { point } from '../types/Point.js'
  12. import { proportionalSize } from '../utils/utils.js'
  13. import { reference } from '../modules/core/regex.js'
  14. import Dom from './Dom.js'
  15. import List from '../types/List.js'
  16. import SVGNumber from '../types/SVGNumber.js'
  17. export default class Element extends Dom {
  18. constructor (node, attrs) {
  19. super(node, attrs)
  20. // initialize data object
  21. this.dom = {}
  22. // create circular reference
  23. this.node.instance = this
  24. if (node.hasAttribute('svgjs:data')) {
  25. // pull svgjs data from the dom (getAttributeNS doesn't work in html5)
  26. this.setData(JSON.parse(node.getAttribute('svgjs:data')) || {})
  27. }
  28. }
  29. // Move element by its center
  30. center (x, y) {
  31. return this.cx(x).cy(y)
  32. }
  33. // Move by center over x-axis
  34. cx (x) {
  35. return x == null
  36. ? this.x() + this.width() / 2
  37. : this.x(x - this.width() / 2)
  38. }
  39. // Move by center over y-axis
  40. cy (y) {
  41. return y == null
  42. ? this.y() + this.height() / 2
  43. : this.y(y - this.height() / 2)
  44. }
  45. // Get defs
  46. defs () {
  47. const root = this.root()
  48. return root && root.defs()
  49. }
  50. // Relative move over x and y axes
  51. dmove (x, y) {
  52. return this.dx(x).dy(y)
  53. }
  54. // Relative move over x axis
  55. dx (x = 0) {
  56. return this.x(new SVGNumber(x).plus(this.x()))
  57. }
  58. // Relative move over y axis
  59. dy (y = 0) {
  60. return this.y(new SVGNumber(y).plus(this.y()))
  61. }
  62. getEventHolder () {
  63. return this
  64. }
  65. // Set height of element
  66. height (height) {
  67. return this.attr('height', height)
  68. }
  69. // Move element to given x and y values
  70. move (x, y) {
  71. return this.x(x).y(y)
  72. }
  73. // return array of all ancestors of given type up to the root svg
  74. parents (until = this.root()) {
  75. const isSelector = typeof until === 'string'
  76. if (!isSelector) {
  77. until = makeInstance(until)
  78. }
  79. const parents = new List()
  80. let parent = this
  81. while (
  82. (parent = parent.parent())
  83. && parent.node !== globals.document
  84. && parent.nodeName !== '#document-fragment') {
  85. parents.push(parent)
  86. if (!isSelector && (parent.node === until.node)) {
  87. break
  88. }
  89. if (isSelector && parent.matches(until)) {
  90. break
  91. }
  92. if (parent.node === this.root().node) {
  93. // We worked our way to the root and didn't match `until`
  94. return null
  95. }
  96. }
  97. return parents
  98. }
  99. // Get referenced element form attribute value
  100. reference (attr) {
  101. attr = this.attr(attr)
  102. if (!attr) return null
  103. const m = (attr + '').match(reference)
  104. return m ? makeInstance(m[1]) : null
  105. }
  106. // Get parent document
  107. root () {
  108. const p = this.parent(getClass(root))
  109. return p && p.root()
  110. }
  111. // set given data to the elements data property
  112. setData (o) {
  113. this.dom = o
  114. return this
  115. }
  116. // Set element size to given width and height
  117. size (width, height) {
  118. const p = proportionalSize(this, width, height)
  119. return this
  120. .width(new SVGNumber(p.width))
  121. .height(new SVGNumber(p.height))
  122. }
  123. // Set width of element
  124. width (width) {
  125. return this.attr('width', width)
  126. }
  127. // write svgjs data to the dom
  128. writeDataToDom () {
  129. // remove previously set data
  130. this.node.removeAttribute('svgjs:data')
  131. if (Object.keys(this.dom).length) {
  132. this.node.setAttribute('svgjs:data', JSON.stringify(this.dom)) // see #428
  133. }
  134. return super.writeDataToDom()
  135. }
  136. // Move over x-axis
  137. x (x) {
  138. return this.attr('x', x)
  139. }
  140. // Move over y-axis
  141. y (y) {
  142. return this.attr('y', y)
  143. }
  144. }
  145. extend(Element, {
  146. bbox, rbox, inside, point, ctm, screenCTM
  147. })
  148. register(Element, 'Element')