@@ -15,6 +15,11 @@ From 2010 till ∞
15
15
typecode-js v 0.1
16
16
*/
17
17
18
+ // This module uses jQuery Hashchange (http://benalman.com/projects/jquery-hashchange-plugin/)
19
+ // as a fallback to emulate pushstate behavior on browsers that don't support pushstate.
20
+ // The hashchange plugin subsequently requires jQuery Migrate (https://github.com/jquery/jquery-migrate)
21
+ // if being used with jQuery version 1.9 +
22
+
18
23
define ( [ 'jquery' ] , function ( $ ) {
19
24
20
25
var window = this ,
@@ -31,23 +36,34 @@ define(['jquery'], function($) {
31
36
32
37
internal = {
33
38
name : 'mod.PushstateHelper' ,
34
- previous_path : window . location . pathname
39
+ previous_path : o . use_hash ?
40
+ window . location . hash . replace ( '#' , '' ) : window . location . pathname
35
41
} ;
36
42
37
43
fn = {
38
44
init : function ( ) {
39
45
$ ( document ) . on ( 'click' , '.js-use-pushstate, .js-simulate-pushstate, .js-do-popstate' , handlers . doc_click ) ;
40
- $ ( window ) . on ( 'popstate' , handlers . popstate ) . on ( 'pushstate' , handlers . pushstate ) . on ( 'simulate-pushstate' , handlers . simulate_pushstate ) ;
41
- o . app . events . on ( 'app.featuresInitialized' , handlers . features_initialized ) ;
42
- if ( o . use_hash ) {
43
- fn . statechange ( window . location . hash ) ;
46
+ $ ( window )
47
+ . on ( 'popstate' , handlers . popstate )
48
+ . on ( 'pushstate' , handlers . pushstate )
49
+ . on ( 'simulate-pushstate' , handlers . simulate_pushstate ) ;
50
+
51
+ if ( o . use_hash ) {
52
+ require ( [ 'jquery_migrate' , 'hashchange' ] , function ( ) {
53
+ $ ( window ) . on ( 'hashchange' , handlers . hashchange ) ;
54
+ fn . statechange ( window . location . hash ) ;
55
+ } ) ;
44
56
} else {
45
57
fn . statechange ( window . location . pathname ) ;
46
58
}
47
59
} ,
48
60
statechange : function ( pathname , data ) {
49
61
var i , path_components , position , _event_data ;
50
62
63
+ if ( o . use_hash ) {
64
+ pathname = pathname . replace ( '#' , '' ) ;
65
+ }
66
+
51
67
path_components = PushstateHelper . get_path_components ( pathname ) ;
52
68
53
69
_event_data = {
@@ -69,25 +85,23 @@ define(['jquery'], function($) {
69
85
} ;
70
86
71
87
handlers = {
72
- features_initialized : function ( e , d ) {
73
- //fn.statechange(window.location);
74
- } ,
75
88
doc_click : function ( e , d ) {
76
- var _href , _data ;
89
+ var $t , _href , _data ;
77
90
e . preventDefault ( ) ;
78
- _href = $ ( this ) . attr ( 'HREF' ) ;
79
- _data = $ ( this ) . data ( ) ;
80
- if ( $ ( this ) . hasClass ( 'js-use-pushstate' ) ) {
91
+ $t = $ ( this ) ;
92
+ _href = $t . attr ( 'HREF' ) ;
93
+ _data = $t . data ( ) ;
94
+ if ( $t . hasClass ( 'js-use-pushstate' ) ) {
81
95
if ( o . use_hash ) {
82
96
window . location . hash = _href ;
83
97
fn . statechange ( window . location . hash , _data ) ;
84
98
} else {
85
99
history . pushState ( null , null , _href ) ;
86
100
fn . statechange ( window . location . pathname , _data ) ;
87
101
}
88
- } else if ( $ ( this ) . hasClass ( 'js-simulate-pushstate' ) ) {
102
+ } else if ( $t . hasClass ( 'js-simulate-pushstate' ) ) {
89
103
fn . statechange ( _href , _data ) ;
90
- } else if ( $ ( this ) . hasClass ( 'js-do-popstate' ) ) {
104
+ } else if ( $t . hasClass ( 'js-do-popstate' ) ) {
91
105
history . back ( ) ;
92
106
}
93
107
} ,
@@ -108,14 +122,19 @@ define(['jquery'], function($) {
108
122
history . pushState ( null , null , d . pathname ) ;
109
123
}
110
124
}
111
- if ( ! d . prevent_propagation ) {
125
+ if ( ! d || ! d . prevent_propagation ) {
112
126
fn . statechange ( d . pathname , d ) ;
113
127
}
114
128
} ,
115
129
simulate_pushstate : function ( e , d ) {
116
- if ( ! d . prevent_propagation ) {
130
+ if ( ! d || ! d . prevent_propagation ) {
117
131
fn . statechange ( d . pathname , d ) ;
118
132
}
133
+ } ,
134
+ hashchange : function ( e , d ) {
135
+ if ( ! d || ! d . prevent_propagation ) {
136
+ fn . statechange ( window . location . hash ) ;
137
+ }
119
138
}
120
139
} ;
121
140
@@ -127,7 +146,6 @@ define(['jquery'], function($) {
127
146
128
147
PushstateHelper . get_path_components = function ( path ) {
129
148
var components , position ;
130
- components = [ ] ;
131
149
components = path . split ( '/' ) ;
132
150
position = components . length ;
133
151
while ( position -- ) {
0 commit comments