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; }
11 <script type="text/javascript">
12 function highlight() {
13 document.getElementById(location.hash.replace(/#/, "")).className = "highlight";
17 <body onload="prettyPrint(); highlight();">
18 <pre class="prettyprint lang-js"><span id='Ext-state-Stateful'>/**
19 </span> * @class Ext.state.Stateful
20 * A mixin for being able to save the state of an object to an underlying
21 * {@link Ext.state.Provider}.
23 Ext.define('Ext.state.Stateful', {
25 /* Begin Definitions */
28 observable: 'Ext.util.Observable'
31 requires: ['Ext.state.Manager'],
35 <span id='Ext-state-Stateful-cfg-stateful'> /**
36 </span> * @cfg {Boolean} stateful
37 * <p>A flag which causes the object to attempt to restore the state of
38 * internal properties from a saved state on startup. The object must have
39 * a <code>{@link #stateId}</code> for state to be managed.
40 * Auto-generated ids are not guaranteed to be stable across page loads and
41 * cannot be relied upon to save and restore the same state for a object.<p>
42 * <p>For state saving to work, the state manager's provider must have been
43 * set to an implementation of {@link Ext.state.Provider} which overrides the
44 * {@link Ext.state.Provider#set set} and {@link Ext.state.Provider#get get}
45 * methods to save and recall name/value pairs. A built-in implementation,
46 * {@link Ext.state.CookieProvider} is available.</p>
47 * <p>To set the state provider for the current page:</p>
48 * <pre><code>
49 Ext.state.Manager.setProvider(new Ext.state.CookieProvider({
50 expires: new Date(new Date().getTime()+(1000*60*60*24*7)), //7 days from now
52 * </code></pre>
53 * <p>A stateful object attempts to save state when one of the events
54 * listed in the <code>{@link #stateEvents}</code> configuration fires.</p>
55 * <p>To save state, a stateful object first serializes its state by
56 * calling <b><code>{@link #getState}</code></b>. By default, this function does
57 * nothing. The developer must provide an implementation which returns an
58 * object hash which represents the restorable state of the object.</p>
59 * <p>The value yielded by getState is passed to {@link Ext.state.Manager#set}
60 * which uses the configured {@link Ext.state.Provider} to save the object
61 * keyed by the <code>{@link #stateId}</code>.</p>
62 * <p>During construction, a stateful object attempts to <i>restore</i>
63 * its state by calling {@link Ext.state.Manager#get} passing the
64 * <code>{@link #stateId}</code></p>
65 * <p>The resulting object is passed to <b><code>{@link #applyState}</code></b>.
66 * The default implementation of <code>{@link #applyState}</code> simply copies
67 * properties into the object, but a developer may override this to support
68 * more behaviour.</p>
69 * <p>You can perform extra processing on state save and restore by attaching
70 * handlers to the {@link #beforestaterestore}, {@link #staterestore},
71 * {@link #beforestatesave} and {@link #statesave} events.</p>
75 <span id='Ext-state-Stateful-cfg-stateId'> /**
76 </span> * @cfg {String} stateId
77 * The unique id for this object to use for state management purposes.
78 * <p>See {@link #stateful} for an explanation of saving and restoring state.</p>
81 <span id='Ext-state-Stateful-cfg-stateEvents'> /**
82 </span> * @cfg {String[]} stateEvents
83 * <p>An array of events that, when fired, should trigger this object to
84 * save its state. Defaults to none. <code>stateEvents</code> may be any type
85 * of event supported by this object, including browser or custom events
86 * (e.g., <tt>['click', 'customerchange']</tt>).</p>
87 * <p>See <code>{@link #stateful}</code> for an explanation of saving and
88 * restoring object state.</p>
91 <span id='Ext-state-Stateful-cfg-saveDelay'> /**
92 </span> * @cfg {Number} saveDelay
93 * A buffer to be applied if many state events are fired within a short period.
97 autoGenIdRe: /^((\w+-)|(ext-comp-))\d{4,}$/i,
99 constructor: function(config) {
102 config = config || {};
103 if (Ext.isDefined(config.stateful)) {
104 me.stateful = config.stateful;
106 if (Ext.isDefined(config.saveDelay)) {
107 me.saveDelay = config.saveDelay;
109 me.stateId = me.stateId || config.stateId;
111 if (!me.stateEvents) {
114 if (config.stateEvents) {
115 me.stateEvents.concat(config.stateEvents);
118 <span id='Ext-state-Stateful-event-beforestaterestore'> /**
119 </span> * @event beforestaterestore
120 * Fires before the state of the object is restored. Return false from an event handler to stop the restore.
121 * @param {Ext.state.Stateful} this
122 * @param {Object} state The hash of state values returned from the StateProvider. If this
123 * event is not vetoed, then the state object is passed to <b><tt>applyState</tt></b>. By default,
124 * that simply copies property values into this object. The method maybe overriden to
125 * provide custom state restoration.
127 'beforestaterestore',
129 <span id='Ext-state-Stateful-event-staterestore'> /**
130 </span> * @event staterestore
131 * Fires after the state of the object is restored.
132 * @param {Ext.state.Stateful} this
133 * @param {Object} state The hash of state values returned from the StateProvider. This is passed
134 * to <b><tt>applyState</tt></b>. By default, that simply copies property values into this
135 * object. The method maybe overriden to provide custom state restoration.
139 <span id='Ext-state-Stateful-event-beforestatesave'> /**
140 </span> * @event beforestatesave
141 * Fires before the state of the object is saved to the configured state provider. Return false to stop the save.
142 * @param {Ext.state.Stateful} this
143 * @param {Object} state The hash of state values. This is determined by calling
144 * <b><tt>getState()</tt></b> on the object. This method must be provided by the
145 * developer to return whetever representation of state is required, by default, Ext.state.Stateful
146 * has a null implementation.
150 <span id='Ext-state-Stateful-event-statesave'> /**
151 </span> * @event statesave
152 * Fires after the state of the object is saved to the configured state provider.
153 * @param {Ext.state.Stateful} this
154 * @param {Object} state The hash of state values. This is determined by calling
155 * <b><tt>getState()</tt></b> on the object. This method must be provided by the
156 * developer to return whetever representation of state is required, by default, Ext.state.Stateful
157 * has a null implementation.
161 me.mixins.observable.constructor.call(me);
162 if (me.stateful !== false) {
163 me.initStateEvents();
168 <span id='Ext-state-Stateful-method-initStateEvents'> /**
169 </span> * Initializes any state events for this object.
172 initStateEvents: function() {
173 this.addStateEvents(this.stateEvents);
176 <span id='Ext-state-Stateful-method-addStateEvents'> /**
177 </span> * Add events that will trigger the state to be saved.
178 * @param {String/String[]} events The event name or an array of event names.
180 addStateEvents: function(events){
181 if (!Ext.isArray(events)) {
189 for (; i < len; ++i) {
190 me.on(events[i], me.onStateChange, me);
194 <span id='Ext-state-Stateful-method-onStateChange'> /**
195 </span> * This method is called when any of the {@link #stateEvents} are fired.
198 onStateChange: function(){
200 delay = me.saveDelay;
204 me.stateTask = Ext.create('Ext.util.DelayedTask', me.saveState, me);
206 me.stateTask.delay(me.saveDelay);
212 <span id='Ext-state-Stateful-method-saveState'> /**
213 </span> * Saves the state of the object to the persistence store.
216 saveState: function() {
221 if (me.stateful !== false) {
222 id = me.getStateId();
224 state = me.getState();
225 if (me.fireEvent('beforestatesave', me, state) !== false) {
226 Ext.state.Manager.set(id, state);
227 me.fireEvent('statesave', me, state);
233 <span id='Ext-state-Stateful-method-getState'> /**
234 </span> * Gets the current state of the object. By default this function returns null,
235 * it should be overridden in subclasses to implement methods for getting the state.
236 * @return {Object} The current state
238 getState: function(){
242 <span id='Ext-state-Stateful-method-applyState'> /**
243 </span> * Applies the state to the object. This should be overridden in subclasses to do
244 * more complex state operations. By default it applies the state properties onto
245 * the current object.
246 * @param {Object} state The state
248 applyState: function(state) {
250 Ext.apply(this, state);
254 <span id='Ext-state-Stateful-method-getStateId'> /**
255 </span> * Gets the state id for this object.
256 * @return {String} The state id, null if not found.
258 getStateId: function() {
263 id = me.autoGenIdRe.test(String(me.id)) ? null : me.id;
268 <span id='Ext-state-Stateful-method-initState'> /**
269 </span> * Initializes the state of the object upon construction.
272 initState: function(){
274 id = me.getStateId(),
277 if (me.stateful !== false) {
279 state = Ext.state.Manager.get(id);
281 state = Ext.apply({}, state);
282 if (me.fireEvent('beforestaterestore', me, state) !== false) {
283 me.applyState(state);
284 me.fireEvent('staterestore', me, state);
291 <span id='Ext-state-Stateful-method-savePropToState'> /**
292 </span> * Conditionally saves a single property from this object to the given state object.
293 * The idea is to only save state which has changed from the initial state so that
294 * current software settings do not override future software settings. Only those
295 * values that are user-changed state should be saved.
297 * @param {String} propName The name of the property to save.
298 * @param {Object} state The state object in to which to save the property.
299 * @param {String} stateName (optional) The name to use for the property in state.
300 * @return {Boolean} True if the property was saved, false if not.
302 savePropToState: function (propName, state, stateName) {
304 value = me[propName],
305 config = me.initialConfig;
307 if (me.hasOwnProperty(propName)) {
308 if (!config || config[propName] !== value) {
310 state[stateName || propName] = value;
318 savePropsToState: function (propNames, state) {
320 Ext.each(propNames, function (propName) {
321 me.savePropToState(propName, state);
326 <span id='Ext-state-Stateful-method-destroy'> /**
327 </span> * Destroys this stateful object.
330 var task = this.stateTask;
334 this.clearListeners();