PageRenderTime 43ms CodeModel.GetById 22ms RepoModel.GetById 0ms app.codeStats 0ms

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

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