OneDriveFile.js 14 KB

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