DropboxFile.js 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216
  1. // $Id = DropboxFile.js,v 1.12 2010-01-02 09 =45 =14 gaudenz Exp $
  2. // Copyright (c) 2006-2014, JGraph Ltd
  3. /**
  4. * Constructs a new point for the optional x and y coordinates. If no
  5. * coordinates are given, then the default values for <x> and <y> are used.
  6. * @constructor
  7. * @class Implements a basic 2D point. Known subclassers = {@link mxRectangle}.
  8. * @param {number} x X-coordinate of the point.
  9. * @param {number} y Y-coordinate of the point.
  10. */
  11. DropboxFile = function(ui, data, stat)
  12. {
  13. DrawioFile.call(this, ui, data);
  14. this.stat = stat;
  15. };
  16. //Extends mxEventSource
  17. mxUtils.extend(DropboxFile, DrawioFile);
  18. /**
  19. * Translates this point by the given vector.
  20. *
  21. * @param {number} dx X-coordinate of the translation.
  22. * @param {number} dy Y-coordinate of the translation.
  23. */
  24. DropboxFile.prototype.getHash = function()
  25. {
  26. return 'D' + encodeURIComponent(this.stat.path.substring(1));
  27. };
  28. /**
  29. * Translates this point by the given vector.
  30. *
  31. * @param {number} dx X-coordinate of the translation.
  32. * @param {number} dy Y-coordinate of the translation.
  33. */
  34. DropboxFile.prototype.getMode = function()
  35. {
  36. return App.MODE_DROPBOX;
  37. };
  38. /**
  39. * Overridden to enable the autosave option in the document properties dialog.
  40. */
  41. DropboxFile.prototype.isAutosaveOptional = function()
  42. {
  43. return true;
  44. };
  45. /**
  46. * Translates this point by the given vector.
  47. *
  48. * @param {number} dx X-coordinate of the translation.
  49. * @param {number} dy Y-coordinate of the translation.
  50. */
  51. DropboxFile.prototype.getTitle = function()
  52. {
  53. return this.stat.name;
  54. };
  55. /**
  56. * Translates this point by the given vector.
  57. *
  58. * @param {number} dx X-coordinate of the translation.
  59. * @param {number} dy Y-coordinate of the translation.
  60. */
  61. DropboxFile.prototype.isRenamable = function()
  62. {
  63. return true;
  64. };
  65. /**
  66. * Translates this point by the given vector.
  67. *
  68. * @param {number} dx X-coordinate of the translation.
  69. * @param {number} dy Y-coordinate of the translation.
  70. */
  71. DropboxFile.prototype.save = function(revision, success, error)
  72. {
  73. this.doSave(this.getTitle(), success, error);
  74. };
  75. /**
  76. * Translates this point by the given vector.
  77. *
  78. * @param {number} dx X-coordinate of the translation.
  79. * @param {number} dy Y-coordinate of the translation.
  80. */
  81. DropboxFile.prototype.saveAs = function(title, success, error)
  82. {
  83. this.doSave(title, success, error);
  84. };
  85. /**
  86. * Translates this point by the given vector.
  87. *
  88. * @param {number} dx X-coordinate of the translation.
  89. * @param {number} dy Y-coordinate of the translation.
  90. */
  91. DropboxFile.prototype.doSave = function(title, success, error)
  92. {
  93. DrawioFile.prototype.save.apply(this, arguments);
  94. this.saveFile(title, false, success, error);
  95. };
  96. /**
  97. * Translates this point by the given vector.
  98. *
  99. * @param {number} dx X-coordinate of the translation.
  100. * @param {number} dy Y-coordinate of the translation.
  101. */
  102. DropboxFile.prototype.saveFile = function(title, revision, success, error)
  103. {
  104. if (!this.isEditable())
  105. {
  106. if (success != null)
  107. {
  108. success();
  109. }
  110. }
  111. else if (!this.savingFile)
  112. {
  113. var fn = mxUtils.bind(this, function(checked)
  114. {
  115. if (checked)
  116. {
  117. this.savingFile = true;
  118. // Makes sure no changes get lost while the file is saved
  119. var prevModified = this.isModified;
  120. var modified = this.isModified();
  121. this.setModified(false);
  122. this.isModified = function()
  123. {
  124. return modified;
  125. };
  126. this.ui.dropbox.saveFile(title, this.getData(), mxUtils.bind(this, function(stat)
  127. {
  128. this.savingFile = false;
  129. this.isModified = prevModified;
  130. this.stat = stat;
  131. this.contentChanged();
  132. if (success != null)
  133. {
  134. success();
  135. }
  136. }),
  137. mxUtils.bind(this, function(resp)
  138. {
  139. this.savingFile = false;
  140. this.isModified = prevModified;
  141. this.setModified(modified || this.isModified());
  142. if (error != null)
  143. {
  144. error(resp);
  145. }
  146. }));
  147. }
  148. else if (error != null)
  149. {
  150. error();
  151. }
  152. });
  153. if (this.getTitle() == title)
  154. {
  155. fn(true);
  156. }
  157. else
  158. {
  159. this.ui.dropbox.checkExists(title, fn);
  160. }
  161. }
  162. else if (error != null)
  163. {
  164. error({code: App.ERROR_BUSY});
  165. }
  166. };
  167. /**
  168. * Translates this point by the given vector.
  169. *
  170. * @param {number} dx X-coordinate of the translation.
  171. * @param {number} dy Y-coordinate of the translation.
  172. */
  173. DropboxFile.prototype.rename = function(title, success, error)
  174. {
  175. this.ui.dropbox.renameFile(this, title, mxUtils.bind(this, function(stat)
  176. {
  177. if (!this.hasSameExtension(title, this.getTitle()))
  178. {
  179. this.stat = stat;
  180. // Required in this case to update hash tag in page
  181. // before saving so that the edit link is correct
  182. this.descriptorChanged();
  183. this.save(true, success, error);
  184. }
  185. else
  186. {
  187. this.stat = stat;
  188. this.descriptorChanged();
  189. if (success != null)
  190. {
  191. success();
  192. }
  193. }
  194. }), error);
  195. };