StorageFile.js 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225
  1. // $Id = StorageFile.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. StorageFile = function(ui, data, title)
  12. {
  13. DrawioFile.call(this, ui, data);
  14. this.title = title;
  15. };
  16. //Extends mxEventSource
  17. mxUtils.extend(StorageFile, DrawioFile);
  18. /**
  19. * Sets the delay for autosave in milliseconds. Default is 2000.
  20. */
  21. StorageFile.prototype.autosaveDelay = 2000;
  22. /**
  23. * Sets the delay for autosave in milliseconds. Default is 20000.
  24. */
  25. StorageFile.prototype.maxAutosaveDelay = 20000;
  26. /**
  27. * Translates this point by the given vector.
  28. *
  29. * @param {number} dx X-coordinate of the translation.
  30. * @param {number} dy Y-coordinate of the translation.
  31. */
  32. StorageFile.prototype.getMode = function()
  33. {
  34. return App.MODE_BROWSER;
  35. };
  36. /**
  37. * Overridden to enable the autosave option in the document properties dialog.
  38. */
  39. StorageFile.prototype.isAutosaveOptional = function()
  40. {
  41. return true;
  42. };
  43. /**
  44. * Translates this point by the given vector.
  45. *
  46. * @param {number} dx X-coordinate of the translation.
  47. * @param {number} dy Y-coordinate of the translation.
  48. */
  49. StorageFile.prototype.getHash = function()
  50. {
  51. return 'L' + encodeURIComponent(this.getTitle());
  52. };
  53. /**
  54. * Translates this point by the given vector.
  55. *
  56. * @param {number} dx X-coordinate of the translation.
  57. * @param {number} dy Y-coordinate of the translation.
  58. */
  59. StorageFile.prototype.getTitle = function()
  60. {
  61. return this.title;
  62. };
  63. /**
  64. * Translates this point by the given vector.
  65. *
  66. * @param {number} dx X-coordinate of the translation.
  67. * @param {number} dy Y-coordinate of the translation.
  68. */
  69. StorageFile.prototype.isRenamable = function()
  70. {
  71. return true;
  72. };
  73. /**
  74. * Translates this point by the given vector.
  75. *
  76. * @param {number} dx X-coordinate of the translation.
  77. * @param {number} dy Y-coordinate of the translation.
  78. */
  79. StorageFile.prototype.save = function(revision, success, error)
  80. {
  81. this.saveAs(this.getTitle(), success, error);
  82. };
  83. /**
  84. * Translates this point by the given vector.
  85. *
  86. * @param {number} dx X-coordinate of the translation.
  87. * @param {number} dy Y-coordinate of the translation.
  88. */
  89. StorageFile.prototype.saveAs = function(title, success, error)
  90. {
  91. DrawioFile.prototype.save.apply(this, arguments);
  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. StorageFile.prototype.saveFile = function(title, revision, success, error)
  101. {
  102. if (!this.isEditable())
  103. {
  104. if (success != null)
  105. {
  106. success();
  107. }
  108. }
  109. else
  110. {
  111. var fn = mxUtils.bind(this, function()
  112. {
  113. if (this.isRenamable())
  114. {
  115. this.title = title;
  116. }
  117. try
  118. {
  119. this.ui.setLocalData(this.title, this.getData(), mxUtils.bind(this, function()
  120. {
  121. this.setModified(false);
  122. this.contentChanged();
  123. if (success != null)
  124. {
  125. success();
  126. }
  127. }));
  128. }
  129. catch (e)
  130. {
  131. if (error != null)
  132. {
  133. error(e);
  134. }
  135. }
  136. });
  137. // Checks for trailing dots
  138. if (this.isRenamable() && title.charAt(0) == '.' && error != null)
  139. {
  140. error({message: mxResources.get('invalidName')});
  141. }
  142. else
  143. {
  144. this.ui.getLocalData(title, mxUtils.bind(this, function(data)
  145. {
  146. if (!this.isRenamable() || this.getTitle() == title || data == null)
  147. {
  148. fn();
  149. }
  150. else
  151. {
  152. this.ui.confirm(mxResources.get('replaceIt', [title]), fn, error);
  153. }
  154. }));
  155. }
  156. }
  157. };
  158. /**
  159. * Translates this point by the given vector.
  160. *
  161. * @param {number} dx X-coordinate of the translation.
  162. * @param {number} dy Y-coordinate of the translation.
  163. */
  164. StorageFile.prototype.rename = function(title, success, error)
  165. {
  166. var oldTitle = this.getTitle();
  167. this.title = title;
  168. // Updates the data if the extension has changed
  169. if (!this.hasSameExtension(oldTitle, title))
  170. {
  171. this.setData(this.ui.getFileData());
  172. }
  173. this.saveFile(title, false, mxUtils.bind(this, function()
  174. {
  175. if (oldTitle != title)
  176. {
  177. this.ui.removeLocalData(oldTitle, success);
  178. }
  179. }), error);
  180. };
  181. /**
  182. * Returns the location as a new object.
  183. * @type mx.Point
  184. */
  185. StorageFile.prototype.open = function()
  186. {
  187. DrawioFile.prototype.open.apply(this, arguments);
  188. // Immediately creates the storage entry
  189. this.saveFile(this.getTitle());
  190. };
  191. /**
  192. * Stops any pending autosaves and removes all listeners.
  193. */
  194. StorageFile.prototype.destroy = function()
  195. {
  196. DrawioFile.prototype.destroy.apply(this, arguments);
  197. if (this.storageListener != null)
  198. {
  199. mxEvent.removeListener(window, 'storage', this.storageListener);
  200. this.storageListener = null;
  201. }
  202. };