DropboxFile.js 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213
  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.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. this.setModified(false);
  120. this.isModified = function()
  121. {
  122. return modified;
  123. };
  124. this.ui.dropbox.saveFile(title, this.getData(), mxUtils.bind(this, function(stat)
  125. {
  126. this.savingFile = false;
  127. this.isModified = prevModified;
  128. this.stat = stat;
  129. this.contentChanged();
  130. if (success != null)
  131. {
  132. success();
  133. }
  134. }), mxUtils.bind(this, function(resp)
  135. {
  136. this.savingFile = false;
  137. this.isModified = prevModified;
  138. this.setModified(modified || this.isModified());
  139. if (error != null)
  140. {
  141. error(resp);
  142. }
  143. }));
  144. }
  145. else if (error != null)
  146. {
  147. error();
  148. }
  149. });
  150. if (this.getTitle() == title)
  151. {
  152. fn(true);
  153. }
  154. else
  155. {
  156. this.ui.dropbox.checkExists(title, fn);
  157. }
  158. }
  159. else if (error != null)
  160. {
  161. error({code: App.ERROR_BUSY});
  162. }
  163. };
  164. /**
  165. * Translates this point by the given vector.
  166. *
  167. * @param {number} dx X-coordinate of the translation.
  168. * @param {number} dy Y-coordinate of the translation.
  169. */
  170. DropboxFile.prototype.rename = function(title, success, error)
  171. {
  172. this.ui.dropbox.renameFile(this, title, mxUtils.bind(this, function(stat)
  173. {
  174. if (!this.hasSameExtension(title, this.getTitle()))
  175. {
  176. this.stat = stat;
  177. // Required in this case to update hash tag in page
  178. // before saving so that the edit link is correct
  179. this.descriptorChanged();
  180. this.save(true, success, error);
  181. }
  182. else
  183. {
  184. this.stat = stat;
  185. this.descriptorChanged();
  186. if (success != null)
  187. {
  188. success();
  189. }
  190. }
  191. }), error);
  192. };