123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107 |
- /**
- * Copyright (c) 2006-2016, JGraph Ltd
- * Copyright (c) 2006-2016, Gaudenz Alder
- */
- /**
- * Creates an object that maps all changes from the given diagramMap to the
- * given graph model.
- */
- function RealtimeMapping(driveRealtime, diagramMap, page)
- {
- this.driveRealtime = driveRealtime;
- this.diagramMap = diagramMap;
- this.page = page;
-
- this.graphModel = new mxGraphModel();
-
- if (page.root != null)
- {
- this.graphModel.setRoot(page.root);
- }
-
- this.ui = this.driveRealtime.ui;
- this.root = this.driveRealtime.root;
- this.graph = this.driveRealtime.graph;
- this.rtModel = this.driveRealtime.rtModel;
- };
- /**
- * Specifies the key of the root element in the model. Default is root.
- */
- RealtimeMapping.prototype.driveRealtime = null;
- /**
- * Specifies the key of the root element in the model. Default is root.
- */
- RealtimeMapping.prototype.diagramMap = null;
- /**
- * Specifies the key of the root element in the model. Default is root.
- */
- RealtimeMapping.prototype.page = null;
- /**
- * Specifies the key of the root element in the model. Default is root.
- */
- RealtimeMapping.prototype.graphModel = null;
- /**
- * Specifies the key of the root element in the model. Default is root.
- */
- RealtimeMapping.prototype.needsUpdate = true;
- /**
- * Specifies the key of the root element in the model. Default is root.
- */
- RealtimeMapping.prototype.selectionMap = null;
- /**
- * Synchronizes the collaboration model and the graph model and installs
- * the required listeners to keep them in sync.
- */
- RealtimeMapping.prototype.init = function()
- {
- this.diagramMap.addEventListener(gapi.drive.realtime.EventType.VALUE_CHANGED, mxUtils.bind(this, function(evt)
- {
- if (!this.driveRealtime.isLocalEvent(evt))
- {
- if (evt.property == this.driveRealtime.rootKey && evt.newValue != null)
- {
- this.beginUpdate();
- this.initGraph();
- this.needsUpdate = true;
- }
- else if (evt.property == 'name' && evt.newValue != null)
- {
- this.driveRealtime.ignoreChange = true;
- this.graph.model.execute(new RenamePage(this.ui, this.page, evt.newValue));
- this.driveRealtime.ignoreChange = false;
- }
- else if (evt.newValue != null)
- {
- if (evt.property == 'pageFormat')
- {
- this.realtimePageFormatChanged(evt.newValue);
- }
- else if (evt.property == 'pageScale')
- {
- this.realtimePageScaleChanged(evt.newValue);
- }
- else if (evt.property == 'backgroundColor')
- {
- this.realtimeBackgroundColorChanged(evt.newValue);
- }
- else if (evt.property == 'shadowVisible')
- {
- this.realtimeShadowVisibleChanged(evt.newValue);
- }
- else if (evt.property == 'foldingEnabled')
- {
- this.realtimeFoldingEnabledChanged(evt.newValue);
- }
- else if (evt.property == 'pageVisible')
- {
- this.realtimePageVisibleChanged(evt.newValue);
- }
- else if (evt.property == 'backgroundImage')
- {
- this.realtimeBackgroundImageChanged(evt.newValue);
- }
- else if (evt.property == 'mathEnabled')
- {
- this.realtimeMathEnabledChanged(evt.newValue);
- }
- }
-
- // Marks the mapping dirty regardless of active state
- if (evt.newValue != null && (evt.property == 'pageFormat' ||
- evt.property == 'pageScale' || evt.property == 'shadowVisible' ||
- evt.property == 'backgroundColor' || evt.property == 'foldingEnabled' ||
- evt.property == 'backgroundImage' || evt.property == 'mathEnabled' ||
- evt.property == 'pageVisible'))
- {
- this.needsUpdate = true;
- }
- }
- }));
- if (this.diagramMap.has(this.driveRealtime.rootKey))
- {
- this.initGraph();
- }
- else
- {
- this.initRealtime();
- }
-
- this.page.root = this.graphModel.getRoot();
- this.selectionMap = this.diagramMap.get('select');
-
- if (this.selectionMap == null)
- {
- this.initializeSelection();
- }
- // Resets selection state to ensure change event
- if (this.driveRealtime.file.isEditable())
- {
- this.selectionMap.set(this.driveRealtime.userId, '');
- }
-
- this.installRemoteSelectionListener();
- };
- /**
- *
- */
- RealtimeMapping.prototype.initializeSelection = function()
- {
- this.selectionMap = this.rtModel.createMap();
-
- if (this.driveRealtime.file.isEditable())
- {
- this.diagramMap.set('select', this.selectionMap);
- }
- //this.log('Selection list created');
- };
- /**
- * Adds a listener for changes to the RT selection map to highlight
- * remote selection
- */
- RealtimeMapping.prototype.installRemoteSelectionListener = function()
- {
- this.selectionMap.addEventListener(gapi.drive.realtime.EventType.VALUE_CHANGED, mxUtils.bind(this, function(evt)
- {
- if (!this.driveRealtime.isLocalEvent(evt) && evt.newValue != null && (this.ui.currentPage == null ||
- this.ui.currentPage == this.page))
- {
- var cellIds = evt.newValue.split(',');
-
- for (var i = 0; i < cellIds.length; i++)
- {
- this.driveRealtime.highlight(this.driveRealtime.model.getCell(cellIds[i]), evt.sessionId);
- }
- }
- }));
- };
- /**
- * Returns true if this diagram is being displayed.
- */
- RealtimeMapping.prototype.isActive = function()
- {
- return this.ui.currentPage == null || this.ui.currentPage.mapping == this;
- };
- /**
- * Syncs initial state from collab model to graph model.
- */
- RealtimeMapping.prototype.getGraphModel = function()
- {
- return (this.isActive()) ? this.driveRealtime.model : this.graphModel;
- };
- /**
- * Syncs initial state from collab model to graph model.
- */
- RealtimeMapping.prototype.initGraph = function()
- {
- if (this.isActive())
- {
- this.activate(true);
- mxClient.NO_FO = (this.graph.mathEnabled) ? true : Editor.prototype.originalNoForeignObject;
- // TODO: Fixes math offset - why?
- this.ui.editor.graph.sizeDidChange();
- }
-
- var rtRoot = this.diagramMap.get(this.driveRealtime.rootKey);
-
- if (rtRoot.cell == null)
- {
- this.createCell(rtRoot);
- this.restoreCell(rtRoot);
- }
- else
- {
- this.installAllRealtimeCellListeners(rtRoot);
- }
-
- // Stores root in current model and local model
- var gm = this.getGraphModel();
- gm.setRoot(rtRoot.cell);
-
- if (gm != this.graphModel)
- {
- this.graphModel.setRoot(gm.getRoot());
- }
- };
- /**
- * Writes the graph properties from the realtime model to the given mxGraphModel node.
- */
- RealtimeMapping.prototype.writeRealtimeToNode = function(node)
- {
- node.setAttribute('shadow', this.diagramMap.get('shadowVisible'));
- node.setAttribute('fold', this.diagramMap.get('foldingEnabled'));
- node.setAttribute('math', this.diagramMap.get('mathEnabled'));
- node.setAttribute('pageScale', this.diagramMap.get('pageScale'));
-
- var img = this.diagramMap.get('backgroundImage');
-
- if (img != null && img.length > 0)
- {
- node.setAttribute('backgroundImage', img);
- }
-
- var color = this.diagramMap.get('backgroundColor');
-
- if (color != null)
- {
- node.setAttribute('background', color);
- }
-
- var pf = this.diagramMap.get('pageFormat');
-
- if (pf != null)
- {
- var values = pf.split(',');
-
- if (values.length > 1)
- {
- node.setAttribute('pageWidth', parseInt(values[0]));
- node.setAttribute('pageHeight', parseInt(values[1]));
- }
- }
- };
- /**
- * Writes the graph properties from the realtime model to the given mxGraphModel node.
- */
- RealtimeMapping.prototype.writeNodeToRealtime = function(node)
- {
- this.diagramMap.set('shadowVisible', node.getAttribute('shadow'));
- this.diagramMap.set('foldingEnabled', node.getAttribute('fold'));
- this.diagramMap.set('mathEnabled', node.getAttribute('math'));
- this.diagramMap.set('pageScale', node.getAttribute('pageScale'));
- this.diagramMap.set('pageVisible', node.getAttribute('pageVisible'));
-
- var img = node.getAttribute('backgroundImage');
-
- if (img != null && img.length > 0)
- {
- this.diagramMap.set('backgroundImage', img);
- }
-
- var color = node.getAttribute('background');
-
- if (color != null)
- {
- this.diagramMap.set('backgroundColor', color);
- }
- this.diagramMap.set('pageFormat', node.getAttribute('pageWidth') + ',' +
- node.getAttribute('pageHeight'));
- };
- /**
- * Syncs initial state from collab model to graph model.
- */
- RealtimeMapping.prototype.activate = function(quiet)
- {
- this.realtimePageFormatChanged(this.diagramMap.get('pageFormat'), quiet);
- this.realtimePageScaleChanged(this.diagramMap.get('pageScale'), quiet);
- this.realtimeMathEnabledChanged(this.diagramMap.get('mathEnabled'), quiet);
- this.realtimeBackgroundColorChanged(this.diagramMap.get('backgroundColor'), quiet);
- this.realtimeShadowVisibleChanged(this.diagramMap.get('shadowVisible'), quiet);
- this.realtimeFoldingEnabledChanged(this.diagramMap.get('foldingEnabled'), quiet);
- this.realtimePageVisibleChanged(this.diagramMap.get('pageVisible'), quiet);
- this.realtimeBackgroundImageChanged(this.diagramMap.get('backgroundImage'), quiet);
- };
- /**
- * Syncs initial state from graph model to collab model.
- */
- RealtimeMapping.prototype.initRealtime = function()
- {
- this.rtModel.beginCompoundOperation();
-
- try
- {
- var rtCell = this.createRealtimeCell(this.getGraphModel().getRoot());
- this.saveRealtimeCell(rtCell.cell);
- this.diagramMap.set(this.driveRealtime.rootKey, rtCell);
-
- if (this.page.graphModelNode != null)
- {
- this.writeNodeToRealtime(this.page.graphModelNode);
- }
- else
- {
- var vs = this.page.viewState;
- var pf = (vs != null) ? vs.pageFormat : mxSettings.getPageFormat();
-
- this.diagramMap.set('shadowVisible', (vs != null && vs.shadowVisible) ? '1' : '0');
- this.diagramMap.set('foldingEnabled', (vs != null && !vs.foldingEnabled) ? '0' : '1');
- this.diagramMap.set('mathEnabled', (vs != null && vs.mathEnabled) ? '1' : '0');
- this.diagramMap.set('pageScale', this.graph.pageScale);
- this.diagramMap.set('pageVisible', (vs != null && !vs.pageVisible) ? '0' : '1');
- this.diagramMap.set('pageFormat', pf.width + ',' + pf.height);
- this.diagramMap.set('backgroundImage', (vs != null && vs.backgroundImage != null) ?
- JSON.stringify(vs.backgroundImage) : '');
- this.diagramMap.set('backgroundColor', (vs != null && vs.background != null) ?
- this.graph.background : '');
- }
-
- this.root.set('modifiedDate', new Date().getTime());
- this.rtModel.endCompoundOperation();
- }
- catch (e)
- {
- this.rtModel.endCompoundOperation();
- this.ui.handleError(e);
- }
- };
- /**
- * Syncs initial state from graph model to collab model.
- */
- RealtimeMapping.prototype.createRealtimeCell = function(cell)
- {
- var rtCell = cell.rtCell;
-
- if (rtCell == null)
- {
- rtCell = this.rtModel.create('Cell');
- rtCell.children = this.rtModel.createList();
- rtCell.cell = cell;
- cell.rtCell = rtCell;
- rtCell.cellId = cell.id;
- rtCell.type = (cell.vertex) ? 'vertex' : ((cell.edge) ? 'edge' : '');
- rtCell.connectable = (cell.connectable == null || cell.connectable) ? '1' : '0';
-
- if (mxUtils.isNode(cell.value))
- {
- rtCell.xmlValue = mxUtils.getXml(cell.value);
- }
- else if (cell.value != null)
- {
- rtCell.value = cell.value;
- }
-
- rtCell.style = (cell.style != null) ? cell.style : null;
- rtCell.geometry = (cell.geometry != null) ? mxUtils.getXml(this.driveRealtime.codec.encode(cell.geometry)) : null;
- rtCell.visible = (cell.visible == null || cell.visible) ? '1' : '0';
- rtCell.collapsed = (cell.collapsed != null && cell.collapsed) ? '1' : '0';
- for (var i = 0; i < this.graphModel.getChildCount(cell); i++)
- {
- var child = this.graphModel.getChildAt(cell, i);
- this.createRealtimeCell(child);
-
- if (child.rtCell.parent == null)
- {
- child.rtCell.parent = rtCell;
- rtCell.children.push(child.rtCell);
- }
- }
-
- this.installRealtimeCellListeners(rtCell);
- }
- return rtCell;
- };
- /**
- * Syncs initial state from graph model to collab model.
- */
- RealtimeMapping.prototype.saveRealtimeCell = function(cell)
- {
- if (cell.source != null)
- {
- if (cell.source.rtCell == null)
- {
- this.createRealtimeCell(cell.source);
- }
-
- cell.rtCell.source = cell.source.rtCell;
- }
- else
- {
- cell.rtCell.source = null;
- }
- if (cell.target != null)
- {
- if (cell.target.rtCell == null)
- {
- this.createRealtimeCell(cell.target);
- }
-
- cell.rtCell.target = cell.target.rtCell;
- }
- else
- {
- cell.rtCell.target = null;
- }
- for (var i = 0; i < this.graphModel.getChildCount(cell); i++)
- {
- this.saveRealtimeCell(this.graphModel.getChildAt(cell, i));
- }
- };
- /**
- * Syncs initial state from collab model to graph model.
- */
- RealtimeMapping.prototype.createCell = function(rtCell)
- {
- var cell = rtCell.cell;
-
- if (cell == null)
- {
- cell = new mxCell();
- rtCell.cell = cell;
- cell.rtCell = rtCell;
- cell.id = rtCell.cellId;
- cell.vertex = rtCell.type == 'vertex';
- cell.edge = rtCell.type == 'edge';
- cell.connectable = rtCell.connectable != '0';
- cell.value = (rtCell.xmlValue != null) ? mxUtils.parseXml(rtCell.xmlValue).documentElement : rtCell.value;
- cell.style = rtCell.style;
- cell.geometry = (rtCell.geometry != null) ? this.driveRealtime.codec.decode(mxUtils.parseXml(rtCell.geometry).documentElement) : null;
- cell.visible = rtCell.visible != '0';
- cell.collapsed = rtCell.collapsed == '1';
- for (var i = 0; i < rtCell.children.length; i++)
- {
- var rtChild = rtCell.children.get(i);
- this.createCell(rtChild);
-
- if (rtChild.cell.parent == null)
- {
- cell.insert(rtChild.cell);
- }
- }
-
- this.installRealtimeCellListeners(rtCell);
- }
-
- return cell;
- };
- /**
- * Restores connection between edges and terminals.
- */
- RealtimeMapping.prototype.restoreCell = function(rtCell)
- {
- var valid = true;
- if (rtCell.cell != null)
- {
- //console.log('restoreCell', rtCell.cellId);
-
- if (rtCell.source != null)
- {
- // Removes edge if source is no longer in the model
- if (rtCell.source.parent == null)
- {
- //console.log('invalid source', valid, rtCell.cellId, rtCell.source.cellId);
- rtCell.source = null;
- valid = false;
- }
- else
- {
- if (rtCell.source.cell == null)
- {
- this.createCell(rtCell.source);
- }
-
- rtCell.source.cell.insertEdge(rtCell.cell, true);
- }
- }
-
- if (valid && rtCell.target != null)
- {
- // Removes edge if source is no longer in the model
- if (rtCell.target.parent == null)
- {
- //console.log('invalid target', valid, rtCell.cellId, rtCell.target.cellId);
- rtCell.target = null;
- valid = false;
- }
- else
- {
- if (rtCell.target.cell == null)
- {
- this.createCell(rtCell.target);
- }
-
- rtCell.target.cell.insertEdge(rtCell.cell, false);
- }
- }
- // Checks if edge contains required terminals or terminal points
- if (valid && this.graphModel.isEdge(rtCell.cell))
- {
- var geo = this.graphModel.getGeometry(rtCell.cell);
- valid = geo != null &&
- (this.graphModel.getTerminal(rtCell.cell, true) != null || geo.getTerminalPoint(true) != null) &&
- (this.graphModel.getTerminal(rtCell.cell, false) != null || geo.getTerminalPoint(false) != null);
- //console.log('geometry check', valid, rtCell.cellId);
- }
- }
-
- // Removes invalid cell
- if (!valid)
- {
- if (rtCell.parent != null)
- {
- rtCell.parent.children.removeValue(rtCell);
- rtCell.parent = null;
- }
-
- if (rtCell.cell != null)
- {
- // TODO: Remove from source and target?
- //console.log('remove invalid cell', rtCell.cellId);
- this.getGraphModel().remove(rtCell.cell);
- }
- }
- else
- {
- for (var i = 0; i < rtCell.children.length; i++)
- {
- this.restoreCell(rtCell.children.get(i));
- }
- }
- };
- /**
- * Restores connection between edges and terminals.
- */
- RealtimeMapping.prototype.containsRealtimeCell = function(rtCell)
- {
- var tmp = rtCell;
-
- while (tmp.parent != null)
- {
- tmp = tmp.parent;
- }
-
- return tmp == this.diagramMap.get(this.driveRealtime.rootKey);
- };
- /**
- *
- */
- RealtimeMapping.prototype.beginUpdate = function()
- {
- var graphModel = this.getGraphModel();
-
- if (!this.driveRealtime.ignoreChange)
- {
- this.driveRealtime.ignoreChange = true;
- graphModel.beginUpdate();
-
- window.setTimeout(mxUtils.bind(this, function()
- {
- graphModel.endUpdate();
- this.driveRealtime.ignoreChange = false;
- }), 0);
- }
-
- return graphModel;
- };
- /**
- * Syncs initial state from collab model to graph model.
- */
- RealtimeMapping.prototype.installAllRealtimeCellListeners = function(rtCell)
- {
- if (rtCell != null)
- {
- this.installRealtimeCellListeners(rtCell);
-
- for (var i = 0; i < rtCell.children.length; i++)
- {
- this.installAllRealtimeCellListeners(rtCell.children.get(i));
- }
- }
- };
- /**
- * Adds the listener for added and removed cells in the collab model and maps
- * them to the graph model.
- */
- RealtimeMapping.prototype.installRealtimeCellListeners = function(rtCell)
- {
- rtCell.addEventListener(gapi.drive.realtime.EventType.VALUE_CHANGED, mxUtils.bind(this, function(evt)
- {
- this.handleValueChanged(rtCell, evt);
- this.needsUpdate = true;
- }));
-
- rtCell.children.addEventListener(gapi.drive.realtime.EventType.VALUES_ADDED, mxUtils.bind(this, function(evt)
- {
- this.handleValuesAdded(rtCell, evt);
- this.needsUpdate = true;
- }));
-
- rtCell.children.addEventListener(gapi.drive.realtime.EventType.VALUES_REMOVED, mxUtils.bind(this, function(evt)
- {
- this.handleValuesRemoved(rtCell, evt);
- this.needsUpdate = true;
- }));
- };
- /**
- * Adds the listener for added and removed cells in the collab model and maps
- * them to the graph model.
- */
- RealtimeMapping.prototype.handleValueChanged = function(rtCell, evt)
- {
- var cell = rtCell.cell;
- if (!this.driveRealtime.isLocalEvent(evt) && cell != null)
- {
- var value = evt.newValue;
- var key = evt.property;
- var graphModel = this.beginUpdate();
- //console.log('valueChanged: cell=' + rtCell.cellId + ' key=' + key + ' value=' + ((value != null) ? (value.cellId || value) : '[null]'));
-
- if (key == 'type')
- {
- cell.vertex = value == 'vertex';
- cell.edge = value == 'edge';
- }
- else if (key == 'connectable')
- {
- cell.connectable = (value == '1');
- }
- else if (key == 'source' || key == 'target')
- {
- if (value == null)
- {
- if (evt.oldValue != null)
- {
- graphModel.setTerminal(cell, null, key == 'source');
- }
- }
- else
- {
- // Handles the case where an edge is connected to a vertex which is not in the model
- if (value.cell == null || !this.containsRealtimeCell(value) || graphModel.getCell(value.cellId) == null)
- {
- if (rtCell.parent != null)
- {
- rtCell.parent.children.removeValue(rtCell);
- rtCell.parent = null;
- }
-
- graphModel.setTerminal(cell, null, key == 'source');
- graphModel.remove(rtCell.cell);
- rtCell[key] = null;
- }
- else
- {
- graphModel.setTerminal(cell, value.cell, key == 'source');
- }
- }
- }
- else if (key == 'value')
- {
- graphModel.setValue(cell, value);
- }
- else if (key == 'xmlValue')
- {
- graphModel.setValue(cell, mxUtils.parseXml(value).documentElement);
- }
- else if (key == 'style')
- {
- graphModel.setStyle(cell, value);
- }
- else if (key == 'geometry')
- {
- var geometry = (value != null) ? this.driveRealtime.codec.decode(mxUtils.parseXml(value).documentElement) : null;
- graphModel.setGeometry(cell, geometry);
- }
- else if (key == 'collapsed')
- {
- graphModel.setCollapsed(cell, value == '1');
- }
- else if (key == 'visible')
- {
- graphModel.setVisible(cell, value == '1');
- }
- else if (key == 'parent')
- {
- // Removes the child from its previous parent in the realtime model.
- if (evt.oldValue != null)
- {
- //console.log('remove clone', 'parent', evt.oldValue.cellId, 'child', rtCell.cellId);
- evt.oldValue.children.removeValue(rtCell);
- }
- else
- {
- this.createCell(rtCell);
- this.restoreCell(rtCell);
- }
-
- if (value == null)
- {
- graphModel.remove(cell);
- }
- else
- {
- var index = value.children.indexOf(rtCell);
-
- if (index >= 0)
- {
- //console.log('move child', 'parent', value.cellId, 'child', rtCell.cellId, index);
- graphModel.add(value.cell, rtCell.cell, index);
- }
- }
- }
- }
- };
- /**
- * Adds the listener for added and removed cells in the collab model and maps
- * them to the graph model.
- */
- RealtimeMapping.prototype.handleValuesAdded = function(rtCell, evt)
- {
- if (!this.driveRealtime.isLocalEvent(evt))
- {
- var graphModel = this.beginUpdate();
-
- for (var i = 0; i < evt.values.length; i++)
- {
- var rtChild = evt.values[i];
- //console.log('valueAdded', 'parent', rtCell.cellId, 'child', rtChild.cellId, 'index', evt.index + i, rtChild);
-
- // Removes child if the parent of the child and the parent of the children array are not
- // the same. This happens if clients move a cell into different parents concurrently.
- if (rtChild.parent != null)
- {
- if (rtChild.parent != rtCell)
- {
- //console.log('remove clone', 'parent', rtCell.cellId, 'child', rtChild.cellId);
- rtCell.children.removeValue(rtChild);
- }
- else
- {
- if (rtChild.cell == null || rtChild.cell.parent == null)
- {
- this.createCell(rtChild);
- this.restoreCell(rtChild);
- }
-
- // Resolves conflict when two clients change the order of a child at the same
- // time which results in the same child appearing multiple times in the list.
- // Note that the actual child index may be different from the event information
- // at this point so a generic check for duplicates is performed based on the
- // first appearance of the cell in the list.
- var first = rtCell.children.indexOf(rtChild);
- var last = rtCell.children.lastIndexOf(rtChild);
-
- while (first != last)
- {
- //console.log('remove duplicate', rtChild.cellId, last);
- rtCell.children.remove(last);
- last = rtCell.children.lastIndexOf(rtChild);
- }
-
- // Inserts the child at the smallest index to maintain consistency with RT
- if (rtChild.parent == rtCell)
- {
- //console.log('add', 'parent', rtCell.cellId, 'child', rtChild.cellId, 'index', Math.min(first, evt.index + i));
- graphModel.add(rtCell.cell, rtChild.cell, Math.min(first, evt.index + i));
- }
- }
- }
- }
- }
- };
- /**
- * Adds the listener for added and removed cells in the collab model and maps
- * them to the graph model.
- */
- RealtimeMapping.prototype.handleValuesRemoved = function(rtCell, evt)
- {
- if (!this.driveRealtime.isLocalEvent(evt))
- {
- var graphModel = this.beginUpdate();
-
- for (var i = 0; i < evt.values.length; i++)
- {
- var rtChild = evt.values[i];
-
- if (rtChild.cell != null)
- {
- //console.log('valueRemoved', 'parent', rtCell.cellId, 'child', rtChild.cellId,
- // 'index', evt.index + i, rtChild, rtChild.cell);
-
- // Checks if the realtime parent and the graph parent are different and updates the parent
- // in the graph to match the realtime parent. This happens if the child was removed as a
- // clone in another client.
- if (rtChild.parent != null && rtChild.parent != rtCell && rtChild.cell.parent != rtChild.parent.cell)
- {
- //console.log('move clone', rtChild.cellId, evt.index + i, rtChild.parent.cellId);
- var index = rtChild.parent.children.indexOf(rtChild);
- graphModel.add(rtChild.parent.cell, rtChild.cell, index);
- }
- else
- {
- // Checks if the realtime parent contains a duplicate entry of this child
- // and updates the index of the child in the graph. This happens if the
- // child was removed as a duplicate entry in another client.
- var index = rtCell.children.indexOf(rtChild);
-
- if (index >= 0)
- {
- //console.log('adjust duplicate', rtCell.cellId, evt.index + i, rtChild.cellId, 'index', index);
- graphModel.add(rtCell.cell, rtChild.cell, index);
- }
- }
- }
- }
- }
- };
- /**
- * Syncs initial state from collab model to graph model.
- */
- RealtimeMapping.prototype.realtimePageFormatChanged = function(value, quiet)
- {
- if (value != null)
- {
- var values = value.split(',');
-
- if (values.length > 1)
- {
- if (!this.isActive())
- {
- if (this.page.viewState != null)
- {
- this.page.viewState.pageFormat = new mxRectangle(0, 0, parseInt(values[0]), parseInt(values[1]));
- }
- }
- else if (quiet)
- {
- this.graph.pageFormat = new mxRectangle(0, 0, parseInt(values[0]), parseInt(values[1]));
- }
- else
- {
- this.driveRealtime.ignorePageFormatChanged = true;
- this.ui.setPageFormat(new mxRectangle(0, 0, parseInt(values[0]), parseInt(values[1])));
- this.driveRealtime.ignorePageFormatChanged = false;
- }
- }
- }
- };
- /**
- * Syncs initial state from collab model to graph model.
- */
- RealtimeMapping.prototype.realtimePageScaleChanged = function(value, quiet)
- {
- if (value != null)
- {
- if (!this.isActive())
- {
- if (this.page.viewState != null)
- {
- this.page.viewState.pageScale = parseFloat(value);
- }
- }
- else if (quiet)
- {
- this.graph.pageScale = parseFloat(value);
- }
- else
- {
- this.driveRealtime.ignorePageScaleChanged = true;
- this.ui.setPageScale(parseFloat(value));
- this.driveRealtime.ignorePageScaleChanged = false;
- }
- }
- };
- /**
- * Syncs initial state from collab model to graph model.
- */
- RealtimeMapping.prototype.realtimeBackgroundColorChanged = function(value, quiet)
- {
- if (!this.isActive())
- {
- if (this.page.viewState != null)
- {
- this.page.viewState.background = (value == '') ? null : value;
- }
- }
- else if (quiet)
- {
- this.graph.background = (value == '') ? null : value;
- }
- else
- {
- this.driveRealtime.ignoreBackgroundColorChanged = true;
- this.ui.setBackgroundColor((value == '') ? null : value);
- this.driveRealtime.ignoreBackgroundColorChanged = false;
- }
- };
- /**
- * Syncs initial state from collab model to graph model.
- */
- RealtimeMapping.prototype.realtimeFoldingEnabledChanged = function(value, quiet)
- {
- if (!this.isActive())
- {
- if (this.page.viewState != null)
- {
- this.page.viewState.foldingEnabled = value == '1';
- }
- }
- else if (quiet)
- {
- this.graph.foldingEnabled = value == '1';
- }
- else
- {
- this.driveRealtime.ignoreFoldingEnabledChanged = true;
- this.ui.setFoldingEnabled(value == '1');
- this.driveRealtime.ignoreFoldingEnabledChanged = false;
- }
- };
- /**
- * Syncs initial state from collab model to graph model.
- */
- RealtimeMapping.prototype.realtimePageVisibleChanged = function(value, quiet)
- {
- if (!this.isActive())
- {
- if (this.page.viewState != null)
- {
- this.page.viewState.pageVisible = value != '0';
- }
- }
- else if (quiet)
- {
- this.graph.pageVisible = value != '0';
- this.graph.pageBreaksVisible = this.graph.pageVisible;
- this.graph.preferPageSize = this.graph.pageVisible;
- }
- else
- {
- this.driveRealtime.ignorePageVisibleChanged = true;
- this.ui.setPageVisible(value != '0');
- this.driveRealtime.ignorePageVisibleChanged = false;
- }
- };
- /**
- * Syncs initial state from collab model to graph model.
- */
- RealtimeMapping.prototype.realtimeShadowVisibleChanged = function(value, quiet)
- {
- // Does not need quiet mode as it's handled independently of refresh
- if (!this.isActive())
- {
- if (this.page.viewState != null)
- {
- this.page.viewState.shadowVisible = value == '1';
- }
- }
- else
- {
- this.driveRealtime.ignoreShadowVisibleChanged = true;
- this.ui.editor.graph.setShadowVisible(value == '1');
- this.driveRealtime.ignoreShadowVisibleChanged = false;
- }
- };
- /**
- * Syncs initial state from collab model to graph model.
- */
- RealtimeMapping.prototype.realtimeBackgroundImageChanged = function(value, quiet)
- {
- var data = (value != null && value.length > 0) ? JSON.parse(value) : null;
-
- if (!this.isActive())
- {
- if (this.page.viewState != null)
- {
- this.page.viewState.backgroundImage = (data != null) ? new mxImage(data.src, data.width, data.height) : null;
- }
- }
- else if (quiet)
- {
- this.graph.setBackgroundImage((data != null) ? new mxImage(data.src, data.width, data.height) : null);
- }
- else
- {
- this.driveRealtime.ignoreBackgroundImageChanged = true;
- this.ui.setBackgroundImage((data != null) ? new mxImage(data.src, data.width, data.height) : null);
- this.driveRealtime.ignoreBackgroundImageChanged = false;
- }
- };
- /**
- * Syncs initial state from collab model to graph model.
- */
- RealtimeMapping.prototype.realtimeMathEnabledChanged = function(value, quiet)
- {
- if (!this.isActive())
- {
- if (this.page.viewState != null)
- {
- this.page.viewState.mathEnabled = urlParams['math'] == '1' || value == '1';
- }
- }
- else if (quiet)
- {
- this.graph.mathEnabled = urlParams['math'] == '1' || value == '1';
- }
- else
- {
- this.driveRealtime.ignoreMathEnabledChanged = true;
- this.ui.setMathEnabled(urlParams['math'] == '1' || value == '1');
- this.driveRealtime.ignoreMathEnabledChanged = false;
- }
- };
- /**
- * Syncs initial state from collab model to graph model.
- */
- RealtimeMapping.prototype.removeAllRealtimeCellListeners = function(rtCell)
- {
- if (rtCell != null)
- {
- rtCell.removeAllEventListeners();
- rtCell.children.removeAllEventListeners();
-
- for (var i = 0; i < rtCell.children.length; i++)
- {
- this.removeAllRealtimeCellListeners(rtCell.children.get(i));
- }
- }
- };
- /**
- * Syncs initial state from collab model to graph model.
- */
- RealtimeMapping.prototype.destroy = function()
- {
- this.diagramMap.removeAllEventListeners();
- this.selectionMap.removeAllEventListeners();
- this.removeAllRealtimeCellListeners(this.diagramMap.get(this.driveRealtime.rootKey));
- };
|