{"version":3,"file":"ajf-core-graph.mjs","sources":["../../../projects/core/graph/src/graph.ts","../../../projects/core/graph/src/graph.html","../../../projects/core/graph/src/graph-module.ts","../../../projects/core/graph/src/graph-node.ts","../../../projects/core/graph/src/public_api.ts","../../../projects/core/graph/src/ajf-core-graph.ts"],"sourcesContent":["/**\n * @license\n * Copyright (C) Gnucoop soc. coop.\n *\n * This file is part of the Advanced JSON forms (ajf).\n *\n * Advanced JSON forms (ajf) is free software: you can redistribute it and/or\n * modify it under the terms of the GNU Affero General Public License as\n * published by the Free Software Foundation, either version 3 of the License,\n * or (at your option) any later version.\n *\n * Advanced JSON forms (ajf) is distributed in the hope that it will be useful,\n * but WITHOUT ANY WARRANTY; without even the implied warranty of\n * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Affero\n * General Public License for more details.\n *\n * You should have received a copy of the GNU Affero General Public License\n * along with Advanced JSON forms (ajf).\n * If not, see http://www.gnu.org/licenses/.\n *\n */\n\nimport {\n  ChangeDetectionStrategy,\n  Component,\n  ElementRef,\n  Input,\n  isDevMode,\n  OnInit,\n  Renderer2,\n  ViewChild,\n  ViewEncapsulation,\n} from '@angular/core';\nimport {AjfGraphNode} from './graph-node';\nimport {BehaviorSubject} from 'rxjs';\nimport * as dagre from 'dagre';\nimport * as svgPanZoom from 'svg-pan-zoom';\n\nconst TEXT_END = 20;\nconst LINE_HEIGHT = 40;\nconst BOX_WIDTH = 170;\n\nconst SvgPanZoom = ((svgPanZoom as any).default || svgPanZoom) as typeof svgPanZoom;\n\ninterface Box {\n  green: boolean;\n  height: number;\n  label: string;\n  name: string;\n  red: boolean;\n  width: number;\n  x: number;\n  y: number;\n  yellow: boolean;\n  color?: string;\n}\n\ninterface IPoint {\n  x: number;\n  y: number;\n}\n\nexport class Edge {\n  constructor(private points: IPoint[]) {}\n\n  public get path(): string {\n    if (this.points.length < 2) {\n      return '';\n    }\n    let result = 'M ';\n    this.points.forEach(pt => {\n      result += `${pt.x} ${pt.y} L`;\n    });\n    return result.substr(0, result.length - 2);\n  }\n}\n\n@Component({\n  selector: 'ajf-graph',\n  templateUrl: 'graph.html',\n  styleUrls: ['graph.scss'],\n  changeDetection: ChangeDetectionStrategy.OnPush,\n  encapsulation: ViewEncapsulation.None,\n})\nexport class AjfGraphComponent implements OnInit {\n  @Input() nodes?: AjfGraphNode[];\n  @ViewChild('graph', {static: true}) graphElement!: ElementRef;\n  boxes$: BehaviorSubject<Box[]> = new BehaviorSubject<Box[]>([]);\n  edges$: BehaviorSubject<Edge[]> = new BehaviorSubject<Edge[]>([]);\n  graph: dagre.graphlib.Graph<{}> = new dagre.graphlib.Graph();\n\n  constructor(private _el: ElementRef, private _renderer: Renderer2) {\n    this.graph.setGraph({marginx: BOX_WIDTH / 2, marginy: LINE_HEIGHT});\n  }\n\n  /**\n   * data una stringa crea un array di stringhe in base ad un TEXT_END\n   * se la ripartizione ricade all'interno di una parola shifta il delimiter fino a trovare uno\n   * spazio bianco.\n   */\n  lines(text: string): string[] {\n    const lines: string[] = [];\n    while (text != null && text.length > 0) {\n      let textEnd = TEXT_END;\n      while (text[textEnd - 1] !== ' ' && text.length > TEXT_END) {\n        textEnd--;\n      }\n      const line = text.slice(0, textEnd);\n      text = text.split(line)[1];\n      lines.push(line);\n    }\n    return lines;\n  }\n\n  ngOnInit(): void {\n    if (this.nodes != null) {\n      const widgetNodes: AjfGraphNode[] = this.nodes;\n\n      widgetNodes.forEach(node => {\n        this.graph.setNode(node.id, {\n          width: BOX_WIDTH,\n          height: this._calculateHeight(node.label),\n          label: node.label,\n          red: node.red,\n          yellow: node.yellow,\n          green: node.green,\n          color: node.color || undefined,\n        });\n        if (node.parentId != null) {\n          try {\n            node.parentId = JSON.parse(node.parentId as string);\n          } catch (e) {\n            if (isDevMode()) {\n              console.log(e);\n            }\n          }\n          const parents: string[] = Array.isArray(node.parentId)\n            ? node.parentId\n            : [node.parentId as string];\n          parents.forEach(parent => {\n            this.graph.setEdge(`${parent}`, node.id, {});\n          });\n        }\n      });\n      try {\n        SvgPanZoom(this.graphElement.nativeElement, {controlIconsEnabled: true});\n      } catch (e) {\n        console.log(e);\n      }\n    }\n\n    dagre.layout(this.graph);\n    const boxes: Box[] = [];\n    this.graph.nodes().forEach((nodeId: any) => {\n      const n: any = this.graph.node(nodeId);\n      if (n) {\n        boxes.push({...n});\n      }\n    });\n    this.boxes$.next(boxes);\n    const edges: Edge[] = [];\n    this.graph.edges().forEach((edge: any) => {\n      edges.push(new Edge(this.graph.edge(edge).points));\n    });\n    this.edges$.next(edges);\n  }\n\n  private _calculateHeight(text: string): number {\n    const linesLength = this.lines(text).length;\n    if (linesLength === 1) {\n      return LINE_HEIGHT * 1.5;\n    }\n    return linesLength * LINE_HEIGHT;\n  }\n}\n","<div class=\"wrapper\">\n  <svg #graph viewBox=\"0 0 1000 2000\" width=\"100%\" height=\"100%\">\n    <g *ngFor=\"let edge of edges$|async\">\n      <path class=\"edge\" [attr.d]=\"edge.path\" fill=\"none\" stroke=\"gray\" stroke-wodth=\"2px\"></path>\n    </g>\n    <g\n      *ngFor=\"let box of boxes$|async\"\n      [attr.fill]=\"box.color? box.color:'#d95989'\"\n      [attr.stroke]=\"box.color !== 'white' ? 'white' : 'black'\"\n      stroke-width=\"1px\"\n      transition=\"fill 0.2s\"\n    >\n      <rect\n        [attr.x]=\"box.x -(box.width/2)\"\n        [attr.y]=\"box.y - (box.height/2)\"\n        [attr.width]=\"box.width\"\n        [attr.height]=\"box.height\"\n        [attr.stroke]=\"box.red === true ? 'red' : box.yellow === true ? 'yellow' : box.green === true ? 'green' : 'black'\"\n        style=\"stroke-width:6\"\n      />\n      <ng-container *ngFor=\"let line of lines(box.label);let idx= index\">\n        <text\n          class=\"text\"\n          [attr.x]=\"box.x\"\n          [attr.y]=\"box.y - (box.height/2) + 30 +(30*idx)\"\n          dominant-baseline=\"middle\"\n          text-anchor=\"middle\"\n        >\n          {{ line }}\n        </text>\n      </ng-container>\n    </g>\n  </svg>\n</div>\n","/**\n * @license\n * Copyright (C) Gnucoop soc. coop.\n *\n * This file is part of the Advanced JSON forms (ajf).\n *\n * Advanced JSON forms (ajf) is free software: you can redistribute it and/or\n * modify it under the terms of the GNU Affero General Public License as\n * published by the Free Software Foundation, either version 3 of the License,\n * or (at your option) any later version.\n *\n * Advanced JSON forms (ajf) is distributed in the hope that it will be useful,\n * but WITHOUT ANY WARRANTY; without even the implied warranty of\n * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Affero\n * General Public License for more details.\n *\n * You should have received a copy of the GNU Affero General Public License\n * along with Advanced JSON forms (ajf).\n * If not, see http://www.gnu.org/licenses/.\n *\n */\n\nimport {NgModule} from '@angular/core';\nimport {AjfGraphComponent} from './graph';\nimport {CommonModule} from '@angular/common';\n\n@NgModule({\n  declarations: [AjfGraphComponent],\n  exports: [AjfGraphComponent],\n  imports: [CommonModule],\n})\nexport class AjfGraphModule {}\n","/**\n * @license\n * Copyright (C) Gnucoop soc. coop.\n *\n * This file is part of the Advanced JSON forms (ajf).\n *\n * Advanced JSON forms (ajf) is free software: you can redistribute it and/or\n * modify it under the terms of the GNU Affero General Public License as\n * published by the Free Software Foundation, either version 3 of the License,\n * or (at your option) any later version.\n *\n * Advanced JSON forms (ajf) is distributed in the hope that it will be useful,\n * but WITHOUT ANY WARRANTY; without even the implied warranty of\n * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Affero\n * General Public License for more details.\n *\n * You should have received a copy of the GNU Affero General Public License\n * along with Advanced JSON forms (ajf).\n * If not, see http://www.gnu.org/licenses/.\n *\n */\n\nexport interface AjfGraphNode {\n  id: string;\n  label: string;\n  parentId: string | string[] | null;\n  green: boolean;\n  red: boolean;\n  yellow: boolean;\n  color?: string;\n}\n","/**\n * @license\n * Copyright (C) Gnucoop soc. coop.\n *\n * This file is part of the Advanced JSON forms (ajf).\n *\n * Advanced JSON forms (ajf) is free software: you can redistribute it and/or\n * modify it under the terms of the GNU Affero General Public License as\n * published by the Free Software Foundation, either version 3 of the License,\n * or (at your option) any later version.\n *\n * Advanced JSON forms (ajf) is distributed in the hope that it will be useful,\n * but WITHOUT ANY WARRANTY; without even the implied warranty of\n * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Affero\n * General Public License for more details.\n *\n * You should have received a copy of the GNU Affero General Public License\n * along with Advanced JSON forms (ajf).\n * If not, see http://www.gnu.org/licenses/.\n *\n */\n\nexport * from './graph-module';\nexport * from './graph-node';\nexport * from './graph';\n","/**\n * Generated bundle index. Do not edit.\n */\n\nexport * from './public_api';\n"],"names":[],"mappings":";;;;;;;;AAAA;;;;;;;;;;;;;;;;;;;;AAoBG;AAkBH,MAAM,QAAQ,GAAG,EAAE;AACnB,MAAM,WAAW,GAAG,EAAE;AACtB,MAAM,SAAS,GAAG,GAAG;AAErB,MAAM,UAAU,IAAK,UAAkB,CAAC,OAAO,IAAI,UAAU,CAAsB;MAoBtE,IAAI,CAAA;AACf,IAAA,WAAA,CAAoB,MAAgB,EAAA;QAAhB,IAAM,CAAA,MAAA,GAAN,MAAM;;AAE1B,IAAA,IAAW,IAAI,GAAA;QACb,IAAI,IAAI,CAAC,MAAM,CAAC,MAAM,GAAG,CAAC,EAAE;AAC1B,YAAA,OAAO,EAAE;;QAEX,IAAI,MAAM,GAAG,IAAI;AACjB,QAAA,IAAI,CAAC,MAAM,CAAC,OAAO,CAAC,EAAE,IAAG;YACvB,MAAM,IAAI,CAAG,EAAA,EAAE,CAAC,CAAC,IAAI,EAAE,CAAC,CAAC,CAAA,EAAA,CAAI;AAC/B,SAAC,CAAC;AACF,QAAA,OAAO,MAAM,CAAC,MAAM,CAAC,CAAC,EAAE,MAAM,CAAC,MAAM,GAAG,CAAC,CAAC;;AAE7C;MASY,iBAAiB,CAAA;IAO5B,WAAoB,CAAA,GAAe,EAAU,SAAoB,EAAA;QAA7C,IAAG,CAAA,GAAA,GAAH,GAAG;QAAsB,IAAS,CAAA,SAAA,GAAT,SAAS;AAJtD,QAA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