import ClientFolderNode from "./ClientFolderNode.js";
import FileClipboard from "./FileClipboard.js";
import FileTree from "../../shared/FileTree.mjs";

export default class ClientFileManager extends FileTree
{
  constructor(session)
  {
    super();
    this.addEvent("click");
    this.session = session;

    this.root = new ClientFolderNode("", undefined, this);
    this.clipboard = new FileClipboard(this);
    this.listeners = [];

    this.element = document.getElementById("filetree");

    this.root.controlElement.className += " root";
    this.root.toggleDisplay();

    this.element.appendChild(this.root.controlElement);
    this.element.appendChild(this.root.listElement);
  }

  reset()
  {
    while(this.root.children.length > 0)
      this.root.children[0].destroy();
  }

  messageReceived(e)
  {
    let folder, node;

    switch(e.action) {
    case "add":
      node = this.registerNode(e.path, e.isFile);

      node.controlElement.addEventListener("click", () => this.triggerEvent("click", node));
      break;
    case "remove":
      node = this.getNode(e.path, e.isFile);

      if(node)
        node.markDeleted();
      break;
    case "move":
      folder = this.getFolder(e.parentPath);
      node = this.getNode(e.oldPath, e.isFile);
      node.name = e.newName;

      // folder might be undefined due to renaming the root node
      if(folder) {
        // detach the element for the node,
        // and orphanize it
        node.destroy();

        node.parentFolder = folder;
        folder.insertNode(node);
        node.append();
      }

      node.triggerEvent("move", e.oldPath);
      break;
    }
  }
}

import ClientFolderNode from "./ClientFolderNode.js";
import FileClipboard from "./FileClipboard.js";
import FileTree from "../../shared/FileTree.mjs";

export default class ClientFileManager extends FileTree
{
  constructor(session)
  {
    super();
    this.addEvent("click");
    this.session = session;

    this.root = new ClientFolderNode("", undefined, this);
    this.clipboard = new FileClipboard(this);
    this.listeners = [];

    this.element = document.getElementById("filetree");

    this.root.controlElement.className += " root";
    this.root.toggleDisplay();

    this.element.appendChild(this.root.controlElement);
    this.element.appendChild(this.root.listElement);
  }

  reset()
  {
    while(this.root.children.length > 0)
      this.root.children[0].destroy();
  }

  messageReceived(message)
  {
    let folder, node;

    switch(message.action) {
    case "add":
      node = this.registerNode(message.path, message.isFile);

      node.controlElement.addEventListener("click", () => this.triggerEvent("click", node));
      break;
    case "remove":
      node = this.getNode(message.path, message.isFile);

      if(node)
        node.markDeleted();
      break;
    case "move":
      folder = this.getFolder(message.parentPath);
      node = this.getNode(message.oldPath, message.isFile);
      node.name = e.newName;

      // folder might be undefined due to renaming the root node
      if(folder) {
        // detach the element for the node,
        // and orphanize it
        node.destroy();

        node.parentFolder = folder;
        folder.insertNode(node);
        node.append();
      }

      node.triggerEvent("move", message.oldPath);
      break;
    }
  }
}

Rename "e" to "message" in messagedReceived on the ClientFileManager for consistency