]>
git.wh0rd.org - tt-rss.git/blob - lib/dojo/hash.js.uncompressed.js
1 define ( "dojo/hash" , [ "./_base/kernel" , "require" , "./_base/config" , "./_base/connect" , "./_base/lang" , "./ready" , "./sniff" ],
2 function ( dojo
, require
, config
, connect
, lang
, ready
, has
){
7 dojo
. hash = function ( /* String? */ hash
, /* Boolean? */ replace
){
9 // Gets or sets the hash string in the browser URL.
11 // Handles getting and setting of location.hash.
13 // - If no arguments are passed, acts as a getter.
14 // - If a string is passed, acts as a setter.
16 // the hash is set - #string.
18 // If true, updates the hash value in the current history
19 // state instead of creating a new history state.
21 // when used as a getter, returns the current hash string.
22 // when used as a setter, returns the new hash string.
24 // | topic.subscribe("/dojo/hashchange", context, callback);
26 // | function callback (hashValue){
27 // | // do something based on the hash value.
31 if (! arguments
. length
){
35 if ( hash
. charAt ( 0 ) == "#" ){
36 hash
= hash
. substring ( 1 );
41 location
. href
= "#" + hash
;
43 return hash
; // String
47 var _recentHash
, _ieUriMonitor
, _connect
,
48 _pollFrequency
= config
. hashPollFrequency
|| 100 ;
51 function _getSegment ( str
, delimiter
){
52 var i
= str
. indexOf ( delimiter
);
53 return ( i
>= 0 ) ? str
. substring ( i
+ 1 ) : "" ;
57 return _getSegment ( location
. href
, "#" );
60 function _dispatchEvent (){
61 connect
. publish ( "/dojo/hashchange" , [ _getHash ()]);
64 function _pollLocation (){
65 if ( _getHash () === _recentHash
){
68 _recentHash
= _getHash ();
72 function _replace ( hash
){
74 if ( _ieUriMonitor
. isTransitioning ()){
75 setTimeout ( lang
. hitch ( null , _replace
, hash
), _pollFrequency
);
78 var href
= _ieUriMonitor
. iframe
. location
. href
;
79 var index
= href
. indexOf ( '?' );
80 // main frame will detect and update itself
81 _ieUriMonitor
. iframe
. location
. replace ( href
. substring ( 0 , index
) + "?" + hash
);
84 location
. replace ( "#" + hash
);
85 ! _connect
&& _pollLocation ();
88 function IEUriMonitor (){
90 // Determine if the browser's URI has changed or if the user has pressed the
91 // back or forward button. If so, call _dispatchEvent.
94 // IE doesn't add changes to the URI's hash into the history unless the hash
95 // value corresponds to an actual named anchor in the document. To get around
96 // this IE difference, we use a background IFrame to maintain a back-forward
97 // history, by updating the IFrame's query string to correspond to the
98 // value of the main browser location's hash value.
100 // E.g. if the value of the browser window's location changes to
102 // #action=someAction
104 // ... then we'd update the IFrame's source to:
106 // ?action=someAction
108 // This design leads to a somewhat complex state machine, which is
113 // Stable state - neither the window's location has changed nor
114 // has the IFrame's location. Note that this is the 99.9% case, so
115 // we optimize for it.
117 // Transitions: s1, s2, s3
121 // Window's location changed - when a user clicks a hyperlink or
122 // code programmatically changes the window's URI.
128 // Iframe's location changed as a result of user pressing back or
129 // forward - when the user presses back or forward, the location of
130 // the background's iframe changes to the previous or next value in
137 // IEUriMonitor has programmatically changed the location of the
138 // background iframe, but it's location hasn't yet changed. In this
139 // case we do nothing because we need to wait for the iframe's
140 // location to reflect its actual state.
142 // Transitions: s4, s5
146 // IEUriMonitor has programmatically changed the location of the
147 // background iframe, and the iframe's location has caught up with
148 // reality. In this case we need to transition to s1.
152 // The hashchange event is always dispatched on the transition back to s1.
155 // create and append iframe
156 var ifr
= document
. createElement ( "iframe" ),
157 IFRAME_ID
= "dojo-hash-iframe" ,
158 ifrSrc
= config
. dojoBlankHtmlUrl
|| require
. toUrl ( "./resources/blank.html" );
160 if ( config
. useXDomain
&& ! config
. dojoBlankHtmlUrl
){
161 console
. warn ( "dojo.hash: When using cross-domain Dojo builds,"
162 + " please save dojo/resources/blank.html to your domain and set djConfig.dojoBlankHtmlUrl"
163 + " to the path on your domain to blank.html" );
167 ifr
. src
= ifrSrc
+ "?" + _getHash ();
168 ifr
. style
. display
= "none" ;
169 document
. body
. appendChild ( ifr
);
171 this . iframe
= dojo
. global
[ IFRAME_ID
];
172 var recentIframeQuery
, transitioning
, expectedIFrameQuery
, docTitle
, ifrOffline
,
173 iframeLoc
= this . iframe
. location
;
175 function resetState (){
176 _recentHash
= _getHash ();
177 recentIframeQuery
= ifrOffline
? _recentHash
: _getSegment ( iframeLoc
. href
, "?" );
178 transitioning
= false ;
179 expectedIFrameQuery
= null ;
182 this . isTransitioning = function (){
183 return transitioning
;
186 this . pollLocation = function (){
189 //see if we can access the iframe's location without a permission denied error
190 var iframeSearch
= _getSegment ( iframeLoc
. href
, "?" );
191 //good, the iframe is same origin (no thrown exception)
192 if ( document
. title
!= docTitle
){ //sync title of main window with title of iframe.
193 docTitle
= this . iframe
. document
. title
= document
. title
;
196 //permission denied - server cannot be reached.
198 console
. error ( "dojo.hash: Error adding history entry. Server unreachable." );
201 var hash
= _getHash ();
202 if ( transitioning
&& _recentHash
=== hash
){
203 // we're in an iframe transition (s4 or s5)
204 if ( ifrOffline
|| iframeSearch
=== expectedIFrameQuery
){
205 // s5 (iframe caught up to main window or iframe offline), transition back to s1
209 // s4 (waiting for iframe to catch up to main window)
210 setTimeout ( lang
. hitch ( this , this . pollLocation
), 0 );
213 } else if ( _recentHash
=== hash
&& ( ifrOffline
|| recentIframeQuery
=== iframeSearch
)){
214 // we're in stable state (s1, iframe query == main window hash), do nothing
216 // the user has initiated a URL change somehow.
217 // sync iframe query <-> main window hash
218 if ( _recentHash
!== hash
){
219 // s2 (main window location changed), set iframe url and transition to s4
221 transitioning
= true ;
222 expectedIFrameQuery
= hash
;
223 ifr
. src
= ifrSrc
+ "?" + expectedIFrameQuery
;
224 ifrOffline
= false ; //we're updating the iframe src - set offline to false so we can check again on next poll.
225 setTimeout ( lang
. hitch ( this , this . pollLocation
), 0 ); //yielded transition to s4 while iframe reloads.
227 } else if (! ifrOffline
){
228 // s3 (iframe location changed via back/forward button), set main window url and transition to s1.
229 location
. href
= "#" + iframeLoc
. search
. substring ( 1 );
234 setTimeout ( lang
. hitch ( this , this . pollLocation
), _pollFrequency
);
236 resetState (); // initialize state (transition to s1)
237 setTimeout ( lang
. hitch ( this , this . pollLocation
), _pollFrequency
);
240 if ( "onhashchange" in dojo
. global
&& (! has ( "ie" ) || ( has ( "ie" ) >= 8 && document
. compatMode
!= "BackCompat" ))){ //need this IE browser test because "onhashchange" exists in IE8 in IE7 mode
241 _connect
= connect
. connect ( dojo
. global
, "onhashchange" , _dispatchEvent
);
243 if ( document
. addEventListener
){ // Non-IE
244 _recentHash
= _getHash ();
245 setInterval ( _pollLocation
, _pollFrequency
); //Poll the window location for changes
246 } else if ( document
. attachEvent
){ // IE7-
247 //Use hidden iframe in versions of IE that don't have onhashchange event
248 _ieUriMonitor
= new IEUriMonitor ();
250 // else non-supported browser, do nothing.