Skip to content

πŸ“‚ Import & Export (FileHandler)

Joppe Koops edited this page Jun 19, 2024 · 5 revisions

File: Meesterproef-SNSimulation/js/FileHandler.js

Methods

  • export(nodes): Exports the network nodes to a JSON file. Prompts the user for a filename and creates a downloadable file with the network setup.

    • Parameters:

      • nodes (Map): The map of nodes to export.
    • Explanation

      Because there is no way to export a file with JavaScript, the function creates an a element with the JSON file as blob in the href and a download attribute with the filename. After which, it programmatically clicks the link to download the file and then removes the link.

      View Code

  • import(nodes, links): Imports network nodes and links from a JSON file. Prompts the user to select a file and updates the nodes and links based on the file content.

    • Parameters:

      • nodes (Map): The map of nodes to update with imported data.
      • links (Map): The map of links to update with imported data.
    • Returns: Promise - Resolves when the import is complete.

    • Explanation

      Because there is no way to prompt for a file in JavaScript, this function does a similar trick to the export function. It creates an HTML <input type="file"> and then clicks it. Thereafter, it reads the file with a new FileReader() object. It then rebuilds the nodes map from the imported JSON file.

      View Code

  • mapToArray(map): Converts a map to an array of objects. Each object represents an item from the map, including nested maps and ignoring HTML elements.

    • Parameters:

      • map (Map): The map to convert to an array.
    • Returns: Array - The array representation of the map.

      View Code

Design rational

Clone this wiki locally