/
jquery.barca.js
227 lines (183 loc) · 6.36 KB
/
jquery.barca.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
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
/*
* jQuery Barca
*
* Copyright 2011, aligo Kang
* http://aligo.me/
* Dual licensed under the MIT or GPL Version 2 licenses.
*/
(function ( $, undefined ) {
var barca = {
// Here is barca default settings
settings : {
// This text will be displayed in title when loading
// Set false if you wan't
loading : 'loading...',
// This function will called before first ajax request
// It should be to store original page
// Returns a function-object that will be called when the history state being activated
original : function () {
var origin = $('body').html()
return {
callback : function () {
$('body').html(origin)
}
}
},
// Fake hashchange event, for handling with the hash in request url
// Called after ajax request completed
// You may want that smooth scrolling to an anchor, like this:
// function( hash ) {
// $('html, body').stop(true).animate( { scrollTop : $( '#' + hash ).offset().top }, 1000 )
// }
hashchange : function ( hash ) { },
// Whether to use Hashbang mode, set true or keep the default
useHash : function () {
if ( !window.history || !window.history.pushState ) return true
else return false
},
// Useful in Hashbang mode, the prefix of url in your site
baseurl : function () {
return window.location.protocol + '//' + window.location.host + '/'
}
},
// Stores the history state objects
stack : [],
// Current history state id
state : 1,
inited : false,
// Stores the hashes
hashStack : [],
// Called when the history state being activated
popStack : function ( state_id ) {
barca.state = state_id
var state = barca.stack[state_id]
if ( state && state.callback ) {
state.callback()
return true
}
return false
}
}
// Utilities
var u = {
getValueOrCall : function ( target ) {
return ( $.isFunction(target) ) ? target() : target
},
inArray : function ( elem, array, start ) {
var start = start || 0
if ( array.indexOf ) {
return array.indexOf( elem, start )
}
for ( var i = start, length = array.length; i < length; i++ ) {
if ( array[ i ] === elem ) return i
}
return -1
},
allInArray : function ( elem, array ) {
var indices = []
var idx = u.inArray( elem, array )
while ( idx !== -1 ) {
indices.push( idx )
idx = u.inArray( elem, array, idx+1 )
}
return indices
}
}
$.extend({
barcaSetup : function ( target, settings ) {
if ( !settings ) {
settings = target
target = $.extend( true, barca.settings, settings )
} else {
$.extend( true, target, barca.settings, settings )
}
for( var field in { context: 1, url: 1 } ) {
if ( field in settings ) {
target[ field ] = settings[ field ]
} else if( field in barca.settings ) {
target[ field ] = barca.settings[ field ]
}
}
target.href = u.getValueOrCall( target.url )
var href = target.href.split('#')
target.url = href[0]
target.hash = href[1] || undefined
target.useHash = u.getValueOrCall( target.useHash )
target.baseurl = u.getValueOrCall( target.baseurl )
target.loading = target.loading || window.document.title
return target
},
barca : function ( url, options ) {
if ( typeof url === "object" ) {
options = url
url = undefined
}else{
options.url = url
}
options = options || {}
var s = jQuery.barcaSetup( {}, options )
var success = s.success
s.success = function ( data, status, xhr ){
var args = arguments
var state_id = barca.state
barca.stack[state_id] = {
callback : function () {
success.apply( this, args )
s.hashchange( s.hash )
}
}
barca.stack[state_id].callback()
if ( !s.useHash ) window.history.replaceState( state_id, window.document.title, window.location.href )
if ( window._gaq ) _gaq.push( ['_trackPageview', s.href] )
}
if ( !barca.inited ) {
barca.inited = true
barca.stack[1] = s.original()
if ( !s.useHash ) {
window.history.replaceState( 1, window.document.title, window.location.href )
} else {
barca.hashStack[barca.state] = window.location.hash
}
}
barca.state ++
if ( !s.useHash ) {
window.history.pushState( barca.state, s.loading, s.href )
} else {
if ( 0 === u.inArray( s.baseurl, s.url ) ) {
var hashstate = s.url.substring( s.baseurl.length )
if ( '/' !== hashstate.substring( 0, 1 ) ) hashstate = '/' + hashstate
hashstate = '#!' + hashstate
if ( undefined !== s.hash ) hashstate = hashstate + '#' + s.hash
barca.hashStack[barca.state] = hashstate
barca.hashStack.length = barca.state + 1
window.location.hash = hashstate
}
}
window.document.title = s.loading
var xhr = barca.xhr
if ( xhr && xhr.readyState < 4) {
xhr.onreadystatechange = $.noop
xhr.abort()
}
barca.xhr = $.ajax(s)
}
})
if ( -1 === u.inArray( 'state', $.event.props ) ) $.event.props.push('state')
$(window).bind('popstate', function ( event ) {
var state_id = event.state
if ( state_id ) {
barca.popStack( state_id )
}
})
$(window).bind('hashchange', function () {
if ( barca.hashStack[barca.state] !== window.location.hash ) {
var indices = u.allInArray( window.location.hash, barca.hashStack )
if ( 0 !== indices.length ) {
indices.sort(function ( a, b ) {
return Math.abs( a - barca.state ) - Math.abs( b - barca.state )
})
barca.popStack( indices[0] )
}
}
})
})(jQuery);