RealtimeMapping.js 28 KB

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