manipulator.js 2.68 KB
Newer Older
XhmikosR's avatar
XhmikosR committed
1
/*!
XhmikosR's avatar
XhmikosR committed
2
  * Bootstrap manipulator.js v5.0.0-alpha3 (https://getbootstrap.com/)
XhmikosR's avatar
XhmikosR committed
3
  * Copyright 2011-2020 The Bootstrap Authors (https://github.com/twbs/bootstrap/graphs/contributors)
XhmikosR's avatar
XhmikosR committed
4
  * Licensed under MIT (https://github.com/twbs/bootstrap/blob/main/LICENSE)
XhmikosR's avatar
XhmikosR committed
5
6
7
8
  */
(function (global, factory) {
  typeof exports === 'object' && typeof module !== 'undefined' ? module.exports = factory() :
  typeof define === 'function' && define.amd ? define(factory) :
XhmikosR's avatar
XhmikosR committed
9
  (global = typeof globalThis !== 'undefined' ? globalThis : global || self, global.Manipulator = factory());
XhmikosR's avatar
XhmikosR committed
10
}(this, (function () { 'use strict';
XhmikosR's avatar
XhmikosR committed
11
12
13

  /**
   * --------------------------------------------------------------------------
XhmikosR's avatar
XhmikosR committed
14
   * Bootstrap (v5.0.0-alpha3): dom/manipulator.js
XhmikosR's avatar
XhmikosR committed
15
   * Licensed under MIT (https://github.com/twbs/bootstrap/blob/main/LICENSE)
XhmikosR's avatar
XhmikosR committed
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
   * --------------------------------------------------------------------------
   */
  function normalizeData(val) {
    if (val === 'true') {
      return true;
    }

    if (val === 'false') {
      return false;
    }

    if (val === Number(val).toString()) {
      return Number(val);
    }

    if (val === '' || val === 'null') {
      return null;
    }

    return val;
  }

  function normalizeDataKey(key) {
    return key.replace(/[A-Z]/g, function (chr) {
XhmikosR's avatar
XhmikosR committed
40
      return "-" + chr.toLowerCase();
XhmikosR's avatar
XhmikosR committed
41
42
43
44
45
    });
  }

  var Manipulator = {
    setDataAttribute: function setDataAttribute(element, key, value) {
XhmikosR's avatar
XhmikosR committed
46
      element.setAttribute("data-bs-" + normalizeDataKey(key), value);
XhmikosR's avatar
XhmikosR committed
47
48
    },
    removeDataAttribute: function removeDataAttribute(element, key) {
XhmikosR's avatar
XhmikosR committed
49
      element.removeAttribute("data-bs-" + normalizeDataKey(key));
XhmikosR's avatar
XhmikosR committed
50
51
52
53
54
55
    },
    getDataAttributes: function getDataAttributes(element) {
      if (!element) {
        return {};
      }

XhmikosR's avatar
XhmikosR committed
56
57
58
59
60
61
62
      var attributes = {};
      Object.keys(element.dataset).filter(function (key) {
        return key.startsWith('bs');
      }).forEach(function (key) {
        var pureKey = key.replace(/^bs/, '');
        pureKey = pureKey.charAt(0).toLowerCase() + pureKey.slice(1, pureKey.length);
        attributes[pureKey] = normalizeData(element.dataset[key]);
XhmikosR's avatar
XhmikosR committed
63
64
65
66
      });
      return attributes;
    },
    getDataAttribute: function getDataAttribute(element, key) {
XhmikosR's avatar
XhmikosR committed
67
      return normalizeData(element.getAttribute("data-bs-" + normalizeDataKey(key)));
XhmikosR's avatar
XhmikosR committed
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
    },
    offset: function offset(element) {
      var rect = element.getBoundingClientRect();
      return {
        top: rect.top + document.body.scrollTop,
        left: rect.left + document.body.scrollLeft
      };
    },
    position: function position(element) {
      return {
        top: element.offsetTop,
        left: element.offsetLeft
      };
    }
  };

  return Manipulator;

XhmikosR's avatar
XhmikosR committed
86
})));
XhmikosR's avatar
XhmikosR committed
87
//# sourceMappingURL=manipulator.js.map