manipulator.js 2.94 KB
Newer Older
1
2
/**
 * --------------------------------------------------------------------------
3
 * Bootstrap (v4.1.1): dom/manipulator.js
4
5
6
7
 * Licensed under MIT (https://github.com/twbs/bootstrap/blob/master/LICENSE)
 * --------------------------------------------------------------------------
 */

8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
const regexDataKey = /[A-Z]/g

function normalizeData(val) {
  if (val === 'true') {
    return true
  } else if (val === 'false') {
    return false
  } else if (val === 'null') {
    return null
  } else if (val === Number(val).toString()) {
    return Number(val)
  } else if (val === '') {
    return null
  }

  return val
}

function normalizeDataKey(key) {
  return key.replace(regexDataKey, (chr) => chr.toLowerCase())
}

30
31
32
33
34
35
36
37
38
39
40
41
42
const Manipulator = {
  setChecked(input, val) {
    if (input instanceof HTMLInputElement) {
      input.checked = val
      input.bsChecked = val
    }
  },

  isChecked(input) {
    if (input instanceof HTMLInputElement) {
      return input.bsChecked || input.checked
    }
    throw new Error('INPUT parameter is not an HTMLInputElement')
43
44
45
  },

  setDataAttribute(element, key, value) {
46
    element.setAttribute(`data-${normalizeDataKey(key)}`, value)
47
48
49
  },

  removeDataAttribute(element, key) {
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
    element.removeAttribute(`data-${normalizeDataKey(key)}`)
  },

  getDataAttributes(element) {
    if (typeof element === 'undefined' || element === null) {
      return {}
    }

    let attributes
    if (Object.getOwnPropertyDescriptor(HTMLElement.prototype, 'dataset')) {
      attributes = {
        ...element.dataset
      }
    } else {
      attributes = {}
      for (let i = 0; i < element.attributes.length; i++) {
        const attribute = element.attributes[i]

        if (attribute.nodeName.indexOf('data-') !== -1) {
          // remove 'data-' part of the attribute name
          const attributeName = attribute
            .nodeName
            .substring('data-'.length)
            .replace(/-./g, (str) => str.charAt(1).toUpperCase())

          attributes[attributeName] = attribute.nodeValue
        }
      }
78
79
    }

80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
    for (const key in attributes) {
      if (!Object.prototype.hasOwnProperty.call(attributes, key)) {
        continue
      }

      attributes[key] = normalizeData(attributes[key])
    }

    return attributes
  },

  getDataAttribute(element, key) {
    return normalizeData(element
      .getAttribute(`data-${normalizeDataKey(key)}`)
    )
Johann-S's avatar
Johann-S committed
95
96
  },

97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
  offset(element) {
    const rect = element.getBoundingClientRect()

    return {
      top: rect.top + document.body.scrollTop,
      left: rect.left + document.body.scrollLeft
    }
  },

  position(element) {
    return {
      top: element.offsetTop,
      left: element.offsetLeft
    }
  },

Johann-S's avatar
Johann-S committed
113
114
115
116
117
118
119
120
121
122
  toggleClass(element, className) {
    if (typeof element === 'undefined' || element === null) {
      return
    }

    if (element.classList.contains(className)) {
      element.classList.remove(className)
    } else {
      element.classList.add(className)
    }
123
124
125
126
  }
}

export default Manipulator