OneDriveFile.js 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255
  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. * Translates this point by the given vector.
  14. *
  15. * @param {number} dx X-coordinate of the translation.
  16. * @param {number} dy Y-coordinate of the translation.
  17. */
  18. OneDriveFile.prototype.getHash = function()
  19. {
  20. return 'W' + encodeURIComponent(this.meta.id);
  21. };
  22. /**
  23. * Translates this point by the given vector.
  24. *
  25. * @param {number} dx X-coordinate of the translation.
  26. * @param {number} dy Y-coordinate of the translation.
  27. */
  28. OneDriveFile.prototype.getMode = function()
  29. {
  30. return App.MODE_ONEDRIVE;
  31. };
  32. /**
  33. * Overridden to enable the autosave option in the document properties dialog.
  34. */
  35. OneDriveFile.prototype.isAutosaveOptional = function()
  36. {
  37. return true;
  38. };
  39. /**
  40. * Translates this point by the given vector.
  41. *
  42. * @param {number} dx X-coordinate of the translation.
  43. * @param {number} dy Y-coordinate of the translation.
  44. */
  45. OneDriveFile.prototype.getTitle = function()
  46. {
  47. return this.meta.name;
  48. };
  49. /**
  50. * Translates this point by the given vector.
  51. *
  52. * @param {number} dx X-coordinate of the translation.
  53. * @param {number} dy Y-coordinate of the translation.
  54. */
  55. OneDriveFile.prototype.isRenamable = function()
  56. {
  57. return true;
  58. };
  59. /**
  60. * Translates this point by the given vector.
  61. *
  62. * @param {number} dx X-coordinate of the translation.
  63. * @param {number} dy Y-coordinate of the translation.
  64. */
  65. OneDriveFile.prototype.save = function(revision, success, error)
  66. {
  67. this.doSave(this.getTitle(), success, error);
  68. };
  69. /**
  70. * Translates this point by the given vector.
  71. *
  72. * @param {number} dx X-coordinate of the translation.
  73. * @param {number} dy Y-coordinate of the translation.
  74. */
  75. OneDriveFile.prototype.saveAs = function(title, success, error)
  76. {
  77. this.doSave(title, success, error);
  78. };
  79. /**
  80. * Translates this point by the given vector.
  81. *
  82. * @param {number} dx X-coordinate of the translation.
  83. * @param {number} dy Y-coordinate of the translation.
  84. */
  85. OneDriveFile.prototype.doSave = function(title, success, error)
  86. {
  87. // Forces update of data for new extensions
  88. var prev = this.meta.name;
  89. this.meta.name = title;
  90. DrawioFile.prototype.save.apply(this, arguments);
  91. this.meta.name = prev;
  92. this.saveFile(title, false, success, error);
  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.saveFile = function(title, revision, success, error)
  101. {
  102. if (!this.isEditable())
  103. {
  104. if (success != null)
  105. {
  106. success();
  107. }
  108. }
  109. else if (!this.savingFile)
  110. {
  111. this.savingFile = true;
  112. if (this.getTitle() == title)
  113. {
  114. // Makes sure no changes get lost while the file is saved
  115. var prevModified = this.isModified;
  116. var modified = this.isModified();
  117. var prepare = mxUtils.bind(this, function()
  118. {
  119. this.setModified(false);
  120. this.isModified = function()
  121. {
  122. return modified;
  123. };
  124. });
  125. prepare();
  126. this.ui.oneDrive.saveFile(this, mxUtils.bind(this, function(meta)
  127. {
  128. this.savingFile = false;
  129. this.isModified = prevModified;
  130. this.meta = meta;
  131. this.contentChanged();
  132. if (success != null)
  133. {
  134. success();
  135. }
  136. }),
  137. mxUtils.bind(this, function(err)
  138. {
  139. this.savingFile = false;
  140. this.isModified = prevModified;
  141. this.setModified(modified || this.isModified());
  142. if (error != null)
  143. {
  144. // Handles modified state for retries
  145. if (err != null && err.retry != null)
  146. {
  147. var retry = err.retry;
  148. err.retry = function()
  149. {
  150. prepare();
  151. retry();
  152. };
  153. }
  154. error(err);
  155. }
  156. }));
  157. }
  158. else
  159. {
  160. this.ui.oneDrive.insertFile(title, this.getData(), mxUtils.bind(this, function(file)
  161. {
  162. this.savingFile = false;
  163. if (success != null)
  164. {
  165. success();
  166. }
  167. this.ui.fileLoaded(file);
  168. }), mxUtils.bind(this, function()
  169. {
  170. this.savingFile = false;
  171. if (error != null)
  172. {
  173. error();
  174. }
  175. }));
  176. }
  177. }
  178. else if (error != null)
  179. {
  180. error({code: App.ERROR_BUSY});
  181. }
  182. };
  183. /**
  184. * Translates this point by the given vector.
  185. *
  186. * @param {number} dx X-coordinate of the translation.
  187. * @param {number} dy Y-coordinate of the translation.
  188. */
  189. OneDriveFile.prototype.rename = function(title, success, error)
  190. {
  191. this.ui.oneDrive.renameFile(this, title, mxUtils.bind(this, function(meta)
  192. {
  193. if (!this.hasSameExtension(title, this.getTitle()))
  194. {
  195. this.meta = meta;
  196. this.save(true, success, error);
  197. }
  198. else
  199. {
  200. this.meta = meta;
  201. this.descriptorChanged();
  202. if (success != null)
  203. {
  204. success();
  205. }
  206. }
  207. }), error);
  208. };
  209. /**
  210. * Translates this point by the given vector.
  211. *
  212. * @param {number} dx X-coordinate of the translation.
  213. * @param {number} dy Y-coordinate of the translation.
  214. */
  215. OneDriveFile.prototype.move = function(folderId, success, error)
  216. {
  217. this.ui.oneDrive.moveFile(this.meta.id, folderId, mxUtils.bind(this, function(meta)
  218. {
  219. this.meta = meta;
  220. this.descriptorChanged();
  221. if (success != null)
  222. {
  223. success(meta);
  224. }
  225. }), error);
  226. };