3 This file is part of Ext JS 4
5 Copyright (c) 2011 Sencha Inc
7 Contact: http://www.sencha.com/contact
9 GNU General Public License Usage
10 This file may be used under the terms of the GNU General Public License version 3.0 as published by the Free Software Foundation and appearing in the file LICENSE included in the packaging of this file. Please review the following information to ensure the GNU General Public License version 3.0 requirements will be met: http://www.gnu.org/copyleft/gpl.html.
12 If you are unsure which license is appropriate for your use, please contact the sales department at http://www.sencha.com/contact.
16 * @class Ext.util.Memento
17 * This class manages a set of captured properties from an object. These captured properties
18 * can later be restored to an object.
20 Ext.define('Ext.util.Memento', function () {
22 function captureOne (src, target, prop) {
23 src[prop] = target[prop];
26 function removeOne (src, target, prop) {
30 function restoreOne (src, target, prop) {
31 var value = src[prop];
32 if (value || src.hasOwnProperty(prop)) {
33 restoreValue(target, prop, value);
37 function restoreValue (target, prop, value) {
38 if (Ext.isDefined(value)) {
45 function doMany (doOne, src, target, props) {
47 if (Ext.isArray(props)) {
48 Ext.each(props, function (prop) {
49 doOne(src, target, prop);
52 doOne(src, target, props);
60 * The collection of captured properties.
67 * The default target object for capture/restore (passed to the constructor).
72 * Creates a new memento and optionally captures properties from the target object.
73 * @param {Object} target The target from which to capture properties. If specified in the
74 * constructor, this target becomes the default target for all other operations.
75 * @param {String/String[]} props The property or array of properties to capture.
77 constructor: function (target, props) {
87 * Captures the specified properties from the target object in this memento.
88 * @param {String/String[]} props The property or array of properties to capture.
89 * @param {Object} target The object from which to capture properties.
91 capture: function (props, target) {
92 doMany(captureOne, this.data || (this.data = {}), target || this.target, props);
96 * Removes the specified properties from this memento. These properties will not be
97 * restored later without re-capturing their values.
98 * @param {String/String[]} props The property or array of properties to remove.
100 remove: function (props) {
101 doMany(removeOne, this.data, null, props);
105 * Restores the specified properties from this memento to the target object.
106 * @param {String/String[]} props The property or array of properties to restore.
107 * @param {Boolean} clear True to remove the restored properties from this memento or
108 * false to keep them (default is true).
109 * @param {Object} target The object to which to restore properties.
111 restore: function (props, clear, target) {
112 doMany(restoreOne, this.data, target || this.target, props);
113 if (clear !== false) {
119 * Restores all captured properties in this memento to the target object.
120 * @param {Boolean} clear True to remove the restored properties from this memento or
121 * false to keep them (default is true).
122 * @param {Object} target The object to which to restore properties.
124 restoreAll: function (clear, target) {
126 t = target || this.target;
128 Ext.Object.each(me.data, function (prop, value) {
129 restoreValue(t, prop, value);
132 if (clear !== false) {