nextcloud.js 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577
  1. /**
  2. * Plugin for embed mode in Nextcloud
  3. */
  4. Draw.loadPlugin(function(ui)
  5. {
  6. var loadDescriptor = null;
  7. var ncUser = null;
  8. mxEvent.addListener(window, 'message', mxUtils.bind(this, function(evt)
  9. {
  10. var data = evt.data;
  11. try
  12. {
  13. data = JSON.parse(data);
  14. if (data.action == 'load')
  15. {
  16. if (data.desc != null)
  17. {
  18. loadDescriptor = data.desc;
  19. }
  20. if (data.disableAutoSave)
  21. {
  22. ui.editor.setAutosave(false);
  23. }
  24. }
  25. }
  26. catch (e)
  27. {
  28. // Ignore
  29. }
  30. }));
  31. ui.getCurrentUser = function()
  32. {
  33. if (ncUser == null)
  34. {
  35. ui.remoteInvoke('getCurrentUser', null, null, function(user)
  36. {
  37. ncUser = user == null? new DrawioUser(Date.now(), null, 'Anonymous')
  38. : new DrawioUser(user.uid, null, user.displayName);
  39. }, function()
  40. {
  41. //ignore such that next call we retry
  42. });
  43. //Return a dummy user until we have the actual user in order for UI to be populated
  44. return new DrawioUser(Date.now(), null, 'Anonymous');
  45. }
  46. return ncUser;
  47. };
  48. //======================== Revisions ========================
  49. ui.isRevisionHistoryEnabled = function()
  50. {
  51. var file = ui.getCurrentFile();
  52. return file && file.desc && (!file.desc.ver || file.desc.versionsEnabled);
  53. };
  54. ui.isRevisionHistorySupported = function()
  55. {
  56. return ui.isRevisionHistoryEnabled();
  57. };
  58. /**
  59. * Get revisions of current file
  60. */
  61. ui.getRevisions = function(success, error)
  62. {
  63. var desc = ui.getCurrentFile().desc;
  64. var id = desc.ver > 1? desc.id : desc.path;
  65. function getXml(success, error)
  66. {
  67. ui.remoteInvoke('loadFileVersion', [id, this.revId], null, success, error);
  68. };
  69. function restoreFn(xml)
  70. {
  71. if (ui.spinner.spin(document.body, mxResources.get('restoring')))
  72. {
  73. ui.tryAndHandle(function()
  74. {
  75. ui.replaceFileData(xml);
  76. ui.spinner.stop();
  77. ui.hideDialog();
  78. });
  79. }
  80. };
  81. ui.remoteInvoke('getFileRevisions', [id], null, function(revisions)
  82. {
  83. revisions.sort(function(a, b)
  84. {
  85. return a.timestamp - b.timestamp;
  86. });
  87. //convert to editor format and add getXml function
  88. var revs = [];
  89. for (var i = 0; i < revisions.length; i++)
  90. {
  91. var rev = revisions[i];
  92. rev.modifiedDate = rev.timestamp * 1000;
  93. rev.getXml = mxUtils.bind(rev, getXml);
  94. revs.push(rev);
  95. }
  96. success(revs, restoreFn);
  97. }, error);
  98. };
  99. //============= Embed File with real-time collab support =================
  100. // Use optimistic sync since we cannot save file properties/metadata so far
  101. /**
  102. * Shorter autosave delay for optimistic sync.
  103. */
  104. EmbedFile.prototype.autosaveDelay = 500;
  105. /**
  106. * Delay for last save in ms.
  107. */
  108. EmbedFile.prototype.saveDelay = 0;
  109. /**
  110. *
  111. */
  112. EmbedFile.prototype.isConflict = function(err)
  113. {
  114. return err != null && err.status == 409;
  115. };
  116. /**
  117. * Returns the current user.
  118. */
  119. EmbedFile.prototype.getCurrentUser = function()
  120. {
  121. return ui.getCurrentUser();
  122. };
  123. EmbedFile.prototype.isRealtimeSupported = function()
  124. {
  125. return true;
  126. };
  127. /**
  128. *
  129. */
  130. EmbedFile.prototype.save = function(revision, success, error, unloading, overwrite)
  131. {
  132. this.saveStarted = true;
  133. DrawioFile.prototype.save.apply(this, [revision, mxUtils.bind(this, function()
  134. {
  135. this.saveFile(null, revision, success, error, unloading, overwrite);
  136. this.saveStarted = false;
  137. }), error, unloading, overwrite]);
  138. };
  139. /**
  140. *
  141. */
  142. EmbedFile.prototype.setModified = function(value)
  143. {
  144. DrawioFile.prototype.setModified.apply(this, arguments);
  145. //Set editor modified also to prevent accidental closure or exiting without saving
  146. ui.editor.modified = value;
  147. };
  148. /**
  149. *
  150. */
  151. EmbedFile.prototype.saveFile = function(title, revision, success, error, unloading, overwrite)
  152. {
  153. EditorUi.debug('EmbedFile.saveFile', [this], 'saving', this.savingFile);
  154. try
  155. {
  156. if (!this.isEditable())
  157. {
  158. if (success != null)
  159. {
  160. success();
  161. }
  162. }
  163. else if (!this.savingFile)
  164. {
  165. // Sets shadow modified state during save
  166. this.savingFileTime = new Date();
  167. this.setShadowModified(false);
  168. this.savingFile = true;
  169. var doSave = mxUtils.bind(this, function()
  170. {
  171. try
  172. {
  173. var lastDesc = this.desc;
  174. var savedData = this.getData();
  175. var etag = this.getCurrentEtag();
  176. if (this.sync != null)
  177. {
  178. this.sync.fileSaving();
  179. }
  180. ui.remoteInvoke('saveFile', this.desc.ver > 1? [this.desc.id, this.desc.shareToken, savedData, etag] :
  181. [this.desc.path, savedData, etag], null, mxUtils.bind(this, function(resp)
  182. {
  183. try
  184. {
  185. // Checks for changes during save
  186. this.setModified(this.getShadowModified());
  187. this.savingFile = false;
  188. this.desc = Object.assign({}, this.desc); // Clone the object
  189. Object.assign(this.desc, resp); // Assign the new values
  190. this.fileSaved(savedData, lastDesc, mxUtils.bind(this, function()
  191. {
  192. this.contentChanged();
  193. if (success != null)
  194. {
  195. success();
  196. }
  197. }), error);
  198. }
  199. catch (e)
  200. {
  201. this.savingFile = false;
  202. if (error != null)
  203. {
  204. error(e);
  205. }
  206. else
  207. {
  208. throw e;
  209. }
  210. }
  211. }),
  212. mxUtils.bind(this, function(err)
  213. {
  214. try
  215. {
  216. this.savingFile = false;
  217. if (this.isConflict(err))
  218. {
  219. this.inConflictState = true;
  220. if (this.sync != null)
  221. {
  222. this.savingFile = true;
  223. this.sync.fileConflict(null, mxUtils.bind(this, function()
  224. {
  225. // Adds random cool-off
  226. var delay = 100 + Math.random() * 500;
  227. window.setTimeout(mxUtils.bind(this, function()
  228. {
  229. this.updateFileData();
  230. doSave();
  231. }), delay);
  232. EditorUi.debug('EmbedFile.saveFile.conflict',
  233. [this], 'err', err, 'delay', delay);
  234. }), mxUtils.bind(this, function()
  235. {
  236. this.savingFile = false;
  237. if (error != null)
  238. {
  239. error();
  240. }
  241. }));
  242. }
  243. else if (error != null)
  244. {
  245. error();
  246. }
  247. }
  248. else if (error != null)
  249. {
  250. error(err);
  251. }
  252. }
  253. catch (e)
  254. {
  255. this.savingFile = false;
  256. if (error != null)
  257. {
  258. error(e);
  259. }
  260. else
  261. {
  262. throw e;
  263. }
  264. }
  265. }));
  266. }
  267. catch (e)
  268. {
  269. this.savingFile = false;
  270. if (error != null)
  271. {
  272. error(e);
  273. }
  274. else
  275. {
  276. throw e;
  277. }
  278. }
  279. });
  280. doSave();
  281. }
  282. }
  283. catch (e)
  284. {
  285. if (error != null)
  286. {
  287. error(e);
  288. }
  289. else
  290. {
  291. throw e;
  292. }
  293. }
  294. };
  295. /**
  296. *
  297. */
  298. EmbedFile.prototype.getTitle = function()
  299. {
  300. return this.desc.name;
  301. };
  302. /**
  303. *
  304. */
  305. EmbedFile.prototype.getHash = function()
  306. {
  307. return 'E' + this.getId();
  308. };
  309. /**
  310. * Overridden to enable the autosave option in the document properties dialog.
  311. */
  312. EmbedFile.prototype.isAutosaveOptional = function()
  313. {
  314. return true;
  315. };
  316. /**
  317. *
  318. */
  319. EmbedFile.prototype.getId = function()
  320. {
  321. return this.desc.instanceId + '$$' + this.desc.id;
  322. };
  323. /**
  324. *
  325. */
  326. EmbedFile.prototype.isSyncSupported = function()
  327. {
  328. return this.desc != null && this.desc.id != null && this.desc.instanceId != null;
  329. };
  330. /**
  331. *
  332. */
  333. EmbedFile.prototype.isRevisionHistorySupported = function()
  334. {
  335. return true;
  336. };
  337. EmbedFile.prototype.isOptimisticSync = function()
  338. {
  339. return true;
  340. };
  341. EmbedFile.prototype.getSize = function()
  342. {
  343. return this.desc.size;
  344. };
  345. EmbedFile.prototype.isEditable = function()
  346. {
  347. return this.desc != null && this.desc.writeable;
  348. };
  349. /**
  350. *
  351. */
  352. EmbedFile.prototype.getLatestVersion = function(success, error)
  353. {
  354. ui.remoteInvoke('loadFile', this.desc.ver > 1? [this.desc.id, this.desc.shareToken] : [this.desc.path],
  355. null, mxUtils.bind(this, function(data)
  356. {
  357. var xml = data.xml;
  358. delete data.xml;
  359. success(new EmbedFile(ui, xml, data));
  360. }), error);
  361. };
  362. /**
  363. * Gets the channel ID from the given descriptor.
  364. */
  365. EmbedFile.prototype.getChannelId = function()
  366. {
  367. return 'C-' + DrawioFile.prototype.getChannelId.apply(this, arguments);
  368. };
  369. EmbedFile.prototype.getHash = function()
  370. {
  371. return 'C' + encodeURIComponent(this.getId());
  372. };
  373. /**
  374. * Using MD5 of create timestamp and user ID as crypto key.
  375. */
  376. EmbedFile.prototype.getChannelKey = function()
  377. {
  378. if (typeof CryptoJS !== 'undefined')
  379. {
  380. return CryptoJS.MD5(this.desc.instanceId + this.desc.id).toString();
  381. }
  382. return null;
  383. };
  384. /**
  385. *
  386. */
  387. EmbedFile.prototype.getLastModifiedDate = function()
  388. {
  389. return new Date(this.desc.mtime * 1000);
  390. };
  391. /**
  392. *
  393. */
  394. EmbedFile.prototype.getDescriptor = function()
  395. {
  396. return this.desc;
  397. };
  398. /**
  399. * Updates the descriptor of this file with the one from the given file.
  400. */
  401. EmbedFile.prototype.setDescriptor = function(desc)
  402. {
  403. this.desc = desc;
  404. };
  405. /**
  406. *
  407. */
  408. EmbedFile.prototype.getDescriptorEtag = function(desc)
  409. {
  410. return desc.etag;
  411. };
  412. /**
  413. *
  414. */
  415. EmbedFile.prototype.setDescriptorEtag = function(desc, etag)
  416. {
  417. desc.etag = etag;
  418. };
  419. /**
  420. *
  421. */
  422. EmbedFile.prototype.loadDescriptor = function(success, error)
  423. {
  424. ui.remoteInvoke('getFileInfo', this.desc.ver > 1? [this.desc.id, this.desc.shareToken] : [this.desc.path], null, success, error);
  425. };
  426. var allowAutoSave = true;
  427. EmbedFile.prototype.isAutosaveNow = function(success, error)
  428. {
  429. return allowAutoSave;
  430. };
  431. //Ensure saving is via the file
  432. ui.actions.get('save').funct = function(exit)
  433. {
  434. if (ui.editor.graph.isEditing())
  435. {
  436. ui.editor.graph.stopEditing();
  437. }
  438. var curFile = ui.getCurrentFile();
  439. if (exit)
  440. {
  441. allowAutoSave = false;
  442. }
  443. function doActions()
  444. {
  445. if (exit)
  446. {
  447. ui.actions.get('exit').funct();
  448. }
  449. };
  450. function doSave()
  451. {
  452. if (curFile.saveStarted || curFile.savingFile)
  453. {
  454. setTimeout(doSave, 100);
  455. return;
  456. }
  457. if (curFile.isModified())
  458. {
  459. ui.saveFile(null, doActions);
  460. }
  461. else
  462. {
  463. doActions();
  464. }
  465. };
  466. doSave();
  467. };
  468. //Add file opening here (or it should be for all in EditorUi?)
  469. var origInstallMessageHandler = ui.installMessageHandler;
  470. ui.installMessageHandler = function(callback)
  471. {
  472. origInstallMessageHandler.call(this, function(xml, evt)
  473. {
  474. try
  475. {
  476. // New empty files
  477. if (JSON.parse(evt.data).xml == ' ') return;
  478. }
  479. catch (e) {} // Ignore
  480. callback.apply(this, arguments);
  481. var file = ui.getCurrentFile();
  482. loadDescriptor = loadDescriptor || {};
  483. // New files call this twice, so we need to check if the file is loaded
  484. if (!loadDescriptor.isLoaded)
  485. {
  486. loadDescriptor.isLoaded = true;
  487. file.setDescriptor(loadDescriptor);
  488. ui.fileLoaded(file, true);
  489. }
  490. });
  491. }
  492. ui.editor.setModified = function()
  493. {
  494. //Cancel set modified of the editor and use the file's one
  495. };
  496. //Prefetch current user
  497. ui.getCurrentUser();
  498. });