StorageFile.js 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227
  1. /**
  2. * Copyright (c) 2006-2017, JGraph Ltd
  3. * Copyright (c) 2006-2017, Gaudenz Alder
  4. */
  5. /**
  6. * Constructs a new point for the optional x and y coordinates. If no
  7. * coordinates are given, then the default values for <x> and <y> are used.
  8. * @constructor
  9. * @class Implements a basic 2D point. Known subclassers = {@link mxRectangle}.
  10. * @param {number} x X-coordinate of the point.
  11. * @param {number} y Y-coordinate of the point.
  12. */
  13. StorageFile = function(ui, data, title)
  14. {
  15. DrawioFile.call(this, ui, data);
  16. this.title = title;
  17. };
  18. //Extends mxEventSource
  19. mxUtils.extend(StorageFile, DrawioFile);
  20. /**
  21. * Sets the delay for autosave in milliseconds. Default is 2000.
  22. */
  23. StorageFile.prototype.autosaveDelay = 2000;
  24. /**
  25. * Sets the delay for autosave in milliseconds. Default is 20000.
  26. */
  27. StorageFile.prototype.maxAutosaveDelay = 20000;
  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. StorageFile.prototype.getMode = function()
  35. {
  36. return App.MODE_BROWSER;
  37. };
  38. /**
  39. * Overridden to enable the autosave option in the document properties dialog.
  40. */
  41. StorageFile.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. StorageFile.prototype.getHash = function()
  52. {
  53. return 'L' + encodeURIComponent(this.getTitle());
  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. StorageFile.prototype.getTitle = function()
  62. {
  63. return this.title;
  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. StorageFile.prototype.isRenamable = function()
  72. {
  73. return true;
  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. StorageFile.prototype.save = function(revision, success, error)
  82. {
  83. this.saveAs(this.getTitle(), 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. StorageFile.prototype.saveAs = 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. StorageFile.prototype.saveFile = function(title, revision, success, error)
  103. {
  104. if (!this.isEditable())
  105. {
  106. if (success != null)
  107. {
  108. success();
  109. }
  110. }
  111. else
  112. {
  113. var fn = mxUtils.bind(this, function()
  114. {
  115. if (this.isRenamable())
  116. {
  117. this.title = title;
  118. }
  119. try
  120. {
  121. this.ui.setLocalData(this.title, this.getData(), mxUtils.bind(this, function()
  122. {
  123. this.setModified(false);
  124. this.contentChanged();
  125. if (success != null)
  126. {
  127. success();
  128. }
  129. }));
  130. }
  131. catch (e)
  132. {
  133. if (error != null)
  134. {
  135. error(e);
  136. }
  137. }
  138. });
  139. // Checks for trailing dots
  140. if (this.isRenamable() && title.charAt(0) == '.' && error != null)
  141. {
  142. error({message: mxResources.get('invalidName')});
  143. }
  144. else
  145. {
  146. this.ui.getLocalData(title, mxUtils.bind(this, function(data)
  147. {
  148. if (!this.isRenamable() || this.getTitle() == title || data == null)
  149. {
  150. fn();
  151. }
  152. else
  153. {
  154. this.ui.confirm(mxResources.get('replaceIt', [title]), fn, error);
  155. }
  156. }));
  157. }
  158. }
  159. };
  160. /**
  161. * Translates this point by the given vector.
  162. *
  163. * @param {number} dx X-coordinate of the translation.
  164. * @param {number} dy Y-coordinate of the translation.
  165. */
  166. StorageFile.prototype.rename = function(title, success, error)
  167. {
  168. var oldTitle = this.getTitle();
  169. this.title = title;
  170. // Updates the data if the extension has changed
  171. if (!this.hasSameExtension(oldTitle, title))
  172. {
  173. this.setData(this.ui.getFileData());
  174. }
  175. this.saveFile(title, false, mxUtils.bind(this, function()
  176. {
  177. if (oldTitle != title)
  178. {
  179. this.ui.removeLocalData(oldTitle, success);
  180. }
  181. }), error);
  182. };
  183. /**
  184. * Returns the location as a new object.
  185. * @type mx.Point
  186. */
  187. StorageFile.prototype.open = function()
  188. {
  189. DrawioFile.prototype.open.apply(this, arguments);
  190. // Immediately creates the storage entry
  191. this.saveFile(this.getTitle());
  192. };
  193. /**
  194. * Stops any pending autosaves and removes all listeners.
  195. */
  196. StorageFile.prototype.destroy = function()
  197. {
  198. DrawioFile.prototype.destroy.apply(this, arguments);
  199. if (this.storageListener != null)
  200. {
  201. mxEvent.removeListener(window, 'storage', this.storageListener);
  202. this.storageListener = null;
  203. }
  204. };