DrawioFile.js 55 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484
  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. this.shadowPages = null;
  22. this.created = new Date().getTime();
  23. // Creates the stats object
  24. this.stats = {
  25. opened: 0, /* number of calls to open */
  26. merged: 0, /* number of calls to merge */
  27. fileMerged: 0, /* number of calls to mergeFile */
  28. fileReloaded: 0, /* number of calls to mergeFile */
  29. conflicts: 0, /* number of write conflicts when saving a file */
  30. timeouts: 0, /* number of time we have given up to retry after a write conflict */
  31. saved: 0, /* number of calls to fileSaved */
  32. closed: 0, /* number of calls to close */
  33. destroyed: 0, /* number of calls to close */
  34. joined: 0, /* number of join messages received */
  35. checksumErrors: 0, /* number of checksum errors */
  36. bytesSent: 0, /* number of bytes send in messages */
  37. bytesReceived: 0, /* number of bytes received in messages */
  38. msgSent: 0, /* number of messages sent */
  39. msgReceived: 0, /* number of messages received */
  40. cacheHits: 0, /* number of times the cache returned patches */
  41. cacheMiss: 0, /* number of times we have missed a cache entry */
  42. cacheFail: 0 /* number of times we have failed to read the cache */
  43. };
  44. };
  45. /**
  46. * Global switch for realtime collaboration type to use sync URL parameter
  47. * with the following possible values:
  48. *
  49. * - none: overwrite
  50. * - manual: manual sync
  51. * - auto: automatic sync
  52. */
  53. DrawioFile.SYNC = urlParams['sync'] || 'auto';
  54. /**
  55. * Specifies if last write wins should be used for values and styles.
  56. */
  57. DrawioFile.LAST_WRITE_WINS = true;
  58. // Extends mxEventSource
  59. mxUtils.extend(DrawioFile, mxEventSource);
  60. /**
  61. * Specifies the resource key for all changes saved status message.
  62. */
  63. DrawioFile.prototype.allChangesSavedKey = 'allChangesSaved';
  64. /**
  65. * Specifies the resource key for saving spinner.
  66. */
  67. DrawioFile.prototype.savingSpinnerKey = 'saving';
  68. /**
  69. * Specifies the resource key for saving status message.
  70. */
  71. DrawioFile.prototype.savingStatusKey = 'saving';
  72. /**
  73. * Specifies the delay between the last change and the autosave.
  74. */
  75. DrawioFile.prototype.autosaveDelay = 1500;
  76. /**
  77. * Specifies the maximum delay before an autosave is forced even if the graph
  78. * is being changed.
  79. */
  80. DrawioFile.prototype.maxAutosaveDelay = 30000;
  81. /**
  82. * Specifies the delay for loading the file after an optimistic sync message.
  83. * This should be the delay for the file to be saved minus the delay for the
  84. * sync message to travel.
  85. */
  86. DrawioFile.prototype.optimisticSyncDelay = 300;
  87. /**
  88. * Contains the thread for the next autosave.
  89. */
  90. DrawioFile.prototype.autosaveThread = null;
  91. /**
  92. * Stores the time stamp for the last autosave.
  93. */
  94. DrawioFile.prototype.lastAutosave = null;
  95. /**
  96. * Stores the time stamp for the last autosave.
  97. */
  98. DrawioFile.prototype.lastSaved = null;
  99. /**
  100. * Stores the time stamp for the last autosave.
  101. */
  102. DrawioFile.prototype.lastChanged = null;
  103. /**
  104. * Stores the time stamp when the file was opened.
  105. */
  106. DrawioFile.prototype.opened = null;
  107. /**
  108. * Stores the modified state.
  109. */
  110. DrawioFile.prototype.modified = false;
  111. /**
  112. * Stores a shadow of the modified state.
  113. */
  114. DrawioFile.prototype.shadowModified = false;
  115. /**
  116. * Holds a copy of the current file data.
  117. */
  118. DrawioFile.prototype.data = null;
  119. /**
  120. * Holds a copy of the last saved file data.
  121. */
  122. DrawioFile.prototype.shadowData = null;
  123. /**
  124. * Holds a copy of the parsed last saved file data.
  125. */
  126. DrawioFile.prototype.shadowPages = null;
  127. /**
  128. * Specifies if the graph change listener is enabled. Default is true.
  129. */
  130. DrawioFile.prototype.changeListenerEnabled = true;
  131. /**
  132. * Sets the delay for autosave in milliseconds. Default is 1500.
  133. */
  134. DrawioFile.prototype.lastAutosaveRevision = null;
  135. /**
  136. * Sets the delay between revisions when using autosave. Default is 300000
  137. * ie 5 mins. Set this to 0 to create a revision on every autosave.
  138. */
  139. DrawioFile.prototype.maxAutosaveRevisionDelay = 300000;
  140. /**
  141. * Specifies if notify events should be ignored.
  142. */
  143. DrawioFile.prototype.inConflictState = false;
  144. /**
  145. * Specifies if notify events should be ignored.
  146. */
  147. DrawioFile.prototype.invalidChecksum = false;
  148. /**
  149. * Specifies if error reports should be sent.
  150. */
  151. DrawioFile.prototype.errorReportsEnabled = false;
  152. /**
  153. * Specifies if stats should be sent.
  154. */
  155. DrawioFile.prototype.ageStart = null;
  156. /**
  157. * Specifies if notify events should be ignored.
  158. */
  159. DrawioFile.prototype.getSize = function()
  160. {
  161. return (this.data != null) ? this.data.length : 0;
  162. };
  163. /**
  164. * Adds the listener for automatically saving the diagram for local changes.
  165. */
  166. DrawioFile.prototype.synchronizeFile = function(success, error)
  167. {
  168. if (this.savingFile)
  169. {
  170. if (error != null)
  171. {
  172. error({message: mxResources.get('busy')});
  173. }
  174. }
  175. else
  176. {
  177. if (this.sync != null)
  178. {
  179. // Removes unsaved remote changes
  180. if (this.ownPages != null && this.ui.pages != null)
  181. {
  182. var patch = this.ui.diffPages(
  183. this.ui.pages, this.ownPages);
  184. this.patch([patch]);
  185. this.snapshot = this.ui.getXmlFileData();
  186. }
  187. this.sync.fileChanged(success, error);
  188. }
  189. else
  190. {
  191. this.updateFile(success, error);
  192. }
  193. }
  194. };
  195. /**
  196. * Adds the listener for automatically saving the diagram for local changes.
  197. */
  198. DrawioFile.prototype.updateFile = function(success, error, abort, shadow)
  199. {
  200. if (abort == null || !abort())
  201. {
  202. if (this.ui.getCurrentFile() != this || this.invalidChecksum)
  203. {
  204. if (error != null)
  205. {
  206. error();
  207. }
  208. }
  209. else
  210. {
  211. this.getLatestVersion(mxUtils.bind(this, function(latestFile)
  212. {
  213. try
  214. {
  215. if (abort == null || !abort())
  216. {
  217. if (this.ui.getCurrentFile() != this || this.invalidChecksum)
  218. {
  219. if (error != null)
  220. {
  221. error();
  222. }
  223. }
  224. else
  225. {
  226. if (latestFile != null)
  227. {
  228. this.mergeFile(latestFile, success, error, shadow);
  229. }
  230. else
  231. {
  232. this.reloadFile(success, error);
  233. }
  234. }
  235. }
  236. }
  237. catch (e)
  238. {
  239. if (error != null)
  240. {
  241. error(e);
  242. }
  243. }
  244. }), error);
  245. }
  246. }
  247. };
  248. /**
  249. * Adds the listener for automatically saving the diagram for local changes.
  250. */
  251. DrawioFile.prototype.mergeFile = function(file, success, error, diffShadow)
  252. {
  253. var reportError = true;
  254. try
  255. {
  256. this.stats.fileMerged++;
  257. // Takes copy of current shadow document
  258. var shadow = (this.shadowPages != null) ? this.shadowPages :
  259. this.ui.getPagesForNode(mxUtils.parseXml(
  260. this.shadowData).documentElement);
  261. // Loads new document as shadow document
  262. var pages = this.ui.getPagesForNode(
  263. mxUtils.parseXml(file.data).
  264. documentElement)
  265. if (pages != null && pages.length > 0)
  266. {
  267. // Patches the current document
  268. var patches = [this.ui.diffPages((diffShadow != null) ?
  269. diffShadow : shadow, pages)];
  270. var ignored = this.ignorePatches(patches);
  271. this.shadowPages = pages;
  272. if (!ignored)
  273. {
  274. // Creates a patch for backup if the checksum fails
  275. this.backupPatch = (this.isModified()) ?
  276. this.ui.diffPages(shadow,
  277. this.ui.pages) : null;
  278. var pending = (this.ownPages != null) ?
  279. this.ui.diffPages(shadow,
  280. this.ownPages) : null;
  281. // Patching previous shadow to verify checksum
  282. var patchedDetails = {};
  283. var currentDetails = {};
  284. var patched = this.ui.patchPages(shadow, patches[0]);
  285. var checksum = this.ui.getHashValueForPages(patched, patchedDetails);
  286. var current = this.ui.getHashValueForPages(this.shadowPages, currentDetails);
  287. if (urlParams['test'] == '1')
  288. {
  289. EditorUi.debug('File.mergeFile', [this], 'patches', patches,
  290. 'backup', this.backupPatch, 'pending', pending, 'checksum',
  291. current == checksum, checksum);
  292. }
  293. if (checksum != null && checksum != current)
  294. {
  295. var fileData = this.compressReportData(this.getAnonymizedXmlForPages(pages));
  296. var data = this.compressReportData(this.getAnonymizedXmlForPages(patched));
  297. var from = this.ui.hashValue(file.getCurrentEtag());
  298. var to = this.ui.hashValue(this.getCurrentEtag());
  299. this.checksumError(error, patches,
  300. 'Shadow Details: ' + JSON.stringify(patchedDetails) +
  301. '\nChecksum: ' + checksum +
  302. '\nCurrent: ' + current +
  303. '\nCurrent Details: ' + JSON.stringify(currentDetails) +
  304. '\nFrom: ' + from +
  305. '\nTo: ' + to +
  306. '\n\nFile Data:\n' + fileData +
  307. '\nPatched Shadow:\n' + data, null, 'mergeFile');
  308. // Abnormal termination
  309. return;
  310. }
  311. else
  312. {
  313. // Patches the own pages
  314. if (this.sync != null)
  315. {
  316. this.sync.patchOwnPages(patches, [pending], true);
  317. }
  318. // Patches the current document
  319. this.patch(patches,
  320. (DrawioFile.LAST_WRITE_WINS) ?
  321. this.backupPatch : null);
  322. }
  323. }
  324. }
  325. else
  326. {
  327. reportError = false;
  328. throw new Error(mxResources.get('notADiagramFile'));
  329. }
  330. this.invalidChecksum = false;
  331. this.inConflictState = false;
  332. this.setDescriptor(file.getDescriptor());
  333. this.descriptorChanged();
  334. this.backupPatch = null;
  335. if (success != null)
  336. {
  337. success();
  338. }
  339. }
  340. catch (e)
  341. {
  342. this.inConflictState = true;
  343. this.invalidChecksum = true;
  344. this.descriptorChanged();
  345. if (error != null)
  346. {
  347. error(e);
  348. }
  349. try
  350. {
  351. if (reportError)
  352. {
  353. if (this.errorReportsEnabled)
  354. {
  355. this.sendErrorReport('Error in mergeFile', null, e);
  356. }
  357. else
  358. {
  359. var user = this.getCurrentUser();
  360. var uid = (user != null) ? user.id : 'unknown';
  361. EditorUi.logError('Error in mergeFile', null,
  362. this.getMode() + '.' + this.getId(),
  363. uid, e);
  364. }
  365. }
  366. }
  367. catch (e2)
  368. {
  369. // ignore
  370. }
  371. }
  372. };
  373. /**
  374. * Adds the listener for automatically saving the diagram for local changes.
  375. */
  376. DrawioFile.prototype.getAnonymizedXmlForPages = function(pages)
  377. {
  378. var enc = new mxCodec(mxUtils.createXmlDocument());
  379. var file = enc.document.createElement('mxfile');
  380. if (pages != null)
  381. {
  382. for (var i = 0; i < pages.length; i++)
  383. {
  384. var temp = enc.encode(new mxGraphModel(pages[i].root));
  385. if (urlParams['dev'] != '1')
  386. {
  387. temp = this.ui.anonymizeNode(temp, true);
  388. }
  389. temp.setAttribute('id', pages[i].getId());
  390. if (pages[i].viewState)
  391. {
  392. this.ui.editor.graph.saveViewState(pages[i].viewState, temp, true);
  393. }
  394. file.appendChild(temp);
  395. }
  396. }
  397. return mxUtils.getPrettyXml(file);
  398. };
  399. /**
  400. * Adds the listener for automatically saving the diagram for local changes.
  401. */
  402. DrawioFile.prototype.compressReportData = function(data, limit, max)
  403. {
  404. limit = (limit != null) ? limit : 10000;
  405. if (max != null && data != null && data.length > max)
  406. {
  407. data = data.substring(0, max) + '[...]';
  408. }
  409. else if (data != null && data.length > limit)
  410. {
  411. data = Graph.compress(data) + '\n';
  412. }
  413. return data;
  414. };
  415. /**
  416. * Adds the listener for automatically saving the diagram for local changes.
  417. */
  418. DrawioFile.prototype.checksumError = function(error, patches, details, etag, functionName)
  419. {
  420. this.stats.checksumErrors++;
  421. this.inConflictState = true;
  422. this.invalidChecksum = true;
  423. this.descriptorChanged();
  424. if (this.sync != null)
  425. {
  426. this.sync.updateOnlineState();
  427. }
  428. if (error != null)
  429. {
  430. error();
  431. }
  432. try
  433. {
  434. if (this.errorReportsEnabled)
  435. {
  436. if (patches != null)
  437. {
  438. for (var i = 0; i < patches.length; i++)
  439. {
  440. this.ui.anonymizePatch(patches[i]);
  441. }
  442. }
  443. var fn = mxUtils.bind(this, function(file)
  444. {
  445. var json = this.compressReportData(
  446. JSON.stringify(patches, null, 2));
  447. var remote = (file != null) ? this.compressReportData(
  448. this.getAnonymizedXmlForPages(
  449. this.ui.getPagesForNode(
  450. mxUtils.parseXml(file.data).documentElement)), 25000) : 'n/a';
  451. this.sendErrorReport('Checksum Error in ' + functionName + ' ' + this.getHash(),
  452. ((details != null) ? (details) : '') + '\n\nPatches:\n' + json +
  453. ((remote != null) ? ('\n\nRemote:\n' + remote) : ''), null, 70000);
  454. });
  455. if (etag == null)
  456. {
  457. fn(null);
  458. }
  459. else
  460. {
  461. this.getLatestVersion(mxUtils.bind(this, function(file)
  462. {
  463. if (file != null && file.getCurrentEtag() == etag)
  464. {
  465. fn(file);
  466. }
  467. else
  468. {
  469. fn(null);
  470. }
  471. }), function() {});
  472. }
  473. }
  474. else
  475. {
  476. var user = this.getCurrentUser();
  477. var uid = (user != null) ? user.id : 'unknown';
  478. EditorUi.logError('Checksum Error in ' + functionName + ' ' + this.getId(),
  479. null, this.getMode() + '.' + this.getId(),
  480. 'user_' + uid + ((this.sync != null) ?
  481. '-client_' + this.sync.clientId : '-nosync'));
  482. // Logs checksum error for file
  483. try
  484. {
  485. EditorUi.logEvent({category: 'CHECKSUM-ERROR-SYNC-FILE-' + this.getHash(),
  486. action: functionName, label: 'user_' + uid + ((this.sync != null) ?
  487. '-client_' + this.sync.clientId : '-nosync')});
  488. }
  489. catch (e)
  490. {
  491. // ignore
  492. }
  493. }
  494. }
  495. catch (e)
  496. {
  497. // ignore
  498. }
  499. };
  500. /**
  501. * Adds the listener for automatically saving the diagram for local changes.
  502. */
  503. DrawioFile.prototype.sendErrorReport = function(title, details, error, max)
  504. {
  505. try
  506. {
  507. var shadow = this.compressReportData(
  508. this.getAnonymizedXmlForPages(
  509. this.shadowPages), 25000);
  510. var data = this.compressReportData(
  511. this.getAnonymizedXmlForPages(
  512. this.ui.pages), 25000);
  513. var user = this.getCurrentUser();
  514. var uid = (user != null) ? this.ui.hashValue(user.id) : 'unknown';
  515. var cid = (this.sync != null) ? '-client_' + this.sync.clientId : '-nosync';
  516. var filename = this.getTitle();
  517. var dot = filename.lastIndexOf('.');
  518. var ext = 'xml';
  519. if (dot > 0)
  520. {
  521. ext = filename.substring(dot);
  522. }
  523. var stack = (error != null) ? error.stack : new Error().stack;
  524. EditorUi.sendReport(title + ' ' + new Date().toISOString() + ':' +
  525. '\n\nAppVersion=' + navigator.appVersion +
  526. '\nFile=' + this.ui.hashValue(this.getId()) + ' (' + this.getMode() + ')' +
  527. ((this.isModified()) ? ' modified' : '') +
  528. '\nSize/Type=' + this.getSize() + ' (' + ext + ')' +
  529. '\nUser=' + uid + cid +
  530. '\nPrefix=' + this.ui.editor.graph.model.prefix +
  531. '\nSync=' + DrawioFile.SYNC +
  532. ((this.sync != null) ? (((this.sync.enabled) ? ' enabled' : '') +
  533. ((this.sync.isConnected()) ? ' connected' : '')) : '') +
  534. '\nPlugins=' + ((mxSettings.settings != null) ? mxSettings.getPlugins() : 'null') +
  535. '\n\nStats:\n' + JSON.stringify(this.stats, null, 2) +
  536. ((details != null) ? ('\n\n' + details) : '') +
  537. ((error != null) ? ('\n\nError: ' + error.message) : '') +
  538. '\n\nStack:\n' + stack +
  539. '\n\nShadow:\n' + shadow +
  540. '\n\nData:\n' + data, max);
  541. }
  542. catch (e)
  543. {
  544. // ignore
  545. }
  546. };
  547. /**
  548. * Adds the listener for automatically saving the diagram for local changes.
  549. */
  550. DrawioFile.prototype.reloadFile = function(success, error)
  551. {
  552. try
  553. {
  554. this.ui.spinner.stop();
  555. var fn = mxUtils.bind(this, function()
  556. {
  557. this.stats.fileReloaded++;
  558. // Restores view state and current page
  559. var viewState = this.ui.editor.graph.getViewState();
  560. var selection = this.ui.editor.graph.getSelectionCells();
  561. var page = this.ui.currentPage;
  562. this.ui.loadFile(this.getHash(), true, null, mxUtils.bind(this, function()
  563. {
  564. if (this.ui.fileLoadedError == null)
  565. {
  566. this.ui.restoreViewState(page, viewState, selection);
  567. if (this.backupPatch != null)
  568. {
  569. this.patch([this.backupPatch]);
  570. }
  571. // Carry-over stats
  572. var file = this.ui.getCurrentFile();
  573. if (file != null)
  574. {
  575. file.stats = this.stats;
  576. }
  577. if (success != null)
  578. {
  579. success();
  580. }
  581. }
  582. }), true);
  583. });
  584. if (this.isModified() && this.backupPatch == null)
  585. {
  586. this.ui.confirm(mxResources.get('allChangesLost'), mxUtils.bind(this, function()
  587. {
  588. this.handleFileSuccess(DrawioFile.SYNC == 'manual');
  589. }), fn, mxResources.get('cancel'), mxResources.get('discardChanges'));
  590. }
  591. else
  592. {
  593. fn();
  594. }
  595. }
  596. catch (e)
  597. {
  598. if (error != null)
  599. {
  600. error(e);
  601. }
  602. }
  603. };
  604. /**
  605. * Shows a conflict dialog to the user.
  606. */
  607. DrawioFile.prototype.copyFile = function(success, error)
  608. {
  609. this.ui.editor.editAsNew(this.ui.getFileData(true),
  610. this.ui.getCopyFilename(this));
  611. };
  612. /**
  613. * Returns true if the patches in the given array are empty.
  614. */
  615. DrawioFile.prototype.ignorePatches = function(patches)
  616. {
  617. var ignore = true;
  618. if (patches != null)
  619. {
  620. for (var i = 0; i < patches.length && ignore; i++)
  621. {
  622. ignore = ignore && mxUtils.isEmptyObject(patches[i]);
  623. }
  624. }
  625. return ignore;
  626. };
  627. /**
  628. * Applies the given patches to the file.
  629. */
  630. DrawioFile.prototype.patch = function(patches, resolver, undoable)
  631. {
  632. // Saves state of undo history
  633. var undoMgr = this.ui.editor.undoManager;
  634. var history = undoMgr.history.slice();
  635. var nextAdd = undoMgr.indexOfNextAdd;
  636. // Hides graph during updates
  637. var graph = this.ui.editor.graph;
  638. graph.container.style.visibility = 'hidden';
  639. // Ignores change events
  640. var prev = this.changeListenerEnabled;
  641. this.changeListenerEnabled = undoable;
  642. // Folding and math change require special handling
  643. var fold = graph.foldingEnabled;
  644. var math = graph.mathEnabled;
  645. // Updates text editor if cell changes during validation
  646. var redraw = graph.cellRenderer.redraw;
  647. graph.cellRenderer.redraw = function(state)
  648. {
  649. if (state.view.graph.isEditing(state.cell))
  650. {
  651. state.view.graph.scrollCellToVisible(state.cell);
  652. state.view.graph.cellEditor.resize();
  653. }
  654. redraw.apply(this, arguments);
  655. };
  656. graph.model.beginUpdate();
  657. try
  658. {
  659. // Applies patches
  660. for (var i = 0; i < patches.length; i++)
  661. {
  662. if (patches[i] != null)
  663. {
  664. this.ui.pages = this.ui.patchPages(this.ui.pages,
  665. patches[i], true, resolver, this.isModified());
  666. }
  667. }
  668. // Always needs at least one page
  669. if (this.ui.pages.length == 0)
  670. {
  671. this.ui.pages.push(this.ui.createPage());
  672. }
  673. // Checks if current page was removed
  674. if (mxUtils.indexOf(this.ui.pages, this.ui.currentPage) < 0)
  675. {
  676. this.ui.selectPage(this.ui.pages[0], true);
  677. }
  678. }
  679. finally
  680. {
  681. // Changes visibility before action states are updated via model event
  682. graph.container.style.visibility = '';
  683. graph.model.endUpdate();
  684. // Restores previous state
  685. graph.cellRenderer.redraw = redraw;
  686. this.changeListenerEnabled = prev;
  687. // Restores history state
  688. if (!undoable)
  689. {
  690. undoMgr.history = history;
  691. undoMgr.indexOfNextAdd = nextAdd;
  692. undoMgr.fireEvent(new mxEventObject(mxEvent.CLEAR));
  693. }
  694. if (this.ui.currentPage == null || this.ui.currentPage.needsUpdate)
  695. {
  696. // Updates the graph and background
  697. if (math != graph.mathEnabled)
  698. {
  699. this.ui.editor.updateGraphComponents();
  700. graph.refresh();
  701. }
  702. else
  703. {
  704. if (fold != graph.foldingEnabled)
  705. {
  706. graph.view.revalidate();
  707. }
  708. else
  709. {
  710. graph.view.validate();
  711. }
  712. graph.sizeDidChange();
  713. }
  714. }
  715. this.ui.updateTabContainer();
  716. this.ui.editor.fireEvent(new mxEventObject('pagesPatched', 'patches', patches));
  717. }
  718. };
  719. /**
  720. * Adds the listener for automatically saving the diagram for local changes.
  721. */
  722. DrawioFile.prototype.save = function(revision, success, error, unloading, overwrite, manual)
  723. {
  724. try
  725. {
  726. if (!this.isEditable())
  727. {
  728. if (error != null)
  729. {
  730. error({message: mxResources.get('readOnly')});
  731. }
  732. else
  733. {
  734. throw new Error(mxResources.get('readOnly'));
  735. }
  736. }
  737. else if (!overwrite && this.invalidChecksum)
  738. {
  739. if (error != null)
  740. {
  741. error({message: mxResources.get('checksum')});
  742. }
  743. else
  744. {
  745. throw new Error(mxResources.get('checksum'));
  746. }
  747. }
  748. else
  749. {
  750. this.updateFileData();
  751. this.clearAutosave();
  752. if (success != null)
  753. {
  754. success();
  755. }
  756. }
  757. }
  758. catch (e)
  759. {
  760. if (error != null)
  761. {
  762. error(e);
  763. }
  764. else
  765. {
  766. throw e;
  767. }
  768. }
  769. };
  770. /**
  771. * Translates this point by the given vector.
  772. *
  773. * @param {number} dx X-coordinate of the translation.
  774. * @param {number} dy Y-coordinate of the translation.
  775. */
  776. DrawioFile.prototype.updateFileData = function()
  777. {
  778. var actualPages = this.ui.pages;
  779. if (this.ownPages != null)
  780. {
  781. // Uses ownPgaes for getting file data below
  782. this.ui.pages = this.ownPages;
  783. // Updates view state in own current page
  784. if (this.ui.currentPage != null)
  785. {
  786. var ownPage = this.ui.getPageById(
  787. this.ui.currentPage.getId(),
  788. this.ownPages);
  789. if (ownPage != null)
  790. {
  791. ownPage.viewState = this.ui.editor.graph.getViewState();
  792. ownPage.needsUpdate = true;
  793. }
  794. }
  795. }
  796. this.setData(this.ui.getFileData(null, null, null, null,
  797. null, null, null, null, this, !this.isCompressed()));
  798. this.ui.pages = actualPages;
  799. };
  800. /**
  801. * Translates this point by the given vector.
  802. *
  803. * @param {number} dx X-coordinate of the translation.
  804. * @param {number} dy Y-coordinate of the translation.
  805. */
  806. DrawioFile.prototype.isCompressedStorage = function()
  807. {
  808. return true;
  809. };
  810. /**
  811. * Translates this point by the given vector.
  812. *
  813. * @param {number} dx X-coordinate of the translation.
  814. * @param {number} dy Y-coordinate of the translation.
  815. */
  816. DrawioFile.prototype.isCompressed = function()
  817. {
  818. var compressed = (this.ui.fileNode != null) ? this.ui.fileNode.getAttribute('compressed') : null;
  819. if (compressed != null)
  820. {
  821. return compressed != 'false';
  822. }
  823. else
  824. {
  825. return this.isCompressedStorage() && Editor.compressXml;
  826. }
  827. };
  828. /**
  829. * Translates this point by the given vector.
  830. *
  831. * @param {number} dx X-coordinate of the translation.
  832. * @param {number} dy Y-coordinate of the translation.
  833. */
  834. DrawioFile.prototype.saveAs = function(filename, success, error) { };
  835. /**
  836. * Translates this point by the given vector.
  837. *
  838. * @param {number} dx X-coordinate of the translation.
  839. * @param {number} dy Y-coordinate of the translation.
  840. */
  841. DrawioFile.prototype.saveFile = function(title, revision, success, error) { };
  842. /**
  843. * Returns true if copy, export and print are not allowed for this file.
  844. */
  845. DrawioFile.prototype.getPublicUrl = function(fn)
  846. {
  847. fn(null);
  848. };
  849. /**
  850. * Returns true if copy, export and print are not allowed for this file.
  851. */
  852. DrawioFile.prototype.isRestricted = function()
  853. {
  854. return false;
  855. };
  856. /**
  857. * Translates this point by the given vector.
  858. *
  859. * @param {number} dx X-coordinate of the translation.
  860. * @param {number} dy Y-coordinate of the translation.
  861. */
  862. DrawioFile.prototype.isModified = function()
  863. {
  864. return this.modified;
  865. };
  866. /**
  867. * Translates this point by the given vector.
  868. *
  869. * @param {number} dx X-coordinate of the translation.
  870. * @param {number} dy Y-coordinate of the translation.
  871. */
  872. DrawioFile.prototype.getShadowModified = function()
  873. {
  874. return this.shadowModified;
  875. };
  876. /**
  877. * Translates this point by the given vector.
  878. *
  879. * @param {number} dx X-coordinate of the translation.
  880. * @param {number} dy Y-coordinate of the translation.
  881. */
  882. DrawioFile.prototype.setShadowModified = function(value)
  883. {
  884. this.shadowModified = value;
  885. };
  886. /**
  887. * Translates this point by the given vector.
  888. *
  889. * @param {number} dx X-coordinate of the translation.
  890. * @param {number} dy Y-coordinate of the translation.
  891. */
  892. DrawioFile.prototype.setModified = function(value)
  893. {
  894. this.modified = value;
  895. this.shadowModified = value;
  896. };
  897. /**
  898. * Specifies if the autosave checkbox should be shown in the document
  899. * properties dialog. Default is false.
  900. */
  901. DrawioFile.prototype.isAutosaveOptional = function()
  902. {
  903. return false;
  904. };
  905. /**
  906. * Translates this point by the given vector.
  907. *
  908. * @param {number} dx X-coordinate of the translation.
  909. * @param {number} dy Y-coordinate of the translation.
  910. */
  911. DrawioFile.prototype.isAutosave = function()
  912. {
  913. return !this.inConflictState && this.ui.editor.autosave;
  914. };
  915. /**
  916. * Translates this point by the given vector.
  917. *
  918. * @param {number} dx X-coordinate of the translation.
  919. * @param {number} dy Y-coordinate of the translation.
  920. */
  921. DrawioFile.prototype.isRenamable = function()
  922. {
  923. return false;
  924. };
  925. /**
  926. * Translates this point by the given vector.
  927. *
  928. * @param {number} dx X-coordinate of the translation.
  929. * @param {number} dy Y-coordinate of the translation.
  930. */
  931. DrawioFile.prototype.rename = function(title, success, error) { };
  932. /**
  933. * Translates this point by the given vector.
  934. *
  935. * @param {number} dx X-coordinate of the translation.
  936. * @param {number} dy Y-coordinate of the translation.
  937. */
  938. DrawioFile.prototype.isMovable = function()
  939. {
  940. return false;
  941. };
  942. /**
  943. * Translates this point by the given vector.
  944. *
  945. * @param {number} dx X-coordinate of the translation.
  946. * @param {number} dy Y-coordinate of the translation.
  947. */
  948. DrawioFile.prototype.isTrashed = function()
  949. {
  950. return false;
  951. };
  952. /**
  953. * Translates this point by the given vector.
  954. *
  955. * @param {number} dx X-coordinate of the translation.
  956. * @param {number} dy Y-coordinate of the translation.
  957. */
  958. DrawioFile.prototype.move = function(folderId, success, error) { };
  959. /**
  960. * Translates this point by the given vector.
  961. *
  962. * @param {number} dx X-coordinate of the translation.
  963. * @param {number} dy Y-coordinate of the translation.
  964. */
  965. DrawioFile.prototype.share = function()
  966. {
  967. this.ui.alert(mxResources.get('sharingAvailable'), null, 380);
  968. };
  969. /**
  970. * Returns the hash of the file which consists of a prefix for the storage
  971. * type and the ID of the file.
  972. */
  973. DrawioFile.prototype.getHash = function()
  974. {
  975. return '';
  976. };
  977. /**
  978. * Returns the ID of the file.
  979. */
  980. DrawioFile.prototype.getId = function()
  981. {
  982. return '';
  983. };
  984. /**
  985. * Returns true if the file is editable.
  986. */
  987. DrawioFile.prototype.isEditable = function()
  988. {
  989. return !this.ui.editor.isChromelessView() || this.ui.editor.editable;
  990. };
  991. /**
  992. * Returns the location as a new object.
  993. * @type mx.Point
  994. */
  995. DrawioFile.prototype.getUi = function()
  996. {
  997. return this.ui;
  998. };
  999. /**
  1000. * Returns the current title of the file.
  1001. */
  1002. DrawioFile.prototype.getTitle = function()
  1003. {
  1004. return '';
  1005. };
  1006. /**
  1007. * Sets the current data of the file.
  1008. */
  1009. DrawioFile.prototype.setData = function(data)
  1010. {
  1011. this.data = data;
  1012. };
  1013. /**
  1014. * Returns the current data of the file.
  1015. */
  1016. DrawioFile.prototype.getData = function()
  1017. {
  1018. return this.data;
  1019. };
  1020. /**
  1021. * Opens this file in the editor.
  1022. */
  1023. DrawioFile.prototype.open = function()
  1024. {
  1025. this.stats.opened++;
  1026. var data = this.getData();
  1027. if (data != null)
  1028. {
  1029. //Remove external fonts of previous file
  1030. function removeExtFont(elems)
  1031. {
  1032. for (var i = 0; elems != null && i < elems.length; i++)
  1033. {
  1034. var e = elems[i];
  1035. if (e.id != null && e.id.indexOf('extFont_') == 0)
  1036. {
  1037. e.parentNode.removeChild(e);
  1038. }
  1039. }
  1040. };
  1041. removeExtFont(document.querySelectorAll('head > style[id]'));
  1042. removeExtFont(document.querySelectorAll('head > link[id]'));
  1043. this.ui.setFileData(data);
  1044. // Updates shadow in case any page IDs have been updated
  1045. // only if the file has not been modified and reopened
  1046. if (!this.isModified())
  1047. {
  1048. var data = this.ui.getXmlFileData();
  1049. if (this.isRealtimeEnabled() && this.isRealtimeSupported())
  1050. {
  1051. this.ownPages = this.ui.getPagesForNode(data);
  1052. this.snapshot = data;
  1053. }
  1054. this.shadowData = mxUtils.getXml(data);
  1055. this.shadowPages = null;
  1056. }
  1057. }
  1058. this.installListeners();
  1059. if (this.isSyncSupported())
  1060. {
  1061. this.startSync();
  1062. }
  1063. };
  1064. /**
  1065. * Hook for subclassers.
  1066. */
  1067. DrawioFile.prototype.isSyncSupported = function()
  1068. {
  1069. return false;
  1070. };
  1071. /**
  1072. * Returns true if all changes should be sent out immediately.
  1073. */
  1074. DrawioFile.prototype.isRealtimeSupported = function()
  1075. {
  1076. return false;
  1077. };
  1078. /**
  1079. * Returns true if all changes should be sent out immediately.
  1080. */
  1081. DrawioFile.prototype.isRealtimeEnabled = function()
  1082. {
  1083. return urlParams['fast-sync'] != '0';
  1084. };
  1085. /**
  1086. * Returns true if all changes should be sent out immediately.
  1087. */
  1088. DrawioFile.prototype.setRealtimeEnabled = function()
  1089. {
  1090. // do nothing
  1091. };
  1092. /**
  1093. * Returns true if all changes should be sent out immediately.
  1094. */
  1095. DrawioFile.prototype.isRealtimeOptional = function()
  1096. {
  1097. return false;
  1098. };
  1099. /**
  1100. * Returns true if all changes should be sent out immediately.
  1101. */
  1102. DrawioFile.prototype.getRealtimeState = function()
  1103. {
  1104. return (this.sync != null && this.sync.p2pCollab != null) ?
  1105. this.sync.p2pCollab.getState() : 3 /* CLOSED */;
  1106. };
  1107. /**
  1108. * Returns true if all changes should be sent out immediately.
  1109. */
  1110. DrawioFile.prototype.getRealtimeError = function()
  1111. {
  1112. return (this.sync != null && this.sync.p2pCollab != null) ?
  1113. this.sync.p2pCollab.getLastError() : null;
  1114. };
  1115. /**
  1116. * Returns true if the notification to update should be sent
  1117. * together with the save request.
  1118. */
  1119. DrawioFile.prototype.isOptimisticSync = function()
  1120. {
  1121. return false;
  1122. };
  1123. /**
  1124. * Hook for subclassers.
  1125. */
  1126. DrawioFile.prototype.isRevisionHistorySupported = function()
  1127. {
  1128. return false;
  1129. };
  1130. /**
  1131. * Hook for subclassers.
  1132. */
  1133. DrawioFile.prototype.getRevisions = function(success, error)
  1134. {
  1135. success(null);
  1136. };
  1137. /**
  1138. * Hook for subclassers to get the latest descriptor of this file
  1139. * and return it in the success handler.
  1140. */
  1141. DrawioFile.prototype.loadDescriptor = function(success, error)
  1142. {
  1143. success(null);
  1144. };
  1145. /**
  1146. * Hook for subclassers to get the latest etag of this file
  1147. * and return it in the success handler.
  1148. */
  1149. DrawioFile.prototype.loadPatchDescriptor = function(success, error)
  1150. {
  1151. this.loadDescriptor(mxUtils.bind(this, function(desc)
  1152. {
  1153. success(desc);
  1154. }), error);
  1155. };
  1156. /**
  1157. * Adds the listener for automatically saving the diagram for local changes.
  1158. */
  1159. DrawioFile.prototype.patchDescriptor = function(desc, patch)
  1160. {
  1161. this.setDescriptorEtag(desc, this.getDescriptorEtag(patch));
  1162. this.descriptorChanged();
  1163. };
  1164. /**
  1165. * Creates a starts the synchronization.
  1166. */
  1167. DrawioFile.prototype.startSync = function()
  1168. {
  1169. if (((DrawioFile.SYNC == 'auto' || DrawioFile.SYNC == 'fast') &&
  1170. urlParams['stealth'] != '1') && (urlParams['rt'] == '1' ||
  1171. !this.ui.editor.chromeless || this.ui.editor.editable))
  1172. {
  1173. if (this.sync == null)
  1174. {
  1175. this.sync = new DrawioFileSync(this);
  1176. }
  1177. this.addListener('realtimeStateChanged', mxUtils.bind(this, function()
  1178. {
  1179. this.ui.fireEvent(new mxEventObject('realtimeStateChanged'));
  1180. }));
  1181. this.sync.start();
  1182. }
  1183. };
  1184. /**
  1185. * Hook for subclassers to check if an error is a conflict.
  1186. */
  1187. DrawioFile.prototype.isConflict = function()
  1188. {
  1189. return false;
  1190. };
  1191. /**
  1192. * Gets the channel ID for sync messages.
  1193. */
  1194. DrawioFile.prototype.getChannelId = function()
  1195. {
  1196. // Slash, space and plus replaced with underscore
  1197. return Graph.compress(this.getHash()).replace(/[\/ +]/g, '_');
  1198. };
  1199. /**
  1200. * Gets the channel ID from the given descriptor.
  1201. */
  1202. DrawioFile.prototype.getChannelKey = function(desc)
  1203. {
  1204. return null;
  1205. };
  1206. /**
  1207. * Returns the current etag.
  1208. */
  1209. DrawioFile.prototype.getCurrentUser = function()
  1210. {
  1211. return null;
  1212. };
  1213. /**
  1214. * Hook for subclassers to get the latest version of this file
  1215. * and return it in the success handler.
  1216. */
  1217. DrawioFile.prototype.getLatestVersion = function(success, error)
  1218. {
  1219. success(null);
  1220. };
  1221. /**
  1222. * Returns the last modified date of this file.
  1223. */
  1224. DrawioFile.prototype.getLastModifiedDate = function()
  1225. {
  1226. return new Date();
  1227. };
  1228. /**
  1229. * Sets the current revision ID.
  1230. */
  1231. DrawioFile.prototype.setCurrentRevisionId = function(id)
  1232. {
  1233. this.setDescriptorRevisionId(this.getDescriptor(), id);
  1234. };
  1235. /**
  1236. * Returns the current revision ID.
  1237. */
  1238. DrawioFile.prototype.getCurrentRevisionId = function()
  1239. {
  1240. return this.getDescriptorRevisionId(this.getDescriptor());
  1241. };
  1242. /**
  1243. * Sets the current etag.
  1244. */
  1245. DrawioFile.prototype.setCurrentEtag = function(etag)
  1246. {
  1247. this.setDescriptorEtag(this.getDescriptor(), etag);
  1248. };
  1249. /**
  1250. * Returns the current etag.
  1251. */
  1252. DrawioFile.prototype.getCurrentEtag = function()
  1253. {
  1254. return this.getDescriptorEtag(this.getDescriptor());
  1255. };
  1256. /**
  1257. * Returns the descriptor from this file.
  1258. */
  1259. DrawioFile.prototype.getDescriptor = function()
  1260. {
  1261. return null;
  1262. };
  1263. /**
  1264. * Sets the descriptor for this file.
  1265. */
  1266. DrawioFile.prototype.setDescriptor = function() { };
  1267. /**
  1268. * Updates the revision ID on the given descriptor.
  1269. */
  1270. DrawioFile.prototype.setDescriptorRevisionId = function(desc, id)
  1271. {
  1272. this.setDescriptorEtag(desc, id);
  1273. };
  1274. /**
  1275. * Returns the revision ID from the given descriptor.
  1276. */
  1277. DrawioFile.prototype.getDescriptorRevisionId = function(desc)
  1278. {
  1279. return this.getDescriptorEtag(desc);
  1280. };
  1281. /**
  1282. * Updates the etag on the given descriptor.
  1283. */
  1284. DrawioFile.prototype.setDescriptorEtag = function(desc, etag) { };
  1285. /**
  1286. * Returns the etag from the given descriptor.
  1287. */
  1288. DrawioFile.prototype.getDescriptorEtag = function(desc)
  1289. {
  1290. return null;
  1291. };
  1292. /**
  1293. * Returns the secret from the given descriptor. This must be stored
  1294. * in a custom property and generated by the saving client so that a
  1295. * token can be obtained from the cache for writing the patch after
  1296. * saving the file. If this cannot be saved in a custom property then
  1297. * null must be returned so that no deltas are used for updating the
  1298. * file (the file is reloaded every time instead). This is needed to
  1299. * make sure nobody with read-only permissions can write a patch to
  1300. * the cache before the saving client wrote the patch and inject
  1301. * data into the file via other clients merging that data.
  1302. */
  1303. DrawioFile.prototype.getDescriptorSecret = function(desc)
  1304. {
  1305. return null;
  1306. };
  1307. /**
  1308. * Installs the change listener.
  1309. */
  1310. DrawioFile.prototype.installListeners = function()
  1311. {
  1312. if (this.changeListener == null)
  1313. {
  1314. this.changeListener = mxUtils.bind(this, function(sender, eventObject)
  1315. {
  1316. var edit = (eventObject != null) ? eventObject.getProperty('edit') : null;
  1317. if (this.changeListenerEnabled && this.isEditable() && (edit == null || !edit.ignoreEdit))
  1318. {
  1319. this.fileChanged();
  1320. }
  1321. });
  1322. this.ui.editor.graph.model.addListener(mxEvent.CHANGE, this.changeListener);
  1323. // Some options trigger autosave
  1324. this.ui.editor.graph.addListener('gridSizeChanged', this.changeListener);
  1325. this.ui.editor.graph.addListener('shadowVisibleChanged', this.changeListener);
  1326. this.ui.addListener('pageFormatChanged', this.changeListener);
  1327. this.ui.addListener('pageScaleChanged', this.changeListener);
  1328. this.ui.addListener('backgroundColorChanged', this.changeListener);
  1329. this.ui.addListener('backgroundImageChanged', this.changeListener);
  1330. this.ui.addListener('foldingEnabledChanged', this.changeListener);
  1331. this.ui.addListener('mathEnabledChanged', this.changeListener);
  1332. this.ui.addListener('gridEnabledChanged', this.changeListener);
  1333. this.ui.addListener('guidesEnabledChanged', this.changeListener);
  1334. this.ui.addListener('tooltipsEnabledChanged', this.changeListener);
  1335. this.ui.addListener('pageViewChanged', this.changeListener);
  1336. this.ui.addListener('connectionPointsChanged', this.changeListener);
  1337. this.ui.addListener('connectionArrowsChanged', this.changeListener);
  1338. }
  1339. };
  1340. /**
  1341. * Returns the location as a new object.
  1342. * @type mx.Point
  1343. */
  1344. DrawioFile.prototype.addAllSavedStatus = function(status)
  1345. {
  1346. if (this.ui.statusContainer != null && this.ui.getCurrentFile() == this)
  1347. {
  1348. status = (status != null) ? status : mxUtils.htmlEntities(mxResources.get(this.allChangesSavedKey));
  1349. this.ui.editor.setStatus('<div title="'+ status + '">' + status + '</div>');
  1350. var links = this.ui.statusContainer.getElementsByTagName('div');
  1351. if (links.length > 0 && this.isRevisionHistorySupported())
  1352. {
  1353. links[0].style.cursor = 'pointer';
  1354. links[0].style.textDecoration = 'underline';
  1355. mxEvent.addListener(links[0], 'click', mxUtils.bind(this, function()
  1356. {
  1357. this.ui.actions.get('revisionHistory').funct();
  1358. }));
  1359. }
  1360. }
  1361. };
  1362. /**
  1363. * Adds the listener for automatically saving the diagram for local changes.
  1364. */
  1365. DrawioFile.prototype.saveDraft = function()
  1366. {
  1367. try
  1368. {
  1369. if (this.draftId == null)
  1370. {
  1371. this.draftId = Editor.guid();
  1372. }
  1373. var draft = {type: 'draft',
  1374. created: this.created,
  1375. modified: new Date().getTime(),
  1376. data: this.ui.getFileData(),
  1377. title: this.getTitle(),
  1378. fileObject: this.fileObject,
  1379. aliveCheck: this.ui.draftAliveCheck};
  1380. this.ui.setDatabaseItem('.draft_' + this.draftId,
  1381. JSON.stringify(draft));
  1382. EditorUi.debug('draft saved', this.draftId, draft);
  1383. }
  1384. catch (e)
  1385. {
  1386. // Removes any stored draft
  1387. this.removeDraft();
  1388. }
  1389. };
  1390. /**
  1391. * Adds the listener for automatically saving the diagram for local changes.
  1392. */
  1393. DrawioFile.prototype.removeDraft = function()
  1394. {
  1395. try
  1396. {
  1397. if (this.draftId != null)
  1398. {
  1399. this.ui.removeDatabaseItem('.draft_' + this.draftId);
  1400. EditorUi.debug('draft deleted', '.draft_' + this.draftId);
  1401. }
  1402. }
  1403. catch (e)
  1404. {
  1405. // ignore
  1406. }
  1407. };
  1408. /**
  1409. * Adds the listener for automatically saving the diagram for local changes.
  1410. */
  1411. DrawioFile.prototype.addUnsavedStatus = function(err)
  1412. {
  1413. if (!this.inConflictState && this.ui.statusContainer != null && this.ui.getCurrentFile() == this)
  1414. {
  1415. if (err instanceof Error && err.message != null && err.message != '')
  1416. {
  1417. var status = mxUtils.htmlEntities(mxResources.get('unsavedChanges'));
  1418. this.ui.editor.setStatus('<div title="'+ status + '" class="geStatusAlert">' +
  1419. status + ' (' + mxUtils.htmlEntities(err.message) + ')</div>');
  1420. // Installs click handler for error message
  1421. var links = this.ui.statusContainer.getElementsByTagName('div');
  1422. if (links != null && links.length > 0)
  1423. {
  1424. links[0].style.cursor = 'pointer';
  1425. mxEvent.addListener(links[0], 'click', mxUtils.bind(this, function()
  1426. {
  1427. this.ui.showError(mxResources.get('unsavedChanges'), mxUtils.htmlEntities(err.message));
  1428. }));
  1429. }
  1430. }
  1431. else
  1432. {
  1433. var msg = this.getErrorMessage(err);
  1434. if (msg == null && this.lastSaved != null)
  1435. {
  1436. var str = this.ui.timeSince(new Date(this.lastSaved));
  1437. // Only show if more than a minute ago
  1438. if (str != null)
  1439. {
  1440. msg = mxResources.get('lastSaved', [str]);
  1441. }
  1442. }
  1443. if (msg != null && msg.length > 60)
  1444. {
  1445. msg = msg.substring(0, 60) + '...';
  1446. }
  1447. var status = mxUtils.htmlEntities(mxResources.get('unsavedChangesClickHereToSave')) +
  1448. ((msg != null && msg != '') ? ' (' + mxUtils.htmlEntities(msg) + ')' : '');
  1449. this.ui.editor.setStatus('<div title="'+ status + '" class="geStatusAlertOrange">' + status +
  1450. ' <img src="' + Editor.saveImage + '"/></div>');
  1451. // Installs click handler for saving
  1452. var links = this.ui.statusContainer.getElementsByTagName('div');
  1453. if (links != null && links.length > 0)
  1454. {
  1455. links[0].style.cursor = 'pointer';
  1456. mxEvent.addListener(links[0], 'click', mxUtils.bind(this, function()
  1457. {
  1458. this.ui.actions.get((this.ui.mode == null || !this.isEditable()) ?
  1459. 'saveAs' : 'save').funct();
  1460. }));
  1461. }
  1462. else
  1463. {
  1464. var status = mxUtils.htmlEntities(mxResources.get('unsavedChanges'));
  1465. this.ui.editor.setStatus('<div title="'+ status + '" class="geStatusAlert">' + status +
  1466. ' (' + mxUtils.htmlEntities(err.message) + ')</div>');
  1467. }
  1468. if (EditorUi.enableDrafts && (this.getMode() == null || EditorUi.isElectronApp))
  1469. {
  1470. if (this.saveDraftThread != null)
  1471. {
  1472. window.clearTimeout(this.saveDraftThread);
  1473. }
  1474. this.saveDraftThread = window.setTimeout(mxUtils.bind(this, function()
  1475. {
  1476. this.saveDraft();
  1477. }), 0);
  1478. }
  1479. }
  1480. }
  1481. };
  1482. /**
  1483. * Halts all timers and shows a conflict status message. The optional error
  1484. * handler is invoked first.
  1485. */
  1486. DrawioFile.prototype.addConflictStatus = function(fn, message)
  1487. {
  1488. if (this.invalidChecksum && message == null)
  1489. {
  1490. message = mxResources.get('checksum');
  1491. }
  1492. this.setConflictStatus(mxUtils.htmlEntities(mxResources.get('fileChangedSync')) +
  1493. ((message != null && message != '') ? ' (' + mxUtils.htmlEntities(message) + ')' : ''));
  1494. this.ui.spinner.stop();
  1495. this.clearAutosave();
  1496. var links = (this.ui.statusContainer != null) ? this.ui.statusContainer.getElementsByTagName('div') : null;
  1497. if (links != null && links.length > 0)
  1498. {
  1499. links[0].style.cursor = 'pointer';
  1500. mxEvent.addListener(links[0], 'click', mxUtils.bind(this, function(evt)
  1501. {
  1502. if (mxEvent.getSource(evt).nodeName != 'IMG')
  1503. {
  1504. fn();
  1505. }
  1506. }));
  1507. }
  1508. else
  1509. {
  1510. this.ui.alert(mxUtils.htmlEntities(mxResources.get('fileChangedSync')), fn);
  1511. }
  1512. };
  1513. /**
  1514. * Halts all timers and shows a conflict status message. The optional error
  1515. * handler is invoked first.
  1516. */
  1517. DrawioFile.prototype.setConflictStatus = function(message)
  1518. {
  1519. this.ui.editor.setStatus('<div title="'+ message + '" class="geStatusAlert">' + message +
  1520. ' <a href="https://www.diagrams.net/doc/faq/synchronize" title="' + mxResources.get('help') +
  1521. '" target="_blank">' + '<img src="' + Editor.helpImage + '"/></a></div>');
  1522. };
  1523. /**
  1524. * Shows a conflict dialog to the user.
  1525. */
  1526. DrawioFile.prototype.showRefreshDialog = function(success, error, message)
  1527. {
  1528. if (message == null)
  1529. {
  1530. message = mxResources.get('checksum');
  1531. }
  1532. if (this.ui.editor.isChromelessView() && !this.ui.editor.editable)
  1533. {
  1534. this.ui.alert(mxResources.get('fileChangedSync'), mxUtils.bind(this, function()
  1535. {
  1536. this.reloadFile(success, error);
  1537. }));
  1538. }
  1539. else
  1540. {
  1541. // Allows for escape key to be pressed while dialog is showing
  1542. this.addConflictStatus(mxUtils.bind(this, function()
  1543. {
  1544. this.showRefreshDialog(success, error);
  1545. }), message);
  1546. this.ui.showError(mxResources.get('warning') + ' (' + message + ')',
  1547. mxResources.get('fileChangedSyncDialog'),
  1548. mxResources.get('makeCopy'), mxUtils.bind(this, function()
  1549. {
  1550. this.copyFile(success, error);
  1551. }), null, mxResources.get('merge'), mxUtils.bind(this, function()
  1552. {
  1553. this.reloadFile(success, error);
  1554. }), mxResources.get('cancel'), mxUtils.bind(this, function()
  1555. {
  1556. this.ui.hideDialog();
  1557. }), 380, 130);
  1558. }
  1559. };
  1560. /**
  1561. * Shows a dialog with no synchronize option.
  1562. */
  1563. DrawioFile.prototype.showCopyDialog = function(success, error, overwrite)
  1564. {
  1565. this.inConflictState = false;
  1566. this.invalidChecksum = false;
  1567. this.addUnsavedStatus();
  1568. this.ui.showError(mxResources.get('externalChanges'),
  1569. mxResources.get('fileChangedOverwriteDialog'),
  1570. mxResources.get('makeCopy'), mxUtils.bind(this, function()
  1571. {
  1572. this.copyFile(success, error);
  1573. }), null, mxResources.get('overwrite'), overwrite,
  1574. mxResources.get('cancel'), mxUtils.bind(this, function()
  1575. {
  1576. this.ui.hideDialog();
  1577. }), 380, 150);
  1578. };
  1579. /**
  1580. * Shows a conflict dialog to the user.
  1581. */
  1582. DrawioFile.prototype.showConflictDialog = function(overwrite, synchronize)
  1583. {
  1584. this.ui.showError(mxResources.get('externalChanges'),
  1585. mxResources.get('fileChangedSyncDialog'),
  1586. mxResources.get('overwrite'), overwrite, null,
  1587. mxResources.get('merge'), synchronize,
  1588. mxResources.get('cancel'), mxUtils.bind(this, function()
  1589. {
  1590. this.ui.hideDialog();
  1591. this.handleFileError(null, false);
  1592. }), 380, 130);
  1593. };
  1594. /**
  1595. * Checks if the client is authorized and calls the next step.
  1596. */
  1597. DrawioFile.prototype.redirectToNewApp = function(error, details)
  1598. {
  1599. this.ui.spinner.stop();
  1600. if (!this.redirectDialogShowing)
  1601. {
  1602. this.redirectDialogShowing = true;
  1603. var url = window.location.protocol + '//' + window.location.host + '/' + this.ui.getSearch(
  1604. ['create', 'title', 'mode', 'url', 'drive', 'splash', 'state']) + '#' + this.getHash();
  1605. var msg = mxResources.get('redirectToNewApp');
  1606. if (details != null)
  1607. {
  1608. msg += ' (' + details + ')';
  1609. }
  1610. var redirect = mxUtils.bind(this, function()
  1611. {
  1612. var fn = mxUtils.bind(this, function()
  1613. {
  1614. this.redirectDialogShowing = false;
  1615. if (window.location.href == url)
  1616. {
  1617. window.location.reload();
  1618. }
  1619. else
  1620. {
  1621. window.location.href = url;
  1622. }
  1623. });
  1624. if (error == null && this.isModified())
  1625. {
  1626. this.ui.confirm(mxResources.get('allChangesLost'), mxUtils.bind(this, function()
  1627. {
  1628. this.redirectDialogShowing = false;
  1629. }), fn, mxResources.get('cancel'), mxResources.get('discardChanges'));
  1630. }
  1631. else
  1632. {
  1633. fn();
  1634. }
  1635. });
  1636. if (error != null)
  1637. {
  1638. if (this.isModified())
  1639. {
  1640. this.ui.confirm(msg, mxUtils.bind(this, function()
  1641. {
  1642. this.redirectDialogShowing = false;
  1643. error();
  1644. }), redirect, mxResources.get('cancel'), mxResources.get('discardChanges'));
  1645. }
  1646. else
  1647. {
  1648. this.ui.confirm(msg, redirect, mxUtils.bind(this, function()
  1649. {
  1650. this.redirectDialogShowing = false;
  1651. error();
  1652. }));
  1653. }
  1654. }
  1655. else
  1656. {
  1657. this.ui.alert(mxResources.get('redirectToNewApp'), redirect);
  1658. }
  1659. }
  1660. };
  1661. /**
  1662. * Adds the listener for automatically saving the diagram for local changes.
  1663. */
  1664. DrawioFile.prototype.handleFileSuccess = function(saved)
  1665. {
  1666. this.ui.spinner.stop();
  1667. if (this.ui.getCurrentFile() == this)
  1668. {
  1669. if (this.isModified())
  1670. {
  1671. this.fileChanged();
  1672. }
  1673. else if (saved)
  1674. {
  1675. if (this.isTrashed())
  1676. {
  1677. this.addAllSavedStatus(mxUtils.htmlEntities(mxResources.get(this.allChangesSavedKey)) + ' (' +
  1678. mxUtils.htmlEntities(mxResources.get('fileMovedToTrash')) + ')');
  1679. }
  1680. else
  1681. {
  1682. this.addAllSavedStatus();
  1683. }
  1684. if (this.sync != null)
  1685. {
  1686. this.sync.resetUpdateStatusThread();
  1687. if (this.sync.remoteFileChanged)
  1688. {
  1689. this.sync.remoteFileChanged = false;
  1690. this.sync.fileChangedNotify();
  1691. }
  1692. }
  1693. }
  1694. else
  1695. {
  1696. this.ui.editor.setStatus('');
  1697. }
  1698. }
  1699. };
  1700. /**
  1701. * Adds the listener for automatically saving the diagram for local changes.
  1702. */
  1703. DrawioFile.prototype.handleFileError = function(err, manual)
  1704. {
  1705. this.ui.spinner.stop();
  1706. if (this.ui.getCurrentFile() == this)
  1707. {
  1708. if (this.inConflictState)
  1709. {
  1710. this.handleConflictError(err, manual);
  1711. }
  1712. else
  1713. {
  1714. if (this.isModified())
  1715. {
  1716. this.addUnsavedStatus(err);
  1717. }
  1718. if (manual)
  1719. {
  1720. this.ui.handleError(err, (err != null) ? mxResources.get('errorSavingFile') : null);
  1721. }
  1722. else if (!this.isModified())
  1723. {
  1724. var msg = this.getErrorMessage(err);
  1725. if (msg != null && msg.length > 60)
  1726. {
  1727. msg = msg.substring(0, 60) + '...';
  1728. }
  1729. this.ui.editor.setStatus('<div class="geStatusAlert">' +
  1730. mxUtils.htmlEntities(mxResources.get('error')) + ((msg != null) ?
  1731. ' (' + mxUtils.htmlEntities(msg) + ')' : '') + '</div>');
  1732. }
  1733. }
  1734. }
  1735. };
  1736. /**
  1737. * Adds the listener for automatically saving the diagram for local changes.
  1738. */
  1739. DrawioFile.prototype.handleConflictError = function(err, manual)
  1740. {
  1741. var success = mxUtils.bind(this, function()
  1742. {
  1743. this.handleFileSuccess(true);
  1744. });
  1745. var error = mxUtils.bind(this, function(err2)
  1746. {
  1747. this.handleFileError(err2, true);
  1748. });
  1749. var overwrite = mxUtils.bind(this, function()
  1750. {
  1751. if (this.ui.spinner.spin(document.body, mxResources.get(this.savingSpinnerKey)))
  1752. {
  1753. this.ui.editor.setStatus('');
  1754. var isRepoFile = (this.constructor == GitHubFile) || (this.constructor == GitLabFile);
  1755. this.save(true, success, error, null, true, (isRepoFile &&
  1756. err != null) ? err.commitMessage : null);
  1757. }
  1758. });
  1759. var synchronize = mxUtils.bind(this, function()
  1760. {
  1761. if (this.ui.spinner.spin(document.body, mxResources.get('updatingDocument')))
  1762. {
  1763. this.synchronizeFile(mxUtils.bind(this, function()
  1764. {
  1765. this.ui.spinner.stop();
  1766. if (this.ui.spinner.spin(document.body, mxResources.get(this.savingSpinnerKey)))
  1767. {
  1768. var isRepoFile = (this.constructor == GitHubFile) || (this.constructor == GitLabFile);
  1769. this.save(true, success, error, null, null, (isRepoFile &&
  1770. err != null) ? err.commitMessage : null);
  1771. }
  1772. }), error);
  1773. }
  1774. })
  1775. if (DrawioFile.SYNC == 'none')
  1776. {
  1777. this.showCopyDialog(success, error, overwrite);
  1778. }
  1779. else if (this.invalidChecksum)
  1780. {
  1781. this.showRefreshDialog(success, error, this.getErrorMessage(err));
  1782. }
  1783. else if (manual)
  1784. {
  1785. this.showConflictDialog(overwrite, synchronize);
  1786. }
  1787. else
  1788. {
  1789. this.addConflictStatus(mxUtils.bind(this, function()
  1790. {
  1791. this.ui.editor.setStatus(mxUtils.htmlEntities(
  1792. mxResources.get('updatingDocument')));
  1793. this.synchronizeFile(success, error);
  1794. }), this.getErrorMessage(err));
  1795. }
  1796. };
  1797. /**
  1798. * Adds the listener for automatically saving the diagram for local changes.
  1799. */
  1800. DrawioFile.prototype.getErrorMessage = function(err)
  1801. {
  1802. var msg = (err != null) ? ((err.error != null) ? err.error.message : err.message) : null;
  1803. if (msg == null && err != null && err.code == App.ERROR_TIMEOUT)
  1804. {
  1805. msg = mxResources.get('timeout');
  1806. }
  1807. return msg;
  1808. };
  1809. /**
  1810. * Returns true if the oldest unsaved change is older than <EditorUi.warnInterval>.
  1811. */
  1812. DrawioFile.prototype.isOverdue = function()
  1813. {
  1814. return this.ageStart != null && (Date.now() - this.ageStart.getTime()) >= this.ui.warnInterval;
  1815. };
  1816. /**
  1817. * Adds the listener for automatically saving the diagram for local changes.
  1818. */
  1819. DrawioFile.prototype.fileChanged = function()
  1820. {
  1821. this.lastChanged = new Date();
  1822. this.setModified(true);
  1823. if (this.isAutosave())
  1824. {
  1825. if (this.savingStatusKey != null)
  1826. {
  1827. this.addAllSavedStatus(mxUtils.htmlEntities(mxResources.get(this.savingStatusKey)) + '...');
  1828. }
  1829. this.ui.scheduleSanityCheck();
  1830. if (this.ageStart == null)
  1831. {
  1832. this.ageStart = new Date();
  1833. }
  1834. this.autosave(this.autosaveDelay, this.maxAutosaveDelay, mxUtils.bind(this, function(resp)
  1835. {
  1836. this.ui.stopSanityCheck();
  1837. // Does not update status if another autosave was scheduled
  1838. if (this.autosaveThread == null)
  1839. {
  1840. this.handleFileSuccess(true);
  1841. this.ageStart = null;
  1842. }
  1843. else if (this.isModified())
  1844. {
  1845. this.ui.scheduleSanityCheck();
  1846. this.ageStart = this.lastChanged;
  1847. }
  1848. }), mxUtils.bind(this, function(err)
  1849. {
  1850. this.handleFileError(err);
  1851. }));
  1852. }
  1853. else
  1854. {
  1855. this.ageStart = null;
  1856. if ((!this.isAutosaveOptional() || !this.ui.editor.autosave) &&
  1857. !this.inConflictState)
  1858. {
  1859. this.addUnsavedStatus();
  1860. }
  1861. }
  1862. if (this.sync != null)
  1863. {
  1864. this.sync.localFileChanged();
  1865. }
  1866. };
  1867. /**
  1868. * Creates a secret and token pair for writing a patch to the cache.
  1869. */
  1870. DrawioFile.prototype.createSecret = function(success)
  1871. {
  1872. var secret = Editor.guid(32);
  1873. if (this.sync != null && !this.isOptimisticSync())
  1874. {
  1875. this.sync.createToken(secret, mxUtils.bind(this, function(token)
  1876. {
  1877. success(secret, token);
  1878. }), mxUtils.bind(this, function()
  1879. {
  1880. success(secret);
  1881. }));
  1882. }
  1883. else
  1884. {
  1885. success(secret);
  1886. }
  1887. };
  1888. /**
  1889. * Invokes sync and updates shadow document.
  1890. */
  1891. DrawioFile.prototype.fileSaving = function()
  1892. {
  1893. if (this.sync != null && this.isOptimisticSync())
  1894. {
  1895. this.sync.fileSaving();
  1896. }
  1897. if (urlParams['test'] == '1')
  1898. {
  1899. EditorUi.debug('DrawioFile.fileSaving', [this]);
  1900. }
  1901. };
  1902. /**
  1903. * Invokes sync and updates shadow document.
  1904. */
  1905. DrawioFile.prototype.fileSaved = function(savedData, lastDesc, success, error, token)
  1906. {
  1907. this.lastSaved = new Date();
  1908. this.ageStart = null;
  1909. try
  1910. {
  1911. this.stats.saved++;
  1912. this.inConflictState = false;
  1913. this.invalidChecksum = false;
  1914. if (this.sync == null || this.isOptimisticSync())
  1915. {
  1916. this.shadowData = savedData;
  1917. this.shadowPages = null;
  1918. if (this.sync != null)
  1919. {
  1920. this.sync.lastModified = this.getLastModifiedDate();
  1921. this.sync.resetUpdateStatusThread();
  1922. }
  1923. if (success != null)
  1924. {
  1925. success();
  1926. }
  1927. }
  1928. else
  1929. {
  1930. this.sync.fileSaved(this.ui.getPagesForNode(
  1931. mxUtils.parseXml(savedData).documentElement),
  1932. lastDesc, success, error, token);
  1933. }
  1934. }
  1935. catch (e)
  1936. {
  1937. this.inConflictState = true;
  1938. this.invalidChecksum = true;
  1939. this.descriptorChanged();
  1940. if (error != null)
  1941. {
  1942. error(e);
  1943. }
  1944. try
  1945. {
  1946. if (this.errorReportsEnabled)
  1947. {
  1948. this.sendErrorReport('Error in fileSaved', null, e);
  1949. }
  1950. else
  1951. {
  1952. var user = this.getCurrentUser();
  1953. var uid = (user != null) ? user.id : 'unknown';
  1954. EditorUi.logError('Error in fileSaved', null,
  1955. this.getMode() + '.' + this.getId(),
  1956. uid, e);
  1957. }
  1958. }
  1959. catch (e2)
  1960. {
  1961. // ignore
  1962. }
  1963. }
  1964. if (urlParams['test'] == '1')
  1965. {
  1966. EditorUi.debug('DrawioFile.fileSaved', [this]);
  1967. }
  1968. };
  1969. /**
  1970. * Adds the listener for automatically saving the diagram for local changes.
  1971. */
  1972. DrawioFile.prototype.autosave = function(delay, maxDelay, success, error)
  1973. {
  1974. if (this.lastAutosave == null)
  1975. {
  1976. this.lastAutosave = Date.now();
  1977. }
  1978. var tmp = (Date.now() - this.lastAutosave < maxDelay) ? delay : 0;
  1979. this.clearAutosave();
  1980. // Starts new timer or executes immediately if not unsaved for maxDelay
  1981. var thread = window.setTimeout(mxUtils.bind(this, function()
  1982. {
  1983. this.lastAutosave = null;
  1984. if (this.autosaveThread == thread)
  1985. {
  1986. this.autosaveThread = null;
  1987. }
  1988. // Workaround for duplicate save if UI is blocking
  1989. // after save while pending autosave triggers
  1990. if (this.isModified() && this.isAutosaveNow())
  1991. {
  1992. var rev = this.isAutosaveRevision();
  1993. if (rev)
  1994. {
  1995. this.lastAutosaveRevision = new Date().getTime();
  1996. }
  1997. this.save(rev, mxUtils.bind(this, function(resp)
  1998. {
  1999. this.autosaveCompleted();
  2000. if (success != null)
  2001. {
  2002. success(resp);
  2003. }
  2004. }), mxUtils.bind(this, function(resp)
  2005. {
  2006. if (error != null)
  2007. {
  2008. error(resp);
  2009. }
  2010. }));
  2011. }
  2012. else
  2013. {
  2014. if (!this.isModified())
  2015. {
  2016. this.ui.editor.setStatus('');
  2017. }
  2018. if (success != null)
  2019. {
  2020. success(null);
  2021. }
  2022. }
  2023. }), tmp);
  2024. this.autosaveThread = thread;
  2025. };
  2026. /**
  2027. * Returns true if an autosave is required at the time of execution.
  2028. * This implementation returns true.
  2029. */
  2030. DrawioFile.prototype.isAutosaveNow = function()
  2031. {
  2032. return true;
  2033. };
  2034. /**
  2035. * Hooks for subclassers after the autosave has completed.
  2036. */
  2037. DrawioFile.prototype.autosaveCompleted = function() { };
  2038. /**
  2039. * Adds the listener for automatically saving the diagram for local changes.
  2040. */
  2041. DrawioFile.prototype.clearAutosave = function()
  2042. {
  2043. if (this.autosaveThread != null)
  2044. {
  2045. window.clearTimeout(this.autosaveThread);
  2046. this.autosaveThread = null;
  2047. }
  2048. };
  2049. /**
  2050. * Returns the location as a new object.
  2051. * @type mx.Point
  2052. */
  2053. DrawioFile.prototype.isAutosaveRevision = function()
  2054. {
  2055. var now = new Date().getTime();
  2056. return (this.lastAutosaveRevision == null) || (now - this.lastAutosaveRevision) > this.maxAutosaveRevisionDelay;
  2057. };
  2058. /**
  2059. * Translates this point by the given vector.
  2060. *
  2061. * @param {number} dx X-coordinate of the translation.
  2062. * @param {number} dy Y-coordinate of the translation.
  2063. */
  2064. DrawioFile.prototype.descriptorChanged = function()
  2065. {
  2066. this.fireEvent(new mxEventObject('descriptorChanged'));
  2067. };
  2068. /**
  2069. * Translates this point by the given vector.
  2070. *
  2071. * @param {number} dx X-coordinate of the translation.
  2072. * @param {number} dy Y-coordinate of the translation.
  2073. */
  2074. DrawioFile.prototype.contentChanged = function()
  2075. {
  2076. this.fireEvent(new mxEventObject('contentChanged'));
  2077. };
  2078. /**
  2079. * Returns the location as a new object.
  2080. */
  2081. DrawioFile.prototype.close = function(unloading)
  2082. {
  2083. this.updateFileData();
  2084. this.stats.closed++;
  2085. if (this.isAutosave() && this.isModified())
  2086. {
  2087. this.save(this.isAutosaveRevision(), null, null, unloading);
  2088. }
  2089. this.destroy();
  2090. };
  2091. /**
  2092. * Returns the location as a new object.
  2093. */
  2094. DrawioFile.prototype.hasSameExtension = function(title, newTitle)
  2095. {
  2096. if (title != null && newTitle != null)
  2097. {
  2098. var dot = title.lastIndexOf('.');
  2099. var ext = (dot > 0) ? title.substring(dot) : '';
  2100. dot = newTitle.lastIndexOf('.');
  2101. return ext === ((dot > 0) ? newTitle.substring(dot) : '');
  2102. }
  2103. return title == newTitle;
  2104. };
  2105. /**
  2106. * Removes the change listener.
  2107. */
  2108. DrawioFile.prototype.removeListeners = function()
  2109. {
  2110. if (this.changeListener != null)
  2111. {
  2112. this.ui.editor.graph.model.removeListener(this.changeListener);
  2113. this.ui.editor.graph.removeListener(this.changeListener);
  2114. this.ui.removeListener(this.changeListener);
  2115. this.changeListener = null;
  2116. }
  2117. };
  2118. /**
  2119. * Stops any pending autosaves and removes all listeners.
  2120. */
  2121. DrawioFile.prototype.destroy = function()
  2122. {
  2123. this.clearAutosave();
  2124. this.removeListeners();
  2125. this.stats.destroyed++;
  2126. if (this.sync != null)
  2127. {
  2128. this.sync.destroy();
  2129. this.sync = null;
  2130. }
  2131. };
  2132. /**
  2133. * Are comments supported
  2134. */
  2135. DrawioFile.prototype.commentsSupported = function()
  2136. {
  2137. return false; //The default is false and files that support it must explicitly state that
  2138. };
  2139. /**
  2140. * Show refresh button?
  2141. */
  2142. DrawioFile.prototype.commentsRefreshNeeded = function()
  2143. {
  2144. return true;
  2145. };
  2146. /**
  2147. * Show save button?
  2148. */
  2149. DrawioFile.prototype.commentsSaveNeeded = function()
  2150. {
  2151. return false;
  2152. };
  2153. /**
  2154. * Get comments of the file
  2155. */
  2156. DrawioFile.prototype.getComments = function(success, error)
  2157. {
  2158. success([]); //placeholder
  2159. };
  2160. /**
  2161. * Add a comment to the file
  2162. */
  2163. DrawioFile.prototype.addComment = function(comment, success, error)
  2164. {
  2165. success(Date.now()); //placeholder
  2166. };
  2167. /**
  2168. * Can add a reply to a reply
  2169. */
  2170. DrawioFile.prototype.canReplyToReplies = function()
  2171. {
  2172. return true;
  2173. };
  2174. /**
  2175. * Can add comments (The permission to comment to this file)
  2176. */
  2177. DrawioFile.prototype.canComment = function()
  2178. {
  2179. return true;
  2180. };
  2181. /**
  2182. * Get a new comment object
  2183. */
  2184. DrawioFile.prototype.newComment = function(content, user)
  2185. {
  2186. return new DrawioComment(this, null, content, Date.now(), Date.now(), false, user);
  2187. };