manipulator.js 1.72 KB
Newer Older
1
2
import Util from '../util'

3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
/**
 * --------------------------------------------------------------------------
 * Bootstrap (v4.0.0-beta): dom/manipulator.js
 * Licensed under MIT (https://github.com/twbs/bootstrap/blob/master/LICENSE)
 * --------------------------------------------------------------------------
 */

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')
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
  },

  setDataAttribute(element, key, value) {
    const $ = Util.jQuery
    if (typeof $ !== 'undefined') {
      $(element).data(key, value)
    }

    element.setAttribute(`data-${key.replace(/[A-Z]/g, (chr) => `-${chr.toLowerCase()}`)}`, value)
  },

  removeDataAttribute(element, key) {
    const $ = Util.jQuery
    if (typeof $ !== 'undefined') {
      $(element).removeData(key)
    }

    element.removeAttribute(`data-${key.replace(/[A-Z]/g, (chr) => `-${chr.toLowerCase()}`)}`)
Johann-S's avatar
Johann-S committed
41
42
  },

43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
  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
59
60
61
62
63
64
65
66
67
68
  toggleClass(element, className) {
    if (typeof element === 'undefined' || element === null) {
      return
    }

    if (element.classList.contains(className)) {
      element.classList.remove(className)
    } else {
      element.classList.add(className)
    }
69
70
71
72
  }
}

export default Manipulator