2 * @class Ext.state.Stateful
3 * A mixin for being able to save the state of an object to an underlying
4 * {@link Ext.state.Provider}.
6 Ext.define('Ext.state.Stateful', {
8 /* Begin Definitions */
11 observable: 'Ext.util.Observable'
14 requires: ['Ext.state.Manager'],
19 * @cfg {Boolean} stateful
20 * <p>A flag which causes the object to attempt to restore the state of
21 * internal properties from a saved state on startup. The object must have
22 * a <code>{@link #stateId}</code> for state to be managed.
23 * Auto-generated ids are not guaranteed to be stable across page loads and
24 * cannot be relied upon to save and restore the same state for a object.<p>
25 * <p>For state saving to work, the state manager's provider must have been
26 * set to an implementation of {@link Ext.state.Provider} which overrides the
27 * {@link Ext.state.Provider#set set} and {@link Ext.state.Provider#get get}
28 * methods to save and recall name/value pairs. A built-in implementation,
29 * {@link Ext.state.CookieProvider} is available.</p>
30 * <p>To set the state provider for the current page:</p>
32 Ext.state.Manager.setProvider(new Ext.state.CookieProvider({
33 expires: new Date(new Date().getTime()+(1000*60*60*24*7)), //7 days from now
36 * <p>A stateful object attempts to save state when one of the events
37 * listed in the <code>{@link #stateEvents}</code> configuration fires.</p>
38 * <p>To save state, a stateful object first serializes its state by
39 * calling <b><code>{@link #getState}</code></b>. By default, this function does
40 * nothing. The developer must provide an implementation which returns an
41 * object hash which represents the restorable state of the object.</p>
42 * <p>The value yielded by getState is passed to {@link Ext.state.Manager#set}
43 * which uses the configured {@link Ext.state.Provider} to save the object
44 * keyed by the <code>{@link stateId}</code></p>.
45 * <p>During construction, a stateful object attempts to <i>restore</i>
46 * its state by calling {@link Ext.state.Manager#get} passing the
47 * <code>{@link #stateId}</code></p>
48 * <p>The resulting object is passed to <b><code>{@link #applyState}</code></b>.
49 * The default implementation of <code>{@link #applyState}</code> simply copies
50 * properties into the object, but a developer may override this to support
52 * <p>You can perform extra processing on state save and restore by attaching
53 * handlers to the {@link #beforestaterestore}, {@link #staterestore},
54 * {@link #beforestatesave} and {@link #statesave} events.</p>
59 * @cfg {String} stateId
60 * The unique id for this object to use for state management purposes.
61 * <p>See {@link #stateful} for an explanation of saving and restoring state.</p>
65 * @cfg {Array} stateEvents
66 * <p>An array of events that, when fired, should trigger this object to
67 * save its state (defaults to none). <code>stateEvents</code> may be any type
68 * of event supported by this object, including browser or custom events
69 * (e.g., <tt>['click', 'customerchange']</tt>).</p>
70 * <p>See <code>{@link #stateful}</code> for an explanation of saving and
71 * restoring object state.</p>
75 * @cfg {Number} saveBuffer A buffer to be applied if many state events are fired within
76 * a short period. Defaults to 100.
80 autoGenIdRe: /^((\w+-)|(ext-comp-))\d{4,}$/i,
82 constructor: function(config) {
85 config = config || {};
86 if (Ext.isDefined(config.stateful)) {
87 me.stateful = config.stateful;
89 if (Ext.isDefined(config.saveDelay)) {
90 me.saveDelay = config.saveDelay;
92 me.stateId = config.stateId;
94 if (!me.stateEvents) {
97 if (config.stateEvents) {
98 me.stateEvents.concat(config.stateEvents);
102 * @event beforestaterestore
103 * Fires before the state of the object is restored. Return false from an event handler to stop the restore.
104 * @param {Ext.state.Stateful} this
105 * @param {Object} state The hash of state values returned from the StateProvider. If this
106 * event is not vetoed, then the state object is passed to <b><tt>applyState</tt></b>. By default,
107 * that simply copies property values into this object. The method maybe overriden to
108 * provide custom state restoration.
110 'beforestaterestore',
113 * @event staterestore
114 * Fires after the state of the object is restored.
115 * @param {Ext.state.Stateful} this
116 * @param {Object} state The hash of state values returned from the StateProvider. This is passed
117 * to <b><tt>applyState</tt></b>. By default, that simply copies property values into this
118 * object. The method maybe overriden to provide custom state restoration.
123 * @event beforestatesave
124 * Fires before the state of the object is saved to the configured state provider. Return false to stop the save.
125 * @param {Ext.state.Stateful} this
126 * @param {Object} state The hash of state values. This is determined by calling
127 * <b><tt>getState()</tt></b> on the object. This method must be provided by the
128 * developer to return whetever representation of state is required, by default, Ext.state.Stateful
129 * has a null implementation.
135 * Fires after the state of the object is saved to the configured state provider.
136 * @param {Ext.state.Stateful} this
137 * @param {Object} state The hash of state values. This is determined by calling
138 * <b><tt>getState()</tt></b> on the object. This method must be provided by the
139 * developer to return whetever representation of state is required, by default, Ext.state.Stateful
140 * has a null implementation.
144 me.mixins.observable.constructor.call(me);
145 if (me.stateful !== false) {
146 me.initStateEvents();
152 * Initializes any state events for this object.
155 initStateEvents: function() {
156 this.addStateEvents(this.stateEvents);
160 * Add events that will trigger the state to be saved.
161 * @param {String/Array} events The event name or an array of event names.
163 addStateEvents: function(events){
164 if (!Ext.isArray(events)) {
172 for (; i < len; ++i) {
173 me.on(events[i], me.onStateChange, me);
178 * This method is called when any of the {@link #stateEvents} are fired.
181 onStateChange: function(){
183 delay = me.saveDelay;
187 me.stateTask = Ext.create('Ext.util.DelayedTask', me.saveState, me);
189 me.stateTask.delay(me.saveDelay);
196 * Saves the state of the object to the persistence store.
199 saveState: function() {
204 if (me.stateful !== false) {
205 id = me.getStateId();
207 state = me.getState();
208 if (me.fireEvent('beforestatesave', me, state) !== false) {
209 Ext.state.Manager.set(id, state);
210 me.fireEvent('statesave', me, state);
217 * Gets the current state of the object. By default this function returns null,
218 * it should be overridden in subclasses to implement methods for getting the state.
219 * @return {Object} The current state
221 getState: function(){
226 * Applies the state to the object. This should be overridden in subclasses to do
227 * more complex state operations. By default it applies the state properties onto
228 * the current object.
229 * @param {Object} state The state
231 applyState: function(state) {
233 Ext.apply(this, state);
238 * Gets the state id for this object.
239 * @return {String} The state id, null if not found.
241 getStateId: function() {
246 id = me.autoGenIdRe.test(String(me.id)) ? null : me.id;
252 * Initializes the state of the object upon construction.
255 initState: function(){
257 id = me.getStateId(),
260 if (me.stateful !== false) {
262 state = Ext.state.Manager.get(id);
264 state = Ext.apply({}, state);
265 if (me.fireEvent('beforestaterestore', me, state) !== false) {
266 me.applyState(state);
267 me.fireEvent('staterestore', me, state);
275 * Destroys this stateful object.
278 var task = this.stateTask;
282 this.clearListeners();