data.js 1.51 KB
Newer Older
Johann-S's avatar
Johann-S committed
1
2
/**
 * --------------------------------------------------------------------------
3
 * Bootstrap (v5.0.0-alpha1): dom/data.js
4
 * Licensed under MIT (https://github.com/twbs/bootstrap/blob/main/LICENSE)
Johann-S's avatar
Johann-S committed
5
6
7
 * --------------------------------------------------------------------------
 */

8
9
10
11
12
/**
 * ------------------------------------------------------------------------
 * Constants
 * ------------------------------------------------------------------------
 */
Johann-S's avatar
Johann-S committed
13

14
15
16
17
18
const mapData = (() => {
  const storeData = {}
  let id = 1
  return {
    set(element, key, data) {
Johann-S's avatar
Johann-S committed
19
20
      if (typeof element.bsKey === 'undefined') {
        element.bsKey = {
21
22
          key,
          id
Johann-S's avatar
Johann-S committed
23
        }
24
25
        id++
      }
Johann-S's avatar
Johann-S committed
26

Johann-S's avatar
Johann-S committed
27
      storeData[element.bsKey.id] = data
28
29
    },
    get(element, key) {
Johann-S's avatar
Johann-S committed
30
      if (!element || typeof element.bsKey === 'undefined') {
Johann-S's avatar
Johann-S committed
31
32
        return null
      }
Johann-S's avatar
Johann-S committed
33

Johann-S's avatar
Johann-S committed
34
      const keyProperties = element.bsKey
35
36
37
      if (keyProperties.key === key) {
        return storeData[keyProperties.id]
      }
XhmikosR's avatar
XhmikosR committed
38

39
      return null
Johann-S's avatar
Johann-S committed
40
    },
41
    delete(element, key) {
Johann-S's avatar
Johann-S committed
42
      if (typeof element.bsKey === 'undefined') {
43
44
45
        return
      }

Johann-S's avatar
Johann-S committed
46
      const keyProperties = element.bsKey
47
48
      if (keyProperties.key === key) {
        delete storeData[keyProperties.id]
Johann-S's avatar
Johann-S committed
49
        delete element.bsKey
50
      }
Johann-S's avatar
Johann-S committed
51
52
53
54
    }
  }
})()

55
56
57
58
59
60
61
62
63
64
65
66
const Data = {
  setData(instance, key, data) {
    mapData.set(instance, key, data)
  },
  getData(instance, key) {
    return mapData.get(instance, key)
  },
  removeData(instance, key) {
    mapData.delete(instance, key)
  }
}

Johann-S's avatar
Johann-S committed
67
export default Data