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.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  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, writeDataToDom } 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. ) {
  86. parents.push(parent)
  87. if (!isSelector && parent.node === until.node) {
  88. break
  89. }
  90. if (isSelector && parent.matches(until)) {
  91. break
  92. }
  93. if (parent.node === this.root().node) {
  94. // We worked our way to the root and didn't match `until`
  95. return null
  96. }
  97. }
  98. return parents
  99. }
  100. // Get referenced element form attribute value
  101. reference(attr) {
  102. attr = this.attr(attr)
  103. if (!attr) return null
  104. const m = (attr + '').match(reference)
  105. return m ? makeInstance(m[1]) : null
  106. }
  107. // Get parent document
  108. root() {
  109. const p = this.parent(getClass(root))
  110. return p && p.root()
  111. }
  112. // set given data to the elements data property
  113. setData(o) {
  114. this.dom = o
  115. return this
  116. }
  117. // Set element size to given width and height
  118. size(width, height) {
  119. const p = proportionalSize(this, width, height)
  120. return this.width(new SVGNumber(p.width)).height(new SVGNumber(p.height))
  121. }
  122. // Set width of element
  123. width(width) {
  124. return this.attr('width', width)
  125. }
  126. // write svgjs data to the dom
  127. writeDataToDom() {
  128. writeDataToDom(this, this.dom)
  129. return super.writeDataToDom()
  130. }
  131. // Move over x-axis
  132. x(x) {
  133. return this.attr('x', x)
  134. }
  135. // Move over y-axis
  136. y(y) {
  137. return this.attr('y', y)
  138. }
  139. }
  140. extend(Element, {
  141. bbox,
  142. rbox,
  143. inside,
  144. point,
  145. ctm,
  146. screenCTM
  147. })
  148. register(Element, 'Element')