DropboxFile.js 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231
  1. /**
  2. * Copyright (c) 2006-2017, JGraph Ltd
  3. * Copyright (c) 2006-2017, Gaudenz Alder
  4. */
  5. DropboxFile = function(ui, data, stat)
  6. {
  7. DrawioFile.call(this, ui, data);
  8. this.stat = stat;
  9. };
  10. //Extends mxEventSource
  11. mxUtils.extend(DropboxFile, 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. DropboxFile.prototype.getHash = function()
  19. {
  20. return 'D' + encodeURIComponent(this.stat.path_display.substring(1));
  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. DropboxFile.prototype.getMode = function()
  29. {
  30. return App.MODE_DROPBOX;
  31. };
  32. /**
  33. * Overridden to enable the autosave option in the document properties dialog.
  34. */
  35. DropboxFile.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. DropboxFile.prototype.getTitle = function()
  46. {
  47. return this.stat.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. DropboxFile.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. DropboxFile.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. DropboxFile.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. DropboxFile.prototype.doSave = function(title, success, error)
  86. {
  87. // Forces update of data for new extensions
  88. var prev = this.stat.name;
  89. this.stat.name = title;
  90. DrawioFile.prototype.save.apply(this, arguments);
  91. this.stat.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. DropboxFile.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. var fn = mxUtils.bind(this, function(checked)
  112. {
  113. if (checked)
  114. {
  115. this.savingFile = true;
  116. // Makes sure no changes get lost while the file is saved
  117. var prevModified = this.isModified;
  118. var modified = this.isModified();
  119. var prepare = mxUtils.bind(this, function()
  120. {
  121. this.setModified(false);
  122. this.isModified = function()
  123. {
  124. return modified;
  125. };
  126. });
  127. prepare();
  128. this.ui.dropbox.saveFile(title, this.getData(), mxUtils.bind(this, function(stat)
  129. {
  130. this.savingFile = false;
  131. this.isModified = prevModified;
  132. this.stat = stat;
  133. this.contentChanged();
  134. if (success != null)
  135. {
  136. success();
  137. }
  138. }), mxUtils.bind(this, function(err)
  139. {
  140. this.savingFile = false;
  141. this.isModified = prevModified;
  142. this.setModified(modified || this.isModified());
  143. if (error != null)
  144. {
  145. // Handles modified state for retries
  146. if (err != null && err.retry != null)
  147. {
  148. var retry = err.retry;
  149. err.retry = function()
  150. {
  151. prepare();
  152. retry();
  153. };
  154. }
  155. error(err);
  156. }
  157. }));
  158. }
  159. else if (error != null)
  160. {
  161. error();
  162. }
  163. });
  164. if (this.getTitle() == title)
  165. {
  166. fn(true);
  167. }
  168. else
  169. {
  170. this.ui.dropbox.checkExists(title, fn);
  171. }
  172. }
  173. else if (error != null)
  174. {
  175. error({code: App.ERROR_BUSY});
  176. }
  177. };
  178. /**
  179. * Translates this point by the given vector.
  180. *
  181. * @param {number} dx X-coordinate of the translation.
  182. * @param {number} dy Y-coordinate of the translation.
  183. */
  184. DropboxFile.prototype.rename = function(title, success, error)
  185. {
  186. this.ui.dropbox.renameFile(this, title, mxUtils.bind(this, function(stat)
  187. {
  188. if (!this.hasSameExtension(title, this.getTitle()))
  189. {
  190. this.stat = stat;
  191. // Required in this case to update hash tag in page
  192. // before saving so that the edit link is correct
  193. this.descriptorChanged();
  194. this.save(true, success, error);
  195. }
  196. else
  197. {
  198. this.stat = stat;
  199. this.descriptorChanged();
  200. if (success != null)
  201. {
  202. success();
  203. }
  204. }
  205. }), error);
  206. };