rice-framework/krad-web/src/main/webapp/krad/scripts/krad.url.js
/**
 * Invoked on document load to check URL indicators for refreshing and also sets up URL change handlers
 *
 * <p>
 * First a check is done to see if the URL hash contains the cache key. If it does, this indicates we need
 * to make a call to the server to retrieve the latest state for the view. This is the hack for browsers that
 * don't support the history API to prevent caching.
 *
 * If the cache key is not present, we then go forward and register handlers for the window popstate and
 * hashchange so we can update the page contents when the back button is used.
 *
 * Finally, we do a check to see if the URL hash contains the page id parm and it is different from the
 * page that is actually loaded in the DOM (which could happen for a bookmarked URL). If so an Ajax call is
 * made to retrieve the requested page
 * </p>
 *
 * @return boolean indicating whether a refresh call was made
 */
Invoked on document load to check URL indicators for refreshing and also sets up URL change handlers


First a check is done to see if the URL hash contains the cache key. If it does, this indicates we need
to make a call to the server to retrieve the latest state for the view. This is the hack for browsers that
don't support the history API to prevent caching.

If the cache key is not present, we then go forward and register handlers for the window popstate and
hashchange so we can update the page contents when the back button is used.

Finally, we do a check to see if the URL hash contains the page id parm and it is different from the
page that is actually loaded in the DOM (which could happen for a bookmarked URL). If so an Ajax call is
made to retrieve the requested page


