You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

62 line
1.9 KiB

  1. /**
  2. * --------------------------------------------------------------------------
  3. * Bootstrap (v5.1.3): dom/data.js
  4. * Licensed under MIT (https://github.com/twbs/bootstrap/blob/main/LICENSE)
  5. * --------------------------------------------------------------------------
  6. */
  7. define([], function() {
  8. 'use strict';
  9. /**
  10. * ------------------------------------------------------------------------
  11. * Constants
  12. * ------------------------------------------------------------------------
  13. */
  14. const elementMap = new Map()
  15. return {
  16. set: function (element, key, instance) {
  17. if (!elementMap.has(element)) {
  18. elementMap.set(element, new Map())
  19. }
  20. const instanceMap = elementMap.get(element)
  21. // make it clear we only want one instance per element
  22. // can be removed later when multiple key/instances are fine to be used
  23. if (!instanceMap.has(key) && instanceMap.size !== 0) {
  24. // eslint-disable-next-line no-console
  25. console.error(`Bootstrap doesn't allow more than one instance per element. Bound instance: ${Array.from(instanceMap.keys())[0]}.`)
  26. return
  27. }
  28. instanceMap.set(key, instance)
  29. },
  30. get: function (element, key) {
  31. if (elementMap.has(element)) {
  32. return elementMap.get(element).get(key) || null
  33. }
  34. return null
  35. },
  36. remove: function (element, key) {
  37. if (!elementMap.has(element)) {
  38. return
  39. }
  40. const instanceMap = elementMap.get(element)
  41. instanceMap.delete(key)
  42. // free up element references if there are no instances left for an element
  43. if (instanceMap.size === 0) {
  44. elementMap.delete(element)
  45. }
  46. }
  47. }
  48. });