Upgrade to ExtJS 4.0.7 - Released 10/19/2011
[extjs.git] / src / util / Memento.js
1 /*
2
3 This file is part of Ext JS 4
4
5 Copyright (c) 2011 Sencha Inc
6
7 Contact:  http://www.sencha.com/contact
8
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.
11
12 If you are unsure which license is appropriate for your use, please contact the sales department at http://www.sencha.com/contact.
13
14 */
15 /**
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.
19  */
20 Ext.define('Ext.util.Memento', function () {
21
22     function captureOne (src, target, prop) {
23         src[prop] = target[prop];
24     }
25
26     function removeOne (src, target, prop) {
27         delete src[prop];
28     }
29
30     function restoreOne (src, target, prop) {
31         var value = src[prop];
32         if (value || src.hasOwnProperty(prop)) {
33             restoreValue(target, prop, value);
34         }
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
45     function doMany (doOne, src, target, props) {
46         if (src) {
47             if (Ext.isArray(props)) {
48                 Ext.each(props, function (prop) {
49                     doOne(src, target, prop);
50                 });
51             } else {
52                 doOne(src, target, props);
53             }
54         }
55     }
56
57     return {
58         /**
59          * @property data
60          * The collection of captured properties.
61          * @private
62          */
63         data: null,
64
65         /**
66          * @property target
67          * The default target object for capture/restore (passed to the constructor).
68          */
69         target: null,
70
71         /**
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.
76          */
77         constructor: function (target, props) {
78             if (target) {
79                 this.target = target;
80                 if (props) {
81                     this.capture(props);
82                 }
83             }
84         },
85
86         /**
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.
90          */
91         capture: function (props, target) {
92             doMany(captureOne, this.data || (this.data = {}), target || this.target, props);
93         },
94
95         /**
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.
99          */
100         remove: function (props) {
101             doMany(removeOne, this.data, null, props);
102         },
103
104         /**
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.
110          */
111         restore: function (props, clear, target) {
112             doMany(restoreOne, this.data, target || this.target, props);
113             if (clear !== false) {
114                 this.remove(props);
115             }
116         },
117
118         /**
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.
123          */
124         restoreAll: function (clear, target) {
125             var me = this,
126                 t = target || this.target;
127
128             Ext.Object.each(me.data, function (prop, value) {
129                 restoreValue(t, prop, value);
130             });
131
132             if (clear !== false) {
133                 delete me.data;
134             }
135         }
136     };
137 }());
138