1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819 |
- /**
- * Copyright (c) 2006-2016, JGraph Ltd
- * Copyright (c) 2006-2016, Gaudenz Alder
- */
- /**
- * Creates an object that synchronizes the graph model and given realtime model.
- * The session ID is used as a prefix in the model to produce unique IDs for new
- * cells.
- */
- function DriveRealtime(file, doc)
- {
- this.realtimeAutosaveDelay = this.defaultRealtimeAutosaveDelay;
- this.realtimeMaxAutosaveDelay = this.defaultRealtimeMaxAutosaveDelay;
-
- this.file = file;
- this.doc = doc;
- this.rtModel = this.doc.getModel();
- this.root = this.rtModel.getRoot();
-
- this.ui = file.getUi()
- this.graph = this.ui.editor.graph;
- this.model = this.graph.model;
- this.userId = this.ui.drive.user.id;
- this.ui.allowAnimation = false;
- this.codec = new mxCodec();
-
- this.disconnectListener = mxUtils.bind(this, function()
- {
- // LATER: How to reload realtime document without refreshing the page
- this.sessionExpiredError();
- });
-
- this.ui.drive.addListener('disconnected', this.disconnectListener);
-
- // Change of autosave triggers an immediate save to update thumbnail
- this.autosaveChangeListener = mxUtils.bind(this, function()
- {
- var prevValue = this.ui.drive.enableThumbnails;
- this.ui.drive.enableThumbnails = this.ui.editor.autosave;
- this.ui.editor.setStatus(mxResources.get('saving') + '...');
-
- this.file.save(true, mxUtils.bind(this, function()
- {
- this.ui.editor.setStatus(mxResources.get('allChangesSaved'));
- }));
-
- this.ui.drive.enableThumbnails = prevValue;
- });
-
- this.ui.editor.addListener('autosaveChanged', this.autosaveChangeListener);
- };
- /**
- * Specifies the key of the root element in the model. Default is root.
- */
- DriveRealtime.prototype.logLevel = 2;
- /**
- * True if a change event is fired for a remote change.
- */
- DriveRealtime.prototype.updateStatusInterval = 20000;
- /**
- * Specifies the key of the root element in the model. Default is root.
- */
- DriveRealtime.prototype.diagramsKey = 'diagrams';
- /**
- * Specifies the key of the root element in the model. Default is root.
- */
- DriveRealtime.prototype.diagrams = null;
- /**
- * Specifies the key of the root element in the model. Default is root.
- */
- DriveRealtime.prototype.rootKey = 'root';
- /**
- * Specifies the key of the root element in the model. Default is root.
- */
- DriveRealtime.prototype.diagramMap = null;
- /**
- * Specifies the key of the root element in the model. Default is root.
- */
- DriveRealtime.prototype.chatHistory = null;
- /**
- * True if a change event is fired for a remote change.
- */
- DriveRealtime.prototype.saving = false;
- /**
- * Sets the delay for autosave in milliseconds. Default is 5000.
- */
- DriveRealtime.prototype.defaultRealtimeAutosaveDelay = 5000;
- /**
- * Sets the delay for autosave in milliseconds. Default is 60000.
- */
- DriveRealtime.prototype.defaultRealtimeMaxAutosaveDelay = 60000;
- /**
- * Sets the delay for autosave in milliseconds. Default is 500.
- */
- DriveRealtime.prototype.realtimeHeartbeat = 30000;
- /**
- * True if a undoableEdit has been scheduled in <executeChange>.
- */
- DriveRealtime.prototype.ignoreChange = false;
- /**
- * True if a undoableEdit has been scheduled in <executeChange>.
- */
- DriveRealtime.prototype.ignorePageFormatChanged = false;
- /**
- * True if a undoableEdit has been scheduled in <executeChange>.
- */
- DriveRealtime.prototype.ignoreScaleChanged = false;
- /**
- * True if a undoableEdit has been scheduled in <executeChange>.
- */
- DriveRealtime.prototype.ignoreBackgroundColorChanged = false;
- /**
- * True if a undoableEdit has been scheduled in <executeChange>.
- */
- DriveRealtime.prototype.ignoreShadowVisibleChanged = false;
- /**
- * True if a undoableEdit has been scheduled in <executeChange>.
- */
- DriveRealtime.prototype.ignoreBackgroundImageChanged = false;
- /**
- * True if a undoableEdit has been scheduled in <executeChange>.
- */
- DriveRealtime.prototype.ignoreMathEnabledChanged = false;
- /**
- * Syncs initial state from collab model to graph model.
- */
- DriveRealtime.prototype.getDiagramMap = function()
- {
- return (this.ui.currentPage != null) ? this.ui.currentPage.mapping.diagramMap : this.diagramMap;
- };
- /**
- * Indirection for legacy support.
- */
- DriveRealtime.prototype.getCurrentPage = function()
- {
- return (this.page != null) ? this.page : this.ui.currentPage;
- };
- /**
- * Synchronizes the collaboration model and the graph model and installs
- * the required listeners to keep them in sync.
- */
- DriveRealtime.prototype.start = function()
- {
- if (urlParams['reset'] == '1')
- {
- this.root.clear();
- this.log('reset realtime');
- }
- var prefix = this.createPrefix();
- this.model.prefix = prefix + '-';
- this.ui.editor.resetGraph();
-
- // Creates diagrams list and default entry
- this.diagrams = this.root.get(this.diagramsKey);
- var diagramsCreated = false;
-
- if (this.diagrams == null)
- {
- this.diagrams = this.rtModel.createList();
- this.root.set(this.diagramsKey, this.diagrams);
- diagramsCreated = true;
- this.log('realtime model initialized');
- }
-
- // Specifies if the file should be saved immediately after setup
- var forceSave = false;
-
- if (this.file.getData() != '')
- {
- this.ui.fileNode = null;
- this.ui.pages = null;
-
- // Converts from XML to realtime
- this.ui.setFileData(this.file.getData());
- this.log('xml converted');
-
- // Logs conversion of old RT models (converted from XML backup)
- if (this.root.has('cells') && diagramsCreated)
- {
- try
- {
- var img = new Image();
-
- // Timestamp is added to bypass client-side cache
- img.src = 'https://log.draw.io/log?severity=CONFIG&msg=converted-oldrt&v=' +
- encodeURIComponent(EditorUi.VERSION) + '&ts=' + new Date().getTime();
- }
- catch (e)
- {
- // ignore
- }
- }
-
- if (this.ui.pages != null)
- {
- for (var i = 0; i < this.ui.pages.length; i++)
- {
- var page = this.ui.pages[i];
-
- var diagramMap = this.rtModel.createMap();
- this.diagrams.push(diagramMap);
-
- this.ui.updatePageRoot(page);
- page.mapping = new RealtimeMapping(this, diagramMap, page);
-
- if (this.file.isEditable())
- {
- // Read or create name for page
- if (page.getName() != '')
- {
- diagramMap.set('name', page.getName());
- }
- else if (!diagramMap.has('name'))
- {
- diagramMap.set('name', mxResources.get('pageWithNumber', [i + 1]));
- }
- }
- // Sync name and initialize
- page.setName(diagramMap.get('name') || mxResources.get('pageWithNumber', [i + 1]));
- page.mapping.init();
- }
- }
- else if (urlParams['pages'] == '0')
- {
- this.diagramMap = this.rtModel.createMap();
- this.diagrams.push(this.diagramMap);
- // Dummy node, should be XML node if used
- this.page = new DiagramPage(document.createElement('diagram'));
- this.page.mapping = new RealtimeMapping(this, this.diagramMap, this.page);
- this.diagramMap.set('name', mxResources.get('pageWithNumber', [1]));
- this.page.setName(this.diagramMap.get('name'));
- this.page.mapping.init();
- }
- else
- {
- this.ui.fileNode = mxUtils.createXmlDocument().createElement('mxfile');
- this.ui.pages = [];
-
- var diagramMap = this.rtModel.createMap();
- this.diagrams.push(diagramMap);
-
- var page = new DiagramPage(this.ui.fileNode.ownerDocument.createElement('diagram'));
- page.mapping = new RealtimeMapping(this, diagramMap, page);
- this.ui.currentPage = page;
- if (this.file.isEditable() && !page.mapping.diagramMap.has('name'))
- {
- page.mapping.diagramMap.set('name', mxResources.get('pageWithNumber', [1]));
- }
-
- page.setName(page.mapping.diagramMap.get('name') || mxResources.get('pageWithNumber', [1]));
- this.ui.pages.push(page);
- page.mapping.init();
- }
-
- forceSave = true;
- }
- else if (this.diagrams.length < 2 && urlParams['pages'] == '0')
- {
- this.ui.fileNode = null;
- this.ui.pages = null;
-
- if (this.diagrams.length == 0)
- {
- this.diagramMap = this.rtModel.createMap();
- this.diagrams.push(this.diagramMap);
- }
- else
- {
- this.diagramMap = this.diagrams.get(0);
- }
-
- // Dummy node, should be XML node if used
- this.page = new DiagramPage(document.createElement('diagram'));
- this.page.mapping = new RealtimeMapping(this, this.diagramMap, this.page);
-
- if (!this.diagramMap.has('name'))
- {
- this.diagramMap.set('name', mxResources.get('pageWithNumber', [1]));
- }
-
- this.page.setName(this.page.mapping.diagramMap.get('name'));
-
- // Avoids scroll offset when switching page
- this.page.mapping.init();
- this.ui.editor.fireEvent(new mxEventObject('resetGraphView'));
- }
- else
- {
- this.ui.fileNode = mxUtils.createXmlDocument().createElement('mxfile');
- this.ui.pages = [];
-
- if (this.diagrams.length == 0)
- {
- this.diagrams.push(this.rtModel.createMap());
- }
-
- var pageIndex = Math.max(0, Math.min(this.diagrams.length - 1, urlParams['page'] || 0));
-
- for (var i = 0; i < this.diagrams.length; i++)
- {
- var page = new DiagramPage(this.ui.fileNode.ownerDocument.createElement('diagram'));
- page.mapping = new RealtimeMapping(this, this.diagrams.get(i), page);
-
- if (this.file.isEditable() && !page.mapping.diagramMap.has('name'))
- {
- page.mapping.diagramMap.set('name', mxResources.get('pageWithNumber', [i + 1]));
- }
-
- page.setName(page.mapping.diagramMap.get('name') || mxResources.get('pageWithNumber', [i + 1]));
- this.ui.pages.push(page);
- }
- // Sets the current page
- this.ui.currentPage = this.ui.pages[Math.max(0, Math.min(this.ui.pages.length - 1, urlParams['page'] || 0))];;
-
- // Initializes graphs and mappings
- for (var i = 0; i < this.ui.pages.length; i++)
- {
- this.ui.pages[i].mapping.init();
- }
- }
- this.installReadOnlyListener();
- this.installUiChangeListeners();
- this.installGraphModelListener();
- this.installPageSelectListener();
- this.chatHistory = this.root.get('chatHistory');
-
- if (this.chatHistory == null)
- {
- this.initializeChat();
- }
-
- this.installSelectionModelListener();
- this.installCollaboratorListener();
- this.updateCollaborators();
-
- this.doc.addEventListener(gapi.drive.realtime.EventType.DOCUMENT_SAVE_STATE_CHANGED, mxUtils.bind(this, function(evt)
- {
- if (this.saving && !evt.isPending && !evt.isSaving && !forceSave)
- {
- // Checks if the mime type of the file has changed on the server-side
- // when the autosave is triggered to make sure we eventually check
- // the mime type even if the user continues to change the diagram
- // within the autosave interval, which will cancel the current thread.
- if (this.ui.isLegacyDriveDomain() && urlParams['ignoremime'] != '1')
- {
- this.ui.drive.verifyMimeType(this.file.getId());
- }
- // Adds tooltip to small spinner and saves a backup XML copy of the file
- if (this.file.isAutosave())
- {
- this.triggerAutosave();
- }
- else
- {
- this.ui.editor.setStatus(mxResources.get('allChangesSaved'));
- }
-
- this.saving = false;
- this.resetUpdateStatusThread();
- this.realtimeHeartbeat = DriveRealtime.prototype.realtimeHeartbeat;
-
- if (this.isAliveThread != null)
- {
- window.clearTimeout(this.isAliveThread);
- this.isAliveThread = null;
- }
- }
-
- if (this.file.isEditable())
- {
- var avail = 10485760 - this.rtModel.bytesUsed;
-
- if (avail > 0 && avail < 500000 && !this.sizeLimitWarningShown)
- {
- // Shows warning just once
- this.sizeLimitWarningShown = true;
-
- this.ui.showError(mxResources.get('warning'), mxResources.get('fileNearlyFullSeeFaq'),
- mxResources.get('close'), mxUtils.bind(this, function()
- {
- // Hides the dialog
- }), null, mxResources.get('show'), mxUtils.bind(this, function()
- {
- // Show FAQ entry
- window.open('https://desk.draw.io/support/solutions/articles/16000041695-what-does-the-error-message-file-nearly-full-please-see-faq-mean');
- })
- );
- }
- }
- }));
-
- var initialized = mxUtils.bind(this, function()
- {
- this.resetUpdateStatusThread();
- this.ui.resetScrollbars();
- this.updateStatus();
- forceSave = false;
- });
- // Updates backup and preview
- if (forceSave)
- {
- this.ui.editor.setStatus(mxResources.get('saving') + '...');
- this.file.save(false, initialized, initialized);
- }
- else
- {
- initialized();
- }
- // Implements separate undo history for each client
- if (this.previousUndoListener == null)
- {
- this.previousUndoListener = this.ui.editor.undoListener;
- this.ui.editor.undoListener = mxUtils.bind(this, function(sender, evt)
- {
- if (!this.ignoreChange)
- {
- this.previousUndoListener.apply(this, arguments);
- }
- });
- }
- };
- /**
- * Syncs initial state from graph model to collab model.
- */
- DriveRealtime.prototype.triggerAutosave = function()
- {
- this.ui.editor.setStatus(mxResources.get('updatingPreview'));
-
- this.file.autosave(this.realtimeAutosaveDelay, this.realtimeMaxAutosaveDelay, mxUtils.bind(this, function(resp)
- {
- // Updates autosave delay to take into account actual delay
- this.realtimeAutosaveDelay = this.defaultRealtimeAutosaveDelay + Math.min(10000, this.file.saveDelay);
-
- // Does not update status if another autosave was scheduled
- if (this.ui.getCurrentFile() == this.file && !this.saving)
- {
- this.ui.editor.setStatus(mxResources.get('allChangesSaved'));
- }
- }),
- mxUtils.bind(this, function(resp)
- {
- this.ui.editor.setStatus(mxResources.get('errorUpdatingPreview'));
-
- // Handles error where mime type cannot be overridden because it has been changed by another app and no
- // new revision was created. This happens eg. if draw.io pro overwrites the mime type and adds a new realtime
- // model to the file which is not visible for this app so we need to switch app to stay connected to RT.
- // This could be improved to let the RT viewers know that the realtime is no longer valid, but currently
- // the focus is on not losing data, so only clients that write to the file are being notified with this.
- if (this.ui.isLegacyDriveDomain() && urlParams['ignoremime'] != '1' && resp != null &&
- resp.error != null && (resp.error.code == 400 || resp.error.code == 403))
- {
- this.ui.drive.verifyMimeType(this.file.getId(), null, true);
- }
- }));
- };
- /**
- * Syncs initial state from graph model to collab model.
- */
- DriveRealtime.prototype.triggerAutosave = function()
- {
- this.ui.editor.setStatus(mxResources.get('updatingPreview'));
-
- this.file.autosave(this.realtimeAutosaveDelay, this.realtimeMaxAutosaveDelay, mxUtils.bind(this, function(resp)
- {
- // Updates autosave delay to take into account actual delay
- this.realtimeAutosaveDelay = this.defaultRealtimeAutosaveDelay + Math.min(10000, this.file.saveDelay);
-
- // Does not update status if another autosave was scheduled
- if (this.ui.getCurrentFile() == this.file && !this.saving)
- {
- this.ui.editor.setStatus(mxResources.get('allChangesSaved'));
- }
- }),
- mxUtils.bind(this, function(resp)
- {
- this.ui.editor.setStatus(mxResources.get('errorUpdatingPreview'));
-
- // Handles error where mime type cannot be overridden because it has been changed by another app and no
- // new revision was created. This happens eg. if draw.io pro overwrites the mime type and adds a new realtime
- // model to the file which is not visible for this app so we need to switch app to stay connected to RT.
- // This could be improved to let the RT viewers know that the realtime is no longer valid, but currently
- // the focus is on not losing data, so only clients that write to the file are being notified with this.
- if (this.ui.isLegacyDriveDomain() && urlParams['ignoremime'] != '1' && resp != null &&
- resp.error != null && (resp.error.code == 400 || resp.error.code == 403))
- {
- this.ui.drive.verifyMimeType(this.file.getId(), null, true);
- }
- }));
- };
- /**
- * Syncs initial state from graph model to collab model.
- */
- DriveRealtime.prototype.installReadOnlyListener = function()
- {
- // Handles change of read-only state
- this.doc.addEventListener(gapi.drive.realtime.EventType.ATTRIBUTE_CHANGED, mxUtils.bind(this, function(evt)
- {
- if (evt.attribute == 'is_read_only')
- {
- this.file.descriptorChanged();
-
- if (!this.file.isEditable())
- {
- this.ui.editor.graph.reset();
- this.ui.editor.setStatus(mxResources.get('readOnly'));
- }
- else
- {
- this.ui.editor.setStatus('');
- }
- }
- }));
- };
- /**
- * Syncs initial state from graph model to collab model.
- */
- DriveRealtime.prototype.installUiChangeListeners = function()
- {
- this.pageFormatListener = mxUtils.bind(this, function(sender, evt)
- {
- if (!this.ignorePageFormatChanged)
- {
- try
- {
- this.setFileModified();
- this.getDiagramMap().set('pageFormat', this.graph.pageFormat.width + ',' + this.graph.pageFormat.height);
- }
- catch (e)
- {
- this.ui.handleError(e);
- }
- }
- });
-
- this.ui.addListener('pageFormatChanged', this.pageFormatListener);
-
- this.pageScaleListener = mxUtils.bind(this, function(sender, evt)
- {
- if (!this.ignorePageScaleChanged)
- {
- try
- {
- this.setFileModified();
- this.getDiagramMap().set('pageScale', this.graph.pageScale);
- }
- catch (e)
- {
- this.ui.handleError(e);
- }
- }
- });
-
- this.ui.addListener('pageScaleChanged', this.pageScaleListener);
-
- this.backgroundColorListener = mxUtils.bind(this, function(sender, evt)
- {
- if (!this.ignoreBackgroundColorChanged)
- {
- try
- {
- this.setFileModified();
- this.getDiagramMap().set('backgroundColor', (this.graph.background != null) ? this.graph.background : '');
- }
- catch (e)
- {
- this.ui.handleError(e);
- }
- }
- });
-
- this.ui.addListener('backgroundColorChanged', this.backgroundColorListener);
-
- this.shadowVisibleListener = mxUtils.bind(this, function(sender, evt)
- {
- if (!this.ignoreShadowVisibleChanged)
- {
- // Special case: This listener is called after switching page
- // so we check if the value has actually changed
- if (this.getDiagramMap().get('shadowVisible') != ((this.graph.shadowVisible) ? '1' : '0'))
- {
- try
- {
- this.setFileModified();
- this.getDiagramMap().set('shadowVisible', (this.graph.shadowVisible) ? '1' : '0');
- }
- catch (e)
- {
- this.ui.handleError(e);
- }
- }
- }
- });
-
- this.graph.addListener('shadowVisibleChanged', this.shadowVisibleListener);
-
- this.foldingEnabledListener = mxUtils.bind(this, function(sender, evt)
- {
- if (!this.ignoreFoldingEnabledChanged)
- {
- try
- {
- this.setFileModified();
- this.getDiagramMap().set('foldingEnabled', (this.graph.foldingEnabled) ? '1' : '0');
- }
- catch (e)
- {
- this.ui.handleError(e);
- }
- }
- });
-
- this.ui.addListener('foldingEnabledChanged', this.foldingEnabledListener);
-
- this.graph.addListener('shadowVisibleChanged', this.shadowVisibleListener);
-
- this.pageVisibleListener = mxUtils.bind(this, function(sender, evt)
- {
- if (!this.ignorePageVisibleChanged)
- {
- try
- {
- this.setFileModified();
- this.getDiagramMap().set('pageVisible', (this.graph.pageVisible) ? '1' : '0');
- }
- catch (e)
- {
- this.ui.handleError(e);
- }
- }
- });
-
- this.ui.addListener('pageViewChanged', this.pageVisibleListener);
-
- this.backgroundImageListener = mxUtils.bind(this, function(sender, evt)
- {
- if (!this.ignoreBackgroundImageChanged)
- {
- try
- {
- this.setFileModified();
- this.getDiagramMap().set('backgroundImage', (this.graph.backgroundImage != null) ? JSON.stringify(this.graph.backgroundImage) : '');
- }
- catch (e)
- {
- this.ui.handleError(e);
- }
- }
- });
-
- this.ui.addListener('backgroundImageChanged', this.backgroundImageListener);
-
- this.mathEnabledListener = mxUtils.bind(this, function(sender, evt)
- {
- if (!this.ignoreMathEnabledChanged)
- {
- try
- {
- this.setFileModified();
- this.getDiagramMap().set('mathEnabled', (this.graph.mathEnabled) ? '1' : '0');
- }
- catch (e)
- {
- this.ui.handleError(e);
- }
- }
- });
-
- this.ui.addListener('mathEnabledChanged', this.mathEnabledListener);
- };
- /**
- * Returns true if the given event is local.
- */
- DriveRealtime.prototype.isLocalEvent = function(evt)
- {
- return evt.isLocal;
- };
- /**
- * Syncs initial state from graph model to collab model.
- */
- DriveRealtime.prototype.resetUpdateStatusThread = function()
- {
- if (this.updateStatusThread != null)
- {
- window.clearInterval(this.updateStatusThread);
- }
-
- this.updateStatusThread = window.setInterval(mxUtils.bind(this, function()
- {
- this.ui.drive.checkToken(mxUtils.bind(this, function()
- {
- this.updateStatus();
- }));
- }), this.updateStatusInterval);
- };
- /**
- * Translates this point by the given vector.
- *
- * @param {number} dx X-coordinate of the translation.
- * @param {number} dy Y-coordinate of the translation.
- */
- DriveRealtime.prototype.timeSince = function(date)
- {
- var seconds = Math.floor((new Date() - date) / 1000);
-
- var interval = Math.floor(seconds / 31536000);
- if (interval > 1)
- {
- return interval + ' ' + mxResources.get('years');
- }
-
- interval = Math.floor(seconds / 2592000);
-
- if (interval > 1)
- {
- return interval + ' ' + mxResources.get('months');
- }
-
- interval = Math.floor(seconds / 86400);
-
- if (interval > 1)
- {
- return interval + ' ' + mxResources.get('days');
- }
-
- interval = Math.floor(seconds / 3600);
-
- if (interval > 1)
- {
- return interval + ' ' + mxResources.get('hours');
- }
-
- interval = Math.floor(seconds / 60);
-
- if (interval > 1)
- {
- return interval + ' ' + mxResources.get('minutes');
- }
-
- if (interval == 1)
- {
- return interval + ' ' + mxResources.get('minute');
- }
-
- return null;
- };
- /**
- * Adds the listener for added and removed cells in the collab model and maps
- * them to the graph model.
- */
- DriveRealtime.prototype.updateStatus = function()
- {
- if (!this.saving)
- {
- // LATER: Check if realtime model contains last modified timestamp
- var mod = this.root.get('modifiedDate');
-
- if (mod != '')
- {
- // LATER: Write out modified date for more than 2 weeks ago
- var str = this.ui.timeSince(new Date(mod));
-
- if (str == null)
- {
- str = mxResources.get('lessThanAMinute');
- }
-
- this.ui.editor.setStatus(mxResources.get('lastChange', [str]) +
- (this.file.isEditable() ? '' : ' (' + mxResources.get('readOnly') + ')'));
- }
- }
- };
- /**
- * Currently the link back from map to page requires a loop.
- */
- DriveRealtime.prototype.getPageIndexForMap = function(map)
- {
- for (var i = 0; i < this.ui.pages.length; i++)
- {
- if (this.ui.pages[i].mapping.diagramMap == map)
- {
- return i;
- }
- }
-
- return null;
- };
- /**
- * Adds the listener for changes on the graph model and maps them to the collab
- * model as a single transaction.
- */
- DriveRealtime.prototype.installPageSelectListener = function()
- {
- // Adds a graph model listener to update the view
- this.pageChangeListener = mxUtils.bind(this, function(sender, evt)
- {
- var page = evt.getProperty('change').relatedPage;
-
- if (page.mapping == null)
- {
- page.mapping = new RealtimeMapping(this, this.rtModel.createMap(), page);
- page.mapping.init();
-
- if (this.file.isEditable())
- {
- page.mapping.diagramMap.set('name', page.getName());
- }
- }
- });
-
- this.ui.editor.addListener('beforePageChange', this.pageChangeListener);
- // Adds a graph model listener to update the view
- this.viewStateListener = mxUtils.bind(this, function(sender, evt)
- {
- var page = this.getCurrentPage();
- // Applies view state from realtime model without firing events
- if (page.viewState == null)
- {
- // Activates from realtime without calling event listeners
- page.mapping.activate(true);
- }
- });
-
- this.ui.editor.addListener('setViewState', this.viewStateListener);
- this.diagrams.addEventListener(gapi.drive.realtime.EventType.VALUES_ADDED, mxUtils.bind(this, function(evt)
- {
- if (!this.isLocalEvent(evt))
- {
- if (evt.movedFromList == null)
- {
- this.ignoreChange = true;
-
- // Switches to pages datastructure
- if (this.ui.pages == null)
- {
- this.ui.fileNode = mxUtils.createXmlDocument().createElement('mxfile');
- this.ui.pages = [];
-
- if (this.page != null)
- {
- this.ui.currentPage = this.page;
- this.ui.pages.push(this.ui.currentPage);
- this.diagramMap = null;
- this.page = null;
- }
- }
-
- for (var i = 0; i < evt.values.length; i++)
- {
- var page = new DiagramPage(document.createElement('diagram'));
- page.mapping = new RealtimeMapping(this, evt.values[i], page);
- page.setName(page.mapping.diagramMap.get('name') || mxResources.get('pageWithNumber',
- [this.ui.pages.length + 1]));
- this.ui.pages.splice(evt.index + i, 0, page);
- page.mapping.init();
- }
-
- // Shows tab container if pages are added with pages disabled
- if (this.ui.pages != null && this.ui.pages.length > 1 &&
- this.ui.tabContainer != null &&
- this.ui.tabContainer.style.height == '0px')
- {
- this.ui.editor.graph.view.validateBackground();
- }
-
- this.ui.updateTabContainer();
- this.ignoreChange = false;
- }
- else if (evt.movedFromList == this.diagrams && evt.movedFromIndex != null)
- {
- this.ignoreChange = true;
-
- for (var i = 0; i < evt.values.length; i++)
- {
- var index = this.getPageIndexForMap(evt.values[i]);
-
- if (index != null)
- {
- this.ui.movePage(index + i, evt.index + i);
- }
- }
-
- this.ignoreChange = false;
- this.ui.updateTabContainer();
- }
- }
- }));
-
- this.diagrams.addEventListener(gapi.drive.realtime.EventType.VALUES_REMOVED, mxUtils.bind(this, function(evt)
- {
- if (!this.isLocalEvent(evt))
- {
- if (evt.movedToList == null)
- {
- this.ignoreChange = true;
-
- for (var i = 0; i < evt.values.length; i++)
- {
- var index = this.getPageIndexForMap(evt.values[i]);
-
- if (index != null)
- {
- var page = this.ui.pages[index];
-
- if (page != null)
- {
- this.ui.removePage(page);
- page.mapping.destroy();
- }
- }
- }
-
- this.ignoreChange = false;
- }
- }
- }));
- };
- /**
- * Returns a string representation of the given ops.
- */
- DriveRealtime.prototype.processChange = function(change)
- {
- //console.log('processChange: ' + this.dump(change));
- if (change instanceof RenamePage)
- {
- change.page.mapping.diagramMap.set('name', change.page.getName());
- }
- else if (change instanceof ChangePage)
- {
- if (change.previousIndex == null)
- {
- this.diagrams.removeValue(change.relatedPage.mapping.diagramMap);
- }
- else
- {
- this.diagrams.insert(change.previousIndex, change.relatedPage.mapping.diagramMap);
- }
- }
- else if (change instanceof MovePage)
- {
- this.diagrams.move(change.newIndex, change.oldIndex + ((change.newIndex < change.oldIndex) ? 1 : 0));
- }
- else if (change instanceof mxRootChange)
- {
- // Only process the root change that sets the current root
- // ie. ignore previous root changes
- if (change.root == this.model.root)
- {
- this.getCurrentPage().mapping.initRealtime();
- }
- }
- else if (change instanceof mxChildChange)
- {
- if (change.parent != change.previous || change.index != change.previousIndex)
- {
- var childRtCell = change.child.rtCell;
-
- if (childRtCell == null)
- {
- childRtCell = this.getCurrentPage().mapping.createRealtimeCell(change.child);
- this.getCurrentPage().mapping.saveRealtimeCell(childRtCell.cell);
- }
- var parentRtCell = (change.parent != null) ? change.parent.rtCell : null;
-
- if (change.previous != null)
- {
- var previousParentRtCell = change.previous.rtCell;
-
- if (previousParentRtCell != null)
- {
- previousParentRtCell.children.removeValue(childRtCell);
- }
- }
- if (parentRtCell != null)
- {
- parentRtCell.children.insert(Math.min(parentRtCell.children.length, change.index), childRtCell);
- }
-
- childRtCell.parent = parentRtCell;
- }
- }
- else if (change.cell != null && change.cell.id != null)
- {
- var rtCell = change.cell.rtCell;
-
- if (rtCell != null)
- {
- if (change instanceof mxTerminalChange)
- {
- var term = (change.terminal != null) ? change.terminal.rtCell : null;
- rtCell[(change.source) ? 'source' : 'target'] = term;
- }
- else if (change instanceof mxGeometryChange)
- {
- rtCell.geometry = (change.geometry != null) ? mxUtils.getXml(this.codec.encode(change.geometry)) : null;
- }
- else if (change instanceof mxStyleChange)
- {
- rtCell.style = change.style;
- }
- else if (change instanceof mxValueChange)
- {
- if (mxUtils.isNode(change.value))
- {
- rtCell.value = null;
- rtCell.xmlValue = mxUtils.getXml(change.value);
- }
- else
- {
- rtCell.xmlValue = null;
- rtCell.value = change.value;
- }
- }
- else if (change instanceof mxCollapseChange)
- {
- rtCell.collapsed = (change.collapsed) ? '1' : '0';
- }
- else if (change instanceof mxVisibleChange)
- {
- rtCell.visible = (change.visible) ? '1' : '0';
- }
- }
- }
- };
- /**
- * Adds the listener for changes on the graph model and maps them to the collab
- * model as a single transaction.
- */
- DriveRealtime.prototype.setFileModified = function()
- {
- this.root.set('modifiedDate', new Date().getTime());
- this.file.setModified(true);
-
- if (!this.saving)
- {
- this.ui.editor.setStatus(mxResources.get('saving') + '...');
- this.saving = true;
- }
- };
- /**
- * Adds the listener for changes on the graph model and maps them to the collab
- * model as a single transaction.
- */
- DriveRealtime.prototype.installGraphModelListener = function()
- {
- // Adds a graph model listener to update the view
- this.graphModelChangeListener = mxUtils.bind(this, function(sender, evt)
- {
- var edit = evt.getProperty('edit');
-
- if (!this.ignoreChange && this.file.isEditable() && !edit.ignoreEdit)
- {
- //console.log('startEdit');
- // TODO: Queuing for async token refresh and too many updates
- this.ui.drive.checkToken(mxUtils.bind(this, function()
- {
- this.rtModel.beginCompoundOperation();
- this.setFileModified();
-
- try
- {
- var changes = edit.changes;
-
- if (edit.undone)
- {
- for (var i = changes.length - 1; i >= 0; i--)
- {
- this.processChange(changes[i]);
- }
- }
- else
- {
- for (var i = 0; i < changes.length; i++)
- {
- this.processChange(changes[i]);
- }
- }
-
- this.rtModel.endCompoundOperation();
- }
- catch (e)
- {
- this.rtModel.endCompoundOperation();
- this.ui.handleError(e);
- }
-
- if (this.isAliveThread == null)
- {
- this.isAliveThread = window.setTimeout(mxUtils.bind(this, function()
- {
- this.ui.editor.setStatus('<div class="geStatusAlert geBlink" style="cursor:pointer;">' +
- mxResources.get('noResponse') + '</div>');
-
- this.isAliveThread = window.setTimeout(mxUtils.bind(this, function()
- {
- this.isAliveThread = null;
- this.timeoutError();
- }), this.realtimeHeartbeat);
- }), this.realtimeHeartbeat);
- }
- }));
-
- //console.log('endEdit');
- }
- });
-
- this.model.addListener(mxEvent.CHANGE, this.graphModelChangeListener);
- };
- /**
- *
- */
- DriveRealtime.prototype.sessionExpiredError = function()
- {
- // LATER: How to reload realtime document without refreshing the page
- this.ui.showError(mxResources.get('error'), mxResources.get('sessionExpired'), mxResources.get('refresh'), mxUtils.bind(this, function()
- {
- this.ui.spinner.spin(document.body, mxResources.get('connecting'));
- this.file.setModified(false);
- window.location.reload();
- }));
- };
- /**
- *
- */
- DriveRealtime.prototype.timeoutError = function()
- {
- // LATER: How to reload realtime document without refreshing the page
- this.ui.showError(mxResources.get('timeout'), mxResources.get('realtimeTimeout'), mxResources.get('discardChangesAndReconnect'), mxUtils.bind(this, function()
- {
- this.ui.spinner.spin(document.body, mxResources.get('connecting'));
- this.file.setModified(false);
- window.location.reload();
- }), null, mxResources.get('ignore'), mxUtils.bind(this, function()
- {
- this.ui.editor.setStatus('<div class="geStatusAlert geBlink" style="cursor:pointer;">' +
- mxResources.get('disconnected') + '</div>');
- this.realtimeHeartbeat *= 2;
- }));
- };
- /**
- *
- */
- DriveRealtime.prototype.initializeChat = function()
- {
- this.chatHistory = this.rtModel.createList();
- this.root.set('chatHistory', this.chatHistory);
- //this.log('Chat history created');
- };
- /**
- * Adds a listener to the graph selection model and writes changes
- * in the RT selection map
- */
- DriveRealtime.prototype.installSelectionModelListener = function()
- {
- this.graphSelectionModelChangeListener = mxUtils.bind(this, function(sender, evt)
- {
- this.ui.drive.checkToken(mxUtils.bind(this, function()
- {
- if (this.file.isEditable() && !this.graph.isSelectionEmpty())
- {
- try
- {
- // LATER: Clear value if selection is empty. This is currently
- // disabled to avoid using up the quota in this case but the
- // better solution is to mark this "transient" (ie no history).
- var cells = this.graph.getSelectionCells();
- var selectedCellIds = '';
-
- for (var i = 0; i < cells.length; i++)
- {
- selectedCellIds += cells[i].id + ',';
- }
-
- this.getCurrentPage().mapping.selectionMap.set(this.userId, selectedCellIds);
- }
- catch (e)
- {
- this.ui.handleError(e);
- }
- }
- }));
- });
-
- this.graph.getSelectionModel().addListener(mxEvent.CHANGE, this.graphSelectionModelChangeListener);
- };
- /**
- * Connects the collaborator event listeners to the draw function.
- */
- DriveRealtime.prototype.installCollaboratorListener = function()
- {
- this.doc.addEventListener(gapi.drive.realtime.EventType.COLLABORATOR_JOINED, mxUtils.bind(this, this.updateCollaborators));
- this.doc.addEventListener(gapi.drive.realtime.EventType.COLLABORATOR_LEFT, mxUtils.bind(this, this.updateCollaborators));
- };
- /**
- * Draw function for the collaborator list.
- */
- DriveRealtime.prototype.updateCollaborators = function()
- {
- var n = this.doc.getCollaborators().length - 1;
-
- if (this.collaboratorsElement == null)
- {
- this.collaboratorsElement = document.createElement('a');
- this.collaboratorsElement.setAttribute('href', 'javascript:void(0);');
- this.collaboratorsElement.className = 'geItem';
- this.collaboratorsElement.style.position = 'absolute';
- this.collaboratorsElement.style.display = 'inline-block';
- this.collaboratorsElement.style.verticalAlign = 'bottom';
- this.collaboratorsElement.style.color = '#666';
- this.collaboratorsElement.style.top = '10px';
- this.collaboratorsElement.style.right = (uiTheme == 'atlas') ? '42px' : '68px';
- this.collaboratorsElement.style.padding = '2px';
- this.collaboratorsElement.style.fontSize = '8pt';
- this.collaboratorsElement.style.verticalAlign = 'middle';
- this.collaboratorsElement.style.backgroundPosition = '100% 60%';
- this.collaboratorsElement.style.backgroundRepeat = 'no-repeat';
-
- this.ui.toolbarContainer.appendChild(this.collaboratorsElement);
-
- mxEvent.addListener(this.collaboratorsElement, 'click', mxUtils.bind(this, function(evt)
- {
- if (this.collabPanel == null)
- {
- var div = document.createElement('div');
- div.className = 'geDialog';
- div.style.position = 'absolute';
- div.style.maxHeight = '400px';
- div.style.maxWidth = '300px';
- div.style.right = '38px';
- div.style.padding = '14px';
- div.style.paddingRight = '30px';
- this.collabPanel = div;
- }
-
- if (this.collabPanel.parentNode == null)
- {
- this.collabPanel.style.top = (this.collaboratorsElement.clientTop + this.collaboratorsElement.clientHeight + this.ui.menubarHeight + 8) + 'px';
- document.body.appendChild(this.collabPanel);
- this.collabPanel.innerHTML = '';
-
- var img = document.createElement('img');
- img.setAttribute('src', Dialog.prototype.closeImage);
- img.setAttribute('title', mxResources.get('close'));
- img.className = 'geDialogClose';
- img.style.top = '8px';
- img.style.right = '8px';
-
- mxEvent.addListener(img, 'click', mxUtils.bind(this, function()
- {
- if (this.collabPanel.parentNode != null)
- {
- this.collabPanel.parentNode.removeChild(this.collabPanel);
- }
- }));
-
- this.collabPanel.appendChild(img);
-
- if (this.doc.getCollaborators().length > 1)
- {
- for (var i = 0; i < this.doc.getCollaborators().length; i = i + 1)
- {
- var collaborator = this.doc.getCollaborators()[i];
-
- if (!collaborator.isMe)
- {
- var elt = document.createElement('div');
- elt.style.cursor = 'pointer';
- elt.style.whiteSpace = 'nowrap';
-
- var img = document.createElement('img');
- img.src = collaborator.photoUrl;
- img.style.backgroundColor = collaborator.color;
- img.style.paddingBottom = '8px';
- img.style.marginRight = '10px';
- img.style.marginTop = '2px';
- img.style.marginBottom = '2px';
- img.style.height = '25px';
- img.style.width = '25px';
- img.setAttribute('align', 'absmiddle');
-
- elt.appendChild(img);
- mxUtils.write(elt, collaborator.displayName);
-
- this.collabPanel.appendChild(elt);
-
- // Click on collaborator shows selection cells, scrolls to first cell
- mxEvent.addListener(elt, 'click', mxUtils.bind(this, function()
- {
- var value = this.getCurrentPage().mapping.selectionMap.get(collaborator.userId);
-
- if (value != null)
- {
- var cellIds = value.split(',');
-
- for (var i = 0; i < cellIds.length; i++)
- {
- var cell = this.model.getCell(cellIds[i]);
- this.highlight(cell, collaborator.sessionId);
-
- if (cell != null && i == 0)
- {
- this.graph.scrollCellToVisible(cell);
- }
- }
- }
- }));
- }
- }
- }
- else
- {
- mxUtils.write(this.collabPanel, mxResources.get('noOtherViewers'));
- }
- }
- else if (this.collabPanel.parentNode != null)
- {
- this.collabPanel.parentNode.removeChild(this.collabPanel);
- }
-
- mxEvent.consume(evt);
- }));
-
- mxEvent.addListener(document.body, 'click', mxUtils.bind(this, function(evt)
- {
- if (!mxEvent.isConsumed(evt) && this.collabPanel != null && this.collabPanel.parentNode != null)
- {
- this.collabPanel.parentNode.removeChild(this.collabPanel);
- }
- }));
- }
-
- var viewers = mxResources.get((n == 0) ? 'noOtherViewers' : ((n == 1) ? 'otherViewer' : 'otherViewers'));
-
- if (n > 0)
- {
- viewers = n + ' ' + viewers;
- this.collaboratorsElement.style.paddingRight = '16px';
- this.collaboratorsElement.style.backgroundImage = 'url(' + IMAGE_PATH + '/expanded.gif)';
- this.collaboratorsElement.style.cursor = 'pointer';
- }
- else
- {
- this.collaboratorsElement.style.paddingRight = '0px';
- this.collaboratorsElement.style.backgroundImage = '';
- this.collaboratorsElement.style.cursor = 'default';
- }
-
- var html = '<div title="' + viewers + '" style="display:inline-block;white-space:nowrap;max-width:110px;overflow:hidden;text-overflow:ellipsis;">' + viewers + '</div>';
-
- var names = new Object();
- var count = 0;
-
- for (var i = 0; i < this.doc.getCollaborators().length && count < 6; i = i + 1)
- {
- var c = this.doc.getCollaborators()[i];
-
- if (!c.isMe && names[c.color] == null)
- {
- names[c.color] = c.displayName;
- count++;
- }
- }
-
- for (var color in names)
- {
- html += '<div title="' + mxUtils.htmlEntities(names[color]) + '" style="display:inline-block;background-color:' + color + ';width:13px;height:13px;margin-left:4px;margin-top:-1px;"></div>';
- }
-
- this.collaboratorsElement.innerHTML = html;
- };
- /**
- * Creates and returns a prefix for cell IDs.
- */
- DriveRealtime.prototype.createPrefix = function()
- {
- var collabs = this.doc.getCollaborators();
-
- for (var i = 0; i < collabs.length; i++)
- {
- if (collabs[i]['isMe'])
- {
- return collabs[i]['sessionId'];
- }
- }
-
- return '';
- };
- DriveRealtime.prototype.highlight = function(cell, sessionId)
- {
- var state = this.graph.view.getState(cell);
-
- if (state != null)
- {
- var color = 'red'; // session not found
-
- for (var i = 0; i < this.doc.getCollaborators().length; i = i + 1)
- {
- var collaborator = this.doc.getCollaborators()[i];
- if (collaborator.sessionId == sessionId)
- {
- color = collaborator.color;
-
- break;
- }
- }
- var sw = Math.max(5, mxUtils.getValue(state.style, mxConstants.STYLE_STROKEWIDTH, 1) + 4);
- var hl = new mxCellHighlight(this.graph, color, sw, false);
- hl.highlight(state);
-
- // Fades out the highlight after a delay
- window.setTimeout(function()
- {
- if (hl.shape != null)
- {
- mxUtils.setPrefixedStyle(hl.shape.node.style, 'transition', 'all 1200ms ease-in-out');
- hl.shape.node.style.opacity = 0;
- }
-
- window.setTimeout(function()
- {
- hl.destroy();
- }, 2000);
- }, 1000);
- }
- };
- /**
- * Writes out a string representing the current state of the document.
- */
- DriveRealtime.prototype.dumpRoot = function()
- {
- return this.dump(this.root);
- };
- /**
- * Creates a dump of the given map.
- */
- DriveRealtime.prototype.dump = function(obj)
- {
- var result = '';
-
- if (obj != null)
- {
- if (obj.constructor == mxCell)
- {
- return obj.id;
- }
- else if (obj.constructor == mxRootChange)
- {
- result += 'mxRootChange[root=' + this.dump(obj.root) + ']';
- }
- else if (obj.constructor == mxChildChange)
- {
- result += 'mxChildChange[parent=' + this.dump(obj.parent) +
- ', child=' + this.dump(obj.child.id) +
- ', index=' + obj.index + ']';
- }
- else if (obj.constructor == mxTerminalChange)
- {
- result += 'mxTerminalChange[cell=' + this.dump(obj.cell) +
- ', terminal=' + this.dump(obj.terminal) +
- ', source=' + obj.source + ']';
- }
- else if (obj.constructor == mxValueChange)
- {
- result += 'mxValueChange[cell=' + this.dump(obj.cell) + ', value=' + obj.value + ']';
- }
- else if (obj.constructor == mxGeometryChange)
- {
- result += 'mxGeometryChange[cell=' + this.dump(obj.cell) + ', geometry=' +
- ((obj.cell.geometry != null) ? mxUtils.getXml(this.codec.encode(obj.cell.geometry)) : '[null]') + ']';
- }
- else if (obj.constructor == mxStyleChange)
- {
- result += 'mxStyleChange[cell=' + this.dump(obj.cell) + ', style=' + obj.style + ']';
- }
- else if (obj.constructor == mxVisibleChange)
- {
- result += 'mxVisibleChange[cell=' + this.dump(obj.cell) + ', visible=' + obj.visible + ']';
- }
- else if (obj.constructor == mxCollapseChange)
- {
- result += 'mxCollapseChange[cell=' + this.dump(obj.cell) + ', collapsed=' + obj.collapsed + ']';
- }
- else if (obj.constructor == mxRtCell)
- {
- result += '[id=' + obj.cellId + ',parent=' + ((obj.parent != null) ? obj.parent.cellId : '[null]');
-
- if (obj.children.length > 0)
- {
- result += ',\n' + obj.children.length + ' children=[' +
- this.dump(obj.children.get(0));
-
- for (var i = 1; i < obj.children.length; i++)
- {
- result += ',' + this.dump(obj.children.get(i));
- }
-
- result += ']';
- }
-
- result += ']\n';
- }
- else if (obj.keys != null)
- {
- var keys = obj.keys();
- result += '{\n';
-
- for (var i = 0; i < keys.length; i++)
- {
- result += keys[i] + '=' + this.dump(obj.get(keys[i])) + ';\n';
- }
-
- result += '}';
- }
- else if (obj.asArray != null)
- {
- var arr = obj.asArray();
- result += '[';
-
- for (var i = 0; i < arr.length; i++)
- {
- result += arr[i] + ';';
- }
-
- result += ']';
- }
- else
- {
- result = obj;
- }
- }
- else
- {
- result = 'null';
- }
-
- return result;
- };
- /**
- * Writes the given text to the log if <logging> is enabled.
- */
- DriveRealtime.prototype.check = function()
- {
- console.log('checking consistency');
- this.checkChildren(this.model.getRoot());
- console.log('checking consistency done');
- };
- /**
- * Writes the given text to the log if <logging> is enabled.
- */
- DriveRealtime.prototype.checkChildren = function(cell)
- {
- if (cell.rtCell == null)
- {
- console.log('no realtime cell', 'cell', cell.id);
- }
- else
- {
- if (cell.getTerminal(true) != null && (cell.rtCell.source == null ||
- cell.rtCell.source != cell.getTerminal(true).rtCell))
- {
- console.log('invalid source', 'edge', cell.id, 'source',
- cell.getTerminal(true).id, 'rtSource', cell.rtCell.source);
- }
-
- if (cell.getTerminal(false) != null && (cell.rtCell.target == null ||
- cell.rtCell.target != cell.getTerminal(false).rtCell))
- {
- console.log('invalid target', 'edge', cell.id, 'target',
- cell.getTerminal(false).id, 'rtTarget', cell.rtCell.target);
- }
-
- var childCount = this.model.getChildCount(cell);
- var children = cell.rtCell.children.asArray();
-
- if (childCount != cell.rtCell.children.length)
- {
- console.log('invalid child count', 'cell', cell.id, 'children',
- children.length, 'childCount', childCount);
- }
-
- for (var i = 0; i < cell.rtCell.children.length; i++)
- {
- var child = this.model.getChildAt(cell, i);
-
- if (child == null)
- {
- console.log('no child', 'index', i, 'child', cell.rtCell.children.get(i));
- }
- else if (cell.rtCell.children.get(i) != child.rtCell)
- {
- console.log('invalid child', 'index', i, 'child',
- cell.rtCell.children.get(i), 'graphChild', child);
- }
- }
- }
-
- for (var i = 0; i < childCount; i++)
- {
- var child = this.model.getChildAt(cell, i);
- this.checkChildren(child);
- }
- };
- /**
- * Writes the given text to the log if <logging> is enabled.
- */
- DriveRealtime.prototype.log = function(message)
- {
- if (this.logLevel > 1)
- {
- //mxLog.debug.apply(mxLog, arguments);
- //console.log(message);
- }
- };
- /**
- * Writes the given text to the log if <logging> is enabled.
- */
- DriveRealtime.prototype.warn = function(message)
- {
- if (this.logLevel > 0)
- {
- //mxLog.debug.apply(mxLog, arguments);
- //console.log(message);
- }
- };
- /**
- * Destroys the instance and removes all listeners.
- */
- DriveRealtime.prototype.destroy = function(unloading)
- {
- unloading = (unloading != null) ? unloading : false;
-
- if (this.pageFormatListener != null)
- {
- this.ui.removeListener(this.pageFormatListener);
- this.pageFormatListener = null;
- }
-
- if (this.pageScaleListener != null)
- {
- this.ui.removeListener(this.pageScaleListener);
- this.pageScaleListener = null;
- }
- if (this.backgroundColorListener != null)
- {
- this.ui.removeListener(this.backgroundColorListener);
- this.backgroundColorListener = null;
- }
-
- if (this.shadowVisibleListener != null)
- {
- this.graph.removeListener(this.shadowVisibleListener);
- this.shadowVisibleListener = null;
- }
- if (this.foldingEnabledListener != null)
- {
- this.ui.removeListener(this.foldingEnabledListener);
- this.foldingEnabledListener = null;
- }
- if (this.pageVisibleListener != null)
- {
- this.ui.removeListener(this.pageVisibleListener);
- this.pageVisibleListener = null;
- }
- if (this.backgroundImageListener != null)
- {
- this.ui.removeListener(this.backgroundImageListener);
- this.backgroundImageListener = null;
- }
- if (this.mathEnabledListener != null)
- {
- this.ui.removeListener(this.mathEnabledListener);
- this.mathEnabledListener = null;
- }
-
- if (this.previousUndoListener != null)
- {
- this.ui.editor.undoListener = this.previousUndoListener;
- this.previousUndoListener = null;
- }
- if (this.graphSelectionModelChangeListener != null)
- {
- this.graph.selectionModel.removeListener(this.graphSelectionModelChangeListener);
- this.graphSelectionModelChangeListener = null;
- }
-
- if (this.disconnectListener != null)
- {
- this.ui.drive.removeListener(this.disconnectListener);
- this.disconnectListener = null;
- }
-
- if (this.autosaveChangeListener != null)
- {
- this.ui.editor.removeListener(this.autosaveChangeListener);
- this.autosaveChangeListener = null;
- }
-
- if (this.graphModelChangeListener != null)
- {
- this.model.removeListener(this.graphModelChangeListener);
- this.graphModelChangeListener = null;
- }
-
- if (this.pageChangeListener != null)
- {
- this.ui.editor.removeListener(this.pageChangeListener);
- this.pageChangeListener = null;
- }
- if (this.viewStateListener != null)
- {
- this.ui.editor.removeListener(this.viewStateListener);
- this.viewStateListener = null;
- }
-
- if (this.collaboratorsElement != null)
- {
- this.collaboratorsElement.parentNode.removeChild(this.collaboratorsElement);
- this.collaboratorsElement = null;
- }
-
- if (this.updateStatusThread != null)
- {
- window.clearInterval(this.updateStatusThread);
- this.updateStatusThread = null;
- }
-
- this.ui.allowAnimation = true;
- try
- {
- // KNOWN: Cannot access g of null error in realtime
- // when called from window.onBeforeUnload handler
- if (!unloading && !this.doc.isClosed)
- {
- this.doc.close();
- }
- }
- catch (e)
- {
- // ignores possible document closed errors
- }
- };
|