{"size": 2443, "ext": "js", "max_stars_repo_path": "packages/html/stories/Constituent.stories.js", "max_stars_repo_name": "mcyph/mxgraph", "max_stars_repo_stars_event_min_datetime": null, "max_stars_repo_stars_event_max_datetime": null, "max_issues_repo_path": "packages/html/stories/Constituent.stories.js", "max_issues_repo_name": "mcyph/mxgraph", "max_issues_repo_issues_event_min_datetime": "2021-07-31T05:13:12.000Z", "max_issues_repo_issues_event_max_datetime": "2021-07-31T05:13:12.000Z", "max_forks_repo_path": "packages/html/stories/Constituent.stories.js", "max_forks_repo_name": "mcyph/mxgraph", "max_forks_repo_forks_event_min_datetime": null, "max_forks_repo_forks_event_max_datetime": null}
{"max_stars_count": null, "max_issues_count": 1, "max_forks_count": null, "avg_line_length": 23.4903846154, "max_line_length": 68, "alphanum_fraction": 0.6307818256}
import mxgraph from '@mxgraph/core';

import { globalTypes } from '../.storybook/preview';

export default {
  title: 'Layouts/Constituent',
  argTypes: {
    ...globalTypes,
    contextMenu: {
      type: 'boolean',
      defaultValue: false
    },
    rubberBand: {
      type: 'boolean',
      defaultValue: true
    }
  }
};

const Template = ({ label, ...args }) => {
  const {
    mxGraph,
    mxEvent,
    mxGraphHandler,
    mxRubberband
  } = mxgraph;

  const container = document.createElement('div');
  container.style.position = 'relative';
  container.style.overflow = 'hidden';
  container.style.width = `${args.width}px`;
  container.style.height = `${args.height}px`;
  container.style.background = 'url(/images/grid.gif)';
  container.style.cursor = 'default';

  // Disables the built-in context menu
  mxEvent.disableContextMenu(container);

  class MyCustomGraphHandler extends mxGraphHandler {
    /**
     * Redirects start drag to parent.
     */
    getInitialCellForEvent(me) {
      let cell = super.getInitialCellForEvent(me);
      if (this.graph.isPart(cell)) {
        cell = cell.getParent();
      }
      return cell;
    }
  }

  class MyCustomGraph extends mxGraph {
    foldingEnabled = false;

    recursiveResize = true;

    isPart(cell) {
      // Helper method to mark parts with constituent=1 in the style
      return this.getCurrentCellStyle(cell).constituent == '1';
    }

    selectCellForEvent(cell, evt) {
      // Redirects selection to parent
      if (this.isPart(cell)) {
        cell = cell.getParent();
      }
      super.selectCellForEvent(cell, evt);
    }

    createGraphHandler() {
      return new MyCustomGraphHandler(this);
    }
  }

  // Creates the graph inside the given container
  const graph = new MyCustomGraph(container);

  // Enables rubberband selection
  new mxRubberband(graph);

  // Gets the default parent for inserting new cells. This
  // is normally the first child of the root (ie. layer 0).
  const parent = graph.getDefaultParent();

  // Adds cells to the model in a single step
  graph.batchUpdate(() => {
    const v1 = graph.insertVertex({
      parent,
      position: [20, 20],
      size: [120, 70],
    });
    const v2 = graph.insertVertex({
      parent: v1,
      value: 'Constituent',
      position: [20, 20],
      size: [80, 30],
      style: 'constituent=1;',
    });
  });

  return container;
}

export const Default = Template.bind({});