PageRenderTime 122ms CodeModel.GetById 33ms RepoModel.GetById 1ms app.codeStats 0ms

/overlay/usr/local/www/js/diagramly/OneDriveFile.js

https://gitlab.com/bobandrey37/webapp
JavaScript | 590 lines | 365 code | 67 blank | 158 comment | 72 complexity | 9af88165604e5b7bcec7dd080dd3309d MD5 | raw file
  1. /**
  2. * Copyright (c) 2006-2017, JGraph Ltd
  3. * Copyright (c) 2006-2017, Gaudenz Alder
  4. */
  5. OneDriveFile = function(ui, data, meta)
  6. {
  7. DrawioFile.call(this, ui, data);
  8. this.meta = meta;
  9. };
  10. //Extends mxEventSource
  11. mxUtils.extend(OneDriveFile, DrawioFile);
  12. /**
  13. * Shorter autosave delay for optimistic sync.
  14. */
  15. OneDriveFile.prototype.autosaveDelay = 300;
  16. /**
  17. * Hook for subclassers.
  18. */
  19. OneDriveFile.prototype.isRealtimeSupported = function()
  20. {
  21. return true;
  22. };
  23. /**
  24. * Translates this point by the given vector.
  25. *
  26. * @param {number} dx X-coordinate of the translation.
  27. * @param {number} dy Y-coordinate of the translation.
  28. */
  29. OneDriveFile.prototype.share = function()
  30. {
  31. var url = this.meta.webUrl;
  32. url = url.substring(0, url.lastIndexOf('/'));
  33. if (this.meta.parentReference != null)
  34. {
  35. try
  36. {
  37. // Best effort guessing of the web interface URL for the file
  38. if (this.meta.parentReference.driveType == 'personal')
  39. {
  40. url = 'https://onedrive.live.com/?cid=' + encodeURIComponent(this.meta.parentReference.driveId) +
  41. '&id=' + encodeURIComponent(this.meta.id);
  42. }
  43. else if (this.meta.parentReference.driveType == 'documentLibrary')
  44. {
  45. var path = this.meta.parentReference.path;
  46. path = path.substring(path.indexOf('/root:') + 6);
  47. var id = this.meta.webUrl;
  48. var url = id.substring(0, id.length - path.length - this.meta.name.length - ((path.length > 0) ? 1 : 0));
  49. id = id.substring(id.indexOf('/', 8));
  50. url = url + '/Forms/AllItems.aspx?id=' + id + '&parent=' + id.substring(0, id.lastIndexOf('/'));
  51. }
  52. else if (this.meta.parentReference.driveType == 'business')
  53. {
  54. var url = this.meta['@microsoft.graph.downloadUrl'];
  55. var idx = url.indexOf('/_layouts/15/download.aspx?');
  56. // Strips protocol
  57. var id = this.meta.webUrl;
  58. var parent = id;
  59. id = id.substring(8);
  60. // Gets path and parent path
  61. id = id.substring(id.indexOf('/'));
  62. parent = parent.substring(0, parent.lastIndexOf('/'));
  63. parent = parent.substring(parent.indexOf('/', 8))
  64. url = url.substring(0, idx) + '/_layouts/15/onedrive.aspx?id=' + id + '&parent=' + parent;
  65. }
  66. }
  67. catch (e)
  68. {
  69. // ignore
  70. }
  71. }
  72. this.ui.editor.graph.openLink(url);
  73. };
  74. /**
  75. * Translates this point by the given vector.
  76. *
  77. * @param {number} dx X-coordinate of the translation.
  78. * @param {number} dy Y-coordinate of the translation.
  79. */
  80. OneDriveFile.prototype.getId = function()
  81. {
  82. return this.getIdOf(this.meta);
  83. };
  84. /**
  85. * Translates this point by the given vector.
  86. *
  87. * @param {number} dx X-coordinate of the translation.
  88. * @param {number} dy Y-coordinate of the translation.
  89. */
  90. OneDriveFile.prototype.getParentId = function()
  91. {
  92. return this.getIdOf(this.meta, true);
  93. };
  94. /**
  95. * Translates this point by the given vector.
  96. *
  97. * @param {number} dx X-coordinate of the translation.
  98. * @param {number} dy Y-coordinate of the translation.
  99. */
  100. OneDriveFile.prototype.getIdOf = function(itemObj, parent)
  101. {
  102. //TODO driveId is most probably always there. No need to check if it exists. Also, after some time, the code that check the old id format won't be needed
  103. return ((itemObj.parentReference != null && itemObj.parentReference.driveId != null) ? itemObj.parentReference.driveId + '/' : '') +
  104. ((parent != null) ? itemObj.parentReference.id : itemObj.id);
  105. };
  106. /**
  107. * Gets the channel ID for sync messages.
  108. */
  109. OneDriveFile.prototype.getChannelId = function()
  110. {
  111. return 'W-' + DrawioFile.prototype.getChannelId.apply(this, arguments);
  112. };
  113. /**
  114. * Translates this point by the given vector.
  115. *
  116. * @param {number} dx X-coordinate of the translation.
  117. * @param {number} dy Y-coordinate of the translation.
  118. */
  119. OneDriveFile.prototype.getHash = function()
  120. {
  121. return 'W' + encodeURIComponent(this.getId());
  122. };
  123. /**
  124. * Translates this point by the given vector.
  125. *
  126. * @param {number} dx X-coordinate of the translation.
  127. * @param {number} dy Y-coordinate of the translation.
  128. */
  129. OneDriveFile.prototype.getMode = function()
  130. {
  131. return App.MODE_ONEDRIVE;
  132. };
  133. /**
  134. * Overridden to enable the autosave option in the document properties dialog.
  135. */
  136. OneDriveFile.prototype.isAutosaveOptional = function()
  137. {
  138. return true;
  139. };
  140. /**
  141. * Translates this point by the given vector.
  142. *
  143. * @param {number} dx X-coordinate of the translation.
  144. * @param {number} dy Y-coordinate of the translation.
  145. */
  146. OneDriveFile.prototype.getTitle = function()
  147. {
  148. return this.meta.name;
  149. };
  150. /**
  151. * Translates this point by the given vector.
  152. *
  153. * @param {number} dx X-coordinate of the translation.
  154. * @param {number} dy Y-coordinate of the translation.
  155. */
  156. OneDriveFile.prototype.isRenamable = function()
  157. {
  158. return true;
  159. };
  160. /**
  161. * Returns true if the notification to update should be sent
  162. * together with the save request.
  163. */
  164. OneDriveFile.prototype.isOptimisticSync = function()
  165. {
  166. return true;
  167. };
  168. /**
  169. * Enabling fast syncin OneDrive production.
  170. */
  171. OneDriveFile.prototype.isSyncEnabled = function()
  172. {
  173. return true;
  174. };
  175. /**
  176. * Hook for subclassers.
  177. */
  178. OneDriveFile.prototype.isSyncSupported = function()
  179. {
  180. return true;
  181. };
  182. /**
  183. * Specifies if notify events should be ignored.
  184. */
  185. OneDriveFile.prototype.getSize = function()
  186. {
  187. return this.meta.size;
  188. };
  189. /**
  190. * Adds the listener for automatically saving the diagram for local changes.
  191. */
  192. OneDriveFile.prototype.isConflict = function(req)
  193. {
  194. return req != null && (req.getStatus() == 412 || req.getStatus() == 409);
  195. };
  196. /**
  197. * Returns the current etag.
  198. */
  199. OneDriveFile.prototype.getCurrentUser = function()
  200. {
  201. return (this.ui.oneDrive != null) ? this.ui.oneDrive.user : null;
  202. };
  203. /**
  204. * Adds the listener for automatically saving the diagram for local changes.
  205. */
  206. OneDriveFile.prototype.loadDescriptor = function(success, error)
  207. {
  208. this.ui.oneDrive.executeRequest(this.ui.oneDrive.getItemURL(this.getId()), mxUtils.bind(this, function(req)
  209. {
  210. if (req.getStatus() >= 200 && req.getStatus() <= 299)
  211. {
  212. success(JSON.parse(req.getText()));
  213. }
  214. else if (error != null)
  215. {
  216. error();
  217. }
  218. }), error);
  219. };
  220. /**
  221. * Adds the listener for automatically saving the diagram for local changes.
  222. */
  223. OneDriveFile.prototype.getLatestVersion = function(success, error)
  224. {
  225. this.ui.oneDrive.getFile(this.getId(), success, error);
  226. };
  227. /**
  228. * Hook for subclassers to update the descriptor from given file
  229. */
  230. OneDriveFile.prototype.getDescriptor = function()
  231. {
  232. return this.meta;
  233. };
  234. /**
  235. * Hook for subclassers to update the descriptor from given file
  236. */
  237. OneDriveFile.prototype.setDescriptor = function(desc)
  238. {
  239. this.meta = desc;
  240. };
  241. /**
  242. * Adds all listeners.
  243. */
  244. OneDriveFile.prototype.getDescriptorEtag = function(desc)
  245. {
  246. return desc.eTag;
  247. };
  248. /**
  249. * Adds the listener for automatically saving the diagram for local changes.
  250. */
  251. OneDriveFile.prototype.setDescriptorEtag = function(desc, etag)
  252. {
  253. desc.eTag = etag;
  254. };
  255. /**
  256. * Adds the listener for automatically saving the diagram for local changes.
  257. */
  258. OneDriveFile.prototype.loadPatchDescriptor = function(success, error)
  259. {
  260. var url = this.ui.oneDrive.getItemURL(this.getId());
  261. this.ui.oneDrive.executeRequest(url + '?select=etag,file' , mxUtils.bind(this, function(req)
  262. {
  263. if (req.getStatus() >= 200 && req.getStatus() <= 299)
  264. {
  265. success(JSON.parse(req.getText()));
  266. }
  267. else
  268. {
  269. error(this.ui.oneDrive.parseRequestText(req));
  270. }
  271. }), error)
  272. };
  273. /**
  274. * Using MD5 of create timestamp and user ID as crypto key.
  275. */
  276. OneDriveFile.prototype.getChannelKey = function()
  277. {
  278. if (typeof CryptoJS !== 'undefined')
  279. {
  280. return CryptoJS.MD5(this.meta.createdDateTime +
  281. ((this.meta.createdBy != null &&
  282. this.meta.createdBy.user != null) ?
  283. this.meta.createdBy.user.id : '')).toString();
  284. }
  285. return null;
  286. };
  287. /**
  288. * Adds all listeners.
  289. */
  290. OneDriveFile.prototype.getLastModifiedDate = function()
  291. {
  292. return new Date(this.meta.lastModifiedDateTime);
  293. };
  294. /**
  295. * Translates this point by the given vector.
  296. *
  297. * @param {number} dx X-coordinate of the translation.
  298. * @param {number} dy Y-coordinate of the translation.
  299. */
  300. OneDriveFile.prototype.save = function(revision, success, error, unloading, overwrite)
  301. {
  302. this.doSave(this.getTitle(), revision, success, error, unloading, overwrite);
  303. };
  304. /**
  305. * Translates this point by the given vector.
  306. *
  307. * @param {number} dx X-coordinate of the translation.
  308. * @param {number} dy Y-coordinate of the translation.
  309. */
  310. OneDriveFile.prototype.saveAs = function(title, success, error)
  311. {
  312. this.doSave(title, false, success, error);
  313. };
  314. /**
  315. * Translates this point by the given vector.
  316. *
  317. * @param {number} dx X-coordinate of the translation.
  318. * @param {number} dy Y-coordinate of the translation.
  319. */
  320. OneDriveFile.prototype.doSave = function(title, revision, success, error, unloading, overwrite)
  321. {
  322. // Forces update of data for new extensions
  323. var prev = this.meta.name;
  324. this.meta.name = title;
  325. DrawioFile.prototype.save.apply(this, [null, mxUtils.bind(this, function()
  326. {
  327. this.meta.name = prev;
  328. this.saveFile(title, revision, success, error, unloading, overwrite);
  329. }), error, unloading, overwrite]);
  330. };
  331. /**
  332. * Translates this point by the given vector.
  333. *
  334. * @param {number} dx X-coordinate of the translation.
  335. * @param {number} dy Y-coordinate of the translation.
  336. */
  337. OneDriveFile.prototype.saveFile = function(title, revision, success, error, unloading, overwrite)
  338. {
  339. if (!this.isEditable())
  340. {
  341. if (success != null)
  342. {
  343. success();
  344. }
  345. }
  346. else if (!this.savingFile)
  347. {
  348. if (this.getTitle() == title)
  349. {
  350. var doSave = mxUtils.bind(this, function()
  351. {
  352. try
  353. {
  354. // Sets shadow modified state during save
  355. this.savingFileTime = new Date();
  356. this.setShadowModified(false);
  357. this.savingFile = true;
  358. var etag = (!overwrite && this.constructor == OneDriveFile &&
  359. (DrawioFile.SYNC == 'manual' || DrawioFile.SYNC == 'auto')) ?
  360. this.getCurrentEtag() : null;
  361. var lastDesc = this.meta;
  362. this.fileSaving();
  363. this.ui.oneDrive.saveFile(this, mxUtils.bind(this, function(meta, savedData)
  364. {
  365. // Checks for changes during save
  366. this.setModified(this.getShadowModified());
  367. this.savingFile = false;
  368. this.meta = meta;
  369. this.fileSaved(savedData, lastDesc, mxUtils.bind(this, function()
  370. {
  371. this.contentChanged();
  372. if (success != null)
  373. {
  374. success();
  375. }
  376. }), error);
  377. }), mxUtils.bind(this, function(err, req)
  378. {
  379. try
  380. {
  381. this.savingFile = false;
  382. if (this.isConflict(req))
  383. {
  384. this.inConflictState = true;
  385. if (this.sync != null)
  386. {
  387. this.savingFile = true;
  388. this.sync.fileConflict(null, mxUtils.bind(this, function()
  389. {
  390. // Adds random cool-off
  391. window.setTimeout(mxUtils.bind(this, function()
  392. {
  393. this.updateFileData();
  394. doSave();
  395. }), 100 + Math.random() * 500);
  396. }), mxUtils.bind(this, function()
  397. {
  398. this.savingFile = false;
  399. if (error != null)
  400. {
  401. error();
  402. }
  403. }));
  404. }
  405. else if (error != null)
  406. {
  407. error();
  408. }
  409. }
  410. else if (error != null)
  411. {
  412. error(err);
  413. }
  414. }
  415. catch (e)
  416. {
  417. this.savingFile = false;
  418. if (error != null)
  419. {
  420. error(e);
  421. }
  422. else
  423. {
  424. throw e;
  425. }
  426. }
  427. }), etag);
  428. }
  429. catch (e)
  430. {
  431. this.savingFile = false;
  432. if (error != null)
  433. {
  434. error(e);
  435. }
  436. else
  437. {
  438. throw e;
  439. }
  440. }
  441. });
  442. doSave();
  443. }
  444. else
  445. {
  446. // Sets shadow modified state during save
  447. this.savingFileTime = new Date();
  448. this.setShadowModified(false);
  449. this.savingFile = true;
  450. this.ui.oneDrive.insertFile(title, this.getData(), mxUtils.bind(this, function(file)
  451. {
  452. // Checks for changes during save
  453. this.setModified(this.getShadowModified());
  454. this.savingFile = false;
  455. if (success != null)
  456. {
  457. success();
  458. }
  459. this.ui.fileLoaded(file);
  460. }), mxUtils.bind(this, function()
  461. {
  462. this.savingFile = false;
  463. if (error != null)
  464. {
  465. error();
  466. }
  467. }));
  468. }
  469. }
  470. };
  471. /**
  472. * Translates this point by the given vector.
  473. *
  474. * @param {number} dx X-coordinate of the translation.
  475. * @param {number} dy Y-coordinate of the translation.
  476. */
  477. OneDriveFile.prototype.rename = function(title, success, error)
  478. {
  479. var etag = this.getCurrentEtag();
  480. this.ui.oneDrive.renameFile(this, title, mxUtils.bind(this, function(meta)
  481. {
  482. if (!this.hasSameExtension(title, this.getTitle()))
  483. {
  484. this.meta = meta;
  485. if (this.sync != null)
  486. {
  487. this.sync.descriptorChanged(etag);
  488. }
  489. this.save(true, success, error);
  490. }
  491. else
  492. {
  493. this.meta = meta;
  494. this.descriptorChanged();
  495. if (this.sync != null)
  496. {
  497. this.sync.descriptorChanged(etag);
  498. }
  499. if (success != null)
  500. {
  501. success(meta);
  502. }
  503. }
  504. }), error);
  505. };
  506. /**
  507. * Translates this point by the given vector.
  508. *
  509. * @param {number} dx X-coordinate of the translation.
  510. * @param {number} dy Y-coordinate of the translation.
  511. */
  512. OneDriveFile.prototype.move = function(folderId, success, error)
  513. {
  514. this.ui.oneDrive.moveFile(this.getId(), folderId, mxUtils.bind(this, function(meta)
  515. {
  516. this.meta = meta;
  517. this.descriptorChanged();
  518. if (success != null)
  519. {
  520. success(meta);
  521. }
  522. }), error);
  523. };