mxAutoSaveManager.js 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215
  1. /**
  2. * Copyright (c) 2006-2015, JGraph Ltd
  3. * Copyright (c) 2006-2015, Gaudenz Alder
  4. */
  5. /**
  6. * Class: mxAutoSaveManager
  7. *
  8. * Manager for automatically saving diagrams. The <save> hook must be
  9. * implemented.
  10. *
  11. * Example:
  12. *
  13. * (code)
  14. * var mgr = new mxAutoSaveManager(editor.graph);
  15. * mgr.save = function()
  16. * {
  17. * mxLog.show();
  18. * mxLog.debug('save');
  19. * };
  20. * (end)
  21. *
  22. * Constructor: mxAutoSaveManager
  23. *
  24. * Constructs a new automatic layout for the given graph.
  25. *
  26. * Arguments:
  27. *
  28. * graph - Reference to the enclosing graph.
  29. */
  30. function mxAutoSaveManager(graph)
  31. {
  32. // Notifies the manager of a change
  33. this.changeHandler = mxUtils.bind(this, function(sender, evt)
  34. {
  35. if (this.isEnabled())
  36. {
  37. this.graphModelChanged(evt.getProperty('edit').changes);
  38. }
  39. });
  40. this.setGraph(graph);
  41. };
  42. /**
  43. * Extends mxEventSource.
  44. */
  45. mxAutoSaveManager.prototype = new mxEventSource();
  46. mxAutoSaveManager.prototype.constructor = mxAutoSaveManager;
  47. /**
  48. * Variable: graph
  49. *
  50. * Reference to the enclosing <mxGraph>.
  51. */
  52. mxAutoSaveManager.prototype.graph = null;
  53. /**
  54. * Variable: autoSaveDelay
  55. *
  56. * Minimum amount of seconds between two consecutive autosaves. Eg. a
  57. * value of 1 (s) means the graph is not stored more than once per second.
  58. * Default is 10.
  59. */
  60. mxAutoSaveManager.prototype.autoSaveDelay = 10;
  61. /**
  62. * Variable: autoSaveThrottle
  63. *
  64. * Minimum amount of seconds between two consecutive autosaves triggered by
  65. * more than <autoSaveThreshhold> changes within a timespan of less than
  66. * <autoSaveDelay> seconds. Eg. a value of 1 (s) means the graph is not
  67. * stored more than once per second even if there are more than
  68. * <autoSaveThreshold> changes within that timespan. Default is 2.
  69. */
  70. mxAutoSaveManager.prototype.autoSaveThrottle = 2;
  71. /**
  72. * Variable: autoSaveThreshold
  73. *
  74. * Minimum amount of ignored changes before an autosave. Eg. a value of 2
  75. * means after 2 change of the graph model the autosave will trigger if the
  76. * condition below is true. Default is 5.
  77. */
  78. mxAutoSaveManager.prototype.autoSaveThreshold = 5;
  79. /**
  80. * Variable: ignoredChanges
  81. *
  82. * Counter for ignored changes in autosave.
  83. */
  84. mxAutoSaveManager.prototype.ignoredChanges = 0;
  85. /**
  86. * Variable: lastSnapshot
  87. *
  88. * Used for autosaving. See <autosave>.
  89. */
  90. mxAutoSaveManager.prototype.lastSnapshot = 0;
  91. /**
  92. * Variable: enabled
  93. *
  94. * Specifies if event handling is enabled. Default is true.
  95. */
  96. mxAutoSaveManager.prototype.enabled = true;
  97. /**
  98. * Variable: changeHandler
  99. *
  100. * Holds the function that handles graph model changes.
  101. */
  102. mxAutoSaveManager.prototype.changeHandler = null;
  103. /**
  104. * Function: isEnabled
  105. *
  106. * Returns true if events are handled. This implementation
  107. * returns <enabled>.
  108. */
  109. mxAutoSaveManager.prototype.isEnabled = function()
  110. {
  111. return this.enabled;
  112. };
  113. /**
  114. * Function: setEnabled
  115. *
  116. * Enables or disables event handling. This implementation
  117. * updates <enabled>.
  118. *
  119. * Parameters:
  120. *
  121. * enabled - Boolean that specifies the new enabled state.
  122. */
  123. mxAutoSaveManager.prototype.setEnabled = function(value)
  124. {
  125. this.enabled = value;
  126. };
  127. /**
  128. * Function: setGraph
  129. *
  130. * Sets the graph that the layouts operate on.
  131. */
  132. mxAutoSaveManager.prototype.setGraph = function(graph)
  133. {
  134. if (this.graph != null)
  135. {
  136. this.graph.getModel().removeListener(this.changeHandler);
  137. }
  138. this.graph = graph;
  139. if (this.graph != null)
  140. {
  141. this.graph.getModel().addListener(mxEvent.CHANGE, this.changeHandler);
  142. }
  143. };
  144. /**
  145. * Function: save
  146. *
  147. * Empty hook that is called if the graph should be saved.
  148. */
  149. mxAutoSaveManager.prototype.save = function()
  150. {
  151. // empty
  152. };
  153. /**
  154. * Function: graphModelChanged
  155. *
  156. * Invoked when the graph model has changed.
  157. */
  158. mxAutoSaveManager.prototype.graphModelChanged = function(changes)
  159. {
  160. var now = new Date().getTime();
  161. var dt = (now - this.lastSnapshot) / 1000;
  162. if (dt > this.autoSaveDelay ||
  163. (this.ignoredChanges >= this.autoSaveThreshold &&
  164. dt > this.autoSaveThrottle))
  165. {
  166. this.save();
  167. this.reset();
  168. }
  169. else
  170. {
  171. // Increments the number of ignored changes
  172. this.ignoredChanges++;
  173. }
  174. };
  175. /**
  176. * Function: reset
  177. *
  178. * Resets all counters.
  179. */
  180. mxAutoSaveManager.prototype.reset = function()
  181. {
  182. this.lastSnapshot = new Date().getTime();
  183. this.ignoredChanges = 0;
  184. };
  185. /**
  186. * Function: destroy
  187. *
  188. * Removes all handlers from the <graph> and deletes the reference to it.
  189. */
  190. mxAutoSaveManager.prototype.destroy = function()
  191. {
  192. this.setGraph(null);
  193. };
  194. __mxOutput.mxAutoSaveManager = typeof mxAutoSaveManager !== 'undefined' ? mxAutoSaveManager : undefined;