graphFrame.ts 60 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352
  1. import { GraphNode } from './graphNode';
  2. import { GraphCanvasComponent, FramePortData } from './graphCanvas';
  3. import { Nullable } from 'babylonjs/types';
  4. import { Observer, Observable } from 'babylonjs/Misc/observable';
  5. import { NodeLink } from './nodeLink';
  6. import { IFrameData } from '../nodeLocationInfo';
  7. import { Color3 } from 'babylonjs/Maths/math.color';
  8. import { NodePort } from './nodePort';
  9. import { SerializationTools } from '../serializationTools';
  10. import { StringTools } from '../stringTools';
  11. import { FrameNodePort } from './frameNodePort';
  12. enum ResizingDirection {
  13. Right,
  14. Left,
  15. Top,
  16. Bottom,
  17. TopRight,
  18. TopLeft,
  19. BottomRight,
  20. BottomLeft
  21. }
  22. export enum FramePortPosition {
  23. Top, Middle, Bottom
  24. };
  25. export class GraphFrame {
  26. private readonly CollapsedWidth = 200;
  27. private static _FrameCounter = 0;
  28. private static _FramePortCounter = 0;
  29. private _name: string;
  30. private _color: Color3;
  31. private _x = 0;
  32. private _y = 0;
  33. private _gridAlignedX = 0;
  34. private _gridAlignedY = 0;
  35. private _width: number;
  36. private _height: number;
  37. public element: HTMLDivElement;
  38. private _borderElement: HTMLDivElement;
  39. private _headerElement: HTMLDivElement;
  40. private _headerTextElement: HTMLDivElement;
  41. private _headerCollapseElement: HTMLDivElement;
  42. private _headerCloseElement: HTMLDivElement;
  43. private _commentsElement: HTMLDivElement;
  44. private _portContainer: HTMLDivElement;
  45. private _outputPortContainer: HTMLDivElement;
  46. private _inputPortContainer: HTMLDivElement;
  47. private _nodes: GraphNode[] = [];
  48. private _ownerCanvas: GraphCanvasComponent;
  49. private _mouseStartPointX: Nullable<number> = null;
  50. private _mouseStartPointY: Nullable<number> = null;
  51. private _onSelectionChangedObserver: Nullable<Observer<Nullable<GraphFrame | GraphNode | NodeLink | NodePort | FramePortData>>>;
  52. private _onGraphNodeRemovalObserver: Nullable<Observer<GraphNode>>;
  53. private _onExposePortOnFrameObserver: Nullable<Observer<GraphNode>>;
  54. private _onNodeLinkDisposedObservers: Nullable<Observer<NodeLink>>[] = [];
  55. private _isCollapsed = false;
  56. private _frameInPorts: FrameNodePort[] = [];
  57. private _frameOutPorts: FrameNodePort[] = [];
  58. private _controlledPorts: NodePort[] = []; // Ports on Nodes that are shown on outside of frame
  59. private _id: number;
  60. private _comments: string;
  61. private _frameIsResizing: boolean;
  62. private _resizingDirection: Nullable<ResizingDirection>;
  63. private _minFrameHeight = 40;
  64. private _minFrameWidth = 220;
  65. private mouseXLimit: Nullable<number>;
  66. public onExpandStateChanged = new Observable<GraphFrame>();
  67. private readonly CloseSVG = `<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 30 30"><g id="Layer_2" data-name="Layer 2"><path d="M16,15l5.85,5.84-1,1L15,15.93,9.15,21.78l-1-1L14,15,8.19,9.12l1-1L15,14l5.84-5.84,1,1Z"/></g></svg>`;
  68. private readonly ExpandSVG = `<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 30 30"><g id="Layer_2" data-name="Layer 2"><path d="M22.31,7.69V22.31H7.69V7.69ZM21.19,8.81H8.81V21.19H21.19Zm-6.75,6.75H11.06V14.44h3.38V11.06h1.12v3.38h3.38v1.12H15.56v3.38H14.44Z"/></g></svg>`;
  69. private readonly CollapseSVG = `<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 30 30"><g id="Layer_2" data-name="Layer 2"><path d="M22.31,7.69V22.31H7.69V7.69ZM21.19,8.81H8.81V21.19H21.19Zm-2.25,6.75H11.06V14.44h7.88Z"/></g></svg>`;
  70. public get id() {
  71. return this._id;
  72. }
  73. public get isCollapsed() {
  74. return this._isCollapsed;
  75. }
  76. private _createInputPort(port: NodePort, node: GraphNode) {
  77. let localPort = FrameNodePort.CreateFrameNodePortElement(port.connectionPoint, node, this._inputPortContainer, null, this._ownerCanvas.globalState, true, GraphFrame._FramePortCounter++, this.id);
  78. this._frameInPorts.push(localPort);
  79. port.delegatedPort = localPort;
  80. this._controlledPorts.push(port);
  81. }
  82. // Mark ports with FramePortPosition for re-arrangement support
  83. private _markFramePortPositions() {
  84. // mark FrameInPorts
  85. if(this._frameInPorts.length == 2){
  86. this._frameInPorts[0].framePortPosition = FramePortPosition.Top;
  87. this._frameInPorts[1].framePortPosition = FramePortPosition.Bottom;
  88. } else {
  89. for(let i = 0; i < this._frameInPorts.length; i++) {
  90. const port = this._frameInPorts[i];
  91. if(i === 0){
  92. port.framePortPosition = FramePortPosition.Top;
  93. } else if(i === this._frameInPorts.length -1){
  94. port.framePortPosition = FramePortPosition.Bottom;
  95. } else {
  96. port.framePortPosition = FramePortPosition.Middle;
  97. }
  98. }
  99. }
  100. // mark FrameOutPorts
  101. if(this._frameOutPorts.length == 2){
  102. this._frameOutPorts[0].framePortPosition = FramePortPosition.Top;
  103. this._frameOutPorts[1].framePortPosition = FramePortPosition.Bottom;
  104. } else {
  105. for(let i = 0; i < this._frameOutPorts.length; i++) {
  106. const port = this._frameOutPorts[i];
  107. if(i === 0){
  108. port.framePortPosition = FramePortPosition.Top
  109. } else if(i === this._frameInPorts.length -1){
  110. port.framePortPosition = FramePortPosition.Bottom
  111. } else {
  112. port.framePortPosition = FramePortPosition.Middle
  113. }
  114. }
  115. }
  116. }
  117. private _createFramePorts() {
  118. for (var node of this._nodes) {
  119. node.isVisible = false;
  120. for (var port of node.outputPorts) { // Output
  121. if (port.connectionPoint.hasEndpoints) {
  122. let portAdded = false;
  123. for (var link of node.links) {
  124. if (link.portA === port && this.nodes.indexOf(link.nodeB!) === -1 || (link.portA === port && port.exposedOnFrame)) {
  125. let localPort: FrameNodePort;
  126. if (!portAdded) {
  127. portAdded = true;
  128. localPort = FrameNodePort.CreateFrameNodePortElement(port.connectionPoint, link.nodeA!, this._outputPortContainer, null, this._ownerCanvas.globalState, false, GraphFrame._FramePortCounter++, this.id);
  129. this._frameOutPorts.push(localPort);
  130. link.isVisible = true;
  131. const onLinkDisposedObserver = link.onDisposedObservable.add((nodeLink: NodeLink) => {
  132. this._redrawFramePorts();
  133. });
  134. this._onNodeLinkDisposedObservers.push(onLinkDisposedObserver);
  135. } else if (this.nodes.indexOf(link.nodeB!) === -1) {
  136. link.isVisible = true;
  137. localPort = this.ports.filter(p => p.connectionPoint === port.connectionPoint)[0];
  138. } else {
  139. localPort = this.ports.filter(p => p.connectionPoint === port.connectionPoint)[0];
  140. }
  141. port.delegatedPort = localPort;
  142. this._controlledPorts.push(port);
  143. }
  144. }
  145. } else if(port.exposedOnFrame) {
  146. let localPort = FrameNodePort.CreateFrameNodePortElement(port.connectionPoint, node, this._outputPortContainer, null, this._ownerCanvas.globalState, false, GraphFrame._FramePortCounter++, this.id);
  147. this._frameOutPorts.push(localPort);
  148. port.delegatedPort = localPort;
  149. this._controlledPorts.push(port);
  150. }
  151. }
  152. for (var port of node.inputPorts) { // Input
  153. if (port.connectionPoint.isConnected) {
  154. for (var link of node.links) {
  155. if (link.portB === port && this.nodes.indexOf(link.nodeA) === -1) {
  156. this._createInputPort(port, node);
  157. link.isVisible = true;
  158. const onLinkDisposedObserver = link.onDisposedObservable.add((nodeLink: NodeLink) => {
  159. this._redrawFramePorts();
  160. });
  161. this._onNodeLinkDisposedObservers.push(onLinkDisposedObserver);
  162. }
  163. }
  164. } else if(port.exposedOnFrame) {
  165. this._createInputPort(port, node);
  166. }
  167. }
  168. }
  169. }
  170. private _redrawFramePorts() {
  171. if(!this.isCollapsed) {
  172. return;
  173. }
  174. this._outputPortContainer.innerHTML = "";
  175. this._inputPortContainer.innerHTML = "";
  176. this.ports.forEach((framePort:FrameNodePort) => {
  177. framePort.dispose();
  178. });
  179. this._controlledPorts.forEach(port => {
  180. port.delegatedPort = null;
  181. port.refresh();
  182. })
  183. this._frameInPorts = [];
  184. this._frameOutPorts = [];
  185. this._controlledPorts = [];
  186. this._createFramePorts();
  187. this.ports.forEach((framePort: FrameNodePort) => framePort.node._refreshLinks());
  188. }
  189. public set isCollapsed(value: boolean) {
  190. if (this._isCollapsed === value) {
  191. return;
  192. }
  193. this._isCollapsed = value;
  194. this._ownerCanvas._frameIsMoving = true;
  195. // Need to delegate the outside ports to the frame
  196. if (value) {
  197. this.element.classList.add("collapsed");
  198. this._moveFrame((this.width - this.CollapsedWidth) / 2, 0);
  199. this._createFramePorts()
  200. this._markFramePortPositions()
  201. } else {
  202. this.element.classList.remove("collapsed");
  203. this._outputPortContainer.innerHTML = "";
  204. this._inputPortContainer.innerHTML = "";
  205. this._frameInPorts.forEach(p => {
  206. p.dispose();
  207. });
  208. this._frameOutPorts.forEach(p => {
  209. p.dispose();
  210. });
  211. this._controlledPorts.forEach(port => {
  212. port.delegatedPort = null;
  213. port.refresh();
  214. })
  215. this._frameInPorts = [];
  216. this._frameOutPorts = [];
  217. this._controlledPorts = [];
  218. this._onNodeLinkDisposedObservers = [];
  219. for (var node of this._nodes) {
  220. node.isVisible = true;
  221. }
  222. this._moveFrame(-(this.width - this.CollapsedWidth) / 2, 0);
  223. }
  224. this.cleanAccumulation();
  225. this._ownerCanvas._frameIsMoving = false;
  226. // UI
  227. if (this._isCollapsed) {
  228. this._headerCollapseElement.innerHTML = this.ExpandSVG;
  229. this._headerCollapseElement.title = "Expand";
  230. } else {
  231. this._headerCollapseElement.innerHTML = this.CollapseSVG;
  232. this._headerCollapseElement.title = "Collapse";
  233. }
  234. this.onExpandStateChanged.notifyObservers(this);
  235. }
  236. public get nodes() {
  237. return this._nodes;
  238. }
  239. public get ports(){
  240. return this._frameInPorts.concat(this._frameOutPorts);
  241. }
  242. public get name() {
  243. return this._name;
  244. }
  245. public set name(value: string) {
  246. this._name = value;
  247. this._headerTextElement.innerHTML = value;
  248. }
  249. public get color() {
  250. return this._color;
  251. }
  252. public set color(value: Color3) {
  253. this._color = value;
  254. this._headerElement.style.background = `rgba(${value.r * 255}, ${value.g * 255}, ${value.b * 255}, 1)`;
  255. this._headerElement.style.borderColor = `rgba(${value.r * 255}, ${value.g * 255}, ${value.b * 255}, 1)`;
  256. this.element.style.background = `rgba(${value.r * 255}, ${value.g * 255}, ${value.b * 255}, 0.7)`;
  257. }
  258. public get x() {
  259. return this._x;
  260. }
  261. public set x(value: number) {
  262. if (this._x === value) {
  263. return;
  264. }
  265. this._x = value;
  266. this._gridAlignedX = this._ownerCanvas.getGridPosition(value);
  267. this.element.style.left = `${this._gridAlignedX}px`;
  268. }
  269. public get y() {
  270. return this._y;
  271. }
  272. public set y(value: number) {
  273. if (this._y === value) {
  274. return;
  275. }
  276. this._y = value;
  277. this._gridAlignedY = this._ownerCanvas.getGridPosition(value);
  278. this.element.style.top = `${this._gridAlignedY}px`;
  279. }
  280. public get width() {
  281. return this._width;
  282. }
  283. public set width(value: number) {
  284. if (this._width === value) {
  285. return;
  286. }
  287. let viableWidth = value > this._minFrameWidth ? value : this._minFrameWidth;
  288. this._width = viableWidth;
  289. var gridAlignedRight = this._ownerCanvas.getGridPositionCeil(viableWidth + this._gridAlignedX);
  290. this.element.style.width = `${gridAlignedRight - this._gridAlignedX}px`;
  291. }
  292. public get height() {
  293. return this._height;
  294. }
  295. public set height(value: number) {
  296. if (this._height === value) {
  297. return;
  298. }
  299. this._height = value;
  300. var gridAlignedBottom = this._ownerCanvas.getGridPositionCeil(value + this._gridAlignedY);
  301. this.element.style.height = `${gridAlignedBottom - this._gridAlignedY}px`;
  302. }
  303. public get comments(): string {
  304. return this._comments;
  305. }
  306. public set comments(comments: string) {
  307. if (comments && !this._comments && comments.length > 0) {
  308. this.element.style.gridTemplateRows = "40px min-content 1fr";
  309. this._borderElement.style.gridRow = "1 / span 3";
  310. this._portContainer.style.gridRow = "3";
  311. this._commentsElement.classList.add("has-comments");
  312. } else if (!comments) {
  313. this.element.style.gridTemplateRows = "40px calc(100% - 40px)";
  314. this._borderElement.style.gridRow = "1 / span 2";
  315. this._portContainer.style.gridRow = "2";
  316. this._commentsElement.classList.remove('has-comments');
  317. }
  318. if (comments === "" || (comments && comments.length >= 0)) {
  319. (this._commentsElement.children[0] as HTMLSpanElement).innerText = comments;
  320. }
  321. this.height = this._borderElement.offsetHeight;
  322. this._comments = comments;
  323. this.updateMinHeightWithComments();
  324. }
  325. public constructor(candidate: Nullable<HTMLDivElement>, canvas: GraphCanvasComponent, doNotCaptureNodes = false) {
  326. this._id = GraphFrame._FrameCounter++;
  327. this._ownerCanvas = canvas;
  328. const root = canvas.frameContainer;
  329. this.element = root.ownerDocument!.createElement("div");
  330. this.element.classList.add("frame-box");
  331. root.appendChild(this.element);
  332. this._headerElement = root.ownerDocument!.createElement("div");
  333. this._headerElement.classList.add("frame-box-header");
  334. this._headerElement.addEventListener("dblclick", () => {
  335. this.isCollapsed = !this.isCollapsed;
  336. });
  337. this.element.appendChild(this._headerElement);
  338. this._borderElement = root.ownerDocument!.createElement("div");
  339. this._borderElement.classList.add("frame-box-border");
  340. this.element.appendChild(this._borderElement);
  341. // add resizing side handles
  342. const rightHandle: HTMLDivElement = root.ownerDocument!.createElement("div");
  343. rightHandle.className = "handle right-handle";
  344. this.element.appendChild(rightHandle);
  345. rightHandle.addEventListener("pointerdown", this._onRightHandlePointerDown);
  346. const leftHandle: HTMLDivElement = root.ownerDocument!.createElement("div");
  347. leftHandle.className = "handle left-handle";
  348. this.element.appendChild(leftHandle);
  349. leftHandle.addEventListener("pointerdown", this._onLeftHandlePointerDown);
  350. const bottomHandle: HTMLDivElement = root.ownerDocument!.createElement("div");
  351. bottomHandle.className = "handle bottom-handle";
  352. this.element.appendChild(bottomHandle);
  353. bottomHandle.addEventListener("pointerdown", this._onBottomHandlePointerDown);
  354. const topHandle: HTMLDivElement = root.ownerDocument!.createElement("div");
  355. topHandle.className = "handle top-handle";
  356. this.element.appendChild(topHandle);
  357. topHandle.addEventListener("pointerdown", this._onTopHandlePointerDown);
  358. const topRightCornerHandle: HTMLDivElement = root.ownerDocument!.createElement("div");
  359. topRightCornerHandle.className = "handle right-handle top-right-corner-handle";
  360. this.element.appendChild(topRightCornerHandle);
  361. topRightCornerHandle.addEventListener("pointerdown", this._onTopRightHandlePointerDown);
  362. const bottomRightCornerHandle: HTMLDivElement = root.ownerDocument!.createElement("div");
  363. bottomRightCornerHandle.className = "handle right-handle bottom-right-corner-handle";
  364. this.element.appendChild(bottomRightCornerHandle);
  365. bottomRightCornerHandle.addEventListener("pointerdown", this._onBottomRightHandlePointerDown);
  366. const topLeftCornerHandle: HTMLDivElement = root.ownerDocument!.createElement("div");
  367. topLeftCornerHandle.className = "handle left-handle top-left-corner-handle";
  368. this.element.appendChild(topLeftCornerHandle);
  369. topLeftCornerHandle.addEventListener("pointerdown", this._onTopLeftHandlePointerDown);
  370. const bottomLeftCornerHandle: HTMLDivElement = root.ownerDocument!.createElement("div");
  371. bottomLeftCornerHandle.className = "handle left-handle bottom-left-corner-handle";
  372. this.element.appendChild(bottomLeftCornerHandle);
  373. bottomLeftCornerHandle.addEventListener("pointerdown", this._onBottomLeftHandlePointerDown);
  374. // add header elements
  375. this._headerTextElement = root.ownerDocument!.createElement("div");
  376. this._headerTextElement.classList.add("frame-box-header-title");
  377. this._headerElement.appendChild(this._headerTextElement);
  378. this._headerCollapseElement = root.ownerDocument!.createElement("div");
  379. this._headerCollapseElement.classList.add("frame-box-header-collapse");
  380. this._headerCollapseElement.classList.add("frame-box-header-button");
  381. this._headerCollapseElement.title = "Collapse";
  382. this._headerCollapseElement.ondragstart= () => false;
  383. this._headerCollapseElement.addEventListener("pointerdown", (evt) => {
  384. this._headerCollapseElement.classList.add("down");
  385. evt.stopPropagation();
  386. });
  387. this._headerCollapseElement.addEventListener("pointerup", (evt) => {
  388. evt.stopPropagation();
  389. this._headerCollapseElement.classList.remove("down");
  390. this.isCollapsed = !this.isCollapsed;
  391. });
  392. this._headerCollapseElement.innerHTML = this.CollapseSVG;
  393. this._headerElement.appendChild(this._headerCollapseElement);
  394. this._headerCloseElement = root.ownerDocument!.createElement("div");
  395. this._headerCloseElement.classList.add("frame-box-header-close");
  396. this._headerCloseElement.classList.add("frame-box-header-button");
  397. this._headerCloseElement.title = "Close";
  398. this._headerCloseElement.ondragstart= () => false;
  399. this._headerCloseElement.addEventListener("pointerdown", (evt) => {
  400. evt.stopPropagation();
  401. });
  402. this._headerCloseElement.addEventListener("pointerup", (evt) => {
  403. evt.stopPropagation();
  404. this.dispose();
  405. });
  406. this._headerCloseElement.innerHTML = this.CloseSVG;
  407. this._headerElement.appendChild(this._headerCloseElement);
  408. this._portContainer = root.ownerDocument!.createElement("div");
  409. this._portContainer.classList.add("port-container");
  410. this.element.appendChild(this._portContainer);
  411. this._outputPortContainer = root.ownerDocument!.createElement("div");
  412. this._outputPortContainer.classList.add("outputsContainer");
  413. this._portContainer.appendChild(this._outputPortContainer);
  414. this._inputPortContainer = root.ownerDocument!.createElement("div");
  415. this._inputPortContainer.classList.add("inputsContainer");
  416. this._portContainer.appendChild(this._inputPortContainer);
  417. this.name = "Frame";
  418. this.color = Color3.FromInts(72, 72, 72);
  419. if (candidate) {
  420. this.x = parseFloat(candidate.style.left!.replace("px", ""));
  421. this.y = parseFloat(candidate.style.top!.replace("px", ""));
  422. this.width = parseFloat(candidate.style.width!.replace("px", ""));
  423. this.height = parseFloat(candidate.style.height!.replace("px", ""));
  424. this.cleanAccumulation();
  425. }
  426. this._headerTextElement.addEventListener("pointerdown", evt => this._onDown(evt));
  427. this._headerTextElement.addEventListener("pointerup", evt => this._onUp(evt));
  428. this._headerTextElement.addEventListener("pointermove", evt => this._onMove(evt));
  429. this._onSelectionChangedObserver = canvas.globalState.onSelectionChangedObservable.add(node => {
  430. if (node === this) {
  431. this.element.classList.add("selected");
  432. } else {
  433. this.element.classList.remove("selected");
  434. }
  435. });
  436. this._onGraphNodeRemovalObserver = canvas.globalState.onGraphNodeRemovalObservable.add((node: GraphNode) => {
  437. // remove node from this._nodes
  438. const index = this._nodes.indexOf(node);
  439. if (index === -1) {
  440. return;
  441. } else {
  442. this._nodes.splice(index, 1);
  443. }
  444. });
  445. this._onExposePortOnFrameObserver = canvas.globalState.onExposePortOnFrameObservable.add((node: GraphNode) => {
  446. if (this.nodes.indexOf(node) === -1) {
  447. return;
  448. }
  449. this._redrawFramePorts();
  450. });
  451. this._commentsElement = document.createElement('div');
  452. this._commentsElement.className = 'frame-comments';
  453. this._commentsElement.style.color = 'white';
  454. this._commentsElement.style.fontSize = '16px';
  455. let commentSpan = document.createElement('span');
  456. commentSpan.className = "frame-comment-span"
  457. this._commentsElement.appendChild(commentSpan)
  458. this.element.appendChild(this._commentsElement);
  459. // Get nodes
  460. if (!doNotCaptureNodes) {
  461. this.refresh();
  462. }
  463. }
  464. public refresh() {
  465. this._nodes = [];
  466. this._ownerCanvas.globalState.onFrameCreatedObservable.notifyObservers(this);
  467. }
  468. public addNode(node: GraphNode) {
  469. let index = this.nodes.indexOf(node);
  470. if (index === -1) {
  471. this.nodes.push(node);
  472. }
  473. }
  474. public removeNode(node: GraphNode) {
  475. let index = this.nodes.indexOf(node);
  476. if (index > -1) {
  477. this.nodes.splice(index, 1);
  478. }
  479. }
  480. public syncNode(node: GraphNode) {
  481. if (this.isCollapsed) {
  482. return;
  483. }
  484. if (node.isOverlappingFrame(this)) {
  485. this.addNode(node);
  486. } else {
  487. this.removeNode(node);
  488. }
  489. }
  490. public cleanAccumulation() {
  491. for (var selectedNode of this._nodes) {
  492. selectedNode.cleanAccumulation();
  493. }
  494. this.x = this._ownerCanvas.getGridPosition(this.x);
  495. this.y = this._ownerCanvas.getGridPosition(this.y);
  496. }
  497. private _onDown(evt: PointerEvent) {
  498. evt.stopPropagation();
  499. this._mouseStartPointX = evt.clientX;
  500. this._mouseStartPointY = evt.clientY;
  501. this._headerTextElement.setPointerCapture(evt.pointerId);
  502. this._ownerCanvas.globalState.onSelectionChangedObservable.notifyObservers(this);
  503. this._ownerCanvas._frameIsMoving = true;
  504. this.move(this._ownerCanvas.getGridPosition(this.x), this._ownerCanvas.getGridPosition(this.y))
  505. }
  506. public move(newX: number, newY: number, align = true) {
  507. let oldX = this.x;
  508. let oldY = this.y;
  509. this.x = newX;
  510. this.y = newY;
  511. for (var selectedNode of this._nodes) {
  512. selectedNode.x += this.x - oldX;
  513. selectedNode.y += this.y - oldY;
  514. if (align) {
  515. selectedNode.cleanAccumulation(true);
  516. }
  517. }
  518. }
  519. private _onUp(evt: PointerEvent) {
  520. evt.stopPropagation();
  521. this.cleanAccumulation();
  522. this._mouseStartPointX = null;
  523. this._mouseStartPointY = null;
  524. this._headerTextElement.releasePointerCapture(evt.pointerId);
  525. this._ownerCanvas._frameIsMoving = false;
  526. }
  527. private _moveFrame(offsetX: number, offsetY: number) {
  528. this.x += offsetX;
  529. this.y += offsetY;
  530. for (var selectedNode of this._nodes) {
  531. selectedNode.x += offsetX;
  532. selectedNode.y += offsetY;
  533. }
  534. }
  535. private _onMove(evt: PointerEvent) {
  536. if (this._mouseStartPointX === null || this._mouseStartPointY === null || evt.ctrlKey || this._frameIsResizing) {
  537. return;
  538. }
  539. let newX = (evt.clientX - this._mouseStartPointX) / this._ownerCanvas.zoom;
  540. let newY = (evt.clientY - this._mouseStartPointY) / this._ownerCanvas.zoom;
  541. this._moveFrame(newX, newY);
  542. this._mouseStartPointX = evt.clientX;
  543. this._mouseStartPointY = evt.clientY;
  544. evt.stopPropagation();
  545. }
  546. public moveFramePortUp(nodePort: FrameNodePort){
  547. let elementsArray: ChildNode[];
  548. if(nodePort.isInput) {
  549. if(this._inputPortContainer.children.length < 2) {
  550. return;
  551. }
  552. elementsArray = Array.from(this._inputPortContainer.childNodes);
  553. this._movePortUp(elementsArray, nodePort, this._frameInPorts);
  554. } else {
  555. if(this._outputPortContainer.children.length < 2) {
  556. return;
  557. }
  558. elementsArray = Array.from(this._outputPortContainer.childNodes);
  559. this._movePortUp(elementsArray, nodePort, this._frameOutPorts);
  560. }
  561. this.ports.forEach((framePort: FrameNodePort) => framePort.node._refreshLinks());
  562. }
  563. private _movePortUp(elementsArray: ChildNode[], nodePort: FrameNodePort, framePortList: FrameNodePort[]) {
  564. // update UI
  565. const indexInElementArray = (elementsArray as HTMLElement[]).findIndex(elem => elem.dataset.framePortId === `${nodePort.framePortId}`)
  566. if(indexInElementArray === 0){
  567. return;
  568. }
  569. const secondPortElement = elementsArray[indexInElementArray];
  570. const firstPortElement = elementsArray[indexInElementArray -1];
  571. firstPortElement.parentElement?.insertBefore(secondPortElement, firstPortElement);
  572. // update Frame Port Container
  573. const indexInContainer = framePortList.findIndex(framePort => framePort === nodePort);
  574. [framePortList[indexInContainer -1], framePortList[indexInContainer]] = [framePortList[indexInContainer], framePortList[indexInContainer -1]]; // swap idicies
  575. //special case framePortList.length == 2
  576. if(framePortList.length == 2) {
  577. framePortList[1].framePortPosition = FramePortPosition.Bottom;
  578. framePortList[0].framePortPosition = FramePortPosition.Top;
  579. } else {
  580. // notify nodePort if it is now at Top (indexInElementArray === 1)
  581. if (indexInElementArray === 1) {
  582. framePortList[1].framePortPosition = FramePortPosition.Middle;
  583. framePortList[0].framePortPosition = FramePortPosition.Top;
  584. } else if(indexInContainer === elementsArray.length-1) {
  585. framePortList[framePortList.length -1].framePortPosition = FramePortPosition.Bottom;
  586. framePortList[framePortList.length -2].framePortPosition = FramePortPosition.Middle;
  587. } else {
  588. nodePort.framePortPosition = FramePortPosition.Middle;
  589. }
  590. }
  591. }
  592. public moveFramePortDown(nodePort: FrameNodePort){
  593. let elementsArray: ChildNode[];
  594. if(nodePort.isInput) {
  595. if(this._inputPortContainer.children.length < 2) {
  596. return;
  597. }
  598. elementsArray = Array.from(this._inputPortContainer.childNodes);
  599. this._movePortDown(elementsArray, nodePort, this._frameInPorts);
  600. } else {
  601. if(this._outputPortContainer.children.length < 2) {
  602. return;
  603. }
  604. elementsArray = Array.from(this._outputPortContainer.childNodes);
  605. this._movePortDown(elementsArray, nodePort, this._frameOutPorts);
  606. }
  607. this.ports.forEach((framePort: FrameNodePort) => framePort.node._refreshLinks());
  608. }
  609. private _movePortDown(elementsArray: ChildNode[], nodePort: FrameNodePort, framePortList: FrameNodePort[]) {
  610. // update UI
  611. const indexInElementArray = (elementsArray as HTMLElement[]).findIndex(elem => elem.dataset.framePortId === `${nodePort.framePortId}`)
  612. if(indexInElementArray === elementsArray.length -1){
  613. return;
  614. }
  615. const firstPort = elementsArray[indexInElementArray];
  616. const secondPort = elementsArray[indexInElementArray + 1];
  617. firstPort.parentElement?.insertBefore(secondPort, firstPort);
  618. // update Frame Port Container
  619. const indexInContainer = framePortList.findIndex(framePort => framePort === nodePort);
  620. [framePortList[indexInContainer], framePortList[indexInContainer + 1]] = [framePortList[indexInContainer + 1], framePortList[indexInContainer]]; // swap idicies
  621. // notify nodePort if it is now at bottom (indexInContainer === elementsArray.length-2)
  622. if(framePortList.length == 2) {
  623. framePortList[0].framePortPosition = FramePortPosition.Top;
  624. framePortList[1].framePortPosition = FramePortPosition.Bottom;
  625. } else {
  626. if(indexInContainer === elementsArray.length-2) {
  627. framePortList[elementsArray.length-2].framePortPosition = FramePortPosition.Middle;
  628. framePortList[elementsArray.length-1].framePortPosition = FramePortPosition.Bottom;
  629. } else if(indexInContainer === 0){
  630. framePortList[0].framePortPosition = FramePortPosition.Top;
  631. framePortList[1].framePortPosition = FramePortPosition.Middle;
  632. } else {
  633. nodePort.framePortPosition = FramePortPosition.Middle;
  634. }
  635. }
  636. }
  637. private initResizing = (evt: PointerEvent) => {
  638. evt.stopPropagation();
  639. this._mouseStartPointX = evt.clientX;
  640. this._mouseStartPointY = evt.clientY;
  641. this._frameIsResizing = true;
  642. }
  643. private cleanUpResizing = (evt: PointerEvent) => {
  644. evt.stopPropagation();
  645. this._frameIsResizing = false;
  646. this._resizingDirection = null;
  647. this._mouseStartPointX = null;
  648. this._mouseStartPointY = null;
  649. this.mouseXLimit = null;
  650. this.refresh();
  651. }
  652. private updateMinHeightWithComments = () => {
  653. if (this.comments && this.comments.length > 0) {
  654. const minFrameHeightWithComments = this._commentsElement.offsetHeight + 40;
  655. this._minFrameHeight = minFrameHeightWithComments;
  656. }
  657. }
  658. private _isResizingTop(){
  659. return this._resizingDirection === ResizingDirection.Top || this._resizingDirection === ResizingDirection.TopRight || this._resizingDirection === ResizingDirection.TopLeft;
  660. }
  661. private _isResizingRight(){
  662. return this._resizingDirection === ResizingDirection.Right || this._resizingDirection === ResizingDirection.TopRight || this._resizingDirection === ResizingDirection.BottomRight;
  663. }
  664. private _isResizingBottom(){
  665. return this._resizingDirection === ResizingDirection.Bottom || this._resizingDirection === ResizingDirection.BottomLeft || this._resizingDirection === ResizingDirection.BottomRight;
  666. }
  667. private _isResizingLeft() {
  668. return this._resizingDirection === ResizingDirection.Left || this._resizingDirection === ResizingDirection.TopLeft || this._resizingDirection === ResizingDirection.BottomLeft;
  669. }
  670. private _onRightHandlePointerDown = (evt: PointerEvent) => {
  671. // tslint:disable-next-line: no-this-assignment
  672. const _this = this;
  673. if (_this.isCollapsed) {
  674. return;
  675. }
  676. this.initResizing(evt);
  677. _this._resizingDirection = ResizingDirection.Right;
  678. _this.mouseXLimit = evt.clientX - (_this.width - _this._minFrameWidth);
  679. _this._ownerCanvas.hostCanvas.addEventListener("pointerup", _this._onRightHandlePointerUp);
  680. _this._ownerCanvas.hostCanvas.addEventListener("pointermove", _this._onRightHandlePointerMove);
  681. }
  682. private _onRightHandlePointerMove = (evt: PointerEvent) => {
  683. const slack = (this.element.offsetWidth - this._minFrameWidth) * this._ownerCanvas.zoom;
  684. const xLimit = (this._mouseStartPointX as number) - slack;
  685. this._moveRightHandle(evt, xLimit);
  686. }
  687. private _moveRightHandle = (evt: PointerEvent, xLimit: number) => {
  688. // tslint:disable-next-line: no-this-assignment
  689. const _this = this;
  690. if (_this.mouseXLimit) {
  691. if (!_this._isResizingRight() || _this._mouseStartPointX === null || _this._mouseStartPointY === null || evt.clientX < xLimit) {
  692. return;
  693. }
  694. if (_this._isResizingRight()) {
  695. evt.stopPropagation();
  696. const distanceMouseMoved = (evt.clientX - _this._mouseStartPointX) / _this._ownerCanvas.zoom;
  697. _this._expandRight(distanceMouseMoved, evt.clientX);
  698. _this._mouseStartPointX = evt.clientX;
  699. }
  700. }
  701. }
  702. private _onRightHandlePointerUp = (evt: PointerEvent) => {
  703. // tslint:disable-next-line: no-this-assignment
  704. const _this = this;
  705. if (_this._isResizingRight()) {
  706. _this.width = parseFloat(_this.element.style.width.replace("px", ""));
  707. _this._ownerCanvas.hostCanvas.removeEventListener("pointerup", _this._onRightHandlePointerUp);
  708. _this._ownerCanvas.hostCanvas.removeEventListener("pointermove", _this._onRightHandlePointerMove);
  709. _this.cleanUpResizing(evt);
  710. }
  711. }
  712. private _onBottomHandlePointerDown = (evt: PointerEvent) => {
  713. // tslint:disable-next-line: no-this-assignment
  714. const _this = this;
  715. if (_this.isCollapsed) {
  716. return;
  717. }
  718. _this.initResizing(evt);
  719. _this._resizingDirection = ResizingDirection.Bottom;
  720. _this._ownerCanvas.hostCanvas.addEventListener("pointermove", _this._onBottomHandlePointerMove);
  721. _this._ownerCanvas.hostCanvas.addEventListener("pointerup", _this._onBottomHandlePointerUp);
  722. }
  723. private _onBottomHandlePointerMove = (evt: PointerEvent) => {
  724. const slack = (this.element.offsetHeight - this._minFrameHeight) * this._ownerCanvas.zoom;
  725. const yLimit = (this._mouseStartPointY as number) - slack;
  726. this._moveBottomHandle(evt, yLimit);
  727. }
  728. private _moveBottomHandle = (evt: PointerEvent, yLimit: number) => {
  729. // tslint:disable-next-line: no-this-assignment
  730. const _this = this;
  731. if (_this._resizingDirection !== ResizingDirection.Bottom || _this._mouseStartPointX === null || _this._mouseStartPointY === null || evt.clientY < yLimit) {
  732. return;
  733. }
  734. if (_this._resizingDirection === ResizingDirection.Bottom) {
  735. evt.stopPropagation();
  736. const distanceMouseMoved = (evt.clientY - _this._mouseStartPointY) / _this._ownerCanvas.zoom;
  737. _this._expandBottom(distanceMouseMoved);
  738. _this._mouseStartPointY = evt.clientY;
  739. }
  740. }
  741. private _onBottomHandlePointerUp = (evt: PointerEvent) => {
  742. // tslint:disable-next-line: no-this-assignment
  743. const _this = this;
  744. if (_this._resizingDirection === ResizingDirection.Bottom) {
  745. _this.height = parseFloat(_this.element.style.height.replace("px", ""));
  746. _this._ownerCanvas.hostCanvas.removeEventListener("pointermove", _this._onBottomHandlePointerMove);
  747. _this._ownerCanvas.hostCanvas.removeEventListener("pointerup", _this._onBottomHandlePointerUp);
  748. _this.cleanUpResizing(evt);
  749. }
  750. }
  751. private _onLeftHandlePointerDown = (evt: PointerEvent) => {
  752. // tslint:disable-next-line: no-this-assignment
  753. const _this = this;
  754. if (_this.isCollapsed) {
  755. return;
  756. }
  757. _this.initResizing(evt);
  758. _this._resizingDirection = ResizingDirection.Left;
  759. _this.mouseXLimit = evt.clientX + _this.width - _this._minFrameWidth;
  760. _this._ownerCanvas.hostCanvas.addEventListener("pointerup", _this._onLeftHandlePointerUp);
  761. _this._ownerCanvas.hostCanvas.addEventListener("pointermove", _this._onLeftHandlePointerMove);
  762. }
  763. private _onLeftHandlePointerMove = (evt: PointerEvent) => {
  764. const slack = (this.element.offsetWidth - this._minFrameWidth) * this._ownerCanvas.zoom;
  765. const xLimit = (this._mouseStartPointX as number) + slack;
  766. this._moveLeftHandle(evt, xLimit);
  767. }
  768. private _moveLeftHandle = (evt: PointerEvent, xLimit: number) => {
  769. // tslint:disable-next-line: no-this-assignment
  770. const _this = this;
  771. if (_this.mouseXLimit) {
  772. if (_this._resizingDirection !== ResizingDirection.Left || _this._mouseStartPointX === null || _this._mouseStartPointY === null || evt.clientX > xLimit) {
  773. return;
  774. }
  775. if (_this._resizingDirection === ResizingDirection.Left) {
  776. evt.stopPropagation();
  777. const distanceMouseMoved = (evt.clientX - _this._mouseStartPointX) / _this._ownerCanvas.zoom;
  778. _this._expandLeft(distanceMouseMoved);
  779. _this._mouseStartPointX = evt.clientX;
  780. }
  781. }
  782. }
  783. private _onLeftHandlePointerUp = (evt: PointerEvent) => {
  784. // tslint:disable-next-line: no-this-assignment
  785. const _this = this;
  786. if (_this._resizingDirection === ResizingDirection.Left) {
  787. _this.x = parseFloat(_this.element.style.left!.replace("px", ""));
  788. _this.width = parseFloat(_this.element.style.width.replace("px", ""));
  789. _this._ownerCanvas.hostCanvas.removeEventListener("pointerup", _this._onLeftHandlePointerUp);
  790. _this._ownerCanvas.hostCanvas.removeEventListener("pointermove", _this._onLeftHandlePointerMove);
  791. _this.cleanUpResizing(evt);
  792. }
  793. }
  794. private _onTopHandlePointerDown = (evt: PointerEvent) => {
  795. // tslint:disable-next-line: no-this-assignment
  796. const _this = this;
  797. if (_this.isCollapsed) {
  798. return;
  799. }
  800. _this.initResizing(evt);
  801. _this._resizingDirection = ResizingDirection.Top;
  802. _this._ownerCanvas.hostCanvas.addEventListener("pointerup", _this._onTopHandlePointerUp);
  803. _this._ownerCanvas.hostCanvas.addEventListener("pointermove", _this._onTopHandlePointerMove);
  804. }
  805. private _onTopHandlePointerMove = (evt: PointerEvent) => {
  806. const slack = (this.element.offsetHeight - this._minFrameHeight) * this._ownerCanvas.zoom;
  807. const yLimit = (this._mouseStartPointY as number) + slack;
  808. this._moveTopHandle(evt, yLimit);
  809. }
  810. private _moveTopHandle = (evt: PointerEvent, yLimit: number) => {
  811. // tslint:disable-next-line: no-this-assignment
  812. const _this = this;
  813. if (!_this._isResizingTop() || _this._mouseStartPointX === null || _this._mouseStartPointY === null || evt.clientY > yLimit) {
  814. return;
  815. }
  816. if (_this._isResizingTop()) {
  817. evt.stopPropagation();
  818. const distanceMouseMoved = (evt.clientY - _this._mouseStartPointY) / _this._ownerCanvas.zoom;
  819. _this._expandTop(distanceMouseMoved);
  820. _this._mouseStartPointY = evt.clientY;
  821. }
  822. }
  823. private _onTopHandlePointerUp = (evt: PointerEvent) => {
  824. // tslint:disable-next-line: no-this-assignment
  825. const _this = this;
  826. if (_this._isResizingTop()) {
  827. _this.y = parseFloat(_this.element.style.top!.replace("px", ""));
  828. _this.height = parseFloat(_this.element.style.height.replace("px", ""));
  829. _this._ownerCanvas.hostCanvas.removeEventListener("pointerup", _this._onTopHandlePointerUp);
  830. _this._ownerCanvas.hostCanvas.removeEventListener("pointermove", _this._onTopHandlePointerMove);
  831. _this.cleanUpResizing(evt);
  832. }
  833. }
  834. private _onTopRightHandlePointerDown = (evt: PointerEvent) => {
  835. // tslint:disable-next-line: no-this-assignment
  836. const _this = this;
  837. if (_this.isCollapsed) {
  838. return;
  839. }
  840. _this.initResizing(evt);
  841. _this._resizingDirection = ResizingDirection.TopRight;
  842. _this._ownerCanvas.hostCanvas.addEventListener("pointerup", _this._onTopRightHandlePointerUp);
  843. _this._ownerCanvas.hostCanvas.addEventListener("pointermove", _this._onTopRightHandlePointerMove);
  844. }
  845. private _onTopRightHandlePointerMove = (evt: PointerEvent) => {
  846. const topSlack = (this.element.offsetHeight - this._minFrameHeight) * this._ownerCanvas.zoom;
  847. const yLimit = (this._mouseStartPointY as number) + topSlack;
  848. const rightSlack = (this.element.offsetWidth - this._minFrameWidth) * this._ownerCanvas.zoom;
  849. const xLimit = (this._mouseStartPointX as number) - rightSlack;
  850. this._moveTopRightHandle(evt, xLimit, yLimit);
  851. }
  852. private _moveTopRightHandle = (evt: PointerEvent, xLimit: number, yLimit: number) => {
  853. // tslint:disable-next-line: no-this-assignment
  854. const _this = this;
  855. if (!(_this._isResizingTop() && _this._isResizingRight()) || _this._mouseStartPointX === null || _this._mouseStartPointY === null) {
  856. return;
  857. }
  858. if (_this._isResizingRight() && _this._isResizingTop()) {
  859. evt.stopPropagation();
  860. if (evt.clientY < yLimit && evt.clientX > xLimit) { // able to move in X and Y
  861. const distanceMouseMovedX = (evt.clientX - _this._mouseStartPointX) / _this._ownerCanvas.zoom;
  862. _this._expandRight(distanceMouseMovedX, evt.clientX);
  863. _this._mouseStartPointX = evt.clientX;
  864. const distanceMouseMovedY = (evt.clientY - _this._mouseStartPointY) / _this._ownerCanvas.zoom;
  865. _this._expandTop(distanceMouseMovedY);
  866. _this._mouseStartPointY = evt.clientY;
  867. } else if (evt.clientY > yLimit && evt.clientX > xLimit) { // able to move in X but not Y
  868. const distanceMouseMovedX = (evt.clientX - _this._mouseStartPointX) / _this._ownerCanvas.zoom;
  869. _this._expandRight(distanceMouseMovedX, evt.clientX);
  870. _this._mouseStartPointX = evt.clientX;
  871. } else if (evt.clientY < yLimit && evt.clientX < xLimit) { // able to move in Y but not X
  872. const distanceMouseMovedY = (evt.clientY - _this._mouseStartPointY) / _this._ownerCanvas.zoom;
  873. _this._expandTop(distanceMouseMovedY);
  874. _this._mouseStartPointY = evt.clientY;
  875. }
  876. }
  877. }
  878. private _onTopRightHandlePointerUp = (evt: PointerEvent) => {
  879. evt.stopPropagation();
  880. // tslint:disable-next-line: no-this-assignment
  881. const _this = this;
  882. if (_this._resizingDirection === ResizingDirection.TopRight) {
  883. _this.y = parseFloat(_this.element.style.top!.replace("px", ""));
  884. _this.height = parseFloat(_this.element.style.height.replace("px", ""));
  885. _this.width = parseFloat(_this.element.style.width.replace("px", ""));
  886. _this._ownerCanvas.hostCanvas.removeEventListener("pointerup", _this._onTopRightHandlePointerUp);
  887. _this._ownerCanvas.hostCanvas.removeEventListener("pointermove", _this._onTopRightHandlePointerMove);
  888. _this.cleanUpResizing(evt);
  889. }
  890. }
  891. private _onBottomRightHandlePointerDown = (evt: PointerEvent) => {
  892. // tslint:disable-next-line: no-this-assignment
  893. const _this = this;
  894. if (_this.isCollapsed) {
  895. return;
  896. }
  897. _this.initResizing(evt);
  898. _this._resizingDirection = ResizingDirection.BottomRight;
  899. _this._ownerCanvas.hostCanvas.addEventListener("pointerup", _this._onBottomRightHandlePointerUp);
  900. _this._ownerCanvas.hostCanvas.addEventListener("pointermove", _this._onBottomRightHandlePointerMove);
  901. }
  902. private _onBottomRightHandlePointerMove = (evt: PointerEvent) => {
  903. const bottomSlack = (this.element.offsetHeight - this._minFrameHeight) * this._ownerCanvas.zoom;
  904. const yLimit = (this._mouseStartPointY as number) - bottomSlack;
  905. const rightSlack = (this.element.offsetWidth - this._minFrameWidth) * this._ownerCanvas.zoom;
  906. const xLimit = (this._mouseStartPointX as number) - rightSlack;
  907. this._moveBottomRightHandle(evt, xLimit, yLimit);
  908. }
  909. private _moveBottomRightHandle = (evt: PointerEvent, xLimit: number, yLimit: number) => {
  910. // tslint:disable-next-line: no-this-assignment
  911. const _this = this;
  912. if (!(_this._isResizingBottom() && _this._isResizingRight()) || _this._mouseStartPointX === null || _this._mouseStartPointY === null) {
  913. return;
  914. }
  915. if (_this._isResizingRight() && _this._isResizingBottom()) {
  916. evt.stopPropagation();
  917. if (evt.clientY > yLimit && evt.clientX > xLimit) { // able to move in X and Y
  918. const distanceMouseMovedX = (evt.clientX - _this._mouseStartPointX) / _this._ownerCanvas.zoom;
  919. _this._expandRight(distanceMouseMovedX, evt.clientX);
  920. _this._mouseStartPointX = evt.clientX;
  921. const distanceMouseMovedY = (evt.clientY - _this._mouseStartPointY) / _this._ownerCanvas.zoom;
  922. _this._expandBottom(distanceMouseMovedY);
  923. _this._mouseStartPointY = evt.clientY;
  924. } else if (evt.clientY < yLimit && evt.clientX > xLimit) { // able to move in X but not Y
  925. const distanceMouseMovedX = (evt.clientX - _this._mouseStartPointX) / _this._ownerCanvas.zoom;
  926. _this._expandRight(distanceMouseMovedX, evt.clientX);
  927. _this._mouseStartPointX = evt.clientX;
  928. } else if (evt.clientY > yLimit && evt.clientX < xLimit) { // able to move in Y but not X
  929. const distanceMouseMovedY = (evt.clientY - _this._mouseStartPointY) / _this._ownerCanvas.zoom;
  930. _this._expandBottom(distanceMouseMovedY);
  931. _this._mouseStartPointY = evt.clientY;
  932. }
  933. }
  934. }
  935. private _onBottomRightHandlePointerUp = (evt: PointerEvent) => {
  936. evt.stopPropagation();
  937. // tslint:disable-next-line: no-this-assignment
  938. const _this = this;
  939. if (_this._resizingDirection === ResizingDirection.BottomRight) {
  940. _this.height = parseFloat(_this.element.style.height.replace("px", ""));
  941. _this.width = parseFloat(_this.element.style.width.replace("px", ""));
  942. _this._ownerCanvas.hostCanvas.removeEventListener("pointerup", _this._onBottomRightHandlePointerUp);
  943. _this._ownerCanvas.hostCanvas.removeEventListener("pointermove", _this._onBottomRightHandlePointerMove);
  944. _this.cleanUpResizing(evt);
  945. }
  946. }
  947. private _onBottomLeftHandlePointerDown = (evt: PointerEvent) => {
  948. // tslint:disable-next-line: no-this-assignment
  949. const _this = this;
  950. if (_this.isCollapsed) {
  951. return;
  952. }
  953. _this.initResizing(evt);
  954. _this._resizingDirection = ResizingDirection.BottomLeft;
  955. _this.mouseXLimit = evt.clientX + _this.width - _this._minFrameWidth;
  956. _this._ownerCanvas.hostCanvas.addEventListener("pointerup", _this._onBottomLeftHandlePointerUp);
  957. _this._ownerCanvas.hostCanvas.addEventListener("pointermove", _this._onBottomLeftHandlePointerMove);
  958. }
  959. private _onBottomLeftHandlePointerMove = (evt: PointerEvent) => {
  960. const bottomSlack = (this.element.offsetHeight - this._minFrameHeight) * this._ownerCanvas.zoom;
  961. const yLimit = (this._mouseStartPointY as number) - bottomSlack;
  962. const leftSlack = (this.element.offsetWidth - this._minFrameWidth) * this._ownerCanvas.zoom;
  963. const xLimit = (this._mouseStartPointX as number) + leftSlack;
  964. this._moveBottomLeftHandle(evt, xLimit, yLimit);
  965. }
  966. private _moveBottomLeftHandle = (evt: PointerEvent, xLimit: number, yLimit: number) => {
  967. // tslint:disable-next-line: no-this-assignment
  968. const _this = this;
  969. if (!(_this._isResizingBottom() && _this._isResizingLeft()) || _this._mouseStartPointX === null || _this._mouseStartPointY === null) {
  970. return;
  971. }
  972. if (_this._isResizingLeft() && _this._isResizingBottom()) {
  973. evt.stopPropagation();
  974. if (evt.clientY > yLimit && evt.clientX < xLimit) { // able to move in X and Y
  975. const distanceMouseMovedX = (evt.clientX - _this._mouseStartPointX) / _this._ownerCanvas.zoom;
  976. _this._expandLeft(distanceMouseMovedX);
  977. _this._mouseStartPointX = evt.clientX;
  978. const distanceMouseMovedY = (evt.clientY - _this._mouseStartPointY) / _this._ownerCanvas.zoom;
  979. _this._expandBottom(distanceMouseMovedY);
  980. _this._mouseStartPointY = evt.clientY;
  981. } else if (evt.clientY < yLimit && evt.clientX < xLimit) { // able to move in X but not Y
  982. const distanceMouseMovedX = (evt.clientX - _this._mouseStartPointX) / _this._ownerCanvas.zoom;
  983. _this._expandLeft(distanceMouseMovedX);
  984. _this._mouseStartPointX = evt.clientX;
  985. } else if (evt.clientY > yLimit && evt.clientX > xLimit) { // able to move in Y but not X
  986. const distanceMouseMovedY = (evt.clientY - _this._mouseStartPointY) / _this._ownerCanvas.zoom;
  987. _this._expandBottom(distanceMouseMovedY);
  988. _this._mouseStartPointY = evt.clientY;
  989. }
  990. }
  991. }
  992. private _onBottomLeftHandlePointerUp = (evt: PointerEvent) => {
  993. evt.stopPropagation();
  994. // tslint:disable-next-line: no-this-assignment
  995. const _this = this;
  996. if (_this._resizingDirection === ResizingDirection.BottomLeft) {
  997. _this.height = parseFloat(_this.element.style.height.replace("px", ""));
  998. _this.x = parseFloat(_this.element.style.left!.replace("px", ""));
  999. _this.width = parseFloat(_this.element.style.width.replace("px", ""));
  1000. _this._ownerCanvas.hostCanvas.removeEventListener("pointerup", _this._onBottomLeftHandlePointerUp);
  1001. _this._ownerCanvas.hostCanvas.removeEventListener("pointermove", _this._onBottomLeftHandlePointerMove);
  1002. _this.cleanUpResizing(evt);
  1003. }
  1004. }
  1005. private _onTopLeftHandlePointerDown = (evt: PointerEvent) => {
  1006. // tslint:disable-next-line: no-this-assignment
  1007. const _this = this;
  1008. if (_this.isCollapsed) {
  1009. return;
  1010. }
  1011. _this.initResizing(evt);
  1012. _this._resizingDirection = ResizingDirection.TopLeft;
  1013. _this.mouseXLimit = evt.clientX + _this.width - _this._minFrameWidth;
  1014. _this._ownerCanvas.hostCanvas.addEventListener("pointerup", _this._onTopLeftHandlePointerUp);
  1015. _this._ownerCanvas.hostCanvas.addEventListener("pointermove", _this._onTopLeftHandlePointerMove);
  1016. }
  1017. private _onTopLeftHandlePointerMove = (evt: PointerEvent) => {
  1018. const topSlack = (this.element.offsetHeight - this._minFrameHeight) * this._ownerCanvas.zoom;
  1019. const yLimit = (this._mouseStartPointY as number) + topSlack;
  1020. const leftSlack = (this.element.offsetWidth - this._minFrameWidth) * this._ownerCanvas.zoom;
  1021. const xLimit = (this._mouseStartPointX as number) + leftSlack;
  1022. this._moveTopLeftHandle(evt, xLimit, yLimit);
  1023. }
  1024. private _moveTopLeftHandle = (evt: PointerEvent, xLimit: number, yLimit: number) => {
  1025. // tslint:disable-next-line: no-this-assignment
  1026. const _this = this;
  1027. if (!(_this._isResizingTop() && _this._isResizingLeft()) || _this._mouseStartPointX === null || _this._mouseStartPointY === null) {
  1028. return;
  1029. }
  1030. if (_this._isResizingLeft() && _this._isResizingTop()) {
  1031. evt.stopPropagation();
  1032. if (evt.clientY < yLimit && evt.clientX < xLimit) { // able to move in X and Y
  1033. const distanceMouseMovedX = (evt.clientX - _this._mouseStartPointX) / _this._ownerCanvas.zoom;
  1034. _this._expandLeft(distanceMouseMovedX);
  1035. _this._mouseStartPointX = evt.clientX;
  1036. const distanceMouseMovedY = (evt.clientY - _this._mouseStartPointY) / _this._ownerCanvas.zoom;
  1037. _this._expandTop(distanceMouseMovedY);
  1038. _this._mouseStartPointY = evt.clientY;
  1039. } else if (evt.clientY > yLimit && evt.clientX < xLimit) { // able to move in X but not Y
  1040. const distanceMouseMovedX = (evt.clientX - _this._mouseStartPointX) / _this._ownerCanvas.zoom;
  1041. _this._expandLeft(distanceMouseMovedX);
  1042. _this._mouseStartPointX = evt.clientX;
  1043. } else if (evt.clientY < yLimit && evt.clientX > xLimit) { // able to move in Y but not X
  1044. const distanceMouseMovedY = (evt.clientY - _this._mouseStartPointY) / _this._ownerCanvas.zoom;
  1045. _this._expandTop(distanceMouseMovedY);
  1046. _this._mouseStartPointY = evt.clientY;
  1047. }
  1048. }
  1049. }
  1050. private _onTopLeftHandlePointerUp = (evt: PointerEvent) => {
  1051. evt.stopPropagation();
  1052. // tslint:disable-next-line: no-this-assignment
  1053. const _this = this;
  1054. if (_this._resizingDirection === ResizingDirection.TopLeft) {
  1055. _this.y = parseFloat(_this.element.style.top!.replace("px", ""));
  1056. _this.height = parseFloat(_this.element.style.height.replace("px", ""));
  1057. _this.x = parseFloat(_this.element.style.left!.replace("px", ""));
  1058. _this.width = parseFloat(_this.element.style.width.replace("px", ""));
  1059. _this._ownerCanvas.hostCanvas.removeEventListener("pointerup", _this._onTopLeftHandlePointerUp);
  1060. _this._ownerCanvas.hostCanvas.removeEventListener("pointermove", _this._onTopLeftHandlePointerMove);
  1061. _this.cleanUpResizing(evt);
  1062. }
  1063. }
  1064. private _expandLeft(widthModification: number) {
  1065. const frameElementWidth = parseFloat(this.element.style.width.replace("px", ""));
  1066. const frameElementLeft = parseFloat(this.element.style.left.replace("px", ""));
  1067. this.element.style.width = `${frameElementWidth - widthModification}px`;
  1068. this.element.style.left = `${frameElementLeft + widthModification}px`;
  1069. this.updateMinHeightWithComments();
  1070. }
  1071. private _expandTop(heightModification: number) {
  1072. const frameElementHeight = parseFloat(this.element.style.height.replace("px", ""));
  1073. const frameElementTop = parseFloat(this.element.style.top.replace("px", ""));
  1074. this.element.style.height = `${frameElementHeight - heightModification}px`;
  1075. this.element.style.top = `${frameElementTop + heightModification}px`;
  1076. }
  1077. private _expandRight(widthModification: number, x: number) {
  1078. const frameElementWidth = parseFloat(this.element.style.width.replace("px", ""));
  1079. if ((frameElementWidth + widthModification) > 20) {
  1080. this._mouseStartPointX = x;
  1081. this.element.style.width = `${frameElementWidth + widthModification}px`;
  1082. }
  1083. this.updateMinHeightWithComments();
  1084. }
  1085. private _expandBottom(heightModification: number) {
  1086. const frameElementHeight = parseFloat(this.element.style.height.replace("px", ""));
  1087. this.element.style.height = `${frameElementHeight + heightModification}px`;
  1088. }
  1089. public dispose() {
  1090. this.isCollapsed = false;
  1091. if (this._onSelectionChangedObserver) {
  1092. this._ownerCanvas.globalState.onSelectionChangedObservable.remove(this._onSelectionChangedObserver);
  1093. };
  1094. if(this._onGraphNodeRemovalObserver) {
  1095. this._ownerCanvas.globalState.onGraphNodeRemovalObservable.remove(this._onGraphNodeRemovalObserver);
  1096. };
  1097. if(this._onExposePortOnFrameObserver) {
  1098. this._ownerCanvas.globalState.onExposePortOnFrameObservable.remove(this._onExposePortOnFrameObserver);
  1099. };
  1100. this.element.parentElement?.removeChild(this.element);
  1101. this._ownerCanvas.frames.splice(this._ownerCanvas.frames.indexOf(this), 1);
  1102. this.onExpandStateChanged.clear();
  1103. }
  1104. public serialize(): IFrameData {
  1105. return {
  1106. x: this._x,
  1107. y: this._y,
  1108. width: this._width,
  1109. height: this._height,
  1110. color: this._color.asArray(),
  1111. name: this.name,
  1112. isCollapsed: this.isCollapsed,
  1113. blocks: this.nodes.map(n => n.block.uniqueId),
  1114. comments: this._comments
  1115. }
  1116. }
  1117. public export() {
  1118. const state = this._ownerCanvas.globalState;
  1119. const json = SerializationTools.Serialize(state.nodeMaterial, state, this);
  1120. StringTools.DownloadAsFile(state.hostDocument, json, this._name + ".json");
  1121. }
  1122. public static Parse(serializationData: IFrameData, canvas: GraphCanvasComponent, map?: {[key: number]: number}) {
  1123. let newFrame = new GraphFrame(null, canvas, true);
  1124. const isCollapsed = !!serializationData.isCollapsed;
  1125. newFrame.x = serializationData.x;
  1126. newFrame.y = serializationData.y;
  1127. newFrame.width = serializationData.width;
  1128. newFrame.height = serializationData.height;
  1129. newFrame.name = serializationData.name;
  1130. newFrame.color = Color3.FromArray(serializationData.color);
  1131. newFrame.comments = serializationData.comments;
  1132. if (serializationData.blocks && map) {
  1133. for (var blockId of serializationData.blocks) {
  1134. let actualId = map[blockId];
  1135. let node = canvas.nodes.filter(n => n.block.uniqueId === actualId);
  1136. if (node.length) {
  1137. newFrame.nodes.push(node[0]);
  1138. node[0].enclosingFrameId = newFrame.id;
  1139. }
  1140. }
  1141. } else {
  1142. newFrame.refresh();
  1143. }
  1144. newFrame.isCollapsed = isCollapsed;
  1145. if (isCollapsed) {
  1146. canvas._frameIsMoving = true;
  1147. newFrame._moveFrame(-(newFrame.width - newFrame.CollapsedWidth) / 2, 0);
  1148. let diff = serializationData.x - newFrame.x;
  1149. newFrame._moveFrame(diff, 0);
  1150. newFrame.cleanAccumulation();
  1151. for (var selectedNode of newFrame.nodes) {
  1152. selectedNode.refresh();
  1153. }
  1154. canvas._frameIsMoving = false;
  1155. }
  1156. return newFrame;
  1157. }
  1158. }