RealtimeMapping.js 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068
  1. /**
  2. * Copyright (c) 2006-2016, JGraph Ltd
  3. * Copyright (c) 2006-2016, Gaudenz Alder
  4. */
  5. /**
  6. * Creates an object that maps all changes from the given diagramMap to the
  7. * given graph model.
  8. */
  9. function RealtimeMapping(driveRealtime, diagramMap, page)
  10. {
  11. this.driveRealtime = driveRealtime;
  12. this.diagramMap = diagramMap;
  13. this.page = page;
  14. this.graphModel = new mxGraphModel();
  15. if (page.root != null)
  16. {
  17. this.graphModel.setRoot(page.root);
  18. }
  19. this.ui = this.driveRealtime.ui;
  20. this.root = this.driveRealtime.root;
  21. this.graph = this.driveRealtime.graph;
  22. this.rtModel = this.driveRealtime.rtModel;
  23. };
  24. /**
  25. * Specifies the key of the root element in the model. Default is root.
  26. */
  27. RealtimeMapping.prototype.driveRealtime = null;
  28. /**
  29. * Specifies the key of the root element in the model. Default is root.
  30. */
  31. RealtimeMapping.prototype.diagramMap = null;
  32. /**
  33. * Specifies the key of the root element in the model. Default is root.
  34. */
  35. RealtimeMapping.prototype.page = null;
  36. /**
  37. * Specifies the key of the root element in the model. Default is root.
  38. */
  39. RealtimeMapping.prototype.graphModel = null;
  40. /**
  41. * Specifies the key of the root element in the model. Default is root.
  42. */
  43. RealtimeMapping.prototype.needsUpdate = true;
  44. /**
  45. * Specifies the key of the root element in the model. Default is root.
  46. */
  47. RealtimeMapping.prototype.selectionMap = null;
  48. /**
  49. * Synchronizes the collaboration model and the graph model and installs
  50. * the required listeners to keep them in sync.
  51. */
  52. RealtimeMapping.prototype.init = function()
  53. {
  54. this.diagramMap.addEventListener(gapi.drive.realtime.EventType.VALUE_CHANGED, mxUtils.bind(this, function(evt)
  55. {
  56. if (!this.driveRealtime.isLocalEvent(evt))
  57. {
  58. if (evt.property == this.driveRealtime.rootKey && evt.newValue != null)
  59. {
  60. this.beginUpdate();
  61. this.initGraph();
  62. this.needsUpdate = true;
  63. }
  64. else if (evt.property == 'name' && evt.newValue != null)
  65. {
  66. this.driveRealtime.ignoreChange = true;
  67. this.graph.model.execute(new RenamePage(this.ui, this.page, evt.newValue));
  68. this.driveRealtime.ignoreChange = false;
  69. }
  70. else if (evt.newValue != null)
  71. {
  72. if (evt.property == 'pageFormat')
  73. {
  74. this.realtimePageFormatChanged(evt.newValue);
  75. }
  76. else if (evt.property == 'pageScale')
  77. {
  78. this.realtimePageScaleChanged(evt.newValue);
  79. }
  80. else if (evt.property == 'backgroundColor')
  81. {
  82. this.realtimeBackgroundColorChanged(evt.newValue);
  83. }
  84. else if (evt.property == 'shadowVisible')
  85. {
  86. this.realtimeShadowVisibleChanged(evt.newValue);
  87. }
  88. else if (evt.property == 'foldingEnabled')
  89. {
  90. this.realtimeFoldingEnabledChanged(evt.newValue);
  91. }
  92. else if (evt.property == 'backgroundImage')
  93. {
  94. this.realtimeBackgroundImageChanged(evt.newValue);
  95. }
  96. else if (evt.property == 'mathEnabled')
  97. {
  98. this.realtimeMathEnabledChanged(evt.newValue);
  99. }
  100. }
  101. // Marks the mapping dirty regardless of active state
  102. if (evt.newValue != null && (evt.property == 'pageFormat' ||
  103. evt.property == 'pageScale' || evt.property == 'shadowVisible' ||
  104. evt.property == 'backgroundColor' || evt.property == 'foldingEnabled' ||
  105. evt.property == 'backgroundImage' || evt.property == 'mathEnabled'))
  106. {
  107. this.needsUpdate = true;
  108. }
  109. }
  110. }));
  111. if (this.diagramMap.has(this.driveRealtime.rootKey))
  112. {
  113. this.initGraph();
  114. }
  115. else
  116. {
  117. this.initRealtime();
  118. }
  119. this.page.root = this.graphModel.getRoot();
  120. this.selectionMap = this.diagramMap.get('select');
  121. if (this.selectionMap == null)
  122. {
  123. this.initializeSelection();
  124. }
  125. // Resets selection state to ensure change event
  126. if (this.driveRealtime.file.isEditable())
  127. {
  128. this.selectionMap.set(this.driveRealtime.userId, '');
  129. }
  130. this.installRemoteSelectionListener();
  131. };
  132. /**
  133. *
  134. */
  135. RealtimeMapping.prototype.initializeSelection = function()
  136. {
  137. this.selectionMap = this.rtModel.createMap();
  138. if (this.driveRealtime.file.isEditable())
  139. {
  140. this.diagramMap.set('select', this.selectionMap);
  141. }
  142. //this.log('Selection list created');
  143. };
  144. /**
  145. * Adds a listener for changes to the RT selection map to highlight
  146. * remote selection
  147. */
  148. RealtimeMapping.prototype.installRemoteSelectionListener = function()
  149. {
  150. this.selectionMap.addEventListener(gapi.drive.realtime.EventType.VALUE_CHANGED, mxUtils.bind(this, function(evt)
  151. {
  152. if (!this.driveRealtime.isLocalEvent(evt) && evt.newValue != null && (this.ui.currentPage == null ||
  153. this.ui.currentPage == this.page))
  154. {
  155. var cellIds = evt.newValue.split(',');
  156. for (var i = 0; i < cellIds.length; i++)
  157. {
  158. this.driveRealtime.highlight(this.driveRealtime.model.getCell(cellIds[i]), evt.sessionId);
  159. }
  160. }
  161. }));
  162. };
  163. /**
  164. * Returns true if this diagram is being displayed.
  165. */
  166. RealtimeMapping.prototype.isActive = function()
  167. {
  168. return this.ui.currentPage == null || this.ui.currentPage.mapping == this;
  169. };
  170. /**
  171. * Syncs initial state from collab model to graph model.
  172. */
  173. RealtimeMapping.prototype.getGraphModel = function()
  174. {
  175. return (this.isActive()) ? this.driveRealtime.model : this.graphModel;
  176. };
  177. /**
  178. * Syncs initial state from collab model to graph model.
  179. */
  180. RealtimeMapping.prototype.initGraph = function()
  181. {
  182. if (this.isActive())
  183. {
  184. this.activate(true);
  185. mxClient.NO_FO = (this.graph.mathEnabled) ? true : Editor.prototype.originalNoForeignObject;
  186. // TODO: Fixes math offset - why?
  187. this.ui.editor.graph.sizeDidChange();
  188. }
  189. var rtRoot = this.diagramMap.get(this.driveRealtime.rootKey);
  190. if (rtRoot.cell == null)
  191. {
  192. this.createCell(rtRoot);
  193. this.restoreCell(rtRoot);
  194. }
  195. else
  196. {
  197. this.installAllRealtimeCellListeners(rtRoot);
  198. }
  199. // Stores root in current model and local model
  200. var gm = this.getGraphModel();
  201. gm.setRoot(rtRoot.cell);
  202. if (gm != this.graphModel)
  203. {
  204. this.graphModel.setRoot(gm.getRoot());
  205. }
  206. };
  207. /**
  208. * Writes the graph properties from the realtime model to the given mxGraphModel node.
  209. */
  210. RealtimeMapping.prototype.writeRealtimeToNode = function(node)
  211. {
  212. node.setAttribute('shadow', this.diagramMap.get('shadowVisible'));
  213. node.setAttribute('fold', this.diagramMap.get('foldingEnabled'));
  214. node.setAttribute('math', this.diagramMap.get('mathEnabled'));
  215. node.setAttribute('pageScale', this.diagramMap.get('pageScale'));
  216. var img = this.diagramMap.get('backgroundImage');
  217. if (img != null && img.length > 0)
  218. {
  219. node.setAttribute('backgroundImage', img);
  220. }
  221. var color = this.diagramMap.get('backgroundColor');
  222. if (color != null)
  223. {
  224. node.setAttribute('background', color);
  225. }
  226. var pf = this.diagramMap.get('pageFormat');
  227. if (pf != null)
  228. {
  229. var values = pf.split(',');
  230. if (values.length > 1)
  231. {
  232. node.setAttribute('pageWidth', parseInt(values[0]));
  233. node.setAttribute('pageHeight', parseInt(values[1]));
  234. }
  235. }
  236. };
  237. /**
  238. * Writes the graph properties from the realtime model to the given mxGraphModel node.
  239. */
  240. RealtimeMapping.prototype.writeNodeToRealtime = function(node)
  241. {
  242. this.diagramMap.set('shadowVisible', node.getAttribute('shadow'));
  243. this.diagramMap.set('foldingEnabled', node.getAttribute('fold'));
  244. this.diagramMap.set('mathEnabled', node.getAttribute('math'));
  245. this.diagramMap.set('pageScale', node.getAttribute('pageScale'));
  246. var img = node.getAttribute('backgroundImage');
  247. if (img != null && img.length > 0)
  248. {
  249. this.diagramMap.set('backgroundImage', img);
  250. }
  251. var color = node.getAttribute('background');
  252. if (color != null)
  253. {
  254. this.diagramMap.set('backgroundColor', color);
  255. }
  256. this.diagramMap.set('pageFormat', node.getAttribute('pageWidth') + ',' +
  257. node.getAttribute('pageHeight'));
  258. };
  259. /**
  260. * Syncs initial state from collab model to graph model.
  261. */
  262. RealtimeMapping.prototype.activate = function(quiet)
  263. {
  264. this.realtimePageFormatChanged(this.diagramMap.get('pageFormat'), quiet);
  265. this.realtimePageScaleChanged(this.diagramMap.get('pageScale'), quiet);
  266. this.realtimeMathEnabledChanged(this.diagramMap.get('mathEnabled'), quiet);
  267. this.realtimeBackgroundColorChanged(this.diagramMap.get('backgroundColor'), quiet);
  268. this.realtimeShadowVisibleChanged(this.diagramMap.get('shadowVisible'), quiet);
  269. this.realtimeFoldingEnabledChanged(this.diagramMap.get('foldingEnabled'), quiet);
  270. this.realtimeBackgroundImageChanged(this.diagramMap.get('backgroundImage'), quiet);
  271. };
  272. /**
  273. * Syncs initial state from graph model to collab model.
  274. */
  275. RealtimeMapping.prototype.initRealtime = function()
  276. {
  277. this.rtModel.beginCompoundOperation();
  278. try
  279. {
  280. var rtCell = this.createRealtimeCell(this.getGraphModel().getRoot());
  281. this.saveRealtimeCell(rtCell.cell);
  282. this.diagramMap.set(this.driveRealtime.rootKey, rtCell);
  283. if (this.page.graphModelNode != null)
  284. {
  285. this.writeNodeToRealtime(this.page.graphModelNode);
  286. }
  287. else
  288. {
  289. this.diagramMap.set('shadowVisible', (this.graph.shadowVisible) ? '1' : '0');
  290. this.diagramMap.set('foldingEnabled', (this.graph.foldingEnabled) ? '1' : '0');
  291. this.diagramMap.set('mathEnabled', (this.graph.mathEnabled) ? '1' : '0');
  292. this.diagramMap.set('pageScale', this.graph.pageScale);
  293. this.diagramMap.set('backgroundImage', (this.graph.backgroundImage != null) ? JSON.stringify(this.graph.backgroundImage) : '');
  294. this.diagramMap.set('backgroundColor', (this.graph.background != null) ? this.graph.background : '');
  295. this.diagramMap.set('pageFormat', this.graph.pageFormat.width + ',' + this.graph.pageFormat.height);
  296. }
  297. this.root.set('modifiedDate', new Date().getTime());
  298. this.rtModel.endCompoundOperation();
  299. }
  300. catch (e)
  301. {
  302. this.rtModel.endCompoundOperation();
  303. this.ui.handleError(e);
  304. }
  305. };
  306. /**
  307. * Syncs initial state from graph model to collab model.
  308. */
  309. RealtimeMapping.prototype.createRealtimeCell = function(cell)
  310. {
  311. var rtCell = cell.rtCell;
  312. if (rtCell == null)
  313. {
  314. rtCell = this.rtModel.create('Cell');
  315. rtCell.children = this.rtModel.createList();
  316. rtCell.cell = cell;
  317. cell.rtCell = rtCell;
  318. rtCell.cellId = cell.id;
  319. rtCell.type = (cell.vertex) ? 'vertex' : ((cell.edge) ? 'edge' : '');
  320. rtCell.connectable = (cell.connectable == null || cell.connectable) ? '1' : '0';
  321. if (mxUtils.isNode(cell.value))
  322. {
  323. rtCell.xmlValue = mxUtils.getXml(cell.value);
  324. }
  325. else if (cell.value != null)
  326. {
  327. rtCell.value = cell.value;
  328. }
  329. rtCell.style = (cell.style != null) ? cell.style : null;
  330. rtCell.geometry = (cell.geometry != null) ? mxUtils.getXml(this.driveRealtime.codec.encode(cell.geometry)) : null;
  331. rtCell.visible = (cell.visible == null || cell.visible) ? '1' : '0';
  332. rtCell.collapsed = (cell.collapsed != null && cell.collapsed) ? '1' : '0';
  333. for (var i = 0; i < this.graphModel.getChildCount(cell); i++)
  334. {
  335. var child = this.graphModel.getChildAt(cell, i);
  336. this.createRealtimeCell(child);
  337. if (child.rtCell.parent == null)
  338. {
  339. child.rtCell.parent = rtCell;
  340. rtCell.children.push(child.rtCell);
  341. }
  342. }
  343. this.installRealtimeCellListeners(rtCell);
  344. }
  345. return rtCell;
  346. };
  347. /**
  348. * Syncs initial state from graph model to collab model.
  349. */
  350. RealtimeMapping.prototype.saveRealtimeCell = function(cell)
  351. {
  352. if (cell.source != null)
  353. {
  354. if (cell.source.rtCell == null)
  355. {
  356. this.createRealtimeCell(cell.source);
  357. }
  358. cell.rtCell.source = cell.source.rtCell;
  359. }
  360. else
  361. {
  362. cell.rtCell.source = null;
  363. }
  364. if (cell.target != null)
  365. {
  366. if (cell.target.rtCell == null)
  367. {
  368. this.createRealtimeCell(cell.target);
  369. }
  370. cell.rtCell.target = cell.target.rtCell;
  371. }
  372. else
  373. {
  374. cell.rtCell.target = null;
  375. }
  376. for (var i = 0; i < this.graphModel.getChildCount(cell); i++)
  377. {
  378. this.saveRealtimeCell(this.graphModel.getChildAt(cell, i));
  379. }
  380. };
  381. /**
  382. * Syncs initial state from collab model to graph model.
  383. */
  384. RealtimeMapping.prototype.createCell = function(rtCell)
  385. {
  386. var cell = rtCell.cell;
  387. if (cell == null)
  388. {
  389. cell = new mxCell();
  390. rtCell.cell = cell;
  391. cell.rtCell = rtCell;
  392. cell.id = rtCell.cellId;
  393. cell.vertex = rtCell.type == 'vertex';
  394. cell.edge = rtCell.type == 'edge';
  395. cell.connectable = rtCell.connectable != '0';
  396. cell.value = (rtCell.xmlValue != null) ? mxUtils.parseXml(rtCell.xmlValue).documentElement : rtCell.value;
  397. cell.style = rtCell.style;
  398. cell.geometry = (rtCell.geometry != null) ? this.driveRealtime.codec.decode(mxUtils.parseXml(rtCell.geometry).documentElement) : null;
  399. cell.visible = rtCell.visible != '0';
  400. cell.collapsed = rtCell.collapsed == '1';
  401. for (var i = 0; i < rtCell.children.length; i++)
  402. {
  403. var rtChild = rtCell.children.get(i);
  404. this.createCell(rtChild);
  405. if (rtChild.cell.parent == null)
  406. {
  407. cell.insert(rtChild.cell);
  408. }
  409. }
  410. this.installRealtimeCellListeners(rtCell);
  411. }
  412. return cell;
  413. };
  414. /**
  415. * Restores connection between edges and terminals.
  416. */
  417. RealtimeMapping.prototype.restoreCell = function(rtCell)
  418. {
  419. var valid = true;
  420. if (rtCell.cell != null)
  421. {
  422. //console.log('restoreCell', rtCell.cellId);
  423. if (rtCell.source != null)
  424. {
  425. // Removes edge if source is no longer in the model
  426. if (rtCell.source.parent == null)
  427. {
  428. //console.log('invalid source', valid, rtCell.cellId, rtCell.source.cellId);
  429. rtCell.source = null;
  430. valid = false;
  431. }
  432. else
  433. {
  434. if (rtCell.source.cell == null)
  435. {
  436. this.createCell(rtCell.source);
  437. }
  438. rtCell.source.cell.insertEdge(rtCell.cell, true);
  439. }
  440. }
  441. if (valid && rtCell.target != null)
  442. {
  443. // Removes edge if source is no longer in the model
  444. if (rtCell.target.parent == null)
  445. {
  446. //console.log('invalid target', valid, rtCell.cellId, rtCell.target.cellId);
  447. rtCell.target = null;
  448. valid = false;
  449. }
  450. else
  451. {
  452. if (rtCell.target.cell == null)
  453. {
  454. this.createCell(rtCell.target);
  455. }
  456. rtCell.target.cell.insertEdge(rtCell.cell, false);
  457. }
  458. }
  459. // Checks if edge contains required terminals or terminal points
  460. if (valid && this.graphModel.isEdge(rtCell.cell))
  461. {
  462. var geo = this.graphModel.getGeometry(rtCell.cell);
  463. valid = geo != null &&
  464. (this.graphModel.getTerminal(rtCell.cell, true) != null || geo.getTerminalPoint(true) != null) &&
  465. (this.graphModel.getTerminal(rtCell.cell, false) != null || geo.getTerminalPoint(false) != null);
  466. //console.log('geometry check', valid, rtCell.cellId);
  467. }
  468. }
  469. // Removes invalid cell
  470. if (!valid)
  471. {
  472. if (rtCell.parent != null)
  473. {
  474. rtCell.parent.children.removeValue(rtCell);
  475. rtCell.parent = null;
  476. }
  477. if (rtCell.cell != null)
  478. {
  479. // TODO: Remove from source and target?
  480. //console.log('remove invalid cell', rtCell.cellId);
  481. this.getGraphModel().remove(rtCell.cell);
  482. }
  483. }
  484. else
  485. {
  486. for (var i = 0; i < rtCell.children.length; i++)
  487. {
  488. this.restoreCell(rtCell.children.get(i));
  489. }
  490. }
  491. };
  492. /**
  493. * Restores connection between edges and terminals.
  494. */
  495. RealtimeMapping.prototype.containsRealtimeCell = function(rtCell)
  496. {
  497. var tmp = rtCell;
  498. while (tmp.parent != null)
  499. {
  500. tmp = tmp.parent;
  501. }
  502. return tmp == this.diagramMap.get(this.driveRealtime.rootKey);
  503. };
  504. /**
  505. *
  506. */
  507. RealtimeMapping.prototype.beginUpdate = function()
  508. {
  509. var graphModel = this.getGraphModel();
  510. if (!this.driveRealtime.ignoreChange)
  511. {
  512. this.driveRealtime.ignoreChange = true;
  513. graphModel.beginUpdate();
  514. window.setTimeout(mxUtils.bind(this, function()
  515. {
  516. graphModel.endUpdate();
  517. this.driveRealtime.ignoreChange = false;
  518. }), 0);
  519. }
  520. return graphModel;
  521. };
  522. /**
  523. * Syncs initial state from collab model to graph model.
  524. */
  525. RealtimeMapping.prototype.installAllRealtimeCellListeners = function(rtCell)
  526. {
  527. if (rtCell != null)
  528. {
  529. this.installRealtimeCellListeners(rtCell);
  530. for (var i = 0; i < rtCell.children.length; i++)
  531. {
  532. this.installAllRealtimeCellListeners(rtCell.children.get(i));
  533. }
  534. }
  535. };
  536. /**
  537. * Adds the listener for added and removed cells in the collab model and maps
  538. * them to the graph model.
  539. */
  540. RealtimeMapping.prototype.installRealtimeCellListeners = function(rtCell)
  541. {
  542. rtCell.addEventListener(gapi.drive.realtime.EventType.VALUE_CHANGED, mxUtils.bind(this, function(evt)
  543. {
  544. this.handleValueChanged(rtCell, evt);
  545. this.needsUpdate = true;
  546. }));
  547. rtCell.children.addEventListener(gapi.drive.realtime.EventType.VALUES_ADDED, mxUtils.bind(this, function(evt)
  548. {
  549. this.handleValuesAdded(rtCell, evt);
  550. this.needsUpdate = true;
  551. }));
  552. rtCell.children.addEventListener(gapi.drive.realtime.EventType.VALUES_REMOVED, mxUtils.bind(this, function(evt)
  553. {
  554. this.handleValuesRemoved(rtCell, evt);
  555. this.needsUpdate = true;
  556. }));
  557. };
  558. /**
  559. * Adds the listener for added and removed cells in the collab model and maps
  560. * them to the graph model.
  561. */
  562. RealtimeMapping.prototype.handleValueChanged = function(rtCell, evt)
  563. {
  564. var cell = rtCell.cell;
  565. if (!this.driveRealtime.isLocalEvent(evt) && cell != null)
  566. {
  567. var value = evt.newValue;
  568. var key = evt.property;
  569. var graphModel = this.beginUpdate();
  570. //console.log('valueChanged: cell=' + rtCell.cellId + ' key=' + key + ' value=' + ((value != null) ? (value.cellId || value) : '[null]'));
  571. if (key == 'type')
  572. {
  573. cell.vertex = value == 'vertex';
  574. cell.edge = value == 'edge';
  575. }
  576. else if (key == 'connectable')
  577. {
  578. cell.connectable = (value == '1');
  579. }
  580. else if (key == 'source' || key == 'target')
  581. {
  582. if (value == null)
  583. {
  584. if (evt.oldValue != null)
  585. {
  586. graphModel.setTerminal(cell, null, key == 'source');
  587. }
  588. }
  589. else
  590. {
  591. // Handles the case where an edge is connected to a vertex which is not in the model
  592. if (value.cell == null || !this.containsRealtimeCell(value) || graphModel.getCell(value.cellId) == null)
  593. {
  594. if (rtCell.parent != null)
  595. {
  596. rtCell.parent.children.removeValue(rtCell);
  597. rtCell.parent = null;
  598. }
  599. graphModel.setTerminal(cell, null, key == 'source');
  600. graphModel.remove(rtCell.cell);
  601. rtCell[key] = null;
  602. }
  603. else
  604. {
  605. graphModel.setTerminal(cell, value.cell, key == 'source');
  606. }
  607. }
  608. }
  609. else if (key == 'value')
  610. {
  611. graphModel.setValue(cell, value);
  612. }
  613. else if (key == 'xmlValue')
  614. {
  615. graphModel.setValue(cell, mxUtils.parseXml(value).documentElement);
  616. }
  617. else if (key == 'style')
  618. {
  619. graphModel.setStyle(cell, value);
  620. }
  621. else if (key == 'geometry')
  622. {
  623. var geometry = (value != null) ? this.driveRealtime.codec.decode(mxUtils.parseXml(value).documentElement) : null;
  624. graphModel.setGeometry(cell, geometry);
  625. }
  626. else if (key == 'collapsed')
  627. {
  628. graphModel.setCollapsed(cell, value == '1');
  629. }
  630. else if (key == 'visible')
  631. {
  632. graphModel.setVisible(cell, value == '1');
  633. }
  634. else if (key == 'parent')
  635. {
  636. // Removes the child from its previous parent in the realtime model.
  637. if (evt.oldValue != null)
  638. {
  639. //console.log('remove clone', 'parent', evt.oldValue.cellId, 'child', rtCell.cellId);
  640. evt.oldValue.children.removeValue(rtCell);
  641. }
  642. else
  643. {
  644. this.createCell(rtCell);
  645. this.restoreCell(rtCell);
  646. }
  647. if (value == null)
  648. {
  649. graphModel.remove(cell);
  650. }
  651. else
  652. {
  653. var index = value.children.indexOf(rtCell);
  654. if (index >= 0)
  655. {
  656. //console.log('move child', 'parent', value.cellId, 'child', rtCell.cellId, index);
  657. graphModel.add(value.cell, rtCell.cell, index);
  658. }
  659. }
  660. }
  661. }
  662. };
  663. /**
  664. * Adds the listener for added and removed cells in the collab model and maps
  665. * them to the graph model.
  666. */
  667. RealtimeMapping.prototype.handleValuesAdded = function(rtCell, evt)
  668. {
  669. if (!this.driveRealtime.isLocalEvent(evt))
  670. {
  671. var graphModel = this.beginUpdate();
  672. for (var i = 0; i < evt.values.length; i++)
  673. {
  674. var rtChild = evt.values[i];
  675. //console.log('valueAdded', 'parent', rtCell.cellId, 'child', rtChild.cellId, 'index', evt.index + i, rtChild);
  676. // Removes child if the parent of the child and the parent of the children array are not
  677. // the same. This happens if clients move a cell into different parents concurrently.
  678. if (rtChild.parent != null)
  679. {
  680. if (rtChild.parent != rtCell)
  681. {
  682. //console.log('remove clone', 'parent', rtCell.cellId, 'child', rtChild.cellId);
  683. rtCell.children.removeValue(rtChild);
  684. }
  685. else
  686. {
  687. if (rtChild.cell == null || rtChild.cell.parent == null)
  688. {
  689. this.createCell(rtChild);
  690. this.restoreCell(rtChild);
  691. }
  692. // Resolves conflict when two clients change the order of a child at the same
  693. // time which results in the same child appearing multiple times in the list.
  694. // Note that the actual child index may be different from the event information
  695. // at this point so a generic check for duplicates is performed based on the
  696. // first appearance of the cell in the list.
  697. var first = rtCell.children.indexOf(rtChild);
  698. var last = rtCell.children.lastIndexOf(rtChild);
  699. while (first != last)
  700. {
  701. //console.log('remove duplicate', rtChild.cellId, last);
  702. rtCell.children.remove(last);
  703. last = rtCell.children.lastIndexOf(rtChild);
  704. }
  705. // Inserts the child at the smallest index to maintain consistency with RT
  706. if (rtChild.parent == rtCell)
  707. {
  708. //console.log('add', 'parent', rtCell.cellId, 'child', rtChild.cellId, 'index', Math.min(first, evt.index + i));
  709. graphModel.add(rtCell.cell, rtChild.cell, Math.min(first, evt.index + i));
  710. }
  711. }
  712. }
  713. }
  714. }
  715. };
  716. /**
  717. * Adds the listener for added and removed cells in the collab model and maps
  718. * them to the graph model.
  719. */
  720. RealtimeMapping.prototype.handleValuesRemoved = function(rtCell, evt)
  721. {
  722. if (!this.driveRealtime.isLocalEvent(evt))
  723. {
  724. var graphModel = this.beginUpdate();
  725. for (var i = 0; i < evt.values.length; i++)
  726. {
  727. var rtChild = evt.values[i];
  728. if (rtChild.cell != null)
  729. {
  730. //console.log('valueRemoved', 'parent', rtCell.cellId, 'child', rtChild.cellId,
  731. // 'index', evt.index + i, rtChild, rtChild.cell);
  732. // Checks if the realtime parent and the graph parent are different and updates the parent
  733. // in the graph to match the realtime parent. This happens if the child was removed as a
  734. // clone in another client.
  735. if (rtChild.parent != null && rtChild.parent != rtCell && rtChild.cell.parent != rtChild.parent.cell)
  736. {
  737. //console.log('move clone', rtChild.cellId, evt.index + i, rtChild.parent.cellId);
  738. var index = rtChild.parent.children.indexOf(rtChild);
  739. graphModel.add(rtChild.parent.cell, rtChild.cell, index);
  740. }
  741. else
  742. {
  743. // Checks if the realtime parent contains a duplicate entry of this child
  744. // and updates the index of the child in the graph. This happens if the
  745. // child was removed as a duplicate entry in another client.
  746. var index = rtCell.children.indexOf(rtChild);
  747. if (index >= 0)
  748. {
  749. //console.log('adjust duplicate', rtCell.cellId, evt.index + i, rtChild.cellId, 'index', index);
  750. graphModel.add(rtCell.cell, rtChild.cell, index);
  751. }
  752. }
  753. }
  754. }
  755. }
  756. };
  757. /**
  758. * Syncs initial state from collab model to graph model.
  759. */
  760. RealtimeMapping.prototype.realtimePageFormatChanged = function(value, quiet)
  761. {
  762. if (value != null)
  763. {
  764. var values = value.split(',');
  765. if (values.length > 1)
  766. {
  767. if (!this.isActive())
  768. {
  769. if (this.page.viewState != null)
  770. {
  771. this.page.viewState.pageFormat = new mxRectangle(0, 0, parseInt(values[0]), parseInt(values[1]));
  772. }
  773. }
  774. else if (quiet)
  775. {
  776. this.graph.pageFormat = new mxRectangle(0, 0, parseInt(values[0]), parseInt(values[1]));
  777. }
  778. else
  779. {
  780. this.driveRealtime.ignorePageFormatChanged = true;
  781. this.ui.setPageFormat(new mxRectangle(0, 0, parseInt(values[0]), parseInt(values[1])));
  782. this.driveRealtime.ignorePageFormatChanged = false;
  783. }
  784. }
  785. }
  786. };
  787. /**
  788. * Syncs initial state from collab model to graph model.
  789. */
  790. RealtimeMapping.prototype.realtimePageScaleChanged = function(value, quiet)
  791. {
  792. if (value != null)
  793. {
  794. if (!this.isActive())
  795. {
  796. if (this.page.viewState != null)
  797. {
  798. this.page.viewState.pageScale = parseFloat(value);
  799. }
  800. }
  801. else if (quiet)
  802. {
  803. this.graph.pageScale = parseFloat(value);
  804. }
  805. else
  806. {
  807. this.driveRealtime.ignorePageScaleChanged = true;
  808. this.ui.setPageScale(parseFloat(value));
  809. this.driveRealtime.ignorePageScaleChanged = false;
  810. }
  811. }
  812. };
  813. /**
  814. * Syncs initial state from collab model to graph model.
  815. */
  816. RealtimeMapping.prototype.realtimeBackgroundColorChanged = function(value, quiet)
  817. {
  818. if (!this.isActive())
  819. {
  820. if (this.page.viewState != null)
  821. {
  822. this.page.viewState.background = (value == '') ? null : value;
  823. }
  824. }
  825. else if (quiet)
  826. {
  827. this.graph.background = (value == '') ? null : value;
  828. }
  829. else
  830. {
  831. this.driveRealtime.ignoreBackgroundColorChanged = true;
  832. this.ui.setBackgroundColor((value == '') ? null : value);
  833. this.driveRealtime.ignoreBackgroundColorChanged = false;
  834. }
  835. };
  836. /**
  837. * Syncs initial state from collab model to graph model.
  838. */
  839. RealtimeMapping.prototype.realtimeFoldingEnabledChanged = function(value, quiet)
  840. {
  841. if (!this.isActive())
  842. {
  843. if (this.page.viewState != null)
  844. {
  845. this.page.viewState.foldingEnabled = value == '1';
  846. }
  847. }
  848. else if (quiet)
  849. {
  850. this.graph.foldingEnabled = value == '1';
  851. }
  852. else
  853. {
  854. this.driveRealtime.ignoreFoldingEnabledChanged = true;
  855. this.ui.setFoldingEnabled(value == '1');
  856. this.driveRealtime.ignoreFoldingEnabledChanged = false;
  857. }
  858. };
  859. /**
  860. * Syncs initial state from collab model to graph model.
  861. */
  862. RealtimeMapping.prototype.realtimeShadowVisibleChanged = function(value, quiet)
  863. {
  864. // Does not need quiet mode as it's handled independently of refresh
  865. if (!this.isActive())
  866. {
  867. if (this.page.viewState != null)
  868. {
  869. this.page.viewState.shadowVisible = value == '1';
  870. }
  871. }
  872. else
  873. {
  874. this.driveRealtime.ignoreShadowVisibleChanged = true;
  875. this.ui.editor.graph.setShadowVisible(value == '1');
  876. this.driveRealtime.ignoreShadowVisibleChanged = false;
  877. }
  878. };
  879. /**
  880. * Syncs initial state from collab model to graph model.
  881. */
  882. RealtimeMapping.prototype.realtimeBackgroundImageChanged = function(value, quiet)
  883. {
  884. var data = (value != null && value.length > 0) ? JSON.parse(value) : null;
  885. if (!this.isActive())
  886. {
  887. if (this.page.viewState != null)
  888. {
  889. this.page.viewState.backgroundImage = (data != null) ? new mxImage(data.src, data.width, data.height) : null;
  890. }
  891. }
  892. else if (quiet)
  893. {
  894. this.graph.setBackgroundImage((data != null) ? new mxImage(data.src, data.width, data.height) : null);
  895. }
  896. else
  897. {
  898. this.driveRealtime.ignoreBackgroundImageChanged = true;
  899. this.ui.setBackgroundImage((data != null) ? new mxImage(data.src, data.width, data.height) : null);
  900. this.driveRealtime.ignoreBackgroundImageChanged = false;
  901. }
  902. };
  903. /**
  904. * Syncs initial state from collab model to graph model.
  905. */
  906. RealtimeMapping.prototype.realtimeMathEnabledChanged = function(value, quiet)
  907. {
  908. if (!this.isActive())
  909. {
  910. if (this.page.viewState != null)
  911. {
  912. this.page.viewState.mathEnabled = urlParams['math'] == '1' || value == '1';
  913. }
  914. }
  915. else if (quiet)
  916. {
  917. this.graph.mathEnabled = urlParams['math'] == '1' || value == '1';
  918. }
  919. else
  920. {
  921. this.driveRealtime.ignoreMathEnabledChanged = true;
  922. this.ui.setMathEnabled(urlParams['math'] == '1' || value == '1');
  923. this.driveRealtime.ignoreMathEnabledChanged = false;
  924. }
  925. };
  926. /**
  927. * Syncs initial state from collab model to graph model.
  928. */
  929. RealtimeMapping.prototype.removeAllRealtimeCellListeners = function(rtCell)
  930. {
  931. if (rtCell != null)
  932. {
  933. rtCell.removeAllEventListeners();
  934. rtCell.children.removeAllEventListeners();
  935. for (var i = 0; i < rtCell.children.length; i++)
  936. {
  937. this.removeAllRealtimeCellListeners(rtCell.children.get(i));
  938. }
  939. }
  940. };
  941. /**
  942. * Syncs initial state from collab model to graph model.
  943. */
  944. RealtimeMapping.prototype.destroy = function()
  945. {
  946. this.diagramMap.removeAllEventListeners();
  947. this.selectionMap.removeAllEventListeners();
  948. this.removeAllRealtimeCellListeners(this.diagramMap.get(this.driveRealtime.rootKey));
  949. };