PageRenderTime 20ms CodeModel.GetById 1ms RepoModel.GetById 0ms app.codeStats 1ms

/examples/ArcGISLink/bin-release/srcview/source/html-template/history/history.js.txt

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