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