123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183 |
- import Util from './Util.js'
- /**
- * Dom工具类
- * 代码使用的leaflet开源工具
- * https://github.com/Leaflet/Leaflet/tree/master/src/core
- * @ignore
- */
- class DomUtil {
- /**
- * Returns an element given its DOM id, or returns the element itself
- * if it was passed directly.
- * @ignore
- * @param {*} id
- */
- static get(id) {
- return typeof id === 'string' ? document.getElementById(id) : id
- }
- /**
- * Returns the value for a certain style attribute on an element,
- * including computed values or values set through CSS.
- * @ignore
- * @param {*} el
- * @param {*} style
- */
- static getStyle(el, style) {
- var value = el.style[style] || (el.currentStyle && el.currentStyle[style])
- if ((!value || value === 'auto') && document.defaultView) {
- var css = document.defaultView.getComputedStyle(el, null)
- value = css ? css[style] : null
- }
- return value === 'auto' ? null : value
- }
- /**
- * Creates an HTML element with `tagName`, sets its class to `className`, and optionally appends it to `container` element.
- * @ignore
- * @param {*} tagName
- * @param {*} className
- * @param {*} container
- */
- static create(tagName, className, container) {
- var el = document.createElement(tagName)
- el.className = className || ''
- if (container) {
- container.appendChild(el)
- }
- return el
- }
- /**
- * Removes `el` from its parent element
- * @ignore
- * @param {*} el
- */
- static remove(el) {
- var parent = el.parentNode
- if (parent) {
- parent.removeChild(el)
- }
- }
- /**
- * Removes all of `el`'s children elements from `el`
- * @ignore
- * @param {*} el
- */
- static empty(el) {
- while (el.firstChild) {
- el.removeChild(el.firstChild)
- }
- }
- /**
- * Returns `true` if the element's class attribute contains `name`.
- * @ignore
- * @param {*} el
- * @param {*} name
- */
- hasClass(el, name) {
- if (el.classList !== undefined) {
- return el.classList.contains(name)
- }
- var className = getClass(el)
- return (
- className.length > 0 &&
- new RegExp('(^|\\s)' + name + '(\\s|$)').test(className)
- )
- }
- /**
- * Adds `name` to the element's class attribute.
- * @ignore
- * @param {*} el
- * @param {*} name
- */
- static addClass(el, name) {
- if (el.classList !== undefined) {
- let classes = Util.splitWords(name)
- for (let i = 0, len = classes.length; i < len; i++) {
- el.classList.add(classes[i])
- }
- } else if (!this.hasClass(el, name)) {
- let className = this.getClass(el)
- this.setClass(el, (className ? className + ' ' : '') + name)
- }
- }
- /**
- * Removes `name` from the element's class attribute.
- * @ignore
- * @param {*} el
- * @param {*} name
- */
- static removeClass(el, name) {
- if (el.classList !== undefined) {
- el.classList.remove(name)
- } else {
- this.setClass(
- el,
- Util.trim(
- (' ' + this.getClass(el) + ' ').replace(' ' + name + ' ', ' ')
- )
- )
- }
- }
- /**
- * Sets the element's class.
- * @ignore
- * @param {*} el
- * @param {*} name
- */
- static setClass(el, name) {
- if (el.className.baseVal === undefined) {
- el.className = name
- } else {
- // in case of SVG element
- el.className.baseVal = name
- }
- }
- /**
- * Returns the element's class.
- * @ignore
- * @param {*} el
- */
- static getClass(el) {
- // Check if the element is an SVGElementInstance and use the correspondingElement instead
- // (Required for linked SVG elements in IE11.)
- if (el.correspondingElement) {
- el = el.correspondingElement
- }
- return el.className.baseVal === undefined ?
- el.className :
- el.className.baseVal
- }
- /**
- * @ignore
- * @param {*} path
- * @param {*} width
- * @param {*} height
- */
- static createSvg(width, height, path, container) {
- let svg = document.createElementNS('http://www.w3.org/2000/svg', 'svg:svg')
- svg.setAttribute('class', 'svg-path')
- svg.setAttribute('width', width)
- svg.setAttribute('height', height)
- svg.setAttribute('viewBox', `0 0 ${width} ${height}`)
- let pathEl = document.createElementNS('http://www.w3.org/2000/svg', 'path')
- pathEl.setAttribute('d', path)
- svg.appendChild(pathEl)
- if (container) {
- container.appendChild(svg)
- }
- return svg
- }
- }
- export default DomUtil
|