DrawioFile.js 40 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808
  1. /**
  2. * Copyright (c) 2006-2017, JGraph Ltd
  3. * Copyright (c) 2006-2017, Gaudenz Alder
  4. */
  5. DrawioFile = function(ui, data)
  6. {
  7. mxEventSource.call(this);
  8. /**
  9. * Holds the x-coordinate of the point.
  10. * @type number
  11. * @default 0
  12. */
  13. this.ui = ui;
  14. /**
  15. * Holds the x-coordinate of the point.
  16. * @type number
  17. * @default 0
  18. */
  19. this.data = data || '';
  20. this.shadowData = this.data;
  21. };
  22. /**
  23. * Global switch for realtime collaboration type to use sync URL parameter
  24. * with the following possible values:
  25. *
  26. * - none: overwrite
  27. * - manual: manual sync
  28. * - auto: automatic sync
  29. */
  30. DrawioFile.SYNC = urlParams['sync'] || 'auto';
  31. /**
  32. * Specifies if last write wins should be used for values and styles.
  33. */
  34. DrawioFile.LAST_WRITE_WINS = true;
  35. // Extends mxEventSource
  36. mxUtils.extend(DrawioFile, mxEventSource);
  37. /**
  38. * Delay for last save in ms.
  39. */
  40. DrawioFile.prototype.allChangesSavedKey = 'allChangesSaved';
  41. /**
  42. * Specifies the delay between the last change and the autosave.
  43. */
  44. DrawioFile.prototype.autosaveDelay = 1500;
  45. /**
  46. * Specifies the maximum delay before an autosave is forced even if the graph
  47. * is being changed.
  48. */
  49. DrawioFile.prototype.maxAutosaveDelay = 30000;
  50. /**
  51. * Contains the thread for the next autosave.
  52. */
  53. DrawioFile.prototype.autosaveThread = null;
  54. /**
  55. * Stores the timestamp for hte last autosave.
  56. */
  57. DrawioFile.prototype.lastAutosave = null;
  58. /**
  59. * Stores the modified state.
  60. */
  61. DrawioFile.prototype.modified = false;
  62. /**
  63. * Holds a copy of the current file data.
  64. */
  65. DrawioFile.prototype.data = null;
  66. /**
  67. * Holds a copy of the last saved file data.
  68. */
  69. DrawioFile.prototype.shadowData = null;
  70. /**
  71. * Holds a copy of the parsed last saved file data.
  72. */
  73. DrawioFile.prototype.shadowPages = null;
  74. /**
  75. * Specifies if the graph change listener is enabled. Default is true.
  76. */
  77. DrawioFile.prototype.changeListenerEnabled = true;
  78. /**
  79. * Sets the delay for autosave in milliseconds. Default is 1500.
  80. */
  81. DrawioFile.prototype.lastAutosaveRevision = null;
  82. /**
  83. * Sets the delay for autosave in milliseconds. Default is 1000.
  84. */
  85. DrawioFile.prototype.maxAutosaveRevisionDelay = 1800000;
  86. /**
  87. * Specifies if notify events should be ignored.
  88. */
  89. DrawioFile.prototype.inConflictState = false;
  90. /**
  91. * Specifies if notify events should be ignored.
  92. */
  93. DrawioFile.prototype.invalidChecksum = false;
  94. /**
  95. * Specifies if notify events should be ignored.
  96. */
  97. DrawioFile.prototype.reportEnabled = true;
  98. /**
  99. * Specifies if notify events should be ignored.
  100. */
  101. DrawioFile.prototype.stats = {
  102. joined: 0, /* number of join messages received */
  103. reloads: 0, /* number of times the file was reloaded */
  104. checksumErrors: 0, /* number of checksum errors */
  105. bytesSent: 0, /* number of bytes send in messages */
  106. bytesReceived: 0, /* number of bytes received in messages */
  107. msgReceived: 0, /* number of messages received */
  108. msgSent: 0, /* number of messages sent */
  109. cacheHits: 0, /* number of times the cache returned patches */
  110. cacheMiss: 0, /* number of times we have given up to read the cache */
  111. conflicts: 0, /* number of write conflicts when saving a file */
  112. timeouts: 0 /* number of time we have given up to retry after a write conflict */
  113. };
  114. /**
  115. * Specifies if notify events should be ignored.
  116. */
  117. DrawioFile.prototype.getSize = function()
  118. {
  119. return (this.data != null) ? this.data.length : 0;
  120. };
  121. /**
  122. * Adds the listener for automatically saving the diagram for local changes.
  123. */
  124. DrawioFile.prototype.synchronizeFile = function(success, error)
  125. {
  126. if (this.savingFile)
  127. {
  128. if (error != null)
  129. {
  130. error({message: mxResources.get('busy')});
  131. }
  132. }
  133. else
  134. {
  135. if (this.sync != null)
  136. {
  137. this.sync.fileChanged(success, error);
  138. }
  139. else
  140. {
  141. this.updateFile(success, error);
  142. }
  143. }
  144. };
  145. /**
  146. * Adds the listener for automatically saving the diagram for local changes.
  147. */
  148. DrawioFile.prototype.updateFile = function(success, error, abort)
  149. {
  150. this.getLatestVersion(mxUtils.bind(this, function(latestFile)
  151. {
  152. try
  153. {
  154. if (abort == null || !abort())
  155. {
  156. if (latestFile != null)
  157. {
  158. this.mergeFile(latestFile, success, error);
  159. }
  160. else
  161. {
  162. this.reloadFile(success, error);
  163. }
  164. }
  165. }
  166. catch (e)
  167. {
  168. if (error != null)
  169. {
  170. error(e);
  171. }
  172. }
  173. }), error);
  174. };
  175. /**
  176. * Adds the listener for automatically saving the diagram for local changes.
  177. */
  178. DrawioFile.prototype.mergeFile = function(file, success, error)
  179. {
  180. try
  181. {
  182. // Takes copy of current shadow document
  183. var shadow = (this.shadowPages != null) ? this.shadowPages :
  184. this.ui.getPagesForNode(mxUtils.parseXml(
  185. this.shadowData).documentElement);
  186. this.checkShadow(shadow);
  187. // Loads new document as shadow document
  188. this.shadowPages = this.ui.getPagesForNode(
  189. mxUtils.parseXml(file.data).
  190. documentElement)
  191. // Creates a patch for backup if the checksum fails
  192. this.backupPatch = (this.isModified()) ?
  193. this.ui.diffPages(shadow,
  194. this.ui.pages) : null;
  195. // Patches the current document
  196. var patches = [this.ui.diffPages(shadow, this.shadowPages)];
  197. if (!this.ignorePatches(patches))
  198. {
  199. // Patching previous shadow to verify checksum
  200. var patched = this.ui.patchPages(shadow, patches[0]);
  201. var checksum = this.ui.getHashValueForPages(patched);
  202. var current = this.ui.getHashValueForPages(this.shadowPages);
  203. if (urlParams['test'] == '1')
  204. {
  205. EditorUi.debug('File.mergeFile', [this],
  206. 'backup', this.backupPatch,
  207. 'patches', patches,
  208. 'checksum', current == checksum, checksum);
  209. }
  210. if (checksum != null && checksum != current)
  211. {
  212. var data = this.compressReportData(this.getAnonymizedXmlForPages(patched));
  213. this.checksumError(error, patches,
  214. 'Checksum: ' + checksum +
  215. '\nCurrent: ' + current +
  216. '\nPatched:\n' + data);
  217. // Abnormal termination
  218. return;
  219. }
  220. else
  221. {
  222. this.patch(patches,
  223. (DrawioFile.LAST_WRITE_WINS) ?
  224. this.backupPatch : null);
  225. this.checkPages();
  226. }
  227. }
  228. this.invalidChecksum = false;
  229. this.inConflictState = false;
  230. this.setDescriptor(file.getDescriptor());
  231. this.descriptorChanged();
  232. this.backupPatch = null;
  233. if (success != null)
  234. {
  235. success();
  236. }
  237. }
  238. catch (e)
  239. {
  240. this.inConflictState = true;
  241. this.invalidChecksum = true;
  242. if (error != null)
  243. {
  244. error(e);
  245. }
  246. try
  247. {
  248. this.sendErrorReport('Error in mergeFile', null, e);
  249. }
  250. catch (e2)
  251. {
  252. // ignore
  253. }
  254. }
  255. };
  256. /**
  257. * Adds the listener for automatically saving the diagram for local changes.
  258. */
  259. DrawioFile.prototype.getAnonymizedXmlForPages = function(pages)
  260. {
  261. var enc = new mxCodec(mxUtils.createXmlDocument());
  262. var file = enc.document.createElement('mxfile');
  263. if (pages != null)
  264. {
  265. for (var i = 0; i < pages.length; i++)
  266. {
  267. var temp = this.ui.anonymizeNode(enc.encode(
  268. new mxGraphModel(pages[i].root)));
  269. temp.setAttribute('id', pages[i].getId());
  270. if (pages[i].getName() != null)
  271. {
  272. temp.setAttribute('name', this.ui.anonymizeString(pages[i].getName()));
  273. }
  274. if (pages[i].viewState)
  275. {
  276. this.ui.editor.graph.saveViewState(pages[i].viewState, temp);
  277. }
  278. file.appendChild(temp);
  279. }
  280. }
  281. return mxUtils.getPrettyXml(file);
  282. };
  283. /**
  284. * Checks if the file is empty.
  285. */
  286. DrawioFile.prototype.checkPages = function()
  287. {
  288. if (this.ui.getCurrentFile() == this && (this.ui.pages == null ||
  289. this.ui.pages.length == 0))
  290. {
  291. this.sendErrorReport(
  292. 'Pages is null or empty',
  293. 'ShadowPages: ' + (this.shadowPages != null) +
  294. '\nShadowData: ' + (this.shadowData != null));
  295. }
  296. };
  297. /**
  298. * Checks if the given shadow is valid.
  299. */
  300. DrawioFile.prototype.checkShadow = function(shadow)
  301. {
  302. if (shadow == null || shadow.length == 0)
  303. {
  304. var data = (this.shadowData == null) ? 'null' :
  305. this.compressReportData(
  306. this.ui.anonymizeString(
  307. this.shadowData),
  308. null, 1000);
  309. this.sendErrorReport(
  310. 'Shadow is null or empty',
  311. 'Shadow: ' + ((shadow != null) ? shadow.length : 'null') +
  312. '\nShadowPages: ' + ((this.shadowPages != null) ?
  313. this.shadowPages.length : 'null') +
  314. '\nShadowData: ' + data);
  315. }
  316. };
  317. /**
  318. * Adds the listener for automatically saving the diagram for local changes.
  319. */
  320. DrawioFile.prototype.compressReportData = function(data, limit, max)
  321. {
  322. limit = (limit != null) ? limit : 10000;
  323. if (data != null && data.length > limit)
  324. {
  325. data = this.ui.editor.graph.compress(data) + '\n';
  326. }
  327. if (max != null && data != null && data.length > max)
  328. {
  329. data = data.substring(0, max) + '[...]';
  330. }
  331. return data;
  332. };
  333. /**
  334. * Adds the listener for automatically saving the diagram for local changes.
  335. */
  336. DrawioFile.prototype.checksumError = function(error, patches, details)
  337. {
  338. this.stats.checksumErrors++;
  339. this.inConflictState = true;
  340. this.invalidChecksum = true;
  341. this.descriptorChanged();
  342. if (this.sync != null)
  343. {
  344. this.sync.updateOnlineState();
  345. }
  346. if (error != null)
  347. {
  348. error();
  349. }
  350. try
  351. {
  352. if (patches != null)
  353. {
  354. for (var i = 0; i < patches.length; i++)
  355. {
  356. this.ui.anonymizePatch(patches[i]);
  357. }
  358. }
  359. var data = this.compressReportData(
  360. this.getAnonymizedXmlForPages(
  361. this.shadowPages));
  362. var json = this.compressReportData(
  363. JSON.stringify(patches, null, 2));
  364. this.sendErrorReport(
  365. 'Checksum Error',
  366. 'Patches:\n' + json +
  367. ((details != null) ? ('\n\n' + details) : '') +
  368. '\n\nData:\n' + data);
  369. }
  370. catch (e)
  371. {
  372. // ignore
  373. }
  374. };
  375. /**
  376. * Adds the listener for automatically saving the diagram for local changes.
  377. */
  378. DrawioFile.prototype.sendErrorReport = function(title, details, error)
  379. {
  380. try
  381. {
  382. var user = this.getCurrentUser();
  383. var uid = (user != null) ? this.ui.hashValue(user.id) : 'unknown';
  384. if (this.stats.start != null)
  385. {
  386. this.stats.uptime = Math.round((new Date().getTime() -
  387. new Date(this.stats.start).getTime()) / 1000);
  388. }
  389. var filename = this.getTitle();
  390. var dot = filename.lastIndexOf('.');
  391. var ext = 'none';
  392. if (dot > 0)
  393. {
  394. ext = filename.substring(dot);
  395. }
  396. var stack = (error != null) ? error.stack : new Error().stack;
  397. EditorUi.sendReport(title + ' ' + new Date().toISOString() + ':' +
  398. '\n\nBrowser=' + navigator.userAgent +
  399. '\nFile=' + this.getId() + ' (' + this.getMode() + ')' +
  400. ((this.sync != null) ? ('\nClient=' + this.sync.clientId) : '') +
  401. '\nUser=' + uid +
  402. '\nExt=' + ext +
  403. '\nSize=' + this.getSize() +
  404. '\nSync=' + DrawioFile.SYNC +
  405. '\n\nStats:\n' + JSON.stringify(this.stats, null, 2) +
  406. ((details != null) ? ('\n\n' + details) : '') +
  407. '\n\nStack:\n' + stack);
  408. }
  409. catch (e)
  410. {
  411. // ignore
  412. }
  413. };
  414. /**
  415. * Adds the listener for automatically saving the diagram for local changes.
  416. */
  417. DrawioFile.prototype.reloadFile = function(success, error)
  418. {
  419. try
  420. {
  421. this.ui.spinner.stop();
  422. var fn = mxUtils.bind(this, function()
  423. {
  424. this.stats.reloads++;
  425. this.reportEnabled = false;
  426. // Restores view state and current page
  427. var viewState = this.ui.editor.graph.getViewState();
  428. var selection = this.ui.editor.graph.getSelectionCells();
  429. var page = this.ui.currentPage;
  430. this.ui.loadFile(this.getHash(), true, null, mxUtils.bind(this, function()
  431. {
  432. if (this.ui.fileLoadedError == null)
  433. {
  434. this.ui.restoreViewState(page, viewState, selection);
  435. if (this.backupPatch != null)
  436. {
  437. this.patch([this.backupPatch]);
  438. }
  439. // Carry-over stats
  440. var file = this.ui.getCurrentFile();
  441. if (file != null)
  442. {
  443. file.stats = this.stats;
  444. }
  445. if (success != null)
  446. {
  447. success();
  448. }
  449. }
  450. }), true);
  451. });
  452. if (this.isModified() && this.backupPatch == null)
  453. {
  454. this.ui.confirm(mxResources.get('allChangesLost'), mxUtils.bind(this, function()
  455. {
  456. this.handleFileSuccess(DrawioFile.SYNC == 'manual');
  457. }), fn, mxResources.get('cancel'), mxResources.get('discardChanges'));
  458. }
  459. else
  460. {
  461. fn();
  462. }
  463. }
  464. catch (e)
  465. {
  466. if (error != null)
  467. {
  468. error(e);
  469. }
  470. }
  471. };
  472. /**
  473. * Shows a conflict dialog to the user.
  474. */
  475. DrawioFile.prototype.copyFile = function(success, error)
  476. {
  477. if (this.constructor == DriveFile && !this.isRestricted())
  478. {
  479. this.makeCopy(mxUtils.bind(this, function()
  480. {
  481. if (this.ui.spinner.spin(document.body, mxResources.get('saving')))
  482. {
  483. try
  484. {
  485. this.save(true, success, error)
  486. }
  487. catch (e)
  488. {
  489. error(e);
  490. }
  491. }
  492. }), error, true);
  493. }
  494. else
  495. {
  496. this.ui.editor.editAsNew(this.ui.getFileData(true),
  497. this.ui.getCopyFilename(this));
  498. }
  499. };
  500. /**
  501. * Returns true if the patches in the given array are empty.
  502. */
  503. DrawioFile.prototype.ignorePatches = function(patches)
  504. {
  505. var ignore = true;
  506. for (var i = 0; i < patches.length && ignore; i++)
  507. {
  508. ignore = ignore && Object.keys(patches[i]).length == 0;
  509. }
  510. return ignore;
  511. };
  512. /**
  513. * Applies the given patches to the file.
  514. */
  515. DrawioFile.prototype.patch = function(patches, resolver)
  516. {
  517. // Saves state of undo history
  518. var undoMgr = this.ui.editor.undoManager;
  519. var history = undoMgr.history.slice();
  520. var nextAdd = undoMgr.indexOfNextAdd;
  521. // Hides graph during updates
  522. var graph = this.ui.editor.graph;
  523. graph.container.style.visibility = 'hidden';
  524. // Ignores change events
  525. var prev = this.changeListenerEnabled;
  526. this.changeListenerEnabled = false;
  527. // Folding and math change require special handling
  528. var fold = graph.foldingEnabled;
  529. var math = graph.mathEnabled;
  530. // Updates text editor if cell changes during validation
  531. var redraw = graph.cellRenderer.redraw;
  532. graph.cellRenderer.redraw = function(state)
  533. {
  534. if (state.view.graph.isEditing(state.cell))
  535. {
  536. state.view.graph.scrollCellToVisible(state.cell);
  537. state.view.graph.cellEditor.resize();
  538. }
  539. redraw.apply(this, arguments);
  540. };
  541. graph.model.beginUpdate();
  542. try
  543. {
  544. // Applies patches
  545. for (var i = 0; i < patches.length; i++)
  546. {
  547. this.ui.pages = this.ui.patchPages(this.ui.pages,
  548. patches[i], true, resolver, this.isModified());
  549. }
  550. // Always needs at least one page
  551. if (this.ui.pages.length == 0)
  552. {
  553. this.ui.pages.push(this.ui.createPage());
  554. }
  555. // Checks if current page was removed
  556. if (mxUtils.indexOf(this.ui.pages, this.ui.currentPage) < 0)
  557. {
  558. this.ui.selectPage(this.ui.pages[0], true);
  559. }
  560. }
  561. finally
  562. {
  563. graph.model.endUpdate();
  564. // Restores previous state
  565. graph.container.style.visibility = '';
  566. graph.cellRenderer.redraw = redraw;
  567. this.changeListenerEnabled = prev;
  568. // Restores history state
  569. undoMgr.history = history;
  570. undoMgr.indexOfNextAdd = nextAdd;
  571. undoMgr.fireEvent(new mxEventObject(mxEvent.CLEAR));
  572. if (this.ui.currentPage == null || this.ui.currentPage.needsUpdate)
  573. {
  574. // Updates the graph and background
  575. if (math != graph.mathEnabled)
  576. {
  577. this.ui.editor.updateGraphComponents();
  578. graph.refresh();
  579. }
  580. else
  581. {
  582. if (fold != graph.foldingEnabled)
  583. {
  584. graph.view.revalidate();
  585. }
  586. else
  587. {
  588. graph.view.validate();
  589. }
  590. graph.sizeDidChange();
  591. }
  592. // Updates view state in format panel if nothing is selected
  593. if (this.ui.format != null && graph.isSelectionEmpty())
  594. {
  595. this.ui.format.refresh();
  596. }
  597. }
  598. this.ui.updateTabContainer();
  599. }
  600. };
  601. /**
  602. * Adds the listener for automatically saving the diagram for local changes.
  603. */
  604. DrawioFile.prototype.save = function(revision, success, error, unloading, overwrite, manual)
  605. {
  606. if (!this.isEditable())
  607. {
  608. if (error != null)
  609. {
  610. error({message: mxResources.get('readOnly')});
  611. }
  612. else
  613. {
  614. throw new Error(mxResources.get('readOnly'));
  615. }
  616. }
  617. else if (!overwrite && this.invalidChecksum)
  618. {
  619. if (error != null)
  620. {
  621. error({message: mxResources.get('checksum')});
  622. }
  623. else
  624. {
  625. throw new Error(mxResources.get('checksum'));
  626. }
  627. }
  628. else
  629. {
  630. this.updateFileData();
  631. this.clearAutosave();
  632. }
  633. };
  634. /**
  635. * Translates this point by the given vector.
  636. *
  637. * @param {number} dx X-coordinate of the translation.
  638. * @param {number} dy Y-coordinate of the translation.
  639. */
  640. DrawioFile.prototype.updateFileData = function()
  641. {
  642. this.setData(this.ui.getFileData(null, null, null, null, null, null, null, null, this));
  643. };
  644. /**
  645. * Translates this point by the given vector.
  646. *
  647. * @param {number} dx X-coordinate of the translation.
  648. * @param {number} dy Y-coordinate of the translation.
  649. */
  650. DrawioFile.prototype.saveAs = function(filename, success, error) { };
  651. /**
  652. * Translates this point by the given vector.
  653. *
  654. * @param {number} dx X-coordinate of the translation.
  655. * @param {number} dy Y-coordinate of the translation.
  656. */
  657. DrawioFile.prototype.saveFile = function(title, revision, success, error) { };
  658. /**
  659. * Returns true if copy, export and print are not allowed for this file.
  660. */
  661. DrawioFile.prototype.getPublicUrl = function(fn)
  662. {
  663. fn(null);
  664. };
  665. /**
  666. * Returns true if copy, export and print are not allowed for this file.
  667. */
  668. DrawioFile.prototype.isRestricted = function()
  669. {
  670. return false;
  671. };
  672. /**
  673. * Translates this point by the given vector.
  674. *
  675. * @param {number} dx X-coordinate of the translation.
  676. * @param {number} dy Y-coordinate of the translation.
  677. */
  678. DrawioFile.prototype.isModified = function()
  679. {
  680. return this.modified;
  681. };
  682. /**
  683. * Translates this point by the given vector.
  684. *
  685. * @param {number} dx X-coordinate of the translation.
  686. * @param {number} dy Y-coordinate of the translation.
  687. */
  688. DrawioFile.prototype.setModified = function(value)
  689. {
  690. this.modified = value;
  691. };
  692. /**
  693. * Specifies if the autosave checkbox should be shown in the document
  694. * properties dialog. Default is false.
  695. */
  696. DrawioFile.prototype.isAutosaveOptional = function()
  697. {
  698. return false;
  699. };
  700. /**
  701. * Translates this point by the given vector.
  702. *
  703. * @param {number} dx X-coordinate of the translation.
  704. * @param {number} dy Y-coordinate of the translation.
  705. */
  706. DrawioFile.prototype.isAutosave = function()
  707. {
  708. return !this.inConflictState && this.ui.editor.autosave;
  709. };
  710. /**
  711. * Translates this point by the given vector.
  712. *
  713. * @param {number} dx X-coordinate of the translation.
  714. * @param {number} dy Y-coordinate of the translation.
  715. */
  716. DrawioFile.prototype.isRenamable = function()
  717. {
  718. return false;
  719. };
  720. /**
  721. * Translates this point by the given vector.
  722. *
  723. * @param {number} dx X-coordinate of the translation.
  724. * @param {number} dy Y-coordinate of the translation.
  725. */
  726. DrawioFile.prototype.rename = function(title, success, error) { };
  727. /**
  728. * Translates this point by the given vector.
  729. *
  730. * @param {number} dx X-coordinate of the translation.
  731. * @param {number} dy Y-coordinate of the translation.
  732. */
  733. DrawioFile.prototype.isMovable = function()
  734. {
  735. return false;
  736. };
  737. /**
  738. * Translates this point by the given vector.
  739. *
  740. * @param {number} dx X-coordinate of the translation.
  741. * @param {number} dy Y-coordinate of the translation.
  742. */
  743. DrawioFile.prototype.move = function(folderId, success, error) { };
  744. /**
  745. * Returns the hash of the file which consists of a prefix for the storage
  746. * type and the ID of the file.
  747. */
  748. DrawioFile.prototype.getHash = function()
  749. {
  750. return '';
  751. };
  752. /**
  753. * Returns the ID of the file.
  754. */
  755. DrawioFile.prototype.getId = function()
  756. {
  757. return '';
  758. };
  759. /**
  760. * Returns true if the file is editable.
  761. */
  762. DrawioFile.prototype.isEditable = function()
  763. {
  764. return !this.ui.editor.isChromelessView() || this.ui.editor.editable;
  765. };
  766. /**
  767. * Returns the location as a new object.
  768. * @type mx.Point
  769. */
  770. DrawioFile.prototype.getUi = function()
  771. {
  772. return this.ui;
  773. };
  774. /**
  775. * Returns the current title of the file.
  776. */
  777. DrawioFile.prototype.getTitle = function()
  778. {
  779. return '';
  780. };
  781. /**
  782. * Sets the current data of the file.
  783. */
  784. DrawioFile.prototype.setData = function(data)
  785. {
  786. this.data = data;
  787. };
  788. /**
  789. * Returns the current data of the file.
  790. */
  791. DrawioFile.prototype.getData = function()
  792. {
  793. return this.data;
  794. };
  795. /**
  796. * Opens this file in the editor.
  797. */
  798. DrawioFile.prototype.open = function()
  799. {
  800. var data = this.getData();
  801. if (data != null)
  802. {
  803. this.ui.setFileData(data);
  804. // Updates shadow in case any page IDs have been updated
  805. this.shadowData = mxUtils.getXml(this.ui.getXmlFileData());
  806. this.stats.openTimestamp = new Date().toISOString();
  807. this.stats.openShadowLength = (this.shadowData != null) ?
  808. this.shadowData.length : 0;
  809. }
  810. this.installListeners();
  811. if (this.isSyncSupported())
  812. {
  813. this.startSync();
  814. }
  815. };
  816. /**
  817. * Hook for subclassers.
  818. */
  819. DrawioFile.prototype.isSyncSupported = function()
  820. {
  821. return false;
  822. };
  823. /**
  824. * Hook for subclassers.
  825. */
  826. DrawioFile.prototype.isRevisionHistorySupported = function()
  827. {
  828. return false;
  829. };
  830. /**
  831. * Hook for subclassers.
  832. */
  833. DrawioFile.prototype.getRevisions = function(success, error)
  834. {
  835. success(null);
  836. };
  837. /**
  838. * Hook for subclassers to get the latest descriptor of this file
  839. * and return it in the success handler.
  840. */
  841. DrawioFile.prototype.loadDescriptor = function(success, error)
  842. {
  843. success(null);
  844. };
  845. /**
  846. * Hook for subclassers to get the latest etag of this file
  847. * and return it in the success handler.
  848. */
  849. DrawioFile.prototype.loadPatchDescriptor = function(success, error)
  850. {
  851. this.loadDescriptor(mxUtils.bind(this, function(desc)
  852. {
  853. success(desc);
  854. }), error);
  855. };
  856. /**
  857. * Creates a starts the synchronization.
  858. */
  859. DrawioFile.prototype.startSync = function()
  860. {
  861. if (DrawioFile.SYNC == 'auto' && urlParams['stealth'] != '1')
  862. {
  863. this.sync = new DrawioFileSync(this);
  864. this.sync.start();
  865. }
  866. };
  867. /**
  868. * Hook for subclassers to check if an error is a conflict.
  869. */
  870. DrawioFile.prototype.isConflict = function()
  871. {
  872. return false;
  873. };
  874. /**
  875. * Gets the channel ID for sync messages.
  876. */
  877. DrawioFile.prototype.getChannelId = function()
  878. {
  879. // Slash, space and plus replaced with underscore
  880. return this.ui.editor.graph.compress(this.getHash()).replace(/[\/ +]/g, '_');
  881. };
  882. /**
  883. * Gets the channel ID from the given descriptor.
  884. */
  885. DrawioFile.prototype.getChannelKey = function(desc)
  886. {
  887. return null;
  888. };
  889. /**
  890. * Returns the current etag.
  891. */
  892. DrawioFile.prototype.getCurrentUser = function()
  893. {
  894. return null;
  895. };
  896. /**
  897. * Hook for subclassers to get the latest version of this file
  898. * and return it in the success handler.
  899. */
  900. DrawioFile.prototype.getLatestVersion = function(success, error)
  901. {
  902. success(null);
  903. };
  904. /**
  905. * Returns the last modified date of this file.
  906. */
  907. DrawioFile.prototype.getLastModifiedDate = function()
  908. {
  909. return new Date();
  910. };
  911. /**
  912. * Sets the current etag.
  913. */
  914. DrawioFile.prototype.setCurrentEtag = function(etag)
  915. {
  916. this.setDescriptorEtag(this.getDescriptor(), etag);
  917. };
  918. /**
  919. * Returns the current etag.
  920. */
  921. DrawioFile.prototype.getCurrentEtag = function()
  922. {
  923. return this.getDescriptorEtag(this.getDescriptor());
  924. };
  925. /**
  926. * Returns the descriptor from this file.
  927. */
  928. DrawioFile.prototype.getDescriptor = function()
  929. {
  930. return null;
  931. };
  932. /**
  933. * Sets the descriptor for this file.
  934. */
  935. DrawioFile.prototype.setDescriptor = function() { };
  936. /**
  937. * Updates the etag on the given descriptor.
  938. */
  939. DrawioFile.prototype.setDescriptorEtag = function(desc, etag) { };
  940. /**
  941. * Returns the etag from the given descriptor.
  942. */
  943. DrawioFile.prototype.getDescriptorEtag = function(desc)
  944. {
  945. return null;
  946. };
  947. /**
  948. * Returns the secret from the given descriptor.
  949. */
  950. DrawioFile.prototype.getDescriptorSecret = function(desc)
  951. {
  952. return null;
  953. };
  954. /**
  955. * Installs the change listener.
  956. */
  957. DrawioFile.prototype.installListeners = function()
  958. {
  959. if (this.changeListener == null)
  960. {
  961. this.changeListener = mxUtils.bind(this, function(sender, eventObject)
  962. {
  963. var edit = (eventObject != null) ? eventObject.getProperty('edit') : null;
  964. if (this.changeListenerEnabled && this.isEditable() && (edit == null || !edit.ignoreEdit))
  965. {
  966. this.fileChanged();
  967. }
  968. });
  969. this.ui.editor.graph.model.addListener(mxEvent.CHANGE, this.changeListener);
  970. // Some options trigger autosave
  971. this.ui.editor.graph.addListener('gridSizeChanged', this.changeListener);
  972. this.ui.editor.graph.addListener('shadowVisibleChanged', this.changeListener);
  973. this.ui.addListener('pageFormatChanged', this.changeListener);
  974. this.ui.addListener('pageScaleChanged', this.changeListener);
  975. this.ui.addListener('backgroundColorChanged', this.changeListener);
  976. this.ui.addListener('backgroundImageChanged', this.changeListener);
  977. this.ui.addListener('foldingEnabledChanged', this.changeListener);
  978. this.ui.addListener('mathEnabledChanged', this.changeListener);
  979. this.ui.addListener('gridEnabledChanged', this.changeListener);
  980. this.ui.addListener('guidesEnabledChanged', this.changeListener);
  981. this.ui.addListener('pageViewChanged', this.changeListener);
  982. }
  983. };
  984. /**
  985. * Returns the location as a new object.
  986. * @type mx.Point
  987. */
  988. DrawioFile.prototype.addAllSavedStatus = function(status)
  989. {
  990. if (this.ui.statusContainer != null)
  991. {
  992. status = (status != null) ? status : mxUtils.htmlEntities(mxResources.get(this.allChangesSavedKey));
  993. if (this.constructor == DriveFile || this.constructor == DropboxFile)
  994. {
  995. this.ui.editor.setStatus('<div title="'+ mxUtils.htmlEntities(mxResources.get('revisionHistory')) +
  996. '" style="text-decoration:underline;cursor:pointer;">' + status + '</div>');
  997. var links = this.ui.statusContainer.getElementsByTagName('div');
  998. if (links.length > 0)
  999. {
  1000. mxEvent.addListener(links[0], 'click', mxUtils.bind(this, function()
  1001. {
  1002. this.ui.actions.get('revisionHistory').funct();
  1003. }));
  1004. }
  1005. }
  1006. else
  1007. {
  1008. this.ui.editor.setStatus(status);
  1009. }
  1010. }
  1011. };
  1012. /**
  1013. * Adds the listener for automatically saving the diagram for local changes.
  1014. */
  1015. DrawioFile.prototype.addUnsavedStatus = function(err)
  1016. {
  1017. if (!this.inConflictState && this.ui.statusContainer != null)
  1018. {
  1019. if (err instanceof Error && err.message != null && err.message != '')
  1020. {
  1021. this.ui.editor.setStatus('<div class="geStatusAlert" style="overflow:hidden;">' +
  1022. mxUtils.htmlEntities(mxResources.get('unsavedChanges')) +
  1023. ' (' + mxUtils.htmlEntities(err.message) + ')</div>');
  1024. }
  1025. else
  1026. {
  1027. // FIXME: Handle multiple tabs
  1028. // if (this.ui.mode == null && urlParams['splash'] == '0')
  1029. // {
  1030. // try
  1031. // {
  1032. // this.ui.updateDraft();
  1033. // this.setModified(false);
  1034. // }
  1035. // catch (e)
  1036. // {
  1037. // // Keeps modified flag unchanged
  1038. // }
  1039. // }
  1040. var msg = this.getErrorMessage(err);
  1041. if (msg != null && msg.length > 60)
  1042. {
  1043. msg = msg.substring(0, 60) + '...';
  1044. }
  1045. this.ui.editor.setStatus('<div class="geStatusAlert" style="cursor:pointer;overflow:hidden;">' +
  1046. mxUtils.htmlEntities(mxResources.get('unsavedChangesClickHereToSave')) +
  1047. ((msg != null && msg != '') ? ' (' + mxUtils.htmlEntities(msg) + ')' : '') + '</div>');
  1048. // Installs click handler for saving
  1049. var links = this.ui.statusContainer.getElementsByTagName('div');
  1050. if (links != null && links.length > 0)
  1051. {
  1052. mxEvent.addListener(links[0], 'click', mxUtils.bind(this, function()
  1053. {
  1054. this.ui.actions.get((this.ui.mode == null || !this.isEditable()) ?
  1055. 'saveAs' : 'save').funct();
  1056. }));
  1057. }
  1058. else
  1059. {
  1060. this.ui.editor.setStatus('<div class="geStatusAlert" style="overflow:hidden;">' +
  1061. mxUtils.htmlEntities(mxResources.get('unsavedChanges')) + '</div>');
  1062. }
  1063. }
  1064. }
  1065. };
  1066. /**
  1067. * Halts all timers and shows a conflict status message. The optional error
  1068. * handler is invoked first.
  1069. */
  1070. DrawioFile.prototype.addConflictStatus = function(fn, message)
  1071. {
  1072. if (this.invalidChecksum)
  1073. {
  1074. message = mxResources.get('checksum');
  1075. }
  1076. this.setConflictStatus(mxUtils.htmlEntities(mxResources.get('fileChangedSync')) +
  1077. ((message != null && message != '') ? ' (' + mxUtils.htmlEntities(message) + ')' : ''));
  1078. this.ui.spinner.stop();
  1079. this.clearAutosave();
  1080. var links = (this.ui.statusContainer != null) ? this.ui.statusContainer.getElementsByTagName('div') : null;
  1081. if (links != null && links.length > 0)
  1082. {
  1083. mxEvent.addListener(links[0], 'click', mxUtils.bind(this, function(evt)
  1084. {
  1085. if (mxEvent.getSource(evt).nodeName != 'IMG')
  1086. {
  1087. fn();
  1088. }
  1089. }));
  1090. }
  1091. else
  1092. {
  1093. fn()
  1094. }
  1095. };
  1096. /**
  1097. * Halts all timers and shows a conflict status message. The optional error
  1098. * handler is invoked first.
  1099. */
  1100. DrawioFile.prototype.setConflictStatus = function(message)
  1101. {
  1102. this.ui.editor.setStatus('<div class="geStatusAlert geBlink" style="cursor:pointer;overflow:hidden;">' + message +
  1103. ' <a href="https://desk.draw.io/support/solutions/articles/16000087947" target="_blank"><img border="0" ' +
  1104. 'title="' + mxUtils.htmlEntities(mxResources.get('help')) + '" style="margin-left:2px;cursor:help;' +
  1105. 'opacity:0.5;width:16px;height:16px;" valign="bottom" src="' + Editor.helpImage + '" style=""/></a></div>');
  1106. };
  1107. /**
  1108. * Shows a conflict dialog to the user.
  1109. */
  1110. DrawioFile.prototype.showRefreshDialog = function(success, error)
  1111. {
  1112. // Allows for escape key to be pressed while dialog is showing
  1113. this.addConflictStatus(mxUtils.bind(this, function()
  1114. {
  1115. this.showRefreshDialog(success, error);
  1116. }));
  1117. this.ui.showError(mxResources.get('error') + ' (' + mxResources.get('checksum') + ')',
  1118. mxResources.get('fileChangedSyncDialog'),
  1119. mxResources.get('makeCopy'), mxUtils.bind(this, function()
  1120. {
  1121. this.copyFile(success, error);
  1122. }), null, mxResources.get('synchronize'), mxUtils.bind(this, function()
  1123. {
  1124. this.reloadFile(success, error);
  1125. }), mxResources.get('cancel'), mxUtils.bind(this, function()
  1126. {
  1127. this.ui.hideDialog();
  1128. }), 360, 150);
  1129. };
  1130. /**
  1131. * Shows a dialog with no synchronize option.
  1132. */
  1133. DrawioFile.prototype.showCopyDialog = function(success, error, overwrite)
  1134. {
  1135. this.inConflictState = false;
  1136. this.invalidChecksum = false;
  1137. this.addUnsavedStatus();
  1138. this.ui.showError(mxResources.get('externalChanges'),
  1139. mxResources.get('fileChangedOverwriteDialog'),
  1140. mxResources.get('makeCopy'), mxUtils.bind(this, function()
  1141. {
  1142. this.copyFile(success, error);
  1143. }), null, mxResources.get('overwrite'), overwrite,
  1144. mxResources.get('cancel'), mxUtils.bind(this, function()
  1145. {
  1146. this.ui.hideDialog();
  1147. }), 360, (this.constructor == DriveFile) ? 180 : 150);
  1148. // Adds important notice to dialog
  1149. if (this.ui.dialog != null && this.ui.dialog.container != null &&
  1150. this.constructor == DriveFile)
  1151. {
  1152. var alert = this.ui.createRealtimeNotice();
  1153. alert.style.left = '0';
  1154. alert.style.right = '0';
  1155. alert.style.borderRadius = '0';
  1156. alert.style.borderLeftStyle = 'none';
  1157. alert.style.borderRightStyle = 'none';
  1158. alert.style.marginBottom = '26px';
  1159. alert.style.padding = '8px 0 8px 0';
  1160. this.ui.dialog.container.appendChild(alert);
  1161. }
  1162. };
  1163. /**
  1164. * Shows a conflict dialog to the user.
  1165. */
  1166. DrawioFile.prototype.showConflictDialog = function(overwrite, synchronize)
  1167. {
  1168. this.ui.showError(mxResources.get('externalChanges'),
  1169. mxResources.get('fileChangedSyncDialog'),
  1170. mxResources.get('overwrite'), overwrite, null,
  1171. mxResources.get('synchronize'), synchronize,
  1172. mxResources.get('cancel'), mxUtils.bind(this, function()
  1173. {
  1174. this.ui.hideDialog();
  1175. this.handleFileError(null, false);
  1176. }), 340, 150);
  1177. };
  1178. /**
  1179. * Checks if the client is authorized and calls the next step.
  1180. */
  1181. DrawioFile.prototype.redirectToNewApp = function(error)
  1182. {
  1183. this.ui.spinner.stop();
  1184. if (!this.redirectDialogShowing)
  1185. {
  1186. this.redirectDialogShowing = true;
  1187. var url = window.location.protocol + '//' + window.location.host + '/' + this.ui.getSearch(
  1188. ['create', 'title', 'mode', 'url', 'drive', 'splash', 'state']) + '#' + this.getHash();
  1189. var redirect = mxUtils.bind(this, function()
  1190. {
  1191. this.ui.spinner.spin(document.body, mxResources.get('loading'));
  1192. this.redirectDialogShowing = false;
  1193. if (window.location.href == url)
  1194. {
  1195. window.location.reload();
  1196. }
  1197. else
  1198. {
  1199. window.location.href = url;
  1200. }
  1201. });
  1202. if (error != null)
  1203. {
  1204. this.ui.confirm(mxResources.get('redirectToNewApp'), redirect, mxUtils.bind(this, function()
  1205. {
  1206. this.redirectDialogShowing = false;
  1207. if (error != null)
  1208. {
  1209. error();
  1210. }
  1211. }));
  1212. }
  1213. else
  1214. {
  1215. this.ui.alert(mxResources.get('redirectToNewApp'), redirect);
  1216. }
  1217. }
  1218. };
  1219. /**
  1220. * Adds the listener for automatically saving the diagram for local changes.
  1221. */
  1222. DrawioFile.prototype.handleFileSuccess = function(saved)
  1223. {
  1224. this.ui.spinner.stop();
  1225. if (this.ui.getCurrentFile() == this)
  1226. {
  1227. if (this.isModified())
  1228. {
  1229. this.fileChanged();
  1230. }
  1231. else if (saved)
  1232. {
  1233. this.addAllSavedStatus();
  1234. if (this.sync != null)
  1235. {
  1236. this.sync.resetUpdateStatusThread();
  1237. if (this.sync.remoteFileChanged)
  1238. {
  1239. this.sync.remoteFileChanged = false;
  1240. this.sync.fileChangedNotify();
  1241. }
  1242. }
  1243. }
  1244. else
  1245. {
  1246. this.ui.editor.setStatus('');
  1247. }
  1248. }
  1249. };
  1250. /**
  1251. * Adds the listener for automatically saving the diagram for local changes.
  1252. */
  1253. DrawioFile.prototype.handleFileError = function(err, manual)
  1254. {
  1255. this.ui.spinner.stop();
  1256. if (this.ui.getCurrentFile() == this)
  1257. {
  1258. if (this.inConflictState)
  1259. {
  1260. this.handleConflictError(err, manual);
  1261. }
  1262. else
  1263. {
  1264. if (this.isModified())
  1265. {
  1266. this.addUnsavedStatus(err);
  1267. }
  1268. if (manual)
  1269. {
  1270. this.ui.handleError(err, (err != null) ? mxResources.get('errorSavingFile') : null);
  1271. }
  1272. else if (!this.isModified())
  1273. {
  1274. var msg = (err != null) ? ((err.error != null) ? err.error.message : err.message) : null;
  1275. if (msg != null && msg.length > 60)
  1276. {
  1277. msg = msg.substring(0, 60) + '...';
  1278. }
  1279. this.ui.editor.setStatus('<div class="geStatusAlert" style="cursor:pointer;overflow:hidden;">' +
  1280. mxUtils.htmlEntities(mxResources.get('error')) +
  1281. ((msg != null) ? ' (' + mxUtils.htmlEntities(msg) + ')' : '') + '</div>');
  1282. }
  1283. }
  1284. }
  1285. };
  1286. /**
  1287. * Adds the listener for automatically saving the diagram for local changes.
  1288. */
  1289. DrawioFile.prototype.handleConflictError = function(err, manual)
  1290. {
  1291. var success = mxUtils.bind(this, function()
  1292. {
  1293. this.handleFileSuccess(true);
  1294. });
  1295. var error = mxUtils.bind(this, function(err2)
  1296. {
  1297. this.handleFileError(err2, true);
  1298. });
  1299. var overwrite = mxUtils.bind(this, function()
  1300. {
  1301. if (this.ui.spinner.spin(document.body, mxResources.get('saving')))
  1302. {
  1303. this.ui.editor.setStatus('');
  1304. this.save(true, success, error, null, true, (this.constructor ==
  1305. GitHubFile && err != null) ? err.commitMessage : null)
  1306. }
  1307. });
  1308. var synchronize = mxUtils.bind(this, function()
  1309. {
  1310. if (this.ui.spinner.spin(document.body, mxResources.get('updatingDocument')))
  1311. {
  1312. this.synchronizeFile(mxUtils.bind(this, function()
  1313. {
  1314. this.ui.spinner.stop();
  1315. if (this.ui.spinner.spin(document.body, mxResources.get('saving')))
  1316. {
  1317. this.save(true, success, error, null, null, (this.constructor ==
  1318. GitHubFile && err != null) ? err.commitMessage : null)
  1319. }
  1320. }), error);
  1321. }
  1322. })
  1323. if (DrawioFile.SYNC == 'none')
  1324. {
  1325. this.showCopyDialog(success, error, overwrite);
  1326. }
  1327. else if (this.invalidChecksum)
  1328. {
  1329. this.showRefreshDialog(success, error);
  1330. }
  1331. else if (manual)
  1332. {
  1333. this.showConflictDialog(overwrite, synchronize);
  1334. }
  1335. else
  1336. {
  1337. this.addConflictStatus(mxUtils.bind(this, function()
  1338. {
  1339. this.ui.editor.setStatus(mxUtils.htmlEntities(
  1340. mxResources.get('updatingDocument')));
  1341. this.synchronizeFile(success, error);
  1342. }), this.getErrorMessage(err));
  1343. }
  1344. };
  1345. /**
  1346. * Adds the listener for automatically saving the diagram for local changes.
  1347. */
  1348. DrawioFile.prototype.getErrorMessage = function(err)
  1349. {
  1350. return (err != null) ? ((err.error != null) ? err.error.message : err.message) : null
  1351. };
  1352. /**
  1353. * Adds the listener for automatically saving the diagram for local changes.
  1354. */
  1355. DrawioFile.prototype.fileChanged = function()
  1356. {
  1357. this.setModified(true);
  1358. if (this.isAutosave())
  1359. {
  1360. this.addAllSavedStatus(mxUtils.htmlEntities(mxResources.get('saving')) + '...');
  1361. this.autosave(this.autosaveDelay, this.maxAutosaveDelay, mxUtils.bind(this, function(resp)
  1362. {
  1363. // Does not update status if another autosave was scheduled
  1364. if (this.autosaveThread == null)
  1365. {
  1366. this.handleFileSuccess(true);
  1367. }
  1368. }), mxUtils.bind(this, function(err)
  1369. {
  1370. this.handleFileError(err);
  1371. }));
  1372. }
  1373. else if ((!this.isAutosaveOptional() || !this.ui.editor.autosave) &&
  1374. !this.inConflictState)
  1375. {
  1376. this.addUnsavedStatus();
  1377. }
  1378. };
  1379. /**
  1380. * Invokes sync and updates shadow document.
  1381. */
  1382. DrawioFile.prototype.fileSaved = function(savedData, lastDesc, success, error)
  1383. {
  1384. this.inConflictState = false;
  1385. this.invalidChecksum = false;
  1386. this.checkPages();
  1387. if (this.sync == null)
  1388. {
  1389. this.shadowData = savedData;
  1390. this.shadowPages = null;
  1391. if (success != null)
  1392. {
  1393. success();
  1394. }
  1395. }
  1396. else
  1397. {
  1398. this.sync.fileSaved(this.ui.getPagesForNode(
  1399. mxUtils.parseXml(savedData).documentElement),
  1400. lastDesc, success, error);
  1401. }
  1402. };
  1403. /**
  1404. * Adds the listener for automatically saving the diagram for local changes.
  1405. */
  1406. DrawioFile.prototype.autosave = function(delay, maxDelay, success, error)
  1407. {
  1408. if (this.lastAutosave == null)
  1409. {
  1410. this.lastAutosave = new Date().getTime();
  1411. }
  1412. var tmp = (new Date().getTime() - this.lastAutosave < maxDelay) ? delay : 0;
  1413. this.clearAutosave();
  1414. // Starts new timer or executes immediately if not unsaved for maxDelay
  1415. var thread = window.setTimeout(mxUtils.bind(this, function()
  1416. {
  1417. this.lastAutosave = null;
  1418. if (this.autosaveThread == thread)
  1419. {
  1420. this.autosaveThread = null;
  1421. }
  1422. // Workaround for duplicate save if UI is blocking
  1423. // after save while pending autosave triggers
  1424. if (this.isModified() && this.isAutosaveNow())
  1425. {
  1426. var rev = this.isAutosaveRevision();
  1427. if (rev)
  1428. {
  1429. this.lastAutosaveRevision = new Date().getTime();
  1430. }
  1431. this.save(rev, mxUtils.bind(this, function(resp)
  1432. {
  1433. this.autosaveCompleted();
  1434. if (success != null)
  1435. {
  1436. success(resp);
  1437. }
  1438. }), mxUtils.bind(this, function(resp)
  1439. {
  1440. if (error != null)
  1441. {
  1442. error(resp);
  1443. }
  1444. }));
  1445. }
  1446. else
  1447. {
  1448. if (!this.isModified())
  1449. {
  1450. this.ui.editor.setStatus('');
  1451. }
  1452. if (success != null)
  1453. {
  1454. success(null);
  1455. }
  1456. }
  1457. }), tmp);
  1458. this.autosaveThread = thread;
  1459. };
  1460. /**
  1461. * Returns true if an autosave is required at the time of execution.
  1462. * This implementation returns true.
  1463. */
  1464. DrawioFile.prototype.isAutosaveNow = function()
  1465. {
  1466. return true;
  1467. };
  1468. /**
  1469. * Hooks for subclassers after the autosave has completed.
  1470. */
  1471. DrawioFile.prototype.autosaveCompleted = function() { };
  1472. /**
  1473. * Adds the listener for automatically saving the diagram for local changes.
  1474. */
  1475. DrawioFile.prototype.clearAutosave = function()
  1476. {
  1477. if (this.autosaveThread != null)
  1478. {
  1479. window.clearTimeout(this.autosaveThread);
  1480. this.autosaveThread = null;
  1481. }
  1482. };
  1483. /**
  1484. * Returns the location as a new object.
  1485. * @type mx.Point
  1486. */
  1487. DrawioFile.prototype.isAutosaveRevision = function()
  1488. {
  1489. var now = new Date().getTime();
  1490. return (this.lastAutosaveRevision == null) || (now - this.lastAutosaveRevision) > this.maxAutosaveRevisionDelay;
  1491. };
  1492. /**
  1493. * Translates this point by the given vector.
  1494. *
  1495. * @param {number} dx X-coordinate of the translation.
  1496. * @param {number} dy Y-coordinate of the translation.
  1497. */
  1498. DrawioFile.prototype.descriptorChanged = function()
  1499. {
  1500. this.fireEvent(new mxEventObject('descriptorChanged'));
  1501. };
  1502. /**
  1503. * Translates this point by the given vector.
  1504. *
  1505. * @param {number} dx X-coordinate of the translation.
  1506. * @param {number} dy Y-coordinate of the translation.
  1507. */
  1508. DrawioFile.prototype.contentChanged = function()
  1509. {
  1510. this.fireEvent(new mxEventObject('contentChanged'));
  1511. };
  1512. /**
  1513. * Returns the location as a new object.
  1514. */
  1515. DrawioFile.prototype.close = function(unloading)
  1516. {
  1517. if (this.isAutosave() && this.isModified())
  1518. {
  1519. this.save(this.isAutosaveRevision(), null, null, unloading);
  1520. }
  1521. this.destroy();
  1522. };
  1523. /**
  1524. * Returns the location as a new object.
  1525. */
  1526. DrawioFile.prototype.hasSameExtension = function(title, newTitle)
  1527. {
  1528. if (title != null && newTitle != null)
  1529. {
  1530. var dot = title.lastIndexOf('.');
  1531. var ext = (dot > 0) ? title.substring(dot) : '';
  1532. dot = newTitle.lastIndexOf('.');
  1533. return ext === ((dot > 0) ? newTitle.substring(dot) : '');
  1534. }
  1535. return title == newTitle;
  1536. };
  1537. /**
  1538. * Removes the change listener.
  1539. */
  1540. DrawioFile.prototype.removeListeners = function()
  1541. {
  1542. if (this.changeListener != null)
  1543. {
  1544. this.ui.editor.graph.model.removeListener(this.changeListener);
  1545. this.ui.editor.graph.removeListener(this.changeListener);
  1546. this.ui.removeListener(this.changeListener);
  1547. this.changeListener = null;
  1548. }
  1549. };
  1550. /**
  1551. * Stops any pending autosaves and removes all listeners.
  1552. */
  1553. DrawioFile.prototype.destroy = function()
  1554. {
  1555. try
  1556. {
  1557. if (!this.ui.isOffline() && this.reportEnabled &&
  1558. (DrawioFile.SYNC == 'auto' ||
  1559. DrawioFile.SYNC == 'manual'))
  1560. {
  1561. var user = this.getCurrentUser();
  1562. var uid = (user != null) ? this.ui.hashValue(user.id) : 'unknown';
  1563. this.stats.end = new Date().toISOString();
  1564. if (this.stats.start != null)
  1565. {
  1566. this.stats.uptime = Math.round((new Date().getTime() -
  1567. new Date(this.stats.start).getTime()) / 1000);
  1568. }
  1569. EditorUi.logEvent({category: 'RT-END-' + DrawioFile.SYNC,
  1570. action: 'file-' + this.getId() +
  1571. '-mode-' + this.getMode() +
  1572. '-size-' + this.getSize() +
  1573. '-user-' + uid +
  1574. ((this.sync != null) ? ('-client-' + this.sync.clientId ) : ''),
  1575. label: this.stats});
  1576. }
  1577. }
  1578. catch (e)
  1579. {
  1580. // ignore
  1581. }
  1582. this.clearAutosave();
  1583. this.removeListeners();
  1584. if (this.sync != null)
  1585. {
  1586. this.sync.destroy();
  1587. this.sync = null;
  1588. }
  1589. };