1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095 |
- /**
- * Copyright (c) 2006-2017, JGraph Ltd
- * Copyright (c) 2006-2017, Gaudenz Alder
- */
- DrawioFile = function(ui, data)
- {
- mxEventSource.call(this);
-
- /**
- * Holds the x-coordinate of the point.
- * @type number
- * @default 0
- */
- this.ui = ui;
-
- /**
- * Holds the x-coordinate of the point.
- * @type number
- * @default 0
- */
- this.data = data || '';
- this.shadowData = this.data;
- this.shadowPages = null;
-
- // Creates the stats object
- this.stats = {
- opened: 0, /* number of calls to open */
- merged: 0, /* number of calls to merge */
- fileMerged: 0, /* number of calls to mergeFile */
- fileReloaded: 0, /* number of calls to mergeFile */
- conflicts: 0, /* number of write conflicts when saving a file */
- timeouts: 0, /* number of time we have given up to retry after a write conflict */
- saved: 0, /* number of calls to fileSaved */
- closed: 0, /* number of calls to close */
- destroyed: 0, /* number of calls to close */
- joined: 0, /* number of join messages received */
- checksumErrors: 0, /* number of checksum errors */
- bytesSent: 0, /* number of bytes send in messages */
- bytesReceived: 0, /* number of bytes received in messages */
- msgSent: 0, /* number of messages sent */
- msgReceived: 0, /* number of messages received */
- cacheHits: 0, /* number of times the cache returned patches */
- cacheMiss: 0, /* number of times we have missed a cache entry */
- cacheFail: 0 /* number of times we have failed to read the cache */
- };
- };
- /**
- * Global switch for realtime collaboration type to use sync URL parameter
- * with the following possible values:
- *
- * - none: overwrite
- * - manual: manual sync
- * - auto: automatic sync
- */
- DrawioFile.SYNC = urlParams['sync'] || 'auto';
- /**
- * Specifies if last write wins should be used for values and styles.
- */
- DrawioFile.LAST_WRITE_WINS = true;
- // Extends mxEventSource
- mxUtils.extend(DrawioFile, mxEventSource);
- /**
- * Delay for last save in ms.
- */
- DrawioFile.prototype.allChangesSavedKey = 'allChangesSaved';
- /**
- * Specifies the delay between the last change and the autosave.
- */
- DrawioFile.prototype.autosaveDelay = 1500;
- /**
- * Specifies the maximum delay before an autosave is forced even if the graph
- * is being changed.
- */
- DrawioFile.prototype.maxAutosaveDelay = 30000;
- /**
- * Contains the thread for the next autosave.
- */
- DrawioFile.prototype.autosaveThread = null;
- /**
- * Stores the timestamp for hte last autosave.
- */
- DrawioFile.prototype.lastAutosave = null;
- /**
- * Stores the timestamp for hte last autosave.
- */
- DrawioFile.prototype.lastSaved = null;
- /**
- * Stores the timestamp for hte last autosave.
- */
- DrawioFile.prototype.lastWarned = null;
- /**
- * Interal to show dialog for unsaved data with autosave.
- * Default is 600000 (10 minutes).
- */
- DrawioFile.prototype.warnInterval = 600000;
- /**
- * Stores the modified state.
- */
- DrawioFile.prototype.modified = false;
- /**
- * Holds a copy of the current file data.
- */
- DrawioFile.prototype.data = null;
- /**
- * Holds a copy of the last saved file data.
- */
- DrawioFile.prototype.shadowData = null;
- /**
- * Holds a copy of the parsed last saved file data.
- */
- DrawioFile.prototype.shadowPages = null;
- /**
- * Specifies if the graph change listener is enabled. Default is true.
- */
- DrawioFile.prototype.changeListenerEnabled = true;
- /**
- * Sets the delay for autosave in milliseconds. Default is 1500.
- */
- DrawioFile.prototype.lastAutosaveRevision = null;
- /**
- * Sets the delay between revisions when using autosave. Default is 300000
- * ie 5 mins. Set this to 0 to create a revision on every autosave.
- */
- DrawioFile.prototype.maxAutosaveRevisionDelay = 300000;
- /**
- * Specifies if notify events should be ignored.
- */
- DrawioFile.prototype.inConflictState = false;
- /**
- * Specifies if notify events should be ignored.
- */
- DrawioFile.prototype.invalidChecksum = false;
- /**
- * Specifies if error reports should be sent.
- */
- DrawioFile.prototype.errorReportsEnabled = false;
- /**
- * Specifies if stats should be sent.
- */
- DrawioFile.prototype.reportEnabled = true;
- /**
- * Specifies if notify events should be ignored.
- */
- DrawioFile.prototype.getSize = function()
- {
- return (this.data != null) ? this.data.length : 0;
- };
- /**
- * Adds the listener for automatically saving the diagram for local changes.
- */
- DrawioFile.prototype.synchronizeFile = function(success, error)
- {
- if (this.savingFile)
- {
- if (error != null)
- {
- error({message: mxResources.get('busy')});
- }
- }
- else
- {
- if (this.sync != null)
- {
- this.sync.fileChanged(success, error);
- }
- else
- {
- this.updateFile(success, error);
- }
- }
- };
- /**
- * Adds the listener for automatically saving the diagram for local changes.
- */
- DrawioFile.prototype.updateFile = function(success, error, abort, shadow)
- {
- if (abort == null || !abort())
- {
- if (this.ui.getCurrentFile() != this || this.invalidChecksum)
- {
- if (error != null)
- {
- error();
- }
- }
- else
- {
- this.getLatestVersion(mxUtils.bind(this, function(latestFile)
- {
- try
- {
- if (abort == null || !abort())
- {
- if (this.ui.getCurrentFile() != this || this.invalidChecksum)
- {
- if (error != null)
- {
- error();
- }
- }
- else
- {
- if (latestFile != null)
- {
- this.mergeFile(latestFile, success, error, shadow);
- }
- else
- {
- this.reloadFile(success, error);
- }
- }
- }
- }
- catch (e)
- {
- if (error != null)
- {
- error(e);
- }
- }
- }), error);
- }
- }
- };
- /**
- * Adds the listener for automatically saving the diagram for local changes.
- */
- DrawioFile.prototype.mergeFile = function(file, success, error, diffShadow)
- {
- var reportError = true;
-
- try
- {
- this.stats.fileMerged++;
-
- // Takes copy of current shadow document
- var shadow = (this.shadowPages != null) ? this.shadowPages :
- this.ui.getPagesForNode(mxUtils.parseXml(
- this.shadowData).documentElement);
-
- // Loads new document as shadow document
- var pages = this.ui.getPagesForNode(
- mxUtils.parseXml(file.data).
- documentElement)
-
- if (pages != null && pages.length > 0)
- {
- this.shadowPages = pages;
- // Creates a patch for backup if the checksum fails
- this.backupPatch = (this.isModified()) ?
- this.ui.diffPages(shadow,
- this.ui.pages) : null;
-
- // Patches the current document
- var patches = [this.ui.diffPages((diffShadow != null) ?
- diffShadow : shadow, this.shadowPages)];
- var ignored = this.ignorePatches(patches);
-
- if (!ignored)
- {
- // Patching previous shadow to verify checksum
- var patched = this.ui.patchPages(shadow, patches[0]);
-
- var patchedDetails = {};
- var checksum = this.ui.getHashValueForPages(patched, patchedDetails);
- var currentDetails = {};
- var current = this.ui.getHashValueForPages(this.shadowPages, currentDetails);
-
- if (urlParams['test'] == '1')
- {
- EditorUi.debug('File.mergeFile', [this],
- 'backup', this.backupPatch,
- 'patches', patches,
- 'checksum', current == checksum, checksum);
- }
-
- if (checksum != null && checksum != current)
- {
- var fileData = this.compressReportData(this.getAnonymizedXmlForPages(pages));
- var data = this.compressReportData(this.getAnonymizedXmlForPages(patched));
- var from = this.ui.hashValue(file.getCurrentEtag());
- var to = this.ui.hashValue(this.getCurrentEtag());
-
- this.checksumError(error, patches,
- 'Shadow Details: ' + JSON.stringify(patchedDetails) +
- '\nChecksum: ' + checksum +
- '\nCurrent: ' + current +
- '\nCurrent Details: ' + JSON.stringify(currentDetails) +
- '\nFrom: ' + from +
- '\nTo: ' + to +
- '\n\nFile Data:\n' + fileData +
- '\nPatched Shadow:\n' + data, null, 'mergeFile');
-
- // Abnormal termination
- return;
- }
- else
- {
- // Patches the current document
- this.patch(patches,
- (DrawioFile.LAST_WRITE_WINS) ?
- this.backupPatch : null);
- }
- }
- }
- else
- {
- reportError = false;
- throw new Error(mxResources.get('notADiagramFile'));
- }
-
- this.invalidChecksum = false;
- this.inConflictState = false;
- this.setDescriptor(file.getDescriptor());
- this.descriptorChanged();
- this.backupPatch = null;
-
- if (success != null)
- {
- success();
- }
- }
- catch (e)
- {
- this.inConflictState = true;
- this.invalidChecksum = true;
- this.descriptorChanged();
-
- if (error != null)
- {
- error(e);
- }
- try
- {
- if (reportError)
- {
- if (this.errorReportsEnabled)
- {
- this.sendErrorReport('Error in mergeFile', null, e);
- }
- else
- {
- var user = this.getCurrentUser();
- var uid = (user != null) ? user.id : 'unknown';
-
- EditorUi.logError('Error in mergeFile', null,
- this.getMode() + '.' + this.getId(),
- uid, e);
- }
- }
- }
- catch (e2)
- {
- // ignore
- }
- }
- };
- /**
- * Adds the listener for automatically saving the diagram for local changes.
- */
- DrawioFile.prototype.getAnonymizedXmlForPages = function(pages)
- {
- var enc = new mxCodec(mxUtils.createXmlDocument());
- var file = enc.document.createElement('mxfile');
-
- if (pages != null)
- {
- for (var i = 0; i < pages.length; i++)
- {
- var temp = enc.encode(new mxGraphModel(pages[i].root));
-
- if (urlParams['dev'] != '1')
- {
- temp = this.ui.anonymizeNode(temp, true);
- }
-
- temp.setAttribute('id', pages[i].getId());
-
- if (pages[i].viewState)
- {
- this.ui.editor.graph.saveViewState(pages[i].viewState, temp, true);
- }
-
- file.appendChild(temp);
- }
- }
- return mxUtils.getPrettyXml(file);
- };
- /**
- * Adds the listener for automatically saving the diagram for local changes.
- */
- DrawioFile.prototype.compressReportData = function(data, limit, max)
- {
- limit = (limit != null) ? limit : 10000;
- if (max != null && data != null && data.length > max)
- {
- data = data.substring(0, max) + '[...]';
- }
- else if (data != null && data.length > limit)
- {
- data = Graph.compress(data) + '\n';
- }
- return data;
- };
- /**
- * Adds the listener for automatically saving the diagram for local changes.
- */
- DrawioFile.prototype.checksumError = function(error, patches, details, etag, functionName)
- {
- this.stats.checksumErrors++;
- this.inConflictState = true;
- this.invalidChecksum = true;
- this.descriptorChanged();
-
- if (this.sync != null)
- {
- this.sync.updateOnlineState();
- }
- if (error != null)
- {
- error();
- }
-
- try
- {
- if (this.errorReportsEnabled)
- {
- if (patches != null)
- {
- for (var i = 0; i < patches.length; i++)
- {
- this.ui.anonymizePatch(patches[i]);
- }
- }
-
- var fn = mxUtils.bind(this, function(file)
- {
- var json = this.compressReportData(
- JSON.stringify(patches, null, 2));
- var remote = (file != null) ? this.compressReportData(
- this.getAnonymizedXmlForPages(
- this.ui.getPagesForNode(
- mxUtils.parseXml(file.data).documentElement)), 25000) : 'n/a';
-
- this.sendErrorReport('Checksum Error in ' + functionName + ' ' + this.getHash(),
- ((details != null) ? (details) : '') + '\n\nPatches:\n' + json +
- ((remote != null) ? ('\n\nRemote:\n' + remote) : ''), null, 70000);
- });
-
- if (etag == null)
- {
- fn(null);
- }
- else
- {
- this.getLatestVersion(mxUtils.bind(this, function(file)
- {
- if (file != null && file.getCurrentEtag() == etag)
- {
- fn(file);
- }
- else
- {
- fn(null);
- }
- }), function() {});
- }
- }
- else
- {
- var user = this.getCurrentUser();
- var uid = (user != null) ? user.id : 'unknown';
-
- EditorUi.logError('Checksum Error in ' + functionName + ' ' + this.getId(),
- null, this.getMode() + '.' + this.getId(), uid + '.' +
- ((this.sync != null) ? this.sync.clientId : 'nosync'));
-
- // Logs checksum error for file
- try
- {
- EditorUi.logEvent({category: 'CHECKSUM-ERROR-SYNC-FILE-' + this.getHash(),
- action: functionName, label: uid + '.' +
- ((this.sync != null) ? this.sync.clientId : 'nosync')});
- }
- catch (e)
- {
- // ignore
- }
- }
- }
- catch (e)
- {
- // ignore
- }
- };
- /**
- * Adds the listener for automatically saving the diagram for local changes.
- */
- DrawioFile.prototype.sendErrorReport = function(title, details, error, max)
- {
- try
- {
- var shadow = this.compressReportData(
- this.getAnonymizedXmlForPages(
- this.shadowPages), 25000);
- var data = this.compressReportData(
- this.getAnonymizedXmlForPages(
- this.ui.pages), 25000);
- var user = this.getCurrentUser();
- var uid = (user != null) ? this.ui.hashValue(user.id) : 'unknown';
- var cid = (this.sync != null) ? this.sync.clientId : 'no sync';
- var filename = this.getTitle();
- var dot = filename.lastIndexOf('.');
- var ext = 'xml';
-
- if (dot > 0)
- {
- ext = filename.substring(dot);
- }
-
- var stack = (error != null) ? error.stack : new Error().stack;
-
- EditorUi.sendReport(title + ' ' + new Date().toISOString() + ':' +
- '\n\nBrowser=' + navigator.userAgent +
- '\nFile=' + this.ui.hashValue(this.getId()) + ' (' + this.getMode() + ')' +
- ((this.isModified()) ? ' modified' : '') +
- '\nSize/Type=' + this.getSize() + ' (' + ext + ')' +
- '\nUser=' + uid + ' (' + cid + ')' +
- '\nPrefix=' + this.ui.editor.graph.model.prefix +
- '\nSync=' + DrawioFile.SYNC +
- ((this.sync != null) ? (((this.sync.enabled) ? ' enabled' : '') +
- ((this.sync.isConnected()) ? ' connected' : '')) : '') +
- '\nPlugins=' + ((mxSettings.settings != null) ? mxSettings.getPlugins() : 'null') +
- '\n\nStats:\n' + JSON.stringify(this.stats, null, 2) +
- ((details != null) ? ('\n\n' + details) : '') +
- ((error != null) ? ('\n\nError: ' + error.message) : '') +
- '\n\nStack:\n' + stack +
- '\n\nShadow:\n' + shadow +
- '\n\nData:\n' + data, max);
- }
- catch (e)
- {
- // ignore
- }
- };
- /**
- * Adds the listener for automatically saving the diagram for local changes.
- */
- DrawioFile.prototype.reloadFile = function(success, error)
- {
- try
- {
- this.ui.spinner.stop();
-
- var fn = mxUtils.bind(this, function()
- {
- this.stats.fileReloaded++;
- this.reportEnabled = false;
-
- // Restores view state and current page
- var viewState = this.ui.editor.graph.getViewState();
- var selection = this.ui.editor.graph.getSelectionCells();
- var page = this.ui.currentPage;
-
- this.ui.loadFile(this.getHash(), true, null, mxUtils.bind(this, function()
- {
- if (this.ui.fileLoadedError == null)
- {
- this.ui.restoreViewState(page, viewState, selection);
-
- if (this.backupPatch != null)
- {
- this.patch([this.backupPatch]);
- }
-
- // Carry-over stats
- var file = this.ui.getCurrentFile();
-
- if (file != null)
- {
- file.stats = this.stats;
- }
-
- if (success != null)
- {
- success();
- }
- }
- }), true);
- });
-
- if (this.isModified() && this.backupPatch == null)
- {
- this.ui.confirm(mxResources.get('allChangesLost'), mxUtils.bind(this, function()
- {
- this.handleFileSuccess(DrawioFile.SYNC == 'manual');
- }), fn, mxResources.get('cancel'), mxResources.get('discardChanges'));
- }
- else
- {
- fn();
- }
- }
- catch (e)
- {
- if (error != null)
- {
- error(e);
- }
- }
- };
- /**
- * Shows a conflict dialog to the user.
- */
- DrawioFile.prototype.copyFile = function(success, error)
- {
- this.ui.editor.editAsNew(this.ui.getFileData(true),
- this.ui.getCopyFilename(this));
- };
- /**
- * Returns true if the patches in the given array are empty.
- */
- DrawioFile.prototype.ignorePatches = function(patches)
- {
- var ignore = true;
-
- for (var i = 0; i < patches.length && ignore; i++)
- {
- ignore = ignore && Object.keys(patches[i]).length == 0;
- }
-
- return ignore;
- };
- /**
- * Applies the given patches to the file.
- */
- DrawioFile.prototype.patch = function(patches, resolver)
- {
- // Saves state of undo history
- var undoMgr = this.ui.editor.undoManager;
- var history = undoMgr.history.slice();
- var nextAdd = undoMgr.indexOfNextAdd;
-
- // Hides graph during updates
- var graph = this.ui.editor.graph;
- graph.container.style.visibility = 'hidden';
- // Ignores change events
- var prev = this.changeListenerEnabled;
- this.changeListenerEnabled = false;
-
- // Folding and math change require special handling
- var fold = graph.foldingEnabled;
- var math = graph.mathEnabled;
-
- // Updates text editor if cell changes during validation
- var redraw = graph.cellRenderer.redraw;
- graph.cellRenderer.redraw = function(state)
- {
- if (state.view.graph.isEditing(state.cell))
- {
- state.view.graph.scrollCellToVisible(state.cell);
- state.view.graph.cellEditor.resize();
- }
-
- redraw.apply(this, arguments);
- };
-
- graph.model.beginUpdate();
- try
- {
- // Applies patches
- for (var i = 0; i < patches.length; i++)
- {
- this.ui.pages = this.ui.patchPages(this.ui.pages,
- patches[i], true, resolver, this.isModified());
- }
-
- // Always needs at least one page
- if (this.ui.pages.length == 0)
- {
- this.ui.pages.push(this.ui.createPage());
- }
- // Checks if current page was removed
- if (mxUtils.indexOf(this.ui.pages, this.ui.currentPage) < 0)
- {
- this.ui.selectPage(this.ui.pages[0], true);
- }
- }
- finally
- {
- // Changes visibility before action states are updated via model event
- graph.container.style.visibility = '';
- graph.model.endUpdate();
-
- // Restores previous state
- graph.cellRenderer.redraw = redraw;
- this.changeListenerEnabled = prev;
-
- // Restores history state
- undoMgr.history = history;
- undoMgr.indexOfNextAdd = nextAdd;
- undoMgr.fireEvent(new mxEventObject(mxEvent.CLEAR));
-
- if (this.ui.currentPage == null || this.ui.currentPage.needsUpdate)
- {
- // Updates the graph and background
- if (math != graph.mathEnabled)
- {
- this.ui.editor.updateGraphComponents();
- graph.refresh();
- }
- else
- {
- if (fold != graph.foldingEnabled)
- {
- graph.view.revalidate();
- }
- else
- {
- graph.view.validate();
- }
-
- graph.sizeDidChange();
- }
- }
-
- this.ui.updateTabContainer();
- }
- };
- /**
- * Adds the listener for automatically saving the diagram for local changes.
- */
- DrawioFile.prototype.save = function(revision, success, error, unloading, overwrite, manual)
- {
- try
- {
- if (!this.isEditable())
- {
- if (error != null)
- {
- error({message: mxResources.get('readOnly')});
- }
- else
- {
- throw new Error(mxResources.get('readOnly'));
- }
- }
- else if (!overwrite && this.invalidChecksum)
- {
- if (error != null)
- {
- error({message: mxResources.get('checksum')});
- }
- else
- {
- throw new Error(mxResources.get('checksum'));
- }
- }
- else
- {
- this.updateFileData();
- this.clearAutosave();
-
- if (success != null)
- {
- success();
- }
- }
- }
- catch (e)
- {
- if (error != null)
- {
- error(e);
- }
- else
- {
- throw e;
- }
- }
- };
- /**
- * Translates this point by the given vector.
- *
- * @param {number} dx X-coordinate of the translation.
- * @param {number} dy Y-coordinate of the translation.
- */
- DrawioFile.prototype.updateFileData = function()
- {
- this.setData(this.ui.getFileData(null, null, null, null, null, null, null, null, this));
- };
- /**
- * Translates this point by the given vector.
- *
- * @param {number} dx X-coordinate of the translation.
- * @param {number} dy Y-coordinate of the translation.
- */
- DrawioFile.prototype.saveAs = function(filename, success, error) { };
- /**
- * Translates this point by the given vector.
- *
- * @param {number} dx X-coordinate of the translation.
- * @param {number} dy Y-coordinate of the translation.
- */
- DrawioFile.prototype.saveFile = function(title, revision, success, error) { };
- /**
- * Returns true if copy, export and print are not allowed for this file.
- */
- DrawioFile.prototype.getPublicUrl = function(fn)
- {
- fn(null);
- };
- /**
- * Returns true if copy, export and print are not allowed for this file.
- */
- DrawioFile.prototype.isRestricted = function()
- {
- return false;
- };
- /**
- * Translates this point by the given vector.
- *
- * @param {number} dx X-coordinate of the translation.
- * @param {number} dy Y-coordinate of the translation.
- */
- DrawioFile.prototype.isModified = function()
- {
- return this.modified;
- };
- /**
- * Translates this point by the given vector.
- *
- * @param {number} dx X-coordinate of the translation.
- * @param {number} dy Y-coordinate of the translation.
- */
- DrawioFile.prototype.setModified = function(value)
- {
- this.modified = value;
- };
- /**
- * Specifies if the autosave checkbox should be shown in the document
- * properties dialog. Default is false.
- */
- DrawioFile.prototype.isAutosaveOptional = function()
- {
- return false;
- };
- /**
- * Translates this point by the given vector.
- *
- * @param {number} dx X-coordinate of the translation.
- * @param {number} dy Y-coordinate of the translation.
- */
- DrawioFile.prototype.isAutosave = function()
- {
- return !this.inConflictState && this.ui.editor.autosave;
- };
- /**
- * Translates this point by the given vector.
- *
- * @param {number} dx X-coordinate of the translation.
- * @param {number} dy Y-coordinate of the translation.
- */
- DrawioFile.prototype.isRenamable = function()
- {
- return false;
- };
- /**
- * Translates this point by the given vector.
- *
- * @param {number} dx X-coordinate of the translation.
- * @param {number} dy Y-coordinate of the translation.
- */
- DrawioFile.prototype.rename = function(title, success, error) { };
- /**
- * Translates this point by the given vector.
- *
- * @param {number} dx X-coordinate of the translation.
- * @param {number} dy Y-coordinate of the translation.
- */
- DrawioFile.prototype.isMovable = function()
- {
- return false;
- };
- /**
- * Translates this point by the given vector.
- *
- * @param {number} dx X-coordinate of the translation.
- * @param {number} dy Y-coordinate of the translation.
- */
- DrawioFile.prototype.move = function(folderId, success, error) { };
- /**
- * Returns the hash of the file which consists of a prefix for the storage
- * type and the ID of the file.
- */
- DrawioFile.prototype.getHash = function()
- {
- return '';
- };
- /**
- * Returns the ID of the file.
- */
- DrawioFile.prototype.getId = function()
- {
- return '';
- };
- /**
- * Returns true if the file is editable.
- */
- DrawioFile.prototype.isEditable = function()
- {
- return !this.ui.editor.isChromelessView() || this.ui.editor.editable;
- };
- /**
- * Returns the location as a new object.
- * @type mx.Point
- */
- DrawioFile.prototype.getUi = function()
- {
- return this.ui;
- };
- /**
- * Returns the current title of the file.
- */
- DrawioFile.prototype.getTitle = function()
- {
- return '';
- };
- /**
- * Sets the current data of the file.
- */
- DrawioFile.prototype.setData = function(data)
- {
- this.data = data;
- };
- /**
- * Returns the current data of the file.
- */
- DrawioFile.prototype.getData = function()
- {
- return this.data;
- };
- /**
- * Opens this file in the editor.
- */
- DrawioFile.prototype.open = function()
- {
- this.stats.opened++;
- var data = this.getData();
-
- if (data != null)
- {
- this.ui.setFileData(data);
-
- // Updates shadow in case any page IDs have been updated
- // only if the file has not been modified and reopened
- if (!this.isModified())
- {
- this.shadowData = mxUtils.getXml(this.ui.getXmlFileData());
- this.shadowPages = null;
- }
- }
- this.installListeners();
-
- if (this.isSyncSupported())
- {
- this.startSync();
- }
- };
- /**
- * Hook for subclassers.
- */
- DrawioFile.prototype.isSyncSupported = function()
- {
- return false;
- };
- /**
- * Hook for subclassers.
- */
- DrawioFile.prototype.isRevisionHistorySupported = function()
- {
- return false;
- };
- /**
- * Hook for subclassers.
- */
- DrawioFile.prototype.getRevisions = function(success, error)
- {
- success(null);
- };
- /**
- * Hook for subclassers to get the latest descriptor of this file
- * and return it in the success handler.
- */
- DrawioFile.prototype.loadDescriptor = function(success, error)
- {
- success(null);
- };
- /**
- * Hook for subclassers to get the latest etag of this file
- * and return it in the success handler.
- */
- DrawioFile.prototype.loadPatchDescriptor = function(success, error)
- {
- this.loadDescriptor(mxUtils.bind(this, function(desc)
- {
- success(desc);
- }), error);
- };
- /**
- * Adds the listener for automatically saving the diagram for local changes.
- */
- DrawioFile.prototype.patchDescriptor = function(desc, patch)
- {
- this.setDescriptorEtag(desc, this.getDescriptorEtag(patch));
- };
- /**
- * Creates a starts the synchronization.
- */
- DrawioFile.prototype.startSync = function()
- {
- if ((DrawioFile.SYNC == 'auto' && urlParams['stealth'] != '1') &&
- (urlParams['rt'] == '1' || !this.ui.editor.chromeless ||
- this.ui.editor.editable))
- {
- if (this.sync == null)
- {
- this.sync = new DrawioFileSync(this);
- }
-
- this.sync.start();
- }
- };
- /**
- * Hook for subclassers to check if an error is a conflict.
- */
- DrawioFile.prototype.isConflict = function()
- {
- return false;
- };
- /**
- * Gets the channel ID for sync messages.
- */
- DrawioFile.prototype.getChannelId = function()
- {
- // Slash, space and plus replaced with underscore
- return Graph.compress(this.getHash()).replace(/[\/ +]/g, '_');
- };
- /**
- * Gets the channel ID from the given descriptor.
- */
- DrawioFile.prototype.getChannelKey = function(desc)
- {
- return null;
- };
- /**
- * Returns the current etag.
- */
- DrawioFile.prototype.getCurrentUser = function()
- {
- return null;
- };
- /**
- * Hook for subclassers to get the latest version of this file
- * and return it in the success handler.
- */
- DrawioFile.prototype.getLatestVersion = function(success, error)
- {
- success(null);
- };
- /**
- * Returns the last modified date of this file.
- */
- DrawioFile.prototype.getLastModifiedDate = function()
- {
- return new Date();
- };
- /**
- * Sets the current etag.
- */
- DrawioFile.prototype.setCurrentEtag = function(etag)
- {
- this.setDescriptorEtag(this.getDescriptor(), etag);
- };
- /**
- * Returns the current etag.
- */
- DrawioFile.prototype.getCurrentEtag = function()
- {
- return this.getDescriptorEtag(this.getDescriptor());
- };
- /**
- * Returns the descriptor from this file.
- */
- DrawioFile.prototype.getDescriptor = function()
- {
- return null;
- };
- /**
- * Sets the descriptor for this file.
- */
- DrawioFile.prototype.setDescriptor = function() { };
- /**
- * Updates the etag on the given descriptor.
- */
- DrawioFile.prototype.setDescriptorEtag = function(desc, etag) { };
- /**
- * Returns the etag from the given descriptor.
- */
- DrawioFile.prototype.getDescriptorEtag = function(desc)
- {
- return null;
- };
- /**
- * Returns the secret from the given descriptor.
- */
- DrawioFile.prototype.getDescriptorSecret = function(desc)
- {
- return null;
- };
- /**
- * Installs the change listener.
- */
- DrawioFile.prototype.installListeners = function()
- {
- if (this.changeListener == null)
- {
- this.changeListener = mxUtils.bind(this, function(sender, eventObject)
- {
- var edit = (eventObject != null) ? eventObject.getProperty('edit') : null;
-
- if (this.changeListenerEnabled && this.isEditable() && (edit == null || !edit.ignoreEdit))
- {
- this.fileChanged();
- }
- });
-
- this.ui.editor.graph.model.addListener(mxEvent.CHANGE, this.changeListener);
-
- // Some options trigger autosave
- this.ui.editor.graph.addListener('gridSizeChanged', this.changeListener);
- this.ui.editor.graph.addListener('shadowVisibleChanged', this.changeListener);
- this.ui.addListener('pageFormatChanged', this.changeListener);
- this.ui.addListener('pageScaleChanged', this.changeListener);
- this.ui.addListener('backgroundColorChanged', this.changeListener);
- this.ui.addListener('backgroundImageChanged', this.changeListener);
- this.ui.addListener('foldingEnabledChanged', this.changeListener);
- this.ui.addListener('mathEnabledChanged', this.changeListener);
- this.ui.addListener('gridEnabledChanged', this.changeListener);
- this.ui.addListener('guidesEnabledChanged', this.changeListener);
- this.ui.addListener('pageViewChanged', this.changeListener);
- this.ui.addListener('connectionPointsChanged', this.changeListener);
- this.ui.addListener('connectionArrowsChanged', this.changeListener);
- }
- };
- /**
- * Returns the location as a new object.
- * @type mx.Point
- */
- DrawioFile.prototype.addAllSavedStatus = function(status)
- {
- if (this.ui.statusContainer != null && this.ui.getCurrentFile() == this)
- {
- status = (status != null) ? status : mxUtils.htmlEntities(mxResources.get(this.allChangesSavedKey));
- this.ui.editor.setStatus('<div title="'+ status + '">' + status + '</div>');
- var links = this.ui.statusContainer.getElementsByTagName('div');
-
- if (links.length > 0 && this.isRevisionHistorySupported())
- {
- links[0].style.cursor = 'pointer';
- links[0].style.textDecoration = 'underline';
-
- mxEvent.addListener(links[0], 'click', mxUtils.bind(this, function()
- {
- this.ui.actions.get('revisionHistory').funct();
- }));
- }
- }
- };
- /**
- * Adds the listener for automatically saving the diagram for local changes.
- */
- DrawioFile.prototype.addUnsavedStatus = function(err)
- {
- if (!this.inConflictState && this.ui.statusContainer != null && this.ui.getCurrentFile() == this)
- {
- if (err instanceof Error && err.message != null && err.message != '')
- {
- var status = mxUtils.htmlEntities(mxResources.get('unsavedChanges'));
-
- this.ui.editor.setStatus('<div title="'+ status +
- '" class="geStatusAlert" style="overflow:hidden;">' + status +
- ' (' + mxUtils.htmlEntities(err.message) + ')</div>');
- }
- else
- {
- // FIXME: Handle multiple tabs
- // if (this.ui.mode == null && urlParams['splash'] == '0')
- // {
- // try
- // {
- // this.ui.updateDraft();
- // this.setModified(false);
- // }
- // catch (e)
- // {
- // // Keeps modified flag unchanged
- // }
- // }
- var msg = this.getErrorMessage(err);
- if (msg == null && this.lastSaved != null)
- {
- var str = this.ui.timeSince(new Date(this.lastSaved));
-
- // Only show if more than a minute ago
- if (str != null)
- {
- msg = mxResources.get('lastSaved', [str]);
- }
- }
-
- if (msg != null && msg.length > 60)
- {
- msg = msg.substring(0, 60) + '...';
- }
- var status = mxUtils.htmlEntities(mxResources.get('unsavedChangesClickHereToSave')) +
- ((msg != null && msg != '') ? ' (' + mxUtils.htmlEntities(msg) + ')' : '');
- this.ui.editor.setStatus('<div title="'+ status +
- '" class="geStatusAlert" style="cursor:pointer;overflow:hidden;">' +
- status + '</div>');
-
- // Installs click handler for saving
- var links = this.ui.statusContainer.getElementsByTagName('div');
-
- if (links != null && links.length > 0)
- {
- mxEvent.addListener(links[0], 'click', mxUtils.bind(this, function()
- {
- this.ui.actions.get((this.ui.mode == null || !this.isEditable()) ?
- 'saveAs' : 'save').funct();
- }));
- }
- else
- {
- var status = mxUtils.htmlEntities(mxResources.get('unsavedChanges'));
-
- this.ui.editor.setStatus('<div title="'+ status +
- '" class="geStatusAlert" style="overflow:hidden;">' + status +
- ' (' + mxUtils.htmlEntities(err.message) + ')</div>');
- }
- }
- }
- };
- /**
- * Halts all timers and shows a conflict status message. The optional error
- * handler is invoked first.
- */
- DrawioFile.prototype.addConflictStatus = function(fn, message)
- {
- if (this.invalidChecksum && message == null)
- {
- message = mxResources.get('checksum');
- }
- this.setConflictStatus(mxUtils.htmlEntities(mxResources.get('fileChangedSync')) +
- ((message != null && message != '') ? ' (' + mxUtils.htmlEntities(message) + ')' : ''));
- this.ui.spinner.stop();
- this.clearAutosave();
- var links = (this.ui.statusContainer != null) ? this.ui.statusContainer.getElementsByTagName('div') : null;
-
- if (links != null && links.length > 0)
- {
- mxEvent.addListener(links[0], 'click', mxUtils.bind(this, function(evt)
- {
- if (mxEvent.getSource(evt).nodeName != 'IMG')
- {
- fn();
- }
- }));
- }
- else
- {
- this.ui.alert(mxUtils.htmlEntities(mxResources.get('fileChangedSync')), fn);
- }
- };
- /**
- * Halts all timers and shows a conflict status message. The optional error
- * handler is invoked first.
- */
- DrawioFile.prototype.setConflictStatus = function(message)
- {
- this.ui.editor.setStatus('<div title="'+ message + '" class="geStatusAlert geBlink" style="cursor:pointer;overflow:hidden;">' +
- message + ' <a href="https://desk.draw.io/support/solutions/articles/16000087947" target="_blank"><img border="0" ' +
- 'style="margin-left:2px;cursor:help;opacity:0.5;width:16px;height:16px;" valign="bottom" src="' + Editor.helpImage +
- '" style=""/></a></div>');
- };
- /**
- * Shows a conflict dialog to the user.
- */
- DrawioFile.prototype.showRefreshDialog = function(success, error, message)
- {
- if (message == null)
- {
- message = mxResources.get('checksum');
- }
-
- if (this.ui.editor.isChromelessView() && !this.ui.editor.editable)
- {
- this.ui.alert(mxResources.get('fileChangedSync'), mxUtils.bind(this, function()
- {
- this.reloadFile(success, error);
- }));
- }
- else
- {
- // Allows for escape key to be pressed while dialog is showing
- this.addConflictStatus(mxUtils.bind(this, function()
- {
- this.showRefreshDialog(success, error);
- }), message);
-
- this.ui.showError(mxResources.get('error') + ' (' + message + ')',
- mxResources.get('fileChangedSyncDialog'),
- mxResources.get('makeCopy'), mxUtils.bind(this, function()
- {
- this.copyFile(success, error);
- }), null, mxResources.get('synchronize'), mxUtils.bind(this, function()
- {
- this.reloadFile(success, error);
- }), mxResources.get('cancel'), mxUtils.bind(this, function()
- {
- this.ui.hideDialog();
- }), 360, 150);
- }
- };
- /**
- * Shows a dialog with no synchronize option.
- */
- DrawioFile.prototype.showCopyDialog = function(success, error, overwrite)
- {
- this.inConflictState = false;
- this.invalidChecksum = false;
- this.addUnsavedStatus();
-
- this.ui.showError(mxResources.get('externalChanges'),
- mxResources.get('fileChangedOverwriteDialog'),
- mxResources.get('makeCopy'), mxUtils.bind(this, function()
- {
- this.copyFile(success, error);
- }), null, mxResources.get('overwrite'), overwrite,
- mxResources.get('cancel'), mxUtils.bind(this, function()
- {
- this.ui.hideDialog();
- }), 360, 150);
- };
- /**
- * Shows a conflict dialog to the user.
- */
- DrawioFile.prototype.showConflictDialog = function(overwrite, synchronize)
- {
- this.ui.showError(mxResources.get('externalChanges'),
- mxResources.get('fileChangedSyncDialog'),
- mxResources.get('overwrite'), overwrite, null,
- mxResources.get('synchronize'), synchronize,
- mxResources.get('cancel'), mxUtils.bind(this, function()
- {
- this.ui.hideDialog();
- this.handleFileError(null, false);
- }), 340, 150);
- };
- /**
- * Checks if the client is authorized and calls the next step.
- */
- DrawioFile.prototype.redirectToNewApp = function(error)
- {
- this.ui.spinner.stop();
-
- if (!this.redirectDialogShowing)
- {
- this.redirectDialogShowing = true;
-
- var url = window.location.protocol + '//' + window.location.host + '/' + this.ui.getSearch(
- ['create', 'title', 'mode', 'url', 'drive', 'splash', 'state']) + '#' + this.getHash();
-
- var redirect = mxUtils.bind(this, function()
- {
- var fn = mxUtils.bind(this, function()
- {
- this.redirectDialogShowing = false;
-
- if (window.location.href == url)
- {
- window.location.reload();
- }
- else
- {
- window.location.href = url;
- }
- });
-
- if (error == null && this.isModified())
- {
- this.ui.confirm(mxResources.get('allChangesLost'), mxUtils.bind(this, function()
- {
- this.redirectDialogShowing = false;
- }), fn, mxResources.get('cancel'), mxResources.get('discardChanges'));
- }
- else
- {
- fn();
- }
- });
-
- if (error != null)
- {
- if (this.isModified())
- {
- this.ui.confirm(mxResources.get('redirectToNewApp'), mxUtils.bind(this, function()
- {
- this.redirectDialogShowing = false;
- error();
- }), redirect, mxResources.get('cancel'), mxResources.get('discardChanges'));
- }
- else
- {
- this.ui.confirm(mxResources.get('redirectToNewApp'), redirect, mxUtils.bind(this, function()
- {
- this.redirectDialogShowing = false;
- error();
- }));
- }
- }
- else
- {
- this.ui.alert(mxResources.get('redirectToNewApp'), redirect);
- }
- }
- };
- /**
- * Adds the listener for automatically saving the diagram for local changes.
- */
- DrawioFile.prototype.handleFileSuccess = function(saved)
- {
- this.ui.spinner.stop();
-
- if (this.ui.getCurrentFile() == this)
- {
- if (this.isModified())
- {
- this.fileChanged();
- }
- else if (saved)
- {
- this.addAllSavedStatus();
- if (this.sync != null)
- {
- this.sync.resetUpdateStatusThread();
-
- if (this.sync.remoteFileChanged)
- {
- this.sync.remoteFileChanged = false;
- this.sync.fileChangedNotify();
- }
- }
- }
- else
- {
- this.ui.editor.setStatus('');
- }
- }
- };
- /**
- * Adds the listener for automatically saving the diagram for local changes.
- */
- DrawioFile.prototype.handleFileError = function(err, manual)
- {
- this.ui.spinner.stop();
-
- if (this.ui.getCurrentFile() == this)
- {
- if (this.inConflictState)
- {
- this.handleConflictError(err, manual);
- }
- else
- {
- if (this.isModified())
- {
- this.addUnsavedStatus(err);
- }
-
- if (manual)
- {
- this.ui.handleError(err, (err != null) ? mxResources.get('errorSavingFile') : null);
- }
- else if (!this.isModified())
- {
- var msg = (err != null) ? ((err.error != null) ? err.error.message : err.message) : null;
-
- if (msg != null && msg.length > 60)
- {
- msg = msg.substring(0, 60) + '...';
- }
-
- this.ui.editor.setStatus('<div class="geStatusAlert" style="cursor:pointer;overflow:hidden;">' +
- mxUtils.htmlEntities(mxResources.get('error')) +
- ((msg != null) ? ' (' + mxUtils.htmlEntities(msg) + ')' : '') + '</div>');
- }
- else if (this.isModified() && !manual && this.isAutosave())
- {
- if (this.lastWarned == null)
- {
- this.lastWarned = Date.now();
- }
- else if (Date.now() - this.lastWarned > this.warnInterval)
- {
- var msg = '';
-
- if (this.lastSaved != null)
- {
- var str = this.ui.timeSince(new Date(this.lastSaved));
-
- // Only show if more than a minute ago
- if (str != null)
- {
- msg = mxResources.get('lastSaved', [str]);
- }
- }
-
- this.ui.showError(mxResources.get('unsavedChanges'), msg, mxResources.get('ignore'),
- mxUtils.bind(this, function()
- {
- this.lastWarned = Date.now();
- this.ui.hideDialog();
- EditorUi.logEvent({category: 'IGNORE-WARN-SAVE-FILE-' + this.getHash() +
- '-size-' + this.getSize(), action: 'ignore'});
- }), null, mxResources.get('save'), mxUtils.bind(this, function()
- {
- this.lastWarned = Date.now();
- this.ui.actions.get((this.ui.mode == null || !this.isEditable()) ?
- 'saveAs' : 'save').funct();
- EditorUi.logEvent({category: 'SAVE-WARN-SAVE-FILE-' + this.getHash() +
- '-size-' + this.getSize(), action: 'save'});
- }), null, null, 360, 120);
- }
- }
- }
- }
- };
- /**
- * Adds the listener for automatically saving the diagram for local changes.
- */
- DrawioFile.prototype.handleConflictError = function(err, manual)
- {
- var success = mxUtils.bind(this, function()
- {
- this.handleFileSuccess(true);
- });
-
- var error = mxUtils.bind(this, function(err2)
- {
- this.handleFileError(err2, true);
- });
-
- var overwrite = mxUtils.bind(this, function()
- {
- if (this.ui.spinner.spin(document.body, mxResources.get('saving')))
- {
- this.ui.editor.setStatus('');
- this.save(true, success, error, null, true, (this.constructor ==
- GitHubFile && err != null) ? err.commitMessage : null)
- }
- });
- var synchronize = mxUtils.bind(this, function()
- {
- if (this.ui.spinner.spin(document.body, mxResources.get('updatingDocument')))
- {
- this.synchronizeFile(mxUtils.bind(this, function()
- {
- this.ui.spinner.stop();
-
- if (this.ui.spinner.spin(document.body, mxResources.get('saving')))
- {
- this.save(true, success, error, null, null, (this.constructor ==
- GitHubFile && err != null) ? err.commitMessage : null)
- }
- }), error);
- }
- })
-
- if (DrawioFile.SYNC == 'none')
- {
- this.showCopyDialog(success, error, overwrite);
- }
- else if (this.invalidChecksum)
- {
- this.showRefreshDialog(success, error, this.getErrorMessage(err));
- }
- else if (manual)
- {
- this.showConflictDialog(overwrite, synchronize);
- }
- else
- {
- this.addConflictStatus(mxUtils.bind(this, function()
- {
- this.ui.editor.setStatus(mxUtils.htmlEntities(
- mxResources.get('updatingDocument')));
- this.synchronizeFile(success, error);
- }), this.getErrorMessage(err));
- }
- };
- /**
- * Adds the listener for automatically saving the diagram for local changes.
- */
- DrawioFile.prototype.getErrorMessage = function(err)
- {
- return (err != null) ? ((err.error != null) ? err.error.message : err.message) : null
- };
- /**
- * Adds the listener for automatically saving the diagram for local changes.
- */
- DrawioFile.prototype.fileChanged = function()
- {
- this.setModified(true);
-
- if (this.isAutosave())
- {
- this.addAllSavedStatus(mxUtils.htmlEntities(mxResources.get('saving')) + '...');
-
- this.autosave(this.autosaveDelay, this.maxAutosaveDelay, mxUtils.bind(this, function(resp)
- {
- // Does not update status if another autosave was scheduled
- if (this.autosaveThread == null)
- {
- this.handleFileSuccess(true);
- }
- }), mxUtils.bind(this, function(err)
- {
- this.handleFileError(err);
- }));
- }
- else if ((!this.isAutosaveOptional() || !this.ui.editor.autosave) &&
- !this.inConflictState)
- {
- this.addUnsavedStatus();
- }
- };
- /**
- * Invokes sync and updates shadow document.
- */
- DrawioFile.prototype.fileSaved = function(savedData, lastDesc, success, error)
- {
- this.lastSaved = new Date();
-
- try
- {
- this.stats.saved++;
- this.inConflictState = false;
- this.invalidChecksum = false;
- if (this.sync == null)
- {
- this.shadowData = savedData;
- this.shadowPages = null;
-
- if (success != null)
- {
- success();
- }
- }
- else
- {
- this.sync.fileSaved(this.ui.getPagesForNode(
- mxUtils.parseXml(savedData).documentElement),
- lastDesc, success, error, savedData);
- }
- }
- catch (e)
- {
- this.inConflictState = true;
- this.invalidChecksum = true;
- this.descriptorChanged();
-
- if (error != null)
- {
- error(e);
- }
- try
- {
- if (this.errorReportsEnabled)
- {
- this.sendErrorReport('Error in fileSaved', null, e);
- }
- else
- {
- var user = this.getCurrentUser();
- var uid = (user != null) ? user.id : 'unknown';
-
- EditorUi.logError('Error in fileSaved', null,
- this.getMode() + '.' + this.getId(),
- uid, e);
- }
- }
- catch (e2)
- {
- // ignore
- }
- }
- };
- /**
- * Adds the listener for automatically saving the diagram for local changes.
- */
- DrawioFile.prototype.autosave = function(delay, maxDelay, success, error)
- {
- if (this.lastAutosave == null)
- {
- this.lastAutosave = new Date().getTime();
- }
-
- var tmp = (new Date().getTime() - this.lastAutosave < maxDelay) ? delay : 0;
- this.clearAutosave();
-
- // Starts new timer or executes immediately if not unsaved for maxDelay
- var thread = window.setTimeout(mxUtils.bind(this, function()
- {
- this.lastAutosave = null;
-
- if (this.autosaveThread == thread)
- {
- this.autosaveThread = null;
- }
-
- // Workaround for duplicate save if UI is blocking
- // after save while pending autosave triggers
- if (this.isModified() && this.isAutosaveNow())
- {
- var rev = this.isAutosaveRevision();
-
- if (rev)
- {
- this.lastAutosaveRevision = new Date().getTime();
- }
-
- this.save(rev, mxUtils.bind(this, function(resp)
- {
- this.autosaveCompleted();
-
- if (success != null)
- {
- success(resp);
- }
- }), mxUtils.bind(this, function(resp)
- {
- if (error != null)
- {
- error(resp);
- }
- }));
- }
- else
- {
- if (!this.isModified())
- {
- this.ui.editor.setStatus('');
- }
-
- if (success != null)
- {
- success(null);
- }
- }
- }), tmp);
- this.autosaveThread = thread;
- };
- /**
- * Returns true if an autosave is required at the time of execution.
- * This implementation returns true.
- */
- DrawioFile.prototype.isAutosaveNow = function()
- {
- return true;
- };
- /**
- * Hooks for subclassers after the autosave has completed.
- */
- DrawioFile.prototype.autosaveCompleted = function() { };
- /**
- * Adds the listener for automatically saving the diagram for local changes.
- */
- DrawioFile.prototype.clearAutosave = function()
- {
- if (this.autosaveThread != null)
- {
- window.clearTimeout(this.autosaveThread);
- this.autosaveThread = null;
- }
- };
- /**
- * Returns the location as a new object.
- * @type mx.Point
- */
- DrawioFile.prototype.isAutosaveRevision = function()
- {
- var now = new Date().getTime();
-
- return (this.lastAutosaveRevision == null) || (now - this.lastAutosaveRevision) > this.maxAutosaveRevisionDelay;
- };
- /**
- * Translates this point by the given vector.
- *
- * @param {number} dx X-coordinate of the translation.
- * @param {number} dy Y-coordinate of the translation.
- */
- DrawioFile.prototype.descriptorChanged = function()
- {
- this.fireEvent(new mxEventObject('descriptorChanged'));
- };
- /**
- * Translates this point by the given vector.
- *
- * @param {number} dx X-coordinate of the translation.
- * @param {number} dy Y-coordinate of the translation.
- */
- DrawioFile.prototype.contentChanged = function()
- {
- this.fireEvent(new mxEventObject('contentChanged'));
- };
- /**
- * Returns the location as a new object.
- */
- DrawioFile.prototype.close = function(unloading)
- {
- this.updateFileData();
- this.stats.closed++;
-
- if (this.isAutosave() && this.isModified())
- {
- this.save(this.isAutosaveRevision(), null, null, unloading);
- }
- this.destroy();
- };
- /**
- * Returns the location as a new object.
- */
- DrawioFile.prototype.hasSameExtension = function(title, newTitle)
- {
- if (title != null && newTitle != null)
- {
- var dot = title.lastIndexOf('.');
- var ext = (dot > 0) ? title.substring(dot) : '';
- dot = newTitle.lastIndexOf('.');
- return ext === ((dot > 0) ? newTitle.substring(dot) : '');
- }
-
- return title == newTitle;
- };
- /**
- * Removes the change listener.
- */
- DrawioFile.prototype.removeListeners = function()
- {
- if (this.changeListener != null)
- {
- this.ui.editor.graph.model.removeListener(this.changeListener);
- this.ui.editor.graph.removeListener(this.changeListener);
- this.ui.removeListener(this.changeListener);
- this.changeListener = null;
- }
- };
- /**
- * Stops any pending autosaves and removes all listeners.
- */
- DrawioFile.prototype.destroy = function()
- {
- this.stats.destroyed++;
-
- try
- {
- if (!this.ui.isOffline() && this.reportEnabled &&
- (DrawioFile.SYNC == 'auto' ||
- DrawioFile.SYNC == 'manual'))
- {
- var user = this.getCurrentUser();
- var uid = (user != null) ? user.id : 'unknown';
-
- EditorUi.logEvent({category: DrawioFile.SYNC + '-DESTROY-FILE-' + DrawioFile.SYNC,
- action: 'file-' + this.getId() +
- '-mode-' + this.getMode() +
- '-size-' + this.getSize() +
- '-user-' + uid +
- ((this.sync != null) ? ('-client-' + this.sync.clientId ) : ''),
- label: this.stats});
- }
- }
- catch (e)
- {
- // ignore
- }
- this.clearAutosave();
- this.removeListeners();
- if (this.sync != null)
- {
- this.sync.destroy();
- this.sync = null;
- }
- };
- /**
- * Are comments supported
- */
- DrawioFile.prototype.commentsSupported = function()
- {
- return false; //The default is false and files that support it must explicitly state that
- };
- /**
- * Show refresh button?
- */
- DrawioFile.prototype.commentsRefreshNeeded = function()
- {
- return true;
- };
- /**
- * Show save button?
- */
- DrawioFile.prototype.commentsSaveNeeded = function()
- {
- return false;
- };
- /**
- * Get comments of the file
- */
- DrawioFile.prototype.getComments = function(success, error)
- {
- success([]); //placeholder
- };
- /**
- * Add a comment to the file
- */
- DrawioFile.prototype.addComment = function(comment, success, error)
- {
- success(Date.now()); //placeholder
- };
- /**
- * Can add a reply to a reply
- */
- DrawioFile.prototype.canReplyToReplies = function()
- {
- return true;
- };
- /**
- * Can add comments (The permission to comment to this file)
- */
- DrawioFile.prototype.canComment = function()
- {
- return true;
- };
- /**
- * Get a new comment object
- */
- DrawioFile.prototype.newComment = function(content, user)
- {
- return new DrawioComment(this, null, content, Date.now(), Date.now(), false, user);
- };
|