Memento.html 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4. <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
  5. <title>The source code</title>
  6. <link href="../resources/prettify/prettify.css" type="text/css" rel="stylesheet" />
  7. <script type="text/javascript" src="../resources/prettify/prettify.js"></script>
  8. <style type="text/css">
  9. .highlight { display: block; background-color: #ddd; }
  10. </style>
  11. <script type="text/javascript">
  12. function highlight() {
  13. document.getElementById(location.hash.replace(/#/, "")).className = "highlight";
  14. }
  15. </script>
  16. </head>
  17. <body onload="prettyPrint(); highlight();">
  18. <pre class="prettyprint lang-js"><span id='Ext-util-Memento'>/**
  19. </span> * @class Ext.util.Memento
  20. * This class manages a set of captured properties from an object. These captured properties
  21. * can later be restored to an object.
  22. */
  23. Ext.define('Ext.util.Memento', (function () {
  24. function captureOne (src, target, prop, prefix) {
  25. src[prefix ? prefix + prop : prop] = target[prop];
  26. }
  27. function removeOne (src, target, prop) {
  28. delete src[prop];
  29. }
  30. function restoreOne (src, target, prop, prefix) {
  31. var name = prefix ? prefix + prop : prop,
  32. value = src[name];
  33. if (value || src.hasOwnProperty(name)) {
  34. restoreValue(target, prop, value);
  35. }
  36. }
  37. function restoreValue (target, prop, value) {
  38. if (Ext.isDefined(value)) {
  39. target[prop] = value;
  40. } else {
  41. delete target[prop];
  42. }
  43. }
  44. function doMany (doOne, src, target, props, prefix) {
  45. if (src) {
  46. if (Ext.isArray(props)) {
  47. var p, pLen = props.length;
  48. for (p = 0; p &lt; pLen; p++) {
  49. doOne(src, target, props[p], prefix);
  50. }
  51. } else {
  52. doOne(src, target, props, prefix);
  53. }
  54. }
  55. }
  56. return {
  57. <span id='Ext-util-Memento-property-data'> /**
  58. </span> * @property data
  59. * The collection of captured properties.
  60. * @private
  61. */
  62. data: null,
  63. <span id='Ext-util-Memento-property-target'> /**
  64. </span> * @property target
  65. * The default target object for capture/restore (passed to the constructor).
  66. */
  67. target: null,
  68. <span id='Ext-util-Memento-method-constructor'> /**
  69. </span> * Creates a new memento and optionally captures properties from the target object.
  70. * @param {Object} target The target from which to capture properties. If specified in the
  71. * constructor, this target becomes the default target for all other operations.
  72. * @param {String/String[]} props The property or array of properties to capture.
  73. */
  74. constructor: function (target, props) {
  75. if (target) {
  76. this.target = target;
  77. if (props) {
  78. this.capture(props);
  79. }
  80. }
  81. },
  82. <span id='Ext-util-Memento-method-capture'> /**
  83. </span> * Captures the specified properties from the target object in this memento.
  84. * @param {String/String[]} props The property or array of properties to capture.
  85. * @param {Object} target The object from which to capture properties.
  86. */
  87. capture: function (props, target, prefix) {
  88. var me = this;
  89. doMany(captureOne, me.data || (me.data = {}), target || me.target, props, prefix);
  90. },
  91. <span id='Ext-util-Memento-method-remove'> /**
  92. </span> * Removes the specified properties from this memento. These properties will not be
  93. * restored later without re-capturing their values.
  94. * @param {String/String[]} props The property or array of properties to remove.
  95. */
  96. remove: function (props) {
  97. doMany(removeOne, this.data, null, props);
  98. },
  99. <span id='Ext-util-Memento-method-restore'> /**
  100. </span> * Restores the specified properties from this memento to the target object.
  101. * @param {String/String[]} props The property or array of properties to restore.
  102. * @param {Boolean} clear True to remove the restored properties from this memento or
  103. * false to keep them (default is true).
  104. * @param {Object} target The object to which to restore properties.
  105. */
  106. restore: function (props, clear, target, prefix) {
  107. doMany(restoreOne, this.data, target || this.target, props, prefix);
  108. if (clear !== false) {
  109. this.remove(props);
  110. }
  111. },
  112. <span id='Ext-util-Memento-method-restoreAll'> /**
  113. </span> * Restores all captured properties in this memento to the target object.
  114. * @param {Boolean} clear True to remove the restored properties from this memento or
  115. * false to keep them (default is true).
  116. * @param {Object} target The object to which to restore properties.
  117. */
  118. restoreAll: function (clear, target) {
  119. var me = this,
  120. t = target || this.target,
  121. data = me.data,
  122. prop;
  123. for (prop in data) {
  124. if (data.hasOwnProperty(prop)) {
  125. restoreValue(t, prop, data[prop]);
  126. }
  127. }
  128. if (clear !== false) {
  129. delete me.data;
  130. }
  131. }
  132. };
  133. }()));
  134. </pre>
  135. </body>
  136. </html>