DropboxFile.js 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249
  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. var doSave = mxUtils.bind(this, function(data)
  129. {
  130. var index = this.stat.path_display.lastIndexOf('/');
  131. var folder = (index > 1) ? this.stat.path_display.substring(1, index + 1) : null;
  132. this.ui.dropbox.saveFile(title, data, mxUtils.bind(this, function(stat)
  133. {
  134. this.savingFile = false;
  135. this.isModified = prevModified;
  136. this.stat = stat;
  137. this.contentChanged();
  138. if (success != null)
  139. {
  140. success();
  141. }
  142. }), mxUtils.bind(this, function(err)
  143. {
  144. this.savingFile = false;
  145. this.isModified = prevModified;
  146. this.setModified(modified || this.isModified());
  147. if (error != null)
  148. {
  149. // Handles modified state for retries
  150. if (err != null && err.retry != null)
  151. {
  152. var retry = err.retry;
  153. err.retry = function()
  154. {
  155. prepare();
  156. retry();
  157. };
  158. }
  159. error(err);
  160. }
  161. }), folder);
  162. });
  163. if (this.ui.useCanvasForExport && /(\.png)$/i.test(this.getTitle()))
  164. {
  165. this.ui.getEmbeddedPng(mxUtils.bind(this, function(data)
  166. {
  167. doSave(this.ui.base64ToBlob(data, 'image/png'));
  168. }), error, (this.ui.getCurrentFile() != this) ? this.getData() : null);
  169. }
  170. else
  171. {
  172. doSave(this.getData());
  173. }
  174. }
  175. else if (error != null)
  176. {
  177. error();
  178. }
  179. });
  180. if (this.getTitle() == title)
  181. {
  182. fn(true);
  183. }
  184. else
  185. {
  186. this.ui.dropbox.checkExists(title, fn);
  187. }
  188. }
  189. else if (error != null)
  190. {
  191. error({code: App.ERROR_BUSY});
  192. }
  193. };
  194. /**
  195. * Translates this point by the given vector.
  196. *
  197. * @param {number} dx X-coordinate of the translation.
  198. * @param {number} dy Y-coordinate of the translation.
  199. */
  200. DropboxFile.prototype.rename = function(title, success, error)
  201. {
  202. this.ui.dropbox.renameFile(this, title, mxUtils.bind(this, function(stat)
  203. {
  204. if (!this.hasSameExtension(title, this.getTitle()))
  205. {
  206. this.stat = stat;
  207. // Required in this case to update hash tag in page
  208. // before saving so that the edit link is correct
  209. this.descriptorChanged();
  210. this.save(true, success, error);
  211. }
  212. else
  213. {
  214. this.stat = stat;
  215. this.descriptorChanged();
  216. if (success != null)
  217. {
  218. success();
  219. }
  220. }
  221. }), error);
  222. };