/examples/CustomMapMoon/bin-release/srcview/source/html-template/history/history.js.txt
Plain Text | 645 lines | 565 code | 80 blank | 0 comment | 0 complexity | 832a9adcaf1c5f2e9558e2771bbb1971 MD5 | raw file
1BrowserHistoryUtils = { 2 addEvent: function(elm, evType, fn, useCapture) { 3 useCapture = useCapture || false; 4 if (elm.addEventListener) { 5 elm.addEventListener(evType, fn, useCapture); 6 return true; 7 } 8 else if (elm.attachEvent) { 9 var r = elm.attachEvent('on' + evType, fn); 10 return r; 11 } 12 else { 13 elm['on' + evType] = fn; 14 } 15 } 16} 17 18BrowserHistory = (function() { 19 // type of browser 20 var browser = { 21 ie: false, 22 firefox: false, 23 safari: false, 24 opera: false, 25 version: -1 26 }; 27 28 // if setDefaultURL has been called, our first clue 29 // that the SWF is ready and listening 30 //var swfReady = false; 31 32 // the URL we'll send to the SWF once it is ready 33 //var pendingURL = ''; 34 35 // Default app state URL to use when no fragment ID present 36 var defaultHash = ''; 37 38 // Last-known app state URL 39 var currentHref = document.location.href; 40 41 // Initial URL (used only by IE) 42 var initialHref = document.location.href; 43 44 // Initial URL (used only by IE) 45 var initialHash = document.location.hash; 46 47 // History frame source URL prefix (used only by IE) 48 var historyFrameSourcePrefix = 'history/historyFrame.html?'; 49 50 // History maintenance (used only by Safari) 51 var currentHistoryLength = -1; 52 53 var historyHash = []; 54 55 var initialState = createState(initialHref, initialHref + '#' + initialHash, initialHash); 56 57 var backStack = []; 58 var forwardStack = []; 59 60 var currentObjectId = null; 61 62 //UserAgent detection 63 var useragent = navigator.userAgent.toLowerCase(); 64 65 if (useragent.indexOf("opera") != -1) { 66 browser.opera = true; 67 } else if (useragent.indexOf("msie") != -1) { 68 browser.ie = true; 69 browser.version = parseFloat(useragent.substring(useragent.indexOf('msie') + 4)); 70 } else if (useragent.indexOf("safari") != -1) { 71 browser.safari = true; 72 browser.version = parseFloat(useragent.substring(useragent.indexOf('safari') + 7)); 73 } else if (useragent.indexOf("gecko") != -1) { 74 browser.firefox = true; 75 } 76 77 if (browser.ie == true && browser.version == 7) { 78 window["_ie_firstload"] = false; 79 } 80 81 // Accessor functions for obtaining specific elements of the page. 82 function getHistoryFrame() 83 { 84 return document.getElementById('ie_historyFrame'); 85 } 86 87 function getAnchorElement() 88 { 89 return document.getElementById('firefox_anchorDiv'); 90 } 91 92 function getFormElement() 93 { 94 return document.getElementById('safari_formDiv'); 95 } 96 97 function getRememberElement() 98 { 99 return document.getElementById("safari_remember_field"); 100 } 101 102 /* Get the Flash player object for performing ExternalInterface callbacks. */ 103 function getPlayer(objectId) { 104 var objectId = objectId || null; 105 var player = null; /* AJH, needed? = document.getElementById(getPlayerId()); */ 106 if (browser.ie && objectId != null) { 107 player = document.getElementById(objectId); 108 } 109 if (player == null) { 110 player = document.getElementsByTagName('object')[0]; 111 } 112 113 if (player == null || player.object == null) { 114 player = document.getElementsByTagName('embed')[0]; 115 } 116 117 return player; 118 } 119 120 function getPlayers() { 121 var players = []; 122 if (players.length == 0) { 123 var tmp = document.getElementsByTagName('object'); 124 players = tmp; 125 } 126 127 if (players.length == 0 || players[0].object == null) { 128 var tmp = document.getElementsByTagName('embed'); 129 players = tmp; 130 } 131 return players; 132 } 133 134 function getIframeHash() { 135 var doc = getHistoryFrame().contentWindow.document; 136 var hash = String(doc.location.search); 137 if (hash.length == 1 && hash.charAt(0) == "?") { 138 hash = ""; 139 } 140 else if (hash.length >= 2 && hash.charAt(0) == "?") { 141 hash = hash.substring(1); 142 } 143 return hash; 144 } 145 146 /* Get the current location hash excluding the '#' symbol. */ 147 function getHash() { 148 // It would be nice if we could use document.location.hash here, 149 // but it's faulty sometimes. 150 var idx = document.location.href.indexOf('#'); 151 return (idx >= 0) ? document.location.href.substr(idx+1) : ''; 152 } 153 154 /* Get the current location hash excluding the '#' symbol. */ 155 function setHash(hash) { 156 // It would be nice if we could use document.location.hash here, 157 // but it's faulty sometimes. 158 if (hash == '') hash = '#' 159 document.location.hash = hash; 160 } 161 162 function createState(baseUrl, newUrl, flexAppUrl) { 163 return { 'baseUrl': baseUrl, 'newUrl': newUrl, 'flexAppUrl': flexAppUrl, 'title': null }; 164 } 165 166 /* Add a history entry to the browser. 167 * baseUrl: the portion of the location prior to the '#' 168 * newUrl: the entire new URL, including '#' and following fragment 169 * flexAppUrl: the portion of the location following the '#' only 170 */ 171 function addHistoryEntry(baseUrl, newUrl, flexAppUrl) { 172 173 //delete all the history entries 174 forwardStack = []; 175 176 if (browser.ie) { 177 //Check to see if we are being asked to do a navigate for the first 178 //history entry, and if so ignore, because it's coming from the creation 179 //of the history iframe 180 if (flexAppUrl == defaultHash && document.location.href == initialHref && window['_ie_firstload']) { 181 currentHref = initialHref; 182 return; 183 } 184 if ((!flexAppUrl || flexAppUrl == defaultHash) && window['_ie_firstload']) { 185 newUrl = baseUrl + '#' + defaultHash; 186 flexAppUrl = defaultHash; 187 } else { 188 // for IE, tell the history frame to go somewhere without a '#' 189 // in order to get this entry into the browser history. 190 getHistoryFrame().src = historyFrameSourcePrefix + flexAppUrl; 191 } 192 setHash(flexAppUrl); 193 } else { 194 195 //ADR 196 if (backStack.length == 0 && initialState.flexAppUrl == flexAppUrl) { 197 initialState = createState(baseUrl, newUrl, flexAppUrl); 198 } else if(backStack.length > 0 && backStack[backStack.length - 1].flexAppUrl == flexAppUrl) { 199 backStack[backStack.length - 1] = createState(baseUrl, newUrl, flexAppUrl); 200 } 201 202 if (browser.safari) { 203 // for Safari, submit a form whose action points to the desired URL 204 if (browser.version <= 419.3) { 205 var file = window.location.pathname.toString(); 206 file = file.substring(file.lastIndexOf("/")+1); 207 getFormElement().innerHTML = '<form name="historyForm" action="'+file+'#' + flexAppUrl + '" method="GET"></form>'; 208 //get the current elements and add them to the form 209 var qs = window.location.search.substring(1); 210 var qs_arr = qs.split("&"); 211 for (var i = 0; i < qs_arr.length; i++) { 212 var tmp = qs_arr[i].split("="); 213 var elem = document.createElement("input"); 214 elem.type = "hidden"; 215 elem.name = tmp[0]; 216 elem.value = tmp[1]; 217 document.forms.historyForm.appendChild(elem); 218 } 219 document.forms.historyForm.submit(); 220 } else { 221 top.location.hash = flexAppUrl; 222 } 223 // We also have to maintain the history by hand for Safari 224 historyHash[history.length] = flexAppUrl; 225 _storeStates(); 226 } else { 227 // Otherwise, write an anchor into the page and tell the browser to go there 228 addAnchor(flexAppUrl); 229 setHash(flexAppUrl); 230 } 231 } 232 backStack.push(createState(baseUrl, newUrl, flexAppUrl)); 233 } 234 235 function _storeStates() { 236 if (browser.safari) { 237 getRememberElement().value = historyHash.join(","); 238 } 239 } 240 241 function handleBackButton() { 242 //The "current" page is always at the top of the history stack. 243 var current = backStack.pop(); 244 if (!current) { return; } 245 var last = backStack[backStack.length - 1]; 246 if (!last && backStack.length == 0){ 247 last = initialState; 248 } 249 forwardStack.push(current); 250 } 251 252 function handleForwardButton() { 253 //summary: private method. Do not call this directly. 254 255 var last = forwardStack.pop(); 256 if (!last) { return; } 257 backStack.push(last); 258 } 259 260 function handleArbitraryUrl() { 261 //delete all the history entries 262 forwardStack = []; 263 } 264 265 /* Called periodically to poll to see if we need to detect navigation that has occurred */ 266 function checkForUrlChange() { 267 268 if (browser.ie) { 269 if (currentHref != document.location.href && currentHref + '#' != document.location.href) { 270 //This occurs when the user has navigated to a specific URL 271 //within the app, and didn't use browser back/forward 272 //IE seems to have a bug where it stops updating the URL it 273 //shows the end-user at this point, but programatically it 274 //appears to be correct. Do a full app reload to get around 275 //this issue. 276 if (browser.version < 7) { 277 currentHref = document.location.href; 278 document.location.reload(); 279 } else { 280 if (getHash() != getIframeHash()) { 281 // this.iframe.src = this.blankURL + hash; 282 var sourceToSet = historyFrameSourcePrefix + getHash(); 283 getHistoryFrame().src = sourceToSet; 284 } 285 } 286 } 287 } 288 289 if (browser.safari) { 290 // For Safari, we have to check to see if history.length changed. 291 if (currentHistoryLength >= 0 && history.length != currentHistoryLength) { 292 //alert("did change: " + history.length + ", " + historyHash.length + "|" + historyHash[history.length] + "|>" + historyHash.join("|")); 293 // If it did change, then we have to look the old state up 294 // in our hand-maintained array since document.location.hash 295 // won't have changed, then call back into BrowserManager. 296 currentHistoryLength = history.length; 297 var flexAppUrl = historyHash[currentHistoryLength]; 298 if (flexAppUrl == '') { 299 //flexAppUrl = defaultHash; 300 } 301 //ADR: to fix multiple 302 if (typeof BrowserHistory_multiple != "undefined" && BrowserHistory_multiple == true) { 303 var pl = getPlayers(); 304 for (var i = 0; i < pl.length; i++) { 305 pl[i].browserURLChange(flexAppUrl); 306 } 307 } else { 308 getPlayer().browserURLChange(flexAppUrl); 309 } 310 _storeStates(); 311 } 312 } 313 if (browser.firefox) { 314 if (currentHref != document.location.href) { 315 var bsl = backStack.length; 316 317 var urlActions = { 318 back: false, 319 forward: false, 320 set: false 321 } 322 323 if ((window.location.hash == initialHash || window.location.href == initialHref) && (bsl == 1)) { 324 urlActions.back = true; 325 // FIXME: could this ever be a forward button? 326 // we can't clear it because we still need to check for forwards. Ugg. 327 // clearInterval(this.locationTimer); 328 handleBackButton(); 329 } 330 331 // first check to see if we could have gone forward. We always halt on 332 // a no-hash item. 333 if (forwardStack.length > 0) { 334 if (forwardStack[forwardStack.length-1].flexAppUrl == getHash()) { 335 urlActions.forward = true; 336 handleForwardButton(); 337 } 338 } 339 340 // ok, that didn't work, try someplace back in the history stack 341 if ((bsl >= 2) && (backStack[bsl - 2])) { 342 if (backStack[bsl - 2].flexAppUrl == getHash()) { 343 urlActions.back = true; 344 handleBackButton(); 345 } 346 } 347 348 if (!urlActions.back && !urlActions.forward) { 349 var foundInStacks = { 350 back: -1, 351 forward: -1 352 } 353 354 for (var i = 0; i < backStack.length; i++) { 355 if (backStack[i].flexAppUrl == getHash() && i != (bsl - 2)) { 356 arbitraryUrl = true; 357 foundInStacks.back = i; 358 } 359 } 360 for (var i = 0; i < forwardStack.length; i++) { 361 if (forwardStack[i].flexAppUrl == getHash() && i != (bsl - 2)) { 362 arbitraryUrl = true; 363 foundInStacks.forward = i; 364 } 365 } 366 handleArbitraryUrl(); 367 } 368 369 // Firefox changed; do a callback into BrowserManager to tell it. 370 currentHref = document.location.href; 371 var flexAppUrl = getHash(); 372 if (flexAppUrl == '') { 373 //flexAppUrl = defaultHash; 374 } 375 //ADR: to fix multiple 376 if (typeof BrowserHistory_multiple != "undefined" && BrowserHistory_multiple == true) { 377 var pl = getPlayers(); 378 for (var i = 0; i < pl.length; i++) { 379 pl[i].browserURLChange(flexAppUrl); 380 } 381 } else { 382 getPlayer().browserURLChange(flexAppUrl); 383 } 384 } 385 } 386 //setTimeout(checkForUrlChange, 50); 387 } 388 389 /* Write an anchor into the page to legitimize it as a URL for Firefox et al. */ 390 function addAnchor(flexAppUrl) 391 { 392 if (document.getElementsByName(flexAppUrl).length == 0) { 393 getAnchorElement().innerHTML += "<a name='" + flexAppUrl + "'>" + flexAppUrl + "</a>"; 394 } 395 } 396 397 var _initialize = function () { 398 if (browser.ie) 399 { 400 var scripts = document.getElementsByTagName('script'); 401 for (var i = 0, s; s = scripts[i]; i++) { 402 if (s.src.indexOf("history.js") > -1) { 403 var iframe_location = (new String(s.src)).replace("history.js", "historyFrame.html"); 404 } 405 } 406 historyFrameSourcePrefix = iframe_location + "?"; 407 var src = historyFrameSourcePrefix; 408 409 var iframe = document.createElement("iframe"); 410 iframe.id = 'ie_historyFrame'; 411 iframe.name = 'ie_historyFrame'; 412 //iframe.src = historyFrameSourcePrefix; 413 try { 414 document.body.appendChild(iframe); 415 } catch(e) { 416 setTimeout(function() { 417 document.body.appendChild(iframe); 418 }, 0); 419 } 420 } 421 422 if (browser.safari) 423 { 424 var rememberDiv = document.createElement("div"); 425 rememberDiv.id = 'safari_rememberDiv'; 426 document.body.appendChild(rememberDiv); 427 rememberDiv.innerHTML = '<input type="text" id="safari_remember_field" style="width: 500px;">'; 428 429 var formDiv = document.createElement("div"); 430 formDiv.id = 'safari_formDiv'; 431 document.body.appendChild(formDiv); 432 433 var reloader_content = document.createElement('div'); 434 reloader_content.id = 'safarireloader'; 435 var scripts = document.getElementsByTagName('script'); 436 for (var i = 0, s; s = scripts[i]; i++) { 437 if (s.src.indexOf("history.js") > -1) { 438 html = (new String(s.src)).replace(".js", ".html"); 439 } 440 } 441 reloader_content.innerHTML = '<iframe id="safarireloader-iframe" src="about:blank" frameborder="no" scrolling="no"></iframe>'; 442 document.body.appendChild(reloader_content); 443 reloader_content.style.position = 'absolute'; 444 reloader_content.style.left = reloader_content.style.top = '-9999px'; 445 iframe = reloader_content.getElementsByTagName('iframe')[0]; 446 447 if (document.getElementById("safari_remember_field").value != "" ) { 448 historyHash = document.getElementById("safari_remember_field").value.split(","); 449 } 450 451 } 452 453 if (browser.firefox) 454 { 455 var anchorDiv = document.createElement("div"); 456 anchorDiv.id = 'firefox_anchorDiv'; 457 document.body.appendChild(anchorDiv); 458 } 459 460 //setTimeout(checkForUrlChange, 50); 461 } 462 463 return { 464 historyHash: historyHash, 465 backStack: function() { return backStack; }, 466 forwardStack: function() { return forwardStack }, 467 getPlayer: getPlayer, 468 initialize: function(src) { 469 _initialize(src); 470 }, 471 setURL: function(url) { 472 document.location.href = url; 473 }, 474 getURL: function() { 475 return document.location.href; 476 }, 477 getTitle: function() { 478 return document.title; 479 }, 480 setTitle: function(title) { 481 try { 482 backStack[backStack.length - 1].title = title; 483 } catch(e) { } 484 //if on safari, set the title to be the empty string. 485 if (browser.safari) { 486 if (title == "") { 487 try { 488 var tmp = window.location.href.toString(); 489 title = tmp.substring((tmp.lastIndexOf("/")+1), tmp.lastIndexOf("#")); 490 } catch(e) { 491 title = ""; 492 } 493 } 494 } 495 document.title = title; 496 }, 497 setDefaultURL: function(def) 498 { 499 defaultHash = def; 500 def = getHash(); 501 //trailing ? is important else an extra frame gets added to the history 502 //when navigating back to the first page. Alternatively could check 503 //in history frame navigation to compare # and ?. 504 if (browser.ie) 505 { 506 window['_ie_firstload'] = true; 507 var sourceToSet = historyFrameSourcePrefix + def; 508 var func = function() { 509 getHistoryFrame().src = sourceToSet; 510 window.location.replace("#" + def); 511 setInterval(checkForUrlChange, 50); 512 } 513 try { 514 func(); 515 } catch(e) { 516 window.setTimeout(function() { func(); }, 0); 517 } 518 } 519 520 if (browser.safari) 521 { 522 currentHistoryLength = history.length; 523 if (historyHash.length == 0) { 524 historyHash[currentHistoryLength] = def; 525 var newloc = "#" + def; 526 window.location.replace(newloc); 527 } else { 528 //alert(historyHash[historyHash.length-1]); 529 } 530 //setHash(def); 531 setInterval(checkForUrlChange, 50); 532 } 533 534 535 if (browser.firefox || browser.opera) 536 { 537 var reg = new RegExp("#" + def + "$"); 538 if (window.location.toString().match(reg)) { 539 } else { 540 var newloc ="#" + def; 541 window.location.replace(newloc); 542 } 543 setInterval(checkForUrlChange, 50); 544 //setHash(def); 545 } 546 547 }, 548 549 /* Set the current browser URL; called from inside BrowserManager to propagate 550 * the application state out to the container. 551 */ 552 setBrowserURL: function(flexAppUrl, objectId) { 553 if (browser.ie && typeof objectId != "undefined") { 554 currentObjectId = objectId; 555 } 556 //fromIframe = fromIframe || false; 557 //fromFlex = fromFlex || false; 558 //alert("setBrowserURL: " + flexAppUrl); 559 //flexAppUrl = (flexAppUrl == "") ? defaultHash : flexAppUrl ; 560 561 var pos = document.location.href.indexOf('#'); 562 var baseUrl = pos != -1 ? document.location.href.substr(0, pos) : document.location.href; 563 var newUrl = baseUrl + '#' + flexAppUrl; 564 565 if (document.location.href != newUrl && document.location.href + '#' != newUrl) { 566 currentHref = newUrl; 567 addHistoryEntry(baseUrl, newUrl, flexAppUrl); 568 currentHistoryLength = history.length; 569 } 570 571 return false; 572 }, 573 574 browserURLChange: function(flexAppUrl) { 575 var objectId = null; 576 if (browser.ie && currentObjectId != null) { 577 objectId = currentObjectId; 578 } 579 pendingURL = ''; 580 581 if (typeof BrowserHistory_multiple != "undefined" && BrowserHistory_multiple == true) { 582 var pl = getPlayers(); 583 for (var i = 0; i < pl.length; i++) { 584 try { 585 pl[i].browserURLChange(flexAppUrl); 586 } catch(e) { } 587 } 588 } else { 589 try { 590 getPlayer(objectId).browserURLChange(flexAppUrl); 591 } catch(e) { } 592 } 593 594 currentObjectId = null; 595 } 596 597 } 598 599})(); 600 601// Initialization 602 603// Automated unit testing and other diagnostics 604 605function setURL(url) 606{ 607 document.location.href = url; 608} 609 610function backButton() 611{ 612 history.back(); 613} 614 615function forwardButton() 616{ 617 history.forward(); 618} 619 620function goForwardOrBackInHistory(step) 621{ 622 history.go(step); 623} 624 625//BrowserHistoryUtils.addEvent(window, "load", function() { BrowserHistory.initialize(); }); 626(function(i) { 627 var u =navigator.userAgent;var e=/*@cc_on!@*/false; 628 var st = setTimeout; 629 if(/webkit/i.test(u)){ 630 st(function(){ 631 var dr=document.readyState; 632 if(dr=="loaded"||dr=="complete"){i()} 633 else{st(arguments.callee,10);}},10); 634 } else if((/mozilla/i.test(u)&&!/(compati)/.test(u)) || (/opera/i.test(u))){ 635 document.addEventListener("DOMContentLoaded",i,false); 636 } else if(e){ 637 (function(){ 638 var t=document.createElement('doc:rdy'); 639 try{t.doScroll('left'); 640 i();t=null; 641 }catch(e){st(arguments.callee,0);}})(); 642 } else{ 643 window.onload=i; 644 } 645})( function() {BrowserHistory.initialize();} );