-
Notifications
You must be signed in to change notification settings - Fork 2
/
State.js
163 lines (129 loc) · 3.85 KB
/
State.js
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
152
153
154
155
156
157
158
159
160
161
162
163
/**
* Yam state handler
*
* Simple static state handler.
*
* @example
*
* @
* @since 0.3.2
*
* @author Andrew Tereshko <[email protected]>
*/
define( function() {
"use strict";
/**
* @class yam.State
*/
$.Class.extend('yam.State',
/* @static */
{
/**
* Stack of states
*
* @private
* @property {Array}
*/
_stack: [],
/**
* Position of current state in stack;
*
* @private
* @property {Numeric}
*/
_currentKey: 0,
/**
* Returns current state
*
* @return {yam.State}
*/
current: function(){
var state;
if( undefined !== this._stack[ this._currentKey ] ){
state = this._stack[ this._currentKey ];
} else {
state = new this();
}
return state;
},
/**
* Create and push new state to stack ( and make it current ).
*
* @param location
* @param data
* @param title
*/
push: function( location, data, title ){
var state = ( location instanceof this ) ? location : new this( location, data, title );
if( this._stack.length ){ this._currentKey++; }
this._stack.splice( this._currentKey );
this._stack.push( state );
$(this)
.trigger( 'locationChange', state )
.trigger( 'push', state );
},
/**
* Replaces current state
*
* @param location
* @param data
* @param title
*/
replace: function( location, data, title ){
var state = ( location instanceof this ) ? location : new this( location, data, title );
this._stack[ this._currentKey ] = state;
$(this)
.trigger( 'locationChange', state )
.trigger( 'replace', state );
},
/**
* Rewinds state to N steps backward
*
* @param steps
*/
back: function( steps ){
steps = new Number( steps );
if( isNaN( steps ) || steps < 1 ) steps = 1;
steps = Math.min( steps, this._currentKey );
if( this._currentKey ){
this._currentKey = this._currentKey - steps;
$(this)
.trigger( 'locationChange', this.current() )
.trigger( 'back', this.current() );
}
},
/**
* Rewinds state to N steps forward
*
* @param steps
*/
forward: function( steps ){
if( steps === undefined ) steps = 1;
$(this).trigger( 'locationChange', state );
$(this).trigger( 'forward', state );
}
},
/* @prototype */
{
location : '',
title : '',
data : undefined,
/**
* @constructor
*
* @param location
* @param data
* @param title
*/
init: function( location, data, title ){
this.location = location;
this.data = data || {};
this.title = title || '';
}
}
);
// backward compatibility
if( app === undefined ) var app = {};
app.State = yam.State;
return yam.State;
});