]> git.proxmox.com Git - extjs.git/blame - extjs/classic/classic/src/util/Memento.js
add extjs 6.0.1 sources
[extjs.git] / extjs / classic / classic / src / util / Memento.js
CommitLineData
6527f429
DM
1/**\r
2 * @class Ext.util.Memento\r
3 * This class manages a set of captured properties from an object. These captured properties\r
4 * can later be restored to an object.\r
5 */\r
6Ext.define('Ext.util.Memento', (function () {\r
7\r
8 function captureOne (src, target, prop, prefix) {\r
9 src[prefix ? prefix + prop : prop] = target[prop];\r
10 }\r
11\r
12 function removeOne (src, target, prop) {\r
13 delete src[prop];\r
14 }\r
15\r
16 function restoreOne (src, target, prop, prefix) {\r
17 var name = prefix ? prefix + prop : prop,\r
18 value = src[name];\r
19\r
20 if (value || src.hasOwnProperty(name)) {\r
21 restoreValue(target, prop, value);\r
22 }\r
23 }\r
24\r
25 function restoreValue (target, prop, value) {\r
26 if (Ext.isDefined(value)) {\r
27 target[prop] = value;\r
28 } else {\r
29 delete target[prop];\r
30 }\r
31 }\r
32\r
33 function doMany (doOne, src, target, props, prefix) {\r
34 if (src) {\r
35 if (Ext.isArray(props)) {\r
36 var p, pLen = props.length;\r
37 for (p = 0; p < pLen; p++) {\r
38 doOne(src, target, props[p], prefix);\r
39 }\r
40 } else {\r
41 doOne(src, target, props, prefix);\r
42 }\r
43 }\r
44 }\r
45\r
46 return {\r
47 /**\r
48 * @property data\r
49 * The collection of captured properties.\r
50 * @private\r
51 */\r
52 data: null,\r
53\r
54 /**\r
55 * @property target\r
56 * The default target object for capture/restore (passed to the constructor).\r
57 */\r
58 target: null,\r
59\r
60 /**\r
61 * Creates a new memento and optionally captures properties from the target object.\r
62 * @param {Object} target The target from which to capture properties. If specified in the\r
63 * constructor, this target becomes the default target for all other operations.\r
64 * @param {String/String[]} props The property or array of properties to capture.\r
65 */\r
66 constructor: function (target, props) {\r
67 this.data = {};\r
68\r
69 if (target) {\r
70 this.target = target;\r
71 if (props) {\r
72 this.capture(props);\r
73 }\r
74 }\r
75 },\r
76\r
77 /**\r
78 * Captures the specified properties from the target object in this memento.\r
79 * @param {String/String[]} props The property or array of properties to capture.\r
80 * @param {Object} target The object from which to capture properties.\r
81 */\r
82 capture: function (props, target, prefix) {\r
83 var me = this;\r
84 doMany(captureOne, me.data || (me.data = {}), target || me.target, props, prefix);\r
85 },\r
86\r
87 /**\r
88 * Removes the specified properties from this memento. These properties will not be\r
89 * restored later without re-capturing their values.\r
90 * @param {String/String[]} props The property or array of properties to remove.\r
91 */\r
92 remove: function (props) {\r
93 doMany(removeOne, this.data, null, props);\r
94 },\r
95\r
96 /**\r
97 * Restores the specified properties from this memento to the target object.\r
98 * @param {String/String[]} props The property or array of properties to restore.\r
99 * @param {Boolean} clear True to remove the restored properties from this memento or\r
100 * false to keep them (default is true).\r
101 * @param {Object} target The object to which to restore properties.\r
102 */\r
103 restore: function (props, clear, target, prefix) {\r
104 doMany(restoreOne, this.data, target || this.target, props, prefix);\r
105 if (clear !== false) {\r
106 this.remove(props);\r
107 }\r
108 },\r
109\r
110 /**\r
111 * Restores all captured properties in this memento to the target object.\r
112 * @param {Boolean} clear True to remove the restored properties from this memento or\r
113 * false to keep them (default is true).\r
114 * @param {Object} target The object to which to restore properties.\r
115 */\r
116 restoreAll: function (clear, target) {\r
117 var me = this,\r
118 t = target || this.target,\r
119 data = me.data,\r
120 prop;\r
121\r
122 clear = clear !== false;\r
123\r
124 for (prop in data) {\r
125 if (data.hasOwnProperty(prop)) {\r
126 restoreValue(t, prop, data[prop]);\r
127 if (clear) {\r
128 delete data[prop];\r
129 }\r
130 }\r
131 }\r
132\r
133 }\r
134 };\r
135}()));\r