Ви не можете вибрати більше 25 тем Теми мають розпочинатися з літери або цифри, можуть містити дефіси (-) і не повинні перевищувати 35 символів.

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249
  1. /**
  2. * @Author: Caven
  3. * @Date: 2019-12-31 17:50:13
  4. */
  5. import Util from './Util'
  6. /**
  7. * Dom Utils
  8. * some code reference leaflet
  9. * https://github.com/Leaflet/Leaflet/tree/master/src/core
  10. */
  11. class DomUtil {
  12. /**
  13. * Returns an element given its DOM id, or returns the element itself
  14. * if it was passed directly.
  15. * @param id
  16. * @returns {HTMLElement|*}
  17. */
  18. static get(id) {
  19. return typeof id === 'string' ? document.getElementById(id) : id
  20. }
  21. /**
  22. * Returns the value for a certain style attribute on an element,
  23. * including computed values or values set through CSS.
  24. * @param el
  25. * @param style
  26. * @returns {null|*}
  27. */
  28. static getStyle(el, style) {
  29. let value = el.style[style] || (el.currentStyle && el.currentStyle[style])
  30. if ((!value || value === 'auto') && document.defaultView) {
  31. let css = document.defaultView.getComputedStyle(el, null)
  32. value = css ? css[style] : null
  33. }
  34. return value === 'auto' ? null : value
  35. }
  36. /**
  37. * Creates an HTML element with `tagName`, sets its class to `className`, and optionally appends it to `container` element.
  38. * @param tagName
  39. * @param className
  40. * @param container
  41. * @returns {HTMLElement}
  42. */
  43. static create(tagName, className, container = null) {
  44. let el = document.createElement(tagName)
  45. el.className = className || ''
  46. if (container) {
  47. container.appendChild(el)
  48. }
  49. return el
  50. }
  51. /**
  52. * Removes `el` from its parent element
  53. * @param {*} el
  54. */
  55. static remove(el) {
  56. let parent = el.parentNode
  57. if (parent) {
  58. parent.removeChild(el)
  59. }
  60. }
  61. /**
  62. * Removes all of `el`'s children elements from `el`
  63. * @param {*} el
  64. */
  65. static empty(el) {
  66. while (el.firstChild) {
  67. el.removeChild(el.firstChild)
  68. }
  69. }
  70. /**
  71. * Returns `true` if the element's class attribute contains `name`.
  72. * @param {*} el
  73. * @param {*} name
  74. */
  75. static hasClass(el, name) {
  76. if (el.classList !== undefined) {
  77. return el.classList.contains(name)
  78. }
  79. let className = this.getClass(el)
  80. return (
  81. className.length > 0 &&
  82. new RegExp('(^|\\s)' + name + '(\\s|$)').test(className)
  83. )
  84. }
  85. /**
  86. * @function Adds `name` to the element's class attribute.
  87. * @param {*} el
  88. * @param {*} name
  89. */
  90. static addClass(el, name) {
  91. if (el.classList !== undefined) {
  92. let classes = Util.splitWords(name)
  93. for (let i = 0, len = classes.length; i < len; i++) {
  94. el.classList.add(classes[i])
  95. }
  96. } else if (!this.hasClass(el, name)) {
  97. let className = this.getClass(el)
  98. this.setClass(el, (className ? className + ' ' : '') + name)
  99. }
  100. }
  101. /**
  102. * @function Removes `name` from the element's class attribute.
  103. * @param {*} el
  104. * @param {*} name
  105. */
  106. static removeClass(el, name) {
  107. if (el.classList !== undefined) {
  108. el.classList.remove(name)
  109. } else {
  110. this.setClass(
  111. el,
  112. Util.trim(
  113. (' ' + this.getClass(el) + ' ').replace(' ' + name + ' ', ' ')
  114. )
  115. )
  116. }
  117. }
  118. /**
  119. * Sets the element's class.
  120. * @param {*} el
  121. * @param {*} name
  122. */
  123. static setClass(el, name) {
  124. if (el.className.baseVal === undefined) {
  125. el.className = name
  126. } else {
  127. // in case of SVG element
  128. el.className.baseVal = name
  129. }
  130. }
  131. /**
  132. * @function Returns the element's class.
  133. * @param {*} el
  134. */
  135. static getClass(el) {
  136. // Check if the element is an SVGElementInstance and use the correspondingElement instead
  137. // (Required for linked SVG elements in IE11.)
  138. if (el.correspondingElement) {
  139. el = el.correspondingElement
  140. }
  141. return el.className.baseVal === undefined
  142. ? el.className
  143. : el.className.baseVal
  144. }
  145. /**
  146. * Creates svg
  147. * @param width
  148. * @param height
  149. * @param path
  150. * @param container
  151. * @returns {SVGElement}
  152. */
  153. static createSvg(width, height, path, container) {
  154. let svg = document.createElementNS('http://www.w3.org/2000/svg', 'svg:svg')
  155. svg.setAttribute('class', 'svg-path')
  156. svg.setAttribute('width', width)
  157. svg.setAttribute('height', height)
  158. svg.setAttribute('viewBox', `0 0 ${width} ${height}`)
  159. let pathEl = document.createElementNS('http://www.w3.org/2000/svg', 'path')
  160. pathEl.setAttribute('d', path)
  161. svg.appendChild(pathEl)
  162. if (container) {
  163. container.appendChild(svg)
  164. }
  165. return svg
  166. }
  167. /**
  168. * Parses string to Dom
  169. * @param domStr
  170. * @param withWrapper
  171. * @param className
  172. * @returns {HTMLDivElement|NodeListOf<ChildNode>}
  173. */
  174. static parseDom(domStr, withWrapper, className) {
  175. withWrapper = withWrapper ?? false
  176. let el = document.createElement('div')
  177. el.className = className || ''
  178. el.innerHTML = domStr
  179. return withWrapper ? el : el.childNodes
  180. }
  181. /**
  182. * enter full screen
  183. * @param el
  184. */
  185. static enterFullscreen(el) {
  186. if (!el) {
  187. return
  188. }
  189. if (el.requestFullscreen) {
  190. el.requestFullscreen()
  191. } else if (el.msRequestFullscreen) {
  192. el.msRequestFullscreen()
  193. } else if (el.mozRequestFullScreen) {
  194. el.mozRequestFullScreen()
  195. } else if (el.webkitRequestFullscreen) {
  196. el.webkitRequestFullscreen()
  197. }
  198. }
  199. /**
  200. * exit full screen
  201. */
  202. static exitFullscreen() {
  203. if (document.exitFullscreen) {
  204. document.exitFullscreen()
  205. } else if (document.msExitFullscreen) {
  206. document.msExitFullscreen()
  207. } else if (document.mozCancelFullScreen) {
  208. document.mozCancelFullScreen()
  209. } else if (document.webkitExitFullscreen) {
  210. document.webkitExitFullscreen()
  211. }
  212. }
  213. /**
  214. * Creates video
  215. * @param url
  216. * @param className
  217. * @param container
  218. * @returns {HTMLElement}
  219. */
  220. static createVideo(url, className, container = null) {
  221. let videoEl = this.create('video', className, container)
  222. let map4 = this.create('source', '', videoEl)
  223. map4.setAttribute('src', url)
  224. map4.setAttribute('type', 'video/map4')
  225. let mov = this.create('source', '', videoEl)
  226. mov.setAttribute('src', url)
  227. mov.setAttribute('type', 'video/quicktime')
  228. return videoEl
  229. }
  230. }
  231. export default DomUtil