Source: map/importer/utils.js

  1. import { Layer } from "../layer/Layer/Layer.js";
  2. import { Folder } from "../layer/Folder/Folder.js";
  3. /**
  4. * Helper function to create a structure object for the layer tree.
  5. * optionally creates a new folder with the given name
  6. *
  7. * @memberof vef.map.importer
  8. * @param {Layer[]} layers
  9. * @param {string} folderName (optional) will be added to root as default
  10. * @param {string} parent (optional) default is '#'
  11. *
  12. * @returns {object} {structure, layers}
  13. */
  14. export function createLayerTreeStructure(layers, folderName, parent) {
  15. if (!Array.isArray(layers) || (layers.length == 0)) return { structure: {}, layers: {} };
  16. parent = (typeof parent == "string") ? parent : "#";
  17. const structure = {};
  18. const layerIDs = layers.map(layer => layer.uniqueId);
  19. const newLayers = layers.reduce((accumulator, layer) => {
  20. accumulator[layer.uniqueId] = layer;
  21. return accumulator;
  22. }, {});
  23. if (typeof folderName == "string") {
  24. const folder = new Folder({ title: folderName });
  25. structure[parent] = [folder.uniqueId];
  26. structure[folder.uniqueId] = layerIDs;
  27. newLayers[folder.uniqueId] = folder;
  28. } else {
  29. structure[parent] = layerIDs;
  30. }
  31. return {
  32. structure: structure,
  33. layers: newLayers
  34. };
  35. }