doms.js 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  1. import Util from './Util.js'
  2. /**
  3. * Dom工具类
  4. * 代码使用的leaflet开源工具
  5. * https://github.com/Leaflet/Leaflet/tree/master/src/core
  6. * @ignore
  7. */
  8. class DomUtil {
  9. /**
  10. * Returns an element given its DOM id, or returns the element itself
  11. * if it was passed directly.
  12. * @ignore
  13. * @param {*} id
  14. */
  15. static get(id) {
  16. return typeof id === 'string' ? document.getElementById(id) : id
  17. }
  18. /**
  19. * Returns the value for a certain style attribute on an element,
  20. * including computed values or values set through CSS.
  21. * @ignore
  22. * @param {*} el
  23. * @param {*} style
  24. */
  25. static getStyle(el, style) {
  26. var value = el.style[style] || (el.currentStyle && el.currentStyle[style])
  27. if ((!value || value === 'auto') && document.defaultView) {
  28. var css = document.defaultView.getComputedStyle(el, null)
  29. value = css ? css[style] : null
  30. }
  31. return value === 'auto' ? null : value
  32. }
  33. /**
  34. * Creates an HTML element with `tagName`, sets its class to `className`, and optionally appends it to `container` element.
  35. * @ignore
  36. * @param {*} tagName
  37. * @param {*} className
  38. * @param {*} container
  39. */
  40. static create(tagName, className, container) {
  41. var el = document.createElement(tagName)
  42. el.className = className || ''
  43. if (container) {
  44. container.appendChild(el)
  45. }
  46. return el
  47. }
  48. /**
  49. * Removes `el` from its parent element
  50. * @ignore
  51. * @param {*} el
  52. */
  53. static remove(el) {
  54. var parent = el.parentNode
  55. if (parent) {
  56. parent.removeChild(el)
  57. }
  58. }
  59. /**
  60. * Removes all of `el`'s children elements from `el`
  61. * @ignore
  62. * @param {*} el
  63. */
  64. static empty(el) {
  65. while (el.firstChild) {
  66. el.removeChild(el.firstChild)
  67. }
  68. }
  69. /**
  70. * Returns `true` if the element's class attribute contains `name`.
  71. * @ignore
  72. * @param {*} el
  73. * @param {*} name
  74. */
  75. hasClass(el, name) {
  76. if (el.classList !== undefined) {
  77. return el.classList.contains(name)
  78. }
  79. var className = getClass(el)
  80. return (
  81. className.length > 0 &&
  82. new RegExp('(^|\\s)' + name + '(\\s|$)').test(className)
  83. )
  84. }
  85. /**
  86. * Adds `name` to the element's class attribute.
  87. * @ignore
  88. * @param {*} el
  89. * @param {*} name
  90. */
  91. static addClass(el, name) {
  92. if (el.classList !== undefined) {
  93. let classes = Util.splitWords(name)
  94. for (let i = 0, len = classes.length; i < len; i++) {
  95. el.classList.add(classes[i])
  96. }
  97. } else if (!this.hasClass(el, name)) {
  98. let className = this.getClass(el)
  99. this.setClass(el, (className ? className + ' ' : '') + name)
  100. }
  101. }
  102. /**
  103. * Removes `name` from the element's class attribute.
  104. * @ignore
  105. * @param {*} el
  106. * @param {*} name
  107. */
  108. static removeClass(el, name) {
  109. if (el.classList !== undefined) {
  110. el.classList.remove(name)
  111. } else {
  112. this.setClass(
  113. el,
  114. Util.trim(
  115. (' ' + this.getClass(el) + ' ').replace(' ' + name + ' ', ' ')
  116. )
  117. )
  118. }
  119. }
  120. /**
  121. * Sets the element's class.
  122. * @ignore
  123. * @param {*} el
  124. * @param {*} name
  125. */
  126. static setClass(el, name) {
  127. if (el.className.baseVal === undefined) {
  128. el.className = name
  129. } else {
  130. // in case of SVG element
  131. el.className.baseVal = name
  132. }
  133. }
  134. /**
  135. * Returns the element's class.
  136. * @ignore
  137. * @param {*} el
  138. */
  139. static getClass(el) {
  140. // Check if the element is an SVGElementInstance and use the correspondingElement instead
  141. // (Required for linked SVG elements in IE11.)
  142. if (el.correspondingElement) {
  143. el = el.correspondingElement
  144. }
  145. return el.className.baseVal === undefined ?
  146. el.className :
  147. el.className.baseVal
  148. }
  149. /**
  150. * @ignore
  151. * @param {*} path
  152. * @param {*} width
  153. * @param {*} height
  154. */
  155. static createSvg(width, height, path, container) {
  156. let svg = document.createElementNS('http://www.w3.org/2000/svg', 'svg:svg')
  157. svg.setAttribute('class', 'svg-path')
  158. svg.setAttribute('width', width)
  159. svg.setAttribute('height', height)
  160. svg.setAttribute('viewBox', `0 0 ${width} ${height}`)
  161. let pathEl = document.createElementNS('http://www.w3.org/2000/svg', 'path')
  162. pathEl.setAttribute('d', path)
  163. svg.appendChild(pathEl)
  164. if (container) {
  165. container.appendChild(svg)
  166. }
  167. return svg
  168. }
  169. }
  170. export default DomUtil