0
0
mirror of https://github.com/twbs/bootstrap.git synced 2025-02-13 10:54:24 +01:00
Bootstrap/js/src/dom/data.js

68 lines
1.5 KiB
JavaScript
Raw Normal View History

2017-08-21 09:11:37 +02:00
/**
* --------------------------------------------------------------------------
2020-09-29 18:33:00 +03:00
* Bootstrap (v5.0.0-alpha2): dom/data.js
2020-06-16 21:41:47 +03:00
* Licensed under MIT (https://github.com/twbs/bootstrap/blob/main/LICENSE)
2017-08-21 09:11:37 +02:00
* --------------------------------------------------------------------------
*/
2018-09-14 14:27:30 +02:00
/**
* ------------------------------------------------------------------------
* Constants
* ------------------------------------------------------------------------
*/
2017-08-21 09:11:37 +02:00
2018-09-14 14:27:30 +02:00
const mapData = (() => {
const storeData = {}
let id = 1
return {
set(element, key, data) {
2020-06-17 10:45:07 +02:00
if (typeof element.bsKey === 'undefined') {
element.bsKey = {
2018-09-14 14:27:30 +02:00
key,
id
2017-09-20 14:19:10 +02:00
}
2018-09-14 14:27:30 +02:00
id++
}
2017-09-20 14:19:10 +02:00
2020-06-17 10:45:07 +02:00
storeData[element.bsKey.id] = data
2018-09-14 14:27:30 +02:00
},
get(element, key) {
2020-06-17 10:45:07 +02:00
if (!element || typeof element.bsKey === 'undefined') {
2017-08-21 09:11:37 +02:00
return null
}
2017-08-27 00:00:50 +02:00
2020-06-17 10:45:07 +02:00
const keyProperties = element.bsKey
2018-09-14 14:27:30 +02:00
if (keyProperties.key === key) {
return storeData[keyProperties.id]
}
2019-02-26 13:20:34 +02:00
2018-09-14 14:27:30 +02:00
return null
2017-09-20 14:19:10 +02:00
},
2018-09-14 14:27:30 +02:00
delete(element, key) {
2020-06-17 10:45:07 +02:00
if (typeof element.bsKey === 'undefined') {
2018-09-14 14:27:30 +02:00
return
}
2020-06-17 10:45:07 +02:00
const keyProperties = element.bsKey
2018-09-14 14:27:30 +02:00
if (keyProperties.key === key) {
delete storeData[keyProperties.id]
2020-06-17 10:45:07 +02:00
delete element.bsKey
2018-09-14 14:27:30 +02:00
}
2017-08-21 09:11:37 +02:00
}
}
})()
2018-09-14 14:27:30 +02:00
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)
}
}
2017-08-21 09:11:37 +02:00
export default Data