model.d.ts 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293
  1. import { Dijkstra, KeyValue, Basecoat } from '@antv/x6-common';
  2. import { Point, Rectangle } from '@antv/x6-geometry';
  3. import { Cell } from './cell';
  4. import { Edge } from './edge';
  5. import { Node } from './node';
  6. import { Collection } from './collection';
  7. import { Graph } from '../graph';
  8. export declare class Model extends Basecoat<Model.EventArgs> {
  9. readonly collection: Collection;
  10. protected readonly batches: KeyValue<number>;
  11. protected readonly addings: WeakMap<Cell, boolean>;
  12. graph: Graph;
  13. protected nodes: KeyValue<boolean>;
  14. protected edges: KeyValue<boolean>;
  15. protected outgoings: KeyValue<string[]>;
  16. protected incomings: KeyValue<string[]>;
  17. protected get [Symbol.toStringTag](): string;
  18. constructor(cells?: Cell[]);
  19. notify<Key extends keyof Model.EventArgs>(name: Key, args: Model.EventArgs[Key]): this;
  20. notify(name: Exclude<string, keyof Model.EventArgs>, args: any): this;
  21. protected setup(): void;
  22. protected sortOnChangeZ(): void;
  23. protected onCellAdded(cell: Cell): void;
  24. protected onCellRemoved(cell: Cell, options: Collection.RemoveOptions): void;
  25. protected onReset(cells: Cell[]): void;
  26. protected onEdgeTerminalChanged(edge: Edge, type: Edge.TerminalType): void;
  27. protected prepareCell(cell: Cell, options: Collection.AddOptions): Cell<Cell.Properties>;
  28. resetCells(cells: Cell[], options?: Collection.SetOptions): this;
  29. clear(options?: Cell.SetOptions): this;
  30. addNode(metadata: Node | Node.Metadata, options?: Model.AddOptions): Node<Node.Properties>;
  31. updateNode(metadata: Node.Metadata, options?: Model.SetOptions): boolean;
  32. createNode(metadata: Node.Metadata): Node<Node.Properties>;
  33. addEdge(metadata: Edge.Metadata | Edge, options?: Model.AddOptions): Edge<Edge.Properties>;
  34. createEdge(metadata: Edge.Metadata): Edge<Edge.Properties>;
  35. updateEdge(metadata: Edge.Metadata, options?: Model.SetOptions): boolean;
  36. addCell(cell: Cell | Cell[], options?: Model.AddOptions): this;
  37. addCells(cells: Cell[], options?: Model.AddOptions): this;
  38. updateCell(prop: Cell.Properties, options?: Model.SetOptions): boolean;
  39. removeCell(cellId: string, options?: Collection.RemoveOptions): Cell | null;
  40. removeCell(cell: Cell, options?: Collection.RemoveOptions): Cell | null;
  41. updateCellId(cell: Cell, newId: string): Cell<Cell.Properties> | undefined;
  42. removeCells(cells: (Cell | string)[], options?: Cell.RemoveOptions): (Cell<Cell.Properties> | null)[];
  43. removeConnectedEdges(cell: Cell | string, options?: Cell.RemoveOptions): Edge<Edge.Properties>[];
  44. disconnectConnectedEdges(cell: Cell | string, options?: Edge.SetOptions): void;
  45. has(id: string): boolean;
  46. has(cell: Cell): boolean;
  47. total(): number;
  48. indexOf(cell: Cell): number;
  49. /**
  50. * Returns a cell from the graph by its id.
  51. */
  52. getCell<T extends Cell = Cell>(id: string): T;
  53. /**
  54. * Returns all the nodes and edges in the graph.
  55. */
  56. getCells(): Cell<Cell.Properties>[];
  57. /**
  58. * Returns the first cell (node or edge) in the graph. The first cell is
  59. * defined as the cell with the lowest `zIndex`.
  60. */
  61. getFirstCell(): Cell<Cell.Properties> | null;
  62. /**
  63. * Returns the last cell (node or edge) in the graph. The last cell is
  64. * defined as the cell with the highest `zIndex`.
  65. */
  66. getLastCell(): Cell<Cell.Properties> | null;
  67. /**
  68. * Returns the lowest `zIndex` value in the graph.
  69. */
  70. getMinZIndex(): number;
  71. /**
  72. * Returns the highest `zIndex` value in the graph.
  73. */
  74. getMaxZIndex(): number;
  75. protected getCellsFromCache<T extends Cell = Cell>(cache: {
  76. [key: string]: boolean;
  77. }): T[];
  78. /**
  79. * Returns all the nodes in the graph.
  80. */
  81. getNodes(): Node<Node.Properties>[];
  82. /**
  83. * Returns all the edges in the graph.
  84. */
  85. getEdges(): Edge<Edge.Properties>[];
  86. /**
  87. * Returns all outgoing edges for the node.
  88. */
  89. getOutgoingEdges(cell: Cell | string): Edge<Edge.Properties>[] | null;
  90. /**
  91. * Returns all incoming edges for the node.
  92. */
  93. getIncomingEdges(cell: Cell | string): Edge<Edge.Properties>[] | null;
  94. /**
  95. * Returns edges connected with cell.
  96. */
  97. getConnectedEdges(cell: Cell | string, options?: Model.GetConnectedEdgesOptions): Edge<Edge.Properties>[];
  98. protected isBoundary(cell: Cell | string, isOrigin: boolean): boolean;
  99. protected getBoundaryNodes(isOrigin: boolean): Node<Node.Properties>[];
  100. /**
  101. * Returns an array of all the roots of the graph.
  102. */
  103. getRoots(): Node<Node.Properties>[];
  104. /**
  105. * Returns an array of all the leafs of the graph.
  106. */
  107. getLeafs(): Node<Node.Properties>[];
  108. /**
  109. * Returns `true` if the node is a root node, i.e. there is no edges
  110. * coming to the node.
  111. */
  112. isRoot(cell: Cell | string): boolean;
  113. /**
  114. * Returns `true` if the node is a leaf node, i.e. there is no edges
  115. * going out from the node.
  116. */
  117. isLeaf(cell: Cell | string): boolean;
  118. /**
  119. * Returns all the neighbors of node in the graph. Neighbors are all
  120. * the nodes connected to node via either incoming or outgoing edge.
  121. */
  122. getNeighbors(cell: Cell, options?: Model.GetNeighborsOptions): Cell<Cell.Properties>[];
  123. /**
  124. * Returns `true` if `cell2` is a neighbor of `cell1`.
  125. */
  126. isNeighbor(cell1: Cell, cell2: Cell, options?: Model.GetNeighborsOptions): boolean;
  127. getSuccessors(cell: Cell, options?: Model.GetPredecessorsOptions): Cell<Cell.Properties>[];
  128. /**
  129. * Returns `true` if `cell2` is a successor of `cell1`.
  130. */
  131. isSuccessor(cell1: Cell, cell2: Cell, options?: Model.GetPredecessorsOptions): boolean;
  132. getPredecessors(cell: Cell, options?: Model.GetPredecessorsOptions): Cell<Cell.Properties>[];
  133. /**
  134. * Returns `true` if `cell2` is a predecessor of `cell1`.
  135. */
  136. isPredecessor(cell1: Cell, cell2: Cell, options?: Model.GetPredecessorsOptions): boolean;
  137. protected matchDistance(distance: number, preset?: number | number[] | ((d: number) => boolean)): boolean;
  138. /**
  139. * Returns the common ancestor of the passed cells.
  140. */
  141. getCommonAncestor(...cells: (Cell | Cell[] | null | undefined)[]): Cell<Cell.Properties> | null;
  142. /**
  143. * Returns an array of cells that result from finding nodes/edges that
  144. * are connected to any of the cells in the cells array. This function
  145. * loops over cells and if the current cell is a edge, it collects its
  146. * source/target nodes; if it is an node, it collects its incoming and
  147. * outgoing edges if both the edge terminal (source/target) are in the
  148. * cells array.
  149. */
  150. getSubGraph(cells: Cell[], options?: Model.GetSubgraphOptions): Cell<Cell.Properties>[];
  151. /**
  152. * Clones the whole subgraph (including all the connected links whose
  153. * source/target is in the subgraph). If `options.deep` is `true`, also
  154. * take into account all the embedded cells of all the subgraph cells.
  155. *
  156. * Returns a map of the form: { [original cell ID]: [clone] }.
  157. */
  158. cloneSubGraph(cells: Cell[], options?: Model.GetSubgraphOptions): KeyValue<Cell<Cell.Properties>>;
  159. cloneCells(cells: Cell[]): KeyValue<Cell<Cell.Properties>>;
  160. /**
  161. * Returns an array of nodes whose bounding box contains point.
  162. * Note that there can be more then one node as nodes might overlap.
  163. */
  164. getNodesFromPoint(x: number, y: number): Node[];
  165. getNodesFromPoint(p: Point.PointLike): Node[];
  166. /**
  167. * Returns an array of nodes whose bounding box top/left coordinate
  168. * falls into the rectangle.
  169. */
  170. getNodesInArea(x: number, y: number, w: number, h: number, options?: Model.GetCellsInAreaOptions): Node[];
  171. getNodesInArea(rect: Rectangle.RectangleLike, options?: Model.GetCellsInAreaOptions): Node[];
  172. /**
  173. * Returns an array of edges whose bounding box top/left coordinate
  174. * falls into the rectangle.
  175. */
  176. getEdgesInArea(x: number, y: number, w: number, h: number, options?: Model.GetCellsInAreaOptions): Edge[];
  177. getEdgesInArea(rect: Rectangle.RectangleLike, options?: Model.GetCellsInAreaOptions): Edge[];
  178. getNodesUnderNode(node: Node, options?: {
  179. by?: 'bbox' | Rectangle.KeyPoint;
  180. }): Node<Node.Properties>[];
  181. /**
  182. * Returns the bounding box that surrounds all cells in the graph.
  183. */
  184. getAllCellsBBox(): Rectangle | null;
  185. /**
  186. * Returns the bounding box that surrounds all the given cells.
  187. */
  188. getCellsBBox(cells: Cell[], options?: Cell.GetCellsBBoxOptions): Rectangle | null;
  189. search(cell: Cell, iterator: Model.SearchIterator, options?: Model.SearchOptions): void;
  190. breadthFirstSearch(cell: Cell, iterator: Model.SearchIterator, options?: Model.GetNeighborsOptions): void;
  191. depthFirstSearch(cell: Cell, iterator: Model.SearchIterator, options?: Model.GetNeighborsOptions): void;
  192. /** *
  193. * Returns an array of IDs of nodes on the shortest
  194. * path between source and target.
  195. */
  196. getShortestPath(source: Cell | string, target: Cell | string, options?: Model.GetShortestPathOptions): string[];
  197. /**
  198. * Translate all cells in the graph by `tx` and `ty` pixels.
  199. */
  200. translate(tx: number, ty: number, options: Cell.TranslateOptions): this;
  201. resize(width: number, height: number, options: Cell.SetOptions): this;
  202. resizeCells(width: number, height: number, cells: Cell[], options?: Cell.SetOptions): this;
  203. toJSON(options?: Model.ToJSONOptions): {
  204. cells: Cell.Properties[];
  205. };
  206. parseJSON(data: Model.FromJSONData): (Edge<Edge.Properties> | Node<Node.Properties>)[];
  207. fromJSON(data: Model.FromJSONData, options?: Model.FromJSONOptions): this;
  208. startBatch(name: Model.BatchName, data?: KeyValue): this;
  209. stopBatch(name: Model.BatchName, data?: KeyValue): this;
  210. batchUpdate<T>(name: Model.BatchName, execute: () => T, data?: KeyValue): T;
  211. hasActiveBatch(name?: Model.BatchName | Model.BatchName[]): boolean;
  212. dispose(): void;
  213. }
  214. export declare namespace Model {
  215. const toStringTag: string;
  216. function isModel(instance: any): instance is Model;
  217. }
  218. export declare namespace Model {
  219. interface SetOptions extends Collection.SetOptions {
  220. }
  221. interface AddOptions extends Collection.AddOptions {
  222. }
  223. interface RemoveOptions extends Collection.RemoveOptions {
  224. }
  225. interface FromJSONOptions extends Collection.SetOptions {
  226. }
  227. type FromJSONData = (Node.Metadata | Edge.Metadata)[] | (Partial<ReturnType<typeof toJSON>> & {
  228. nodes?: Node.Metadata[];
  229. edges?: Edge.Metadata[];
  230. });
  231. type ToJSONData = {
  232. cells: Cell.Properties[];
  233. };
  234. interface GetCellsInAreaOptions {
  235. strict?: boolean;
  236. }
  237. interface SearchOptions extends GetNeighborsOptions {
  238. breadthFirst?: boolean;
  239. }
  240. type SearchIterator = (this: Model, cell: Cell, distance: number) => any;
  241. interface GetNeighborsOptions {
  242. deep?: boolean;
  243. incoming?: boolean;
  244. outgoing?: boolean;
  245. indirect?: boolean;
  246. }
  247. interface GetConnectedEdgesOptions extends GetNeighborsOptions {
  248. enclosed?: boolean;
  249. }
  250. interface GetSubgraphOptions {
  251. deep?: boolean;
  252. }
  253. interface GetShortestPathOptions {
  254. directed?: boolean;
  255. weight?: Dijkstra.Weight;
  256. }
  257. interface GetPredecessorsOptions extends Cell.GetDescendantsOptions {
  258. distance?: number | number[] | ((distance: number) => boolean);
  259. }
  260. }
  261. export declare namespace Model {
  262. interface EventArgs extends Collection.CellEventArgs, Collection.NodeEventArgs, Collection.EdgeEventArgs {
  263. 'batch:start': {
  264. name: BatchName | string;
  265. data: KeyValue;
  266. };
  267. 'batch:stop': {
  268. name: BatchName | string;
  269. data: KeyValue;
  270. };
  271. sorted: null;
  272. reseted: {
  273. current: Cell[];
  274. previous: Cell[];
  275. options: Collection.SetOptions;
  276. };
  277. updated: {
  278. added: Cell[];
  279. merged: Cell[];
  280. removed: Cell[];
  281. options: Collection.SetOptions;
  282. };
  283. }
  284. type BatchName = 'update' | 'add' | 'remove' | 'clear' | 'to-back' | 'to-front' | 'scale' | 'resize' | 'rotate' | 'translate' | 'mouse' | 'layout' | 'add-edge' | 'fit-embeds' | 'dnd' | 'halo' | 'cut' | 'paste' | 'knob' | 'add-vertex' | 'move-anchor' | 'move-vertex' | 'move-segment' | 'move-arrowhead' | 'move-selection';
  285. }
  286. export declare namespace Model {
  287. interface ToJSONOptions extends Cell.ToJSONOptions {
  288. }
  289. function toJSON(cells: Cell[], options?: ToJSONOptions): {
  290. cells: Cell.Properties[];
  291. };
  292. function fromJSON(data: FromJSONData): (Edge<Edge.Properties> | Node<Node.Properties>)[];
  293. }