# content-blocks-builder/trunk/src/utils/dom/data.js

Content Blocks Builder – Create blocks, repeater blocks with carousel, grid, popup layouts, version trunk. 67 lines.

- Page: https://pluginprobe.com/plugins/content-blocks-builder/trunk/code/src/utils/dom/data.js
- Raw: https://pluginprobe.com/plugins/content-blocks-builder/trunk/raw/src/utils/dom/data.js
- Modified: 2023-08-09T10:45:10+00:00

Line numbers below start at 1. Link to a line or a range by appending a fragment to the
page URL, for example `https://pluginprobe.com/plugins/content-blocks-builder/trunk/code/src/utils/dom/data.js#L10-L20`.

```javascript
/**
 * Internal dependencies
 */
import { exposeComponent } from "./utils";

/**
 * Data API
 * Adapted from Bootstrap
 */

/**
 * Constants
 */

const elementMap = new Map();

const Data = {
  set(element, key, instance) {
    if (!elementMap.has(element)) {
      elementMap.set(element, new Map());
    }

    const instanceMap = elementMap.get(element);

    // make it clear we only want one instance per element
    // can be removed later when multiple key/instances are fine to be used
    if (!instanceMap.has(key) && instanceMap.size !== 0) {
      // eslint-disable-next-line no-console
      console.error(
        `Doesn't allow more than one instance per element. Bound instance: ${
          Array.from(instanceMap.keys())[0]
        }.`
      );
      return;
    }

    instanceMap.set(key, instance);
  },

  get(element, key) {
    if (elementMap.has(element)) {
      return elementMap.get(element).get(key) || null;
    }

    return null;
  },

  remove(element, key) {
    if (!elementMap.has(element)) {
      return;
    }

    const instanceMap = elementMap.get(element);

    instanceMap.delete(key);

    // free up element references if there are no instances left for an element
    if (instanceMap.size === 0) {
      elementMap.delete(element);
    }
  },
};

exposeComponent("Data", Data);

export { Data };

```