@return boolean indicating whether a refresh call was made
Invoked on document load to check URL indicators for refreshing and also sets up URL change handlers First a check is done to see if the URL hash contains the cache key . If it does this indicates we need to make a call to the server to retrieve the latest state for the view . This is the hack for browsers that don ' t support the history API to prevent caching . If the cache key is not present we then go forward and register handlers for the window popstate and hashchange so we can update the page contents when the back button is used . Finally we do a check to see if the URL hash contains the page id parm and it is different from the page that is actually loaded in the DOM ( which could happen for a bookmarked URL ) . If so an Ajax call is made to retrieve the requested page @return boolean indicating whether a refresh call was made
function handlePageAndCacheRefreshing() {
    var cacheKeyHashVal = getUrlParameter(kradVariables.CACHE_KEY, window.location.hash);
    if (cacheKeyHashVal) {
        if(history.replaceState){
            removeCacheKeyHTML5();
        } else {
            removeCacheKeyNonHTML5();
        }
        return true;
    }
    var lightboxBackClose = false;
    jQuery(document).bind("afterLoad", function(e){
        if(history.replaceState){
            history.pushState({lightbox: true}, null, window.location + "&" + kradVariables.LIGHTBOX_PARAM + "=true");
        } else {
            var search = window.location.search.substring(1);
            var hash = getUrlQueryString( kradVariables.LIGHTBOX_PARAM, "true", window.location.hash);
            window.location.replace("?" + search + hash);
        }
    });
    jQuery(document).bind("afterClose", function(e){
        if(!lightboxBackClose && history.replaceState &&  window.location &&
            (window.location).toString().indexOf(kradVariables.LIGHTBOX_PARAM + "=true")){
            history.back();
        }
        else if(!lightboxBackClose && window.location.hash &&
            (window.location.hash).toString().indexOf(kradVariables.LIGHTBOX_PARAM + "=true")){
            var search = window.location.search.substring(1);
            var hash = window.location.hash.toString();
            hash = hash.replace("&" + kradVariables.LIGHTBOX_PARAM + "=true", "");
            hash = hash.replace(kradVariables.LIGHTBOX_PARAM + "=true&", "");
            window.location.replace("?" + search + hash);
        }
        lightboxBackClose = false;
    });
    jQuery(window).bind("popstate", function (e) {
        var state = e.state;
        if(!state){
            state = e.originalEvent.state;
        }
        if (state && state.pageId && state.pageId != getCurrentPageId()) {
            navigateToPage(state.pageId);
        }
        if ((!state || (state && state.pageId == getCurrentPageId())) && jQuery.fancybox.isOpen()){
            lightboxBackClose = true;
            jQuery.fancybox.close();
        }
    });
    jQuery(window).bind("hashchange", function (e) {
        var pageId = null;
        if (window.location.hash) {
            pageId = getUrlParameter(kradVariables.PAGE_ID, window.location.hash);
        }
        var inLightbox = getUrlParameter(kradVariables.LIGHTBOX_PARAM, window.location.hash);
        if (pageId && (pageId != getCurrentPageId())) {
            navigateToPage(pageId);
        }
        if (!inLightbox && jQuery.fancybox.isOpen()){
            lightboxBackClose = true;
            jQuery.fancybox.close();
        }
    });
    var singlePageView = (jQuery("input[name='" + kradVariables.SINGLE_PAGE_VIEW + "']").val() == "true");
    if (singlePageView) {
        return false;
    }
    var hashPageId = getUrlParameter(kradVariables.PAGE_ID, window.location.hash);
    var pageRedirect = hashPageId && (getCurrentPageId() != hashPageId);
    if (pageRedirect) {
        navigateToPage(hashPageId);
    }
    return pageRedirect;
}
function handlePageAndCacheRefreshing ( ) { var cacheKeyHashVal = getUrlParameter ( kradVariables . CACHE_KEY , window . location . hash ) ; if ( cacheKeyHashVal ) { if ( history . replaceState ) { removeCacheKeyHTML5 ( ) ; } else { removeCacheKeyNonHTML5 ( ) ; } return true ; } var lightboxBackClose = false ; jQuery ( document ) . bind ( "afterLoad" , function ( e ) { if ( history . replaceState ) { history . pushState ( { lightbox : true } , null , window . location + "&" + kradVariables . LIGHTBOX_PARAM + "=true" ) ; } else { var search = window . location . search . substring ( 1 ) ; var hash = getUrlQueryString ( kradVariables . LIGHTBOX_PARAM , "true" , window . location . hash ) ; window . location . replace ( "?" + search + hash ) ; } } ) ; jQuery ( document ) . bind ( "afterClose" , function ( e ) { if ( ! lightboxBackClose && history . replaceState && window . location && ( window . location ) . toString ( ) . indexOf ( kradVariables . LIGHTBOX_PARAM + "=true" ) ) { history . back ( ) ; } else if ( ! lightboxBackClose && window . location . hash && ( window . location . hash ) . toString ( ) . indexOf ( kradVariables . LIGHTBOX_PARAM + "=true" ) ) { var search = window . location . search . substring ( 1 ) ; var hash = window . location . hash . toString ( ) ; hash = hash . replace ( "&" + kradVariables . LIGHTBOX_PARAM + "=true" , "" ) ; hash = hash . replace ( kradVariables . LIGHTBOX_PARAM + "=true&" , "" ) ; window . location . replace ( "?" + search + hash ) ; } lightboxBackClose = false ; } ) ; jQuery ( window ) . bind ( "popstate" , function ( e ) { var state = e . state ; if ( ! state ) { state = e . originalEvent . state ; } if ( state && state . pageId && state . pageId != getCurrentPageId ( ) ) { navigateToPage ( state . pageId ) ; } if ( ( ! state || ( state && state . pageId == getCurrentPageId ( ) ) ) && jQuery . fancybox . isOpen ( ) ) { lightboxBackClose = true ; jQuery . fancybox . close ( ) ; } } ) ; jQuery ( window ) . bind ( "hashchange" , function ( e ) { var pageId = null ; if ( window . location . hash ) { pageId = getUrlParameter ( kradVariables . PAGE_ID , window . location . hash ) ; } var inLightbox = getUrlParameter ( kradVariables . LIGHTBOX_PARAM , window . location . hash ) ; if ( pageId && ( pageId != getCurrentPageId ( ) ) ) { navigateToPage ( pageId ) ; } if ( ! inLightbox && jQuery . fancybox . isOpen ( ) ) { lightboxBackClose = true ; jQuery . fancybox . close ( ) ; } } ) ; var singlePageView = ( jQuery ( "input[name='" + kradVariables . SINGLE_PAGE_VIEW + "']" ) . val ( ) == "true" ) ; if ( singlePageView ) { return false ; } var hashPageId = getUrlParameter ( kradVariables . PAGE_ID , window . location . hash ) ; var pageRedirect = hashPageId && ( getCurrentPageId ( ) != hashPageId ) ; if ( pageRedirect ) { navigateToPage ( hashPageId ) ; } return pageRedirect ; }
Invoked on document load to check URL indicators for refreshing and also sets up URL change handlers
Invoked on document load to check URL indicators for refreshing and also sets up URL change handlers
// check if we have a cache key on the hash which indicates we need to reload the entire view from the
 // the entire view from the server
 // is a lightbox being closed by the back button
 // handlers for lightbox events to close lightbox on back by adding a history entry on load and removing it on close
 // TODO the back button only works correctly after a lightbox close on firefox because of other browser problems
 // force a history back on close (if back button wasn't clicked to close)
 // TODO just removing attribute because IE has a bug with erasing the hash history so back cant be
 // used as we would like it to
 // setup handlers for page id changes
 // add listener for popstate to change pages
 // add listener on hashchange to change pages when pageId hash changes
 // check whether the view is multi-page, if not we don't need to setup page URL handling
 // check if page id on hash is different from the page we actually have, if so
 // retrieve the correct page
