history.js 46 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943
  1. /**
  2. * History.js Core
  3. * @author Benjamin Arthur Lupton <contact@balupton.com>
  4. * @copyright 2010-2011 Benjamin Arthur Lupton <contact@balupton.com>
  5. * @license New BSD License <http://creativecommons.org/licenses/BSD/>
  6. */
  7. (function(window,undefined){
  8. "use strict";
  9. // ========================================================================
  10. // Initialise
  11. // Localise Globals
  12. var
  13. console = window.console||undefined, // Prevent a JSLint complain
  14. document = window.document, // Make sure we are using the correct document
  15. navigator = window.navigator, // Make sure we are using the correct navigator
  16. sessionStorage = window.sessionStorage||false, // sessionStorage
  17. setTimeout = window.setTimeout,
  18. clearTimeout = window.clearTimeout,
  19. setInterval = window.setInterval,
  20. clearInterval = window.clearInterval,
  21. JSON = window.JSON,
  22. alert = window.alert,
  23. History = window.History = window.History||{}, // Public History Object
  24. history = window.history; // Old History Object
  25. // MooTools Compatibility
  26. JSON.stringify = JSON.stringify||JSON.encode;
  27. JSON.parse = JSON.parse||JSON.decode;
  28. // Check Existence
  29. if ( typeof History.init !== 'undefined' ) {
  30. throw new Error('History.js Core has already been loaded...');
  31. }
  32. // Initialise History
  33. History.init = function(){
  34. // Check Load Status of Adapter
  35. if ( typeof History.Adapter === 'undefined' ) {
  36. return false;
  37. }
  38. // Check Load Status of Core
  39. if ( typeof History.initCore !== 'undefined' ) {
  40. History.initCore();
  41. }
  42. // Check Load Status of HTML4 Support
  43. if ( typeof History.initHtml4 !== 'undefined' ) {
  44. History.initHtml4();
  45. }
  46. // Return true
  47. return true;
  48. };
  49. // ========================================================================
  50. // Initialise Core
  51. // Initialise Core
  52. History.initCore = function(){
  53. // Initialise
  54. if ( typeof History.initCore.initialized !== 'undefined' ) {
  55. // Already Loaded
  56. return false;
  57. }
  58. else {
  59. History.initCore.initialized = true;
  60. }
  61. // ====================================================================
  62. // Options
  63. /**
  64. * History.options
  65. * Configurable options
  66. */
  67. History.options = History.options||{};
  68. /**
  69. * History.options.hashChangeInterval
  70. * How long should the interval be before hashchange checks
  71. */
  72. History.options.hashChangeInterval = History.options.hashChangeInterval || 100;
  73. /**
  74. * History.options.safariPollInterval
  75. * How long should the interval be before safari poll checks
  76. */
  77. History.options.safariPollInterval = History.options.safariPollInterval || 500;
  78. /**
  79. * History.options.doubleCheckInterval
  80. * How long should the interval be before we perform a double check
  81. */
  82. History.options.doubleCheckInterval = History.options.doubleCheckInterval || 500;
  83. /**
  84. * History.options.storeInterval
  85. * How long should we wait between store calls
  86. */
  87. History.options.storeInterval = History.options.storeInterval || 1000;
  88. /**
  89. * History.options.busyDelay
  90. * How long should we wait between busy events
  91. */
  92. History.options.busyDelay = History.options.busyDelay || 250;
  93. /**
  94. * History.options.debug
  95. * If true will enable debug messages to be logged
  96. */
  97. History.options.debug = History.options.debug || false;
  98. /**
  99. * History.options.initialTitle
  100. * What is the title of the initial state
  101. */
  102. History.options.initialTitle = History.options.initialTitle || document.title;
  103. // ====================================================================
  104. // Interval record
  105. /**
  106. * History.intervalList
  107. * List of intervals set, to be cleared when document is unloaded.
  108. */
  109. History.intervalList = [];
  110. /**
  111. * History.clearAllIntervals
  112. * Clears all setInterval instances.
  113. */
  114. History.clearAllIntervals = function(){
  115. var i, il = History.intervalList;
  116. if (typeof il !== "undefined" && il !== null) {
  117. for (i = 0; i < il.length; i++) {
  118. clearInterval(il[i]);
  119. }
  120. History.intervalList = null;
  121. }
  122. };
  123. // ====================================================================
  124. // Debug
  125. /**
  126. * History.debug(message,...)
  127. * Logs the passed arguments if debug enabled
  128. */
  129. History.debug = function(){
  130. if ( (History.options.debug||false) ) {
  131. History.log.apply(History,arguments);
  132. }
  133. };
  134. /**
  135. * History.log(message,...)
  136. * Logs the passed arguments
  137. */
  138. History.log = function(){
  139. // Prepare
  140. var
  141. consoleExists = !(typeof console === 'undefined' || typeof console.log === 'undefined' || typeof console.log.apply === 'undefined'),
  142. textarea = document.getElementById('log'),
  143. message,
  144. i,n,
  145. args,arg
  146. ;
  147. // Write to Console
  148. if ( consoleExists ) {
  149. args = Array.prototype.slice.call(arguments);
  150. message = args.shift();
  151. if ( typeof console.debug !== 'undefined' ) {
  152. console.debug.apply(console,[message,args]);
  153. }
  154. else {
  155. console.log.apply(console,[message,args]);
  156. }
  157. }
  158. else {
  159. message = ("\n"+arguments[0]+"\n");
  160. }
  161. // Write to log
  162. for ( i=1,n=arguments.length; i<n; ++i ) {
  163. arg = arguments[i];
  164. if ( typeof arg === 'object' && typeof JSON !== 'undefined' ) {
  165. try {
  166. arg = JSON.stringify(arg);
  167. }
  168. catch ( Exception ) {
  169. // Recursive Object
  170. }
  171. }
  172. message += "\n"+arg+"\n";
  173. }
  174. // Textarea
  175. if ( textarea ) {
  176. textarea.value += message+"\n-----\n";
  177. textarea.scrollTop = textarea.scrollHeight - textarea.clientHeight;
  178. }
  179. // No Textarea, No Console
  180. else if ( !consoleExists ) {
  181. alert(message);
  182. }
  183. // Return true
  184. return true;
  185. };
  186. // ====================================================================
  187. // Emulated Status
  188. /**
  189. * History.getInternetExplorerMajorVersion()
  190. * Get's the major version of Internet Explorer
  191. * @return {integer}
  192. * @license Public Domain
  193. * @author Benjamin Arthur Lupton <contact@balupton.com>
  194. * @author James Padolsey <https://gist.github.com/527683>
  195. */
  196. History.getInternetExplorerMajorVersion = function(){
  197. var result = History.getInternetExplorerMajorVersion.cached =
  198. (typeof History.getInternetExplorerMajorVersion.cached !== 'undefined')
  199. ? History.getInternetExplorerMajorVersion.cached
  200. : (function(){
  201. var v = 3,
  202. div = document.createElement('div'),
  203. all = div.getElementsByTagName('i');
  204. while ( (div.innerHTML = '<!--[if gt IE ' + (++v) + ']><i></i><![endif]-->') && all[0] ) {}
  205. return (v > 4) ? v : false;
  206. })()
  207. ;
  208. return result;
  209. };
  210. /**
  211. * History.isInternetExplorer()
  212. * Are we using Internet Explorer?
  213. * @return {boolean}
  214. * @license Public Domain
  215. * @author Benjamin Arthur Lupton <contact@balupton.com>
  216. */
  217. History.isInternetExplorer = function(){
  218. var result =
  219. History.isInternetExplorer.cached =
  220. (typeof History.isInternetExplorer.cached !== 'undefined')
  221. ? History.isInternetExplorer.cached
  222. : Boolean(History.getInternetExplorerMajorVersion())
  223. ;
  224. return result;
  225. };
  226. /**
  227. * History.emulated
  228. * Which features require emulating?
  229. */
  230. History.emulated = {
  231. pushState: !Boolean(
  232. window.history && window.history.pushState && window.history.replaceState
  233. && !(
  234. (/ Mobile\/([1-7][a-z]|(8([abcde]|f(1[0-8]))))/i).test(navigator.userAgent) /* disable for versions of iOS before version 4.3 (8F190) */
  235. || (/AppleWebKit\/5([0-2]|3[0-2])/i).test(navigator.userAgent) /* disable for the mercury iOS browser, or at least older versions of the webkit engine */
  236. )
  237. ),
  238. hashChange: Boolean(
  239. !(('onhashchange' in window) || ('onhashchange' in document))
  240. ||
  241. (History.isInternetExplorer() && History.getInternetExplorerMajorVersion() < 8)
  242. )
  243. };
  244. /**
  245. * History.enabled
  246. * Is History enabled?
  247. */
  248. History.enabled = !History.emulated.pushState;
  249. /**
  250. * History.bugs
  251. * Which bugs are present
  252. */
  253. History.bugs = {
  254. /**
  255. * Safari 5 and Safari iOS 4 fail to return to the correct state once a hash is replaced by a `replaceState` call
  256. * https://bugs.webkit.org/show_bug.cgi?id=56249
  257. */
  258. setHash: Boolean(!History.emulated.pushState && navigator.vendor === 'Apple Computer, Inc.' && /AppleWebKit\/5([0-2]|3[0-3])/.test(navigator.userAgent)),
  259. /**
  260. * Safari 5 and Safari iOS 4 sometimes fail to apply the state change under busy conditions
  261. * https://bugs.webkit.org/show_bug.cgi?id=42940
  262. */
  263. safariPoll: Boolean(!History.emulated.pushState && navigator.vendor === 'Apple Computer, Inc.' && /AppleWebKit\/5([0-2]|3[0-3])/.test(navigator.userAgent)),
  264. /**
  265. * MSIE 6 and 7 sometimes do not apply a hash even it was told to (requiring a second call to the apply function)
  266. */
  267. ieDoubleCheck: Boolean(History.isInternetExplorer() && History.getInternetExplorerMajorVersion() < 8),
  268. /**
  269. * MSIE 6 requires the entire hash to be encoded for the hashes to trigger the onHashChange event
  270. */
  271. hashEscape: Boolean(History.isInternetExplorer() && History.getInternetExplorerMajorVersion() < 7)
  272. };
  273. /**
  274. * History.isEmptyObject(obj)
  275. * Checks to see if the Object is Empty
  276. * @param {Object} obj
  277. * @return {boolean}
  278. */
  279. History.isEmptyObject = function(obj) {
  280. for ( var name in obj ) {
  281. return false;
  282. }
  283. return true;
  284. };
  285. /**
  286. * History.cloneObject(obj)
  287. * Clones a object and eliminate all references to the original contexts
  288. * @param {Object} obj
  289. * @return {Object}
  290. */
  291. History.cloneObject = function(obj) {
  292. var hash,newObj;
  293. if ( obj ) {
  294. hash = JSON.stringify(obj);
  295. newObj = JSON.parse(hash);
  296. }
  297. else {
  298. newObj = {};
  299. }
  300. return newObj;
  301. };
  302. // ====================================================================
  303. // URL Helpers
  304. /**
  305. * History.getRootUrl()
  306. * Turns "http://mysite.com/dir/page.html?asd" into "http://mysite.com"
  307. * @return {String} rootUrl
  308. */
  309. History.getRootUrl = function(){
  310. // Create
  311. var rootUrl = document.location.protocol+'//'+(document.location.hostname||document.location.host);
  312. if ( document.location.port||false ) {
  313. rootUrl += ':'+document.location.port;
  314. }
  315. rootUrl += '/';
  316. // Return
  317. return rootUrl;
  318. };
  319. /**
  320. * History.getBaseHref()
  321. * Fetches the `href` attribute of the `<base href="...">` element if it exists
  322. * @return {String} baseHref
  323. */
  324. History.getBaseHref = function(){
  325. // Create
  326. var
  327. baseElements = document.getElementsByTagName('base'),
  328. baseElement = null,
  329. baseHref = '';
  330. // Test for Base Element
  331. if ( baseElements.length === 1 ) {
  332. // Prepare for Base Element
  333. baseElement = baseElements[0];
  334. baseHref = baseElement.href.replace(/[^\/]+$/,'');
  335. }
  336. // Adjust trailing slash
  337. baseHref = baseHref.replace(/\/+$/,'');
  338. if ( baseHref ) baseHref += '/';
  339. // Return
  340. return baseHref;
  341. };
  342. /**
  343. * History.getBaseUrl()
  344. * Fetches the baseHref or basePageUrl or rootUrl (whichever one exists first)
  345. * @return {String} baseUrl
  346. */
  347. History.getBaseUrl = function(){
  348. // Create
  349. var baseUrl = History.getBaseHref()||History.getBasePageUrl()||History.getRootUrl();
  350. // Return
  351. return baseUrl;
  352. };
  353. /**
  354. * History.getPageUrl()
  355. * Fetches the URL of the current page
  356. * @return {String} pageUrl
  357. */
  358. History.getPageUrl = function(){
  359. // Fetch
  360. var
  361. State = History.getState(false,false),
  362. stateUrl = (State||{}).url||document.location.href,
  363. pageUrl;
  364. // Create
  365. pageUrl = stateUrl.replace(/\/+$/,'').replace(/[^\/]+$/,function(part,index,string){
  366. return (/\./).test(part) ? part : part+'/';
  367. });
  368. // Return
  369. return pageUrl;
  370. };
  371. /**
  372. * History.getBasePageUrl()
  373. * Fetches the Url of the directory of the current page
  374. * @return {String} basePageUrl
  375. */
  376. History.getBasePageUrl = function(){
  377. // Create
  378. var basePageUrl = document.location.href.replace(/[#\?].*/,'').replace(/[^\/]+$/,function(part,index,string){
  379. return (/[^\/]$/).test(part) ? '' : part;
  380. }).replace(/\/+$/,'')+'/';
  381. // Return
  382. return basePageUrl;
  383. };
  384. /**
  385. * History.getFullUrl(url)
  386. * Ensures that we have an absolute URL and not a relative URL
  387. * @param {string} url
  388. * @param {Boolean} allowBaseHref
  389. * @return {string} fullUrl
  390. */
  391. History.getFullUrl = function(url,allowBaseHref){
  392. // Prepare
  393. var fullUrl = url, firstChar = url.substring(0,1);
  394. allowBaseHref = (typeof allowBaseHref === 'undefined') ? true : allowBaseHref;
  395. // Check
  396. if ( /[a-z]+\:\/\//.test(url) ) {
  397. // Full URL
  398. }
  399. else if ( firstChar === '/' ) {
  400. // Root URL
  401. fullUrl = History.getRootUrl()+url.replace(/^\/+/,'');
  402. }
  403. else if ( firstChar === '#' ) {
  404. // Anchor URL
  405. fullUrl = History.getPageUrl().replace(/#.*/,'')+url;
  406. }
  407. else if ( firstChar === '?' ) {
  408. // Query URL
  409. fullUrl = History.getPageUrl().replace(/[\?#].*/,'')+url;
  410. }
  411. else {
  412. // Relative URL
  413. if ( allowBaseHref ) {
  414. fullUrl = History.getBaseUrl()+url.replace(/^(\.\/)+/,'');
  415. } else {
  416. fullUrl = History.getBasePageUrl()+url.replace(/^(\.\/)+/,'');
  417. }
  418. // We have an if condition above as we do not want hashes
  419. // which are relative to the baseHref in our URLs
  420. // as if the baseHref changes, then all our bookmarks
  421. // would now point to different locations
  422. // whereas the basePageUrl will always stay the same
  423. }
  424. // Return
  425. return fullUrl.replace(/\#$/,'');
  426. };
  427. /**
  428. * History.getShortUrl(url)
  429. * Ensures that we have a relative URL and not a absolute URL
  430. * @param {string} url
  431. * @return {string} url
  432. */
  433. History.getShortUrl = function(url){
  434. // Prepare
  435. var shortUrl = url, baseUrl = History.getBaseUrl(), rootUrl = History.getRootUrl();
  436. // Trim baseUrl
  437. if ( History.emulated.pushState ) {
  438. // We are in a if statement as when pushState is not emulated
  439. // The actual url these short urls are relative to can change
  440. // So within the same session, we the url may end up somewhere different
  441. shortUrl = shortUrl.replace(baseUrl,'');
  442. }
  443. // Trim rootUrl
  444. shortUrl = shortUrl.replace(rootUrl,'/');
  445. // Ensure we can still detect it as a state
  446. if ( History.isTraditionalAnchor(shortUrl) ) {
  447. shortUrl = './'+shortUrl;
  448. }
  449. // Clean It
  450. shortUrl = shortUrl.replace(/^(\.\/)+/g,'./').replace(/\#$/,'');
  451. // Return
  452. return shortUrl;
  453. };
  454. // ====================================================================
  455. // State Storage
  456. /**
  457. * History.store
  458. * The store for all session specific data
  459. */
  460. History.store = {};
  461. /**
  462. * History.idToState
  463. * 1-1: State ID to State Object
  464. */
  465. History.idToState = History.idToState||{};
  466. /**
  467. * History.stateToId
  468. * 1-1: State String to State ID
  469. */
  470. History.stateToId = History.stateToId||{};
  471. /**
  472. * History.urlToId
  473. * 1-1: State URL to State ID
  474. */
  475. History.urlToId = History.urlToId||{};
  476. /**
  477. * History.storedStates
  478. * Store the states in an array
  479. */
  480. History.storedStates = History.storedStates||[];
  481. /**
  482. * History.savedStates
  483. * Saved the states in an array
  484. */
  485. History.savedStates = History.savedStates||[];
  486. /**
  487. * History.noramlizeStore()
  488. * Noramlize the store by adding necessary values
  489. */
  490. History.normalizeStore = function(){
  491. History.store.idToState = History.store.idToState||{};
  492. History.store.urlToId = History.store.urlToId||{};
  493. History.store.stateToId = History.store.stateToId||{};
  494. };
  495. /**
  496. * History.getState()
  497. * Get an object containing the data, title and url of the current state
  498. * @param {Boolean} friendly
  499. * @param {Boolean} create
  500. * @return {Object} State
  501. */
  502. History.getState = function(friendly,create){
  503. // Prepare
  504. if ( typeof friendly === 'undefined' ) { friendly = true; }
  505. if ( typeof create === 'undefined' ) { create = true; }
  506. // Fetch
  507. var State = History.getLastSavedState();
  508. // Create
  509. if ( !State && create ) {
  510. State = History.createStateObject();
  511. }
  512. // Adjust
  513. if ( friendly ) {
  514. State = History.cloneObject(State);
  515. State.url = State.cleanUrl||State.url;
  516. }
  517. // Return
  518. return State;
  519. };
  520. /**
  521. * History.getIdByState(State)
  522. * Gets a ID for a State
  523. * @param {State} newState
  524. * @return {String} id
  525. */
  526. History.getIdByState = function(newState){
  527. // Fetch ID
  528. var id = History.extractId(newState.url),
  529. str;
  530. if ( !id ) {
  531. // Find ID via State String
  532. str = History.getStateString(newState);
  533. if ( typeof History.stateToId[str] !== 'undefined' ) {
  534. id = History.stateToId[str];
  535. }
  536. else if ( typeof History.store.stateToId[str] !== 'undefined' ) {
  537. id = History.store.stateToId[str];
  538. }
  539. else {
  540. // Generate a new ID
  541. while ( true ) {
  542. id = (new Date()).getTime() + String(Math.random()).replace(/\D/g,'');
  543. if ( typeof History.idToState[id] === 'undefined' && typeof History.store.idToState[id] === 'undefined' ) {
  544. break;
  545. }
  546. }
  547. // Apply the new State to the ID
  548. History.stateToId[str] = id;
  549. History.idToState[id] = newState;
  550. }
  551. }
  552. // Return ID
  553. return id;
  554. };
  555. /**
  556. * History.normalizeState(State)
  557. * Expands a State Object
  558. * @param {object} State
  559. * @return {object}
  560. */
  561. History.normalizeState = function(oldState){
  562. // Variables
  563. var newState, dataNotEmpty;
  564. // Prepare
  565. if ( !oldState || (typeof oldState !== 'object') ) {
  566. oldState = {};
  567. }
  568. // Check
  569. if ( typeof oldState.normalized !== 'undefined' ) {
  570. return oldState;
  571. }
  572. // Adjust
  573. if ( !oldState.data || (typeof oldState.data !== 'object') ) {
  574. oldState.data = {};
  575. }
  576. // ----------------------------------------------------------------
  577. // Create
  578. newState = {};
  579. newState.normalized = true;
  580. newState.title = oldState.title||'';
  581. newState.url = History.getFullUrl(History.unescapeString(oldState.url||document.location.href));
  582. newState.hash = History.getShortUrl(newState.url);
  583. newState.data = History.cloneObject(oldState.data);
  584. // Fetch ID
  585. newState.id = History.getIdByState(newState);
  586. // ----------------------------------------------------------------
  587. // Clean the URL
  588. newState.cleanUrl = newState.url.replace(/\??\&_suid.*/,'');
  589. newState.url = newState.cleanUrl;
  590. // Check to see if we have more than just a url
  591. dataNotEmpty = !History.isEmptyObject(newState.data);
  592. // Apply
  593. if ( newState.title || dataNotEmpty ) {
  594. // Add ID to Hash
  595. newState.hash = History.getShortUrl(newState.url).replace(/\??\&_suid.*/,'');
  596. if ( !/\?/.test(newState.hash) ) {
  597. newState.hash += '?';
  598. }
  599. newState.hash += '&_suid='+newState.id;
  600. }
  601. // Create the Hashed URL
  602. newState.hashedUrl = History.getFullUrl(newState.hash);
  603. // ----------------------------------------------------------------
  604. // Update the URL if we have a duplicate
  605. if ( (History.emulated.pushState || History.bugs.safariPoll) && History.hasUrlDuplicate(newState) ) {
  606. newState.url = newState.hashedUrl;
  607. }
  608. // ----------------------------------------------------------------
  609. // Return
  610. return newState;
  611. };
  612. /**
  613. * History.createStateObject(data,title,url)
  614. * Creates a object based on the data, title and url state params
  615. * @param {object} data
  616. * @param {string} title
  617. * @param {string} url
  618. * @return {object}
  619. */
  620. History.createStateObject = function(data,title,url){
  621. // Hashify
  622. var State = {
  623. 'data': data,
  624. 'title': title,
  625. 'url': url
  626. };
  627. // Expand the State
  628. State = History.normalizeState(State);
  629. // Return object
  630. return State;
  631. };
  632. /**
  633. * History.getStateById(id)
  634. * Get a state by it's UID
  635. * @param {String} id
  636. */
  637. History.getStateById = function(id){
  638. // Prepare
  639. id = String(id);
  640. // Retrieve
  641. var State = History.idToState[id] || History.store.idToState[id] || undefined;
  642. // Return State
  643. return State;
  644. };
  645. /**
  646. * Get a State's String
  647. * @param {State} passedState
  648. */
  649. History.getStateString = function(passedState){
  650. // Prepare
  651. var State, cleanedState, str;
  652. // Fetch
  653. State = History.normalizeState(passedState);
  654. // Clean
  655. cleanedState = {
  656. data: State.data,
  657. title: passedState.title,
  658. url: passedState.url
  659. };
  660. // Fetch
  661. str = JSON.stringify(cleanedState);
  662. // Return
  663. return str;
  664. };
  665. /**
  666. * Get a State's ID
  667. * @param {State} passedState
  668. * @return {String} id
  669. */
  670. History.getStateId = function(passedState){
  671. // Prepare
  672. var State, id;
  673. // Fetch
  674. State = History.normalizeState(passedState);
  675. // Fetch
  676. id = State.id;
  677. // Return
  678. return id;
  679. };
  680. /**
  681. * History.getHashByState(State)
  682. * Creates a Hash for the State Object
  683. * @param {State} passedState
  684. * @return {String} hash
  685. */
  686. History.getHashByState = function(passedState){
  687. // Prepare
  688. var State, hash;
  689. // Fetch
  690. State = History.normalizeState(passedState);
  691. // Hash
  692. hash = State.hash;
  693. // Return
  694. return hash;
  695. };
  696. /**
  697. * History.extractId(url_or_hash)
  698. * Get a State ID by it's URL or Hash
  699. * @param {string} url_or_hash
  700. * @return {string} id
  701. */
  702. History.extractId = function ( url_or_hash ) {
  703. // Prepare
  704. var id,parts,url;
  705. // Extract
  706. parts = /(.*)\&_suid=([0-9]+)$/.exec(url_or_hash);
  707. url = parts ? (parts[1]||url_or_hash) : url_or_hash;
  708. id = parts ? String(parts[2]||'') : '';
  709. // Return
  710. return id||false;
  711. };
  712. /**
  713. * History.isTraditionalAnchor
  714. * Checks to see if the url is a traditional anchor or not
  715. * @param {String} url_or_hash
  716. * @return {Boolean}
  717. */
  718. History.isTraditionalAnchor = function(url_or_hash){
  719. // Check
  720. var isTraditional = !(/[\/\?\.]/.test(url_or_hash));
  721. // Return
  722. return isTraditional;
  723. };
  724. /**
  725. * History.extractState
  726. * Get a State by it's URL or Hash
  727. * @param {String} url_or_hash
  728. * @return {State|null}
  729. */
  730. History.extractState = function(url_or_hash,create){
  731. // Prepare
  732. var State = null, id, url;
  733. create = create||false;
  734. // Fetch SUID
  735. id = History.extractId(url_or_hash);
  736. if ( id ) {
  737. State = History.getStateById(id);
  738. }
  739. // Fetch SUID returned no State
  740. if ( !State ) {
  741. // Fetch URL
  742. url = History.getFullUrl(url_or_hash);
  743. // Check URL
  744. id = History.getIdByUrl(url)||false;
  745. if ( id ) {
  746. State = History.getStateById(id);
  747. }
  748. // Create State
  749. if ( !State && create && !History.isTraditionalAnchor(url_or_hash) ) {
  750. State = History.createStateObject(null,null,url);
  751. }
  752. }
  753. // Return
  754. return State;
  755. };
  756. /**
  757. * History.getIdByUrl()
  758. * Get a State ID by a State URL
  759. */
  760. History.getIdByUrl = function(url){
  761. // Fetch
  762. var id = History.urlToId[url] || History.store.urlToId[url] || undefined;
  763. // Return
  764. return id;
  765. };
  766. /**
  767. * History.getLastSavedState()
  768. * Get an object containing the data, title and url of the current state
  769. * @return {Object} State
  770. */
  771. History.getLastSavedState = function(){
  772. return History.savedStates[History.savedStates.length-1]||undefined;
  773. };
  774. /**
  775. * History.getLastStoredState()
  776. * Get an object containing the data, title and url of the current state
  777. * @return {Object} State
  778. */
  779. History.getLastStoredState = function(){
  780. return History.storedStates[History.storedStates.length-1]||undefined;
  781. };
  782. /**
  783. * History.hasUrlDuplicate
  784. * Checks if a Url will have a url conflict
  785. * @param {Object} newState
  786. * @return {Boolean} hasDuplicate
  787. */
  788. History.hasUrlDuplicate = function(newState) {
  789. // Prepare
  790. var hasDuplicate = false,
  791. oldState;
  792. // Fetch
  793. oldState = History.extractState(newState.url);
  794. // Check
  795. hasDuplicate = oldState && oldState.id !== newState.id;
  796. // Return
  797. return hasDuplicate;
  798. };
  799. /**
  800. * History.storeState
  801. * Store a State
  802. * @param {Object} newState
  803. * @return {Object} newState
  804. */
  805. History.storeState = function(newState){
  806. // Store the State
  807. History.urlToId[newState.url] = newState.id;
  808. // Push the State
  809. History.storedStates.push(History.cloneObject(newState));
  810. // Return newState
  811. return newState;
  812. };
  813. /**
  814. * History.isLastSavedState(newState)
  815. * Tests to see if the state is the last state
  816. * @param {Object} newState
  817. * @return {boolean} isLast
  818. */
  819. History.isLastSavedState = function(newState){
  820. // Prepare
  821. var isLast = false,
  822. newId, oldState, oldId;
  823. // Check
  824. if ( History.savedStates.length ) {
  825. newId = newState.id;
  826. oldState = History.getLastSavedState();
  827. oldId = oldState.id;
  828. // Check
  829. isLast = (newId === oldId);
  830. }
  831. // Return
  832. return isLast;
  833. };
  834. /**
  835. * History.saveState
  836. * Push a State
  837. * @param {Object} newState
  838. * @return {boolean} changed
  839. */
  840. History.saveState = function(newState){
  841. // Check Hash
  842. if ( History.isLastSavedState(newState) ) {
  843. return false;
  844. }
  845. // Push the State
  846. History.savedStates.push(History.cloneObject(newState));
  847. // Return true
  848. return true;
  849. };
  850. /**
  851. * History.getStateByIndex()
  852. * Gets a state by the index
  853. * @param {integer} index
  854. * @return {Object}
  855. */
  856. History.getStateByIndex = function(index){
  857. // Prepare
  858. var State = null;
  859. // Handle
  860. if ( typeof index === 'undefined' ) {
  861. // Get the last inserted
  862. State = History.savedStates[History.savedStates.length-1];
  863. }
  864. else if ( index < 0 ) {
  865. // Get from the end
  866. State = History.savedStates[History.savedStates.length+index];
  867. }
  868. else {
  869. // Get from the beginning
  870. State = History.savedStates[index];
  871. }
  872. // Return State
  873. return State;
  874. };
  875. // ====================================================================
  876. // Hash Helpers
  877. /**
  878. * History.getHash()
  879. * Gets the current document hash
  880. * @return {string}
  881. */
  882. History.getHash = function(){
  883. var hash = History.unescapeHash(document.location.hash);
  884. return hash;
  885. };
  886. /**
  887. * History.unescapeString()
  888. * Unescape a string
  889. * @param {String} str
  890. * @return {string}
  891. */
  892. History.unescapeString = function(str){
  893. // Prepare
  894. var result = str,
  895. tmp;
  896. // Unescape hash
  897. while ( true ) {
  898. tmp = window.unescape(result);
  899. if ( tmp === result ) {
  900. break;
  901. }
  902. result = tmp;
  903. }
  904. // Return result
  905. return result;
  906. };
  907. /**
  908. * History.unescapeHash()
  909. * normalize and Unescape a Hash
  910. * @param {String} hash
  911. * @return {string}
  912. */
  913. History.unescapeHash = function(hash){
  914. // Prepare
  915. var result = History.normalizeHash(hash);
  916. // Unescape hash
  917. result = History.unescapeString(result);
  918. // Return result
  919. return result;
  920. };
  921. /**
  922. * History.normalizeHash()
  923. * normalize a hash across browsers
  924. * @return {string}
  925. */
  926. History.normalizeHash = function(hash){
  927. // Prepare
  928. var result = hash.replace(/[^#]*#/,'').replace(/#.*/, '');
  929. // Return result
  930. return result;
  931. };
  932. /**
  933. * History.setHash(hash)
  934. * Sets the document hash
  935. * @param {string} hash
  936. * @return {History}
  937. */
  938. History.setHash = function(hash,queue){
  939. // Prepare
  940. var adjustedHash, State, pageUrl;
  941. // Handle Queueing
  942. if ( queue !== false && History.busy() ) {
  943. // Wait + Push to Queue
  944. //History.debug('History.setHash: we must wait', arguments);
  945. History.pushQueue({
  946. scope: History,
  947. callback: History.setHash,
  948. args: arguments,
  949. queue: queue
  950. });
  951. return false;
  952. }
  953. // Log
  954. //History.debug('History.setHash: called',hash);
  955. // Prepare
  956. adjustedHash = History.escapeHash(hash);
  957. // Make Busy + Continue
  958. History.busy(true);
  959. // Check if hash is a state
  960. State = History.extractState(hash,true);
  961. if ( State && !History.emulated.pushState ) {
  962. // Hash is a state so skip the setHash
  963. //History.debug('History.setHash: Hash is a state so skipping the hash set with a direct pushState call',arguments);
  964. // PushState
  965. History.pushState(State.data,State.title,State.url,false);
  966. }
  967. else if ( document.location.hash !== adjustedHash ) {
  968. // Hash is a proper hash, so apply it
  969. // Handle browser bugs
  970. if ( History.bugs.setHash ) {
  971. // Fix Safari Bug https://bugs.webkit.org/show_bug.cgi?id=56249
  972. // Fetch the base page
  973. pageUrl = History.getPageUrl();
  974. // Safari hash apply
  975. History.pushState(null,null,pageUrl+'#'+adjustedHash,false);
  976. }
  977. else {
  978. // Normal hash apply
  979. document.location.hash = adjustedHash;
  980. }
  981. }
  982. // Chain
  983. return History;
  984. };
  985. /**
  986. * History.escape()
  987. * normalize and Escape a Hash
  988. * @return {string}
  989. */
  990. History.escapeHash = function(hash){
  991. // Prepare
  992. var result = History.normalizeHash(hash);
  993. // Escape hash
  994. result = window.escape(result);
  995. // IE6 Escape Bug
  996. if ( !History.bugs.hashEscape ) {
  997. // Restore common parts
  998. result = result
  999. .replace(/\%21/g,'!')
  1000. .replace(/\%26/g,'&')
  1001. .replace(/\%3D/g,'=')
  1002. .replace(/\%3F/g,'?');
  1003. }
  1004. // Return result
  1005. return result;
  1006. };
  1007. /**
  1008. * History.getHashByUrl(url)
  1009. * Extracts the Hash from a URL
  1010. * @param {string} url
  1011. * @return {string} url
  1012. */
  1013. History.getHashByUrl = function(url){
  1014. // Extract the hash
  1015. var hash = String(url)
  1016. .replace(/([^#]*)#?([^#]*)#?(.*)/, '$2')
  1017. ;
  1018. // Unescape hash
  1019. hash = History.unescapeHash(hash);
  1020. // Return hash
  1021. return hash;
  1022. };
  1023. /**
  1024. * History.setTitle(title)
  1025. * Applies the title to the document
  1026. * @param {State} newState
  1027. * @return {Boolean}
  1028. */
  1029. History.setTitle = function(newState){
  1030. // Prepare
  1031. var title = newState.title,
  1032. firstState;
  1033. // Initial
  1034. if ( !title ) {
  1035. firstState = History.getStateByIndex(0);
  1036. if ( firstState && firstState.url === newState.url ) {
  1037. title = firstState.title||History.options.initialTitle;
  1038. }
  1039. }
  1040. // Apply
  1041. try {
  1042. document.getElementsByTagName('title')[0].innerHTML = title.replace('<','&lt;').replace('>','&gt;').replace(' & ',' &amp; ');
  1043. }
  1044. catch ( Exception ) { }
  1045. document.title = title;
  1046. // Chain
  1047. return History;
  1048. };
  1049. // ====================================================================
  1050. // Queueing
  1051. /**
  1052. * History.queues
  1053. * The list of queues to use
  1054. * First In, First Out
  1055. */
  1056. History.queues = [];
  1057. /**
  1058. * History.busy(value)
  1059. * @param {boolean} value [optional]
  1060. * @return {boolean} busy
  1061. */
  1062. History.busy = function(value){
  1063. // Apply
  1064. if ( typeof value !== 'undefined' ) {
  1065. //History.debug('History.busy: changing ['+(History.busy.flag||false)+'] to ['+(value||false)+']', History.queues.length);
  1066. History.busy.flag = value;
  1067. }
  1068. // Default
  1069. else if ( typeof History.busy.flag === 'undefined' ) {
  1070. History.busy.flag = false;
  1071. }
  1072. // Queue
  1073. if ( !History.busy.flag ) {
  1074. // Execute the next item in the queue
  1075. clearTimeout(History.busy.timeout);
  1076. var fireNext = function(){
  1077. var i, queue, item;
  1078. if ( History.busy.flag ) return;
  1079. for ( i=History.queues.length-1; i >= 0; --i ) {
  1080. queue = History.queues[i];
  1081. if ( queue.length === 0 ) continue;
  1082. item = queue.shift();
  1083. History.fireQueueItem(item);
  1084. History.busy.timeout = setTimeout(fireNext,History.options.busyDelay);
  1085. }
  1086. };
  1087. History.busy.timeout = setTimeout(fireNext,History.options.busyDelay);
  1088. }
  1089. // Return
  1090. return History.busy.flag;
  1091. };
  1092. /**
  1093. * History.busy.flag
  1094. */
  1095. History.busy.flag = false;
  1096. /**
  1097. * History.fireQueueItem(item)
  1098. * Fire a Queue Item
  1099. * @param {Object} item
  1100. * @return {Mixed} result
  1101. */
  1102. History.fireQueueItem = function(item){
  1103. return item.callback.apply(item.scope||History,item.args||[]);
  1104. };
  1105. /**
  1106. * History.pushQueue(callback,args)
  1107. * Add an item to the queue
  1108. * @param {Object} item [scope,callback,args,queue]
  1109. */
  1110. History.pushQueue = function(item){
  1111. // Prepare the queue
  1112. History.queues[item.queue||0] = History.queues[item.queue||0]||[];
  1113. // Add to the queue
  1114. History.queues[item.queue||0].push(item);
  1115. // Chain
  1116. return History;
  1117. };
  1118. /**
  1119. * History.queue (item,queue), (func,queue), (func), (item)
  1120. * Either firs the item now if not busy, or adds it to the queue
  1121. */
  1122. History.queue = function(item,queue){
  1123. // Prepare
  1124. if ( typeof item === 'function' ) {
  1125. item = {
  1126. callback: item
  1127. };
  1128. }
  1129. if ( typeof queue !== 'undefined' ) {
  1130. item.queue = queue;
  1131. }
  1132. // Handle
  1133. if ( History.busy() ) {
  1134. History.pushQueue(item);
  1135. } else {
  1136. History.fireQueueItem(item);
  1137. }
  1138. // Chain
  1139. return History;
  1140. };
  1141. /**
  1142. * History.clearQueue()
  1143. * Clears the Queue
  1144. */
  1145. History.clearQueue = function(){
  1146. History.busy.flag = false;
  1147. History.queues = [];
  1148. return History;
  1149. };
  1150. // ====================================================================
  1151. // IE Bug Fix
  1152. /**
  1153. * History.stateChanged
  1154. * States whether or not the state has changed since the last double check was initialised
  1155. */
  1156. History.stateChanged = false;
  1157. /**
  1158. * History.doubleChecker
  1159. * Contains the timeout used for the double checks
  1160. */
  1161. History.doubleChecker = false;
  1162. /**
  1163. * History.doubleCheckComplete()
  1164. * Complete a double check
  1165. * @return {History}
  1166. */
  1167. History.doubleCheckComplete = function(){
  1168. // Update
  1169. History.stateChanged = true;
  1170. // Clear
  1171. History.doubleCheckClear();
  1172. // Chain
  1173. return History;
  1174. };
  1175. /**
  1176. * History.doubleCheckClear()
  1177. * Clear a double check
  1178. * @return {History}
  1179. */
  1180. History.doubleCheckClear = function(){
  1181. // Clear
  1182. if ( History.doubleChecker ) {
  1183. clearTimeout(History.doubleChecker);
  1184. History.doubleChecker = false;
  1185. }
  1186. // Chain
  1187. return History;
  1188. };
  1189. /**
  1190. * History.doubleCheck()
  1191. * Create a double check
  1192. * @return {History}
  1193. */
  1194. History.doubleCheck = function(tryAgain){
  1195. // Reset
  1196. History.stateChanged = false;
  1197. History.doubleCheckClear();
  1198. // Fix IE6,IE7 bug where calling history.back or history.forward does not actually change the hash (whereas doing it manually does)
  1199. // Fix Safari 5 bug where sometimes the state does not change: https://bugs.webkit.org/show_bug.cgi?id=42940
  1200. if ( History.bugs.ieDoubleCheck ) {
  1201. // Apply Check
  1202. History.doubleChecker = setTimeout(
  1203. function(){
  1204. History.doubleCheckClear();
  1205. if ( !History.stateChanged ) {
  1206. //History.debug('History.doubleCheck: State has not yet changed, trying again', arguments);
  1207. // Re-Attempt
  1208. tryAgain();
  1209. }
  1210. return true;
  1211. },
  1212. History.options.doubleCheckInterval
  1213. );
  1214. }
  1215. // Chain
  1216. return History;
  1217. };
  1218. // ====================================================================
  1219. // Safari Bug Fix
  1220. /**
  1221. * History.safariStatePoll()
  1222. * Poll the current state
  1223. * @return {History}
  1224. */
  1225. History.safariStatePoll = function(){
  1226. // Poll the URL
  1227. // Get the Last State which has the new URL
  1228. var
  1229. urlState = History.extractState(document.location.href),
  1230. newState;
  1231. // Check for a difference
  1232. if ( !History.isLastSavedState(urlState) ) {
  1233. newState = urlState;
  1234. }
  1235. else {
  1236. return;
  1237. }
  1238. // Check if we have a state with that url
  1239. // If not create it
  1240. if ( !newState ) {
  1241. //History.debug('History.safariStatePoll: new');
  1242. newState = History.createStateObject();
  1243. }
  1244. // Apply the New State
  1245. //History.debug('History.safariStatePoll: trigger');
  1246. History.Adapter.trigger(window,'popstate');
  1247. // Chain
  1248. return History;
  1249. };
  1250. // ====================================================================
  1251. // State Aliases
  1252. /**
  1253. * History.back(queue)
  1254. * Send the browser history back one item
  1255. * @param {Integer} queue [optional]
  1256. */
  1257. History.back = function(queue){
  1258. //History.debug('History.back: called', arguments);
  1259. // Handle Queueing
  1260. if ( queue !== false && History.busy() ) {
  1261. // Wait + Push to Queue
  1262. //History.debug('History.back: we must wait', arguments);
  1263. History.pushQueue({
  1264. scope: History,
  1265. callback: History.back,
  1266. args: arguments,
  1267. queue: queue
  1268. });
  1269. return false;
  1270. }
  1271. // Make Busy + Continue
  1272. History.busy(true);
  1273. // Fix certain browser bugs that prevent the state from changing
  1274. History.doubleCheck(function(){
  1275. History.back(false);
  1276. });
  1277. // Go back
  1278. history.go(-1);
  1279. // End back closure
  1280. return true;
  1281. };
  1282. /**
  1283. * History.forward(queue)
  1284. * Send the browser history forward one item
  1285. * @param {Integer} queue [optional]
  1286. */
  1287. History.forward = function(queue){
  1288. //History.debug('History.forward: called', arguments);
  1289. // Handle Queueing
  1290. if ( queue !== false && History.busy() ) {
  1291. // Wait + Push to Queue
  1292. //History.debug('History.forward: we must wait', arguments);
  1293. History.pushQueue({
  1294. scope: History,
  1295. callback: History.forward,
  1296. args: arguments,
  1297. queue: queue
  1298. });
  1299. return false;
  1300. }
  1301. // Make Busy + Continue
  1302. History.busy(true);
  1303. // Fix certain browser bugs that prevent the state from changing
  1304. History.doubleCheck(function(){
  1305. History.forward(false);
  1306. });
  1307. // Go forward
  1308. history.go(1);
  1309. // End forward closure
  1310. return true;
  1311. };
  1312. /**
  1313. * History.go(index,queue)
  1314. * Send the browser history back or forward index times
  1315. * @param {Integer} queue [optional]
  1316. */
  1317. History.go = function(index,queue){
  1318. //History.debug('History.go: called', arguments);
  1319. // Prepare
  1320. var i;
  1321. // Handle
  1322. if ( index > 0 ) {
  1323. // Forward
  1324. for ( i=1; i<=index; ++i ) {
  1325. History.forward(queue);
  1326. }
  1327. }
  1328. else if ( index < 0 ) {
  1329. // Backward
  1330. for ( i=-1; i>=index; --i ) {
  1331. History.back(queue);
  1332. }
  1333. }
  1334. else {
  1335. throw new Error('History.go: History.go requires a positive or negative integer passed.');
  1336. }
  1337. // Chain
  1338. return History;
  1339. };
  1340. // ====================================================================
  1341. // HTML5 State Support
  1342. // Non-Native pushState Implementation
  1343. if ( History.emulated.pushState ) {
  1344. /*
  1345. * Provide Skeleton for HTML4 Browsers
  1346. */
  1347. // Prepare
  1348. var emptyFunction = function(){};
  1349. History.pushState = History.pushState||emptyFunction;
  1350. History.replaceState = History.replaceState||emptyFunction;
  1351. } // History.emulated.pushState
  1352. // Native pushState Implementation
  1353. else {
  1354. /*
  1355. * Use native HTML5 History API Implementation
  1356. */
  1357. /**
  1358. * History.onPopState(event,extra)
  1359. * Refresh the Current State
  1360. */
  1361. History.onPopState = function(event,extra){
  1362. // Prepare
  1363. var stateId = false, newState = false, currentHash, currentState;
  1364. // Reset the double check
  1365. History.doubleCheckComplete();
  1366. // Check for a Hash, and handle apporiatly
  1367. currentHash = History.getHash();
  1368. if ( currentHash ) {
  1369. // Expand Hash
  1370. currentState = History.extractState(currentHash||document.location.href,true);
  1371. if ( currentState ) {
  1372. // We were able to parse it, it must be a State!
  1373. // Let's forward to replaceState
  1374. //History.debug('History.onPopState: state anchor', currentHash, currentState);
  1375. History.replaceState(currentState.data, currentState.title, currentState.url, false);
  1376. }
  1377. else {
  1378. // Traditional Anchor
  1379. //History.debug('History.onPopState: traditional anchor', currentHash);
  1380. History.Adapter.trigger(window,'anchorchange');
  1381. History.busy(false);
  1382. }
  1383. // We don't care for hashes
  1384. History.expectedStateId = false;
  1385. return false;
  1386. }
  1387. // Ensure
  1388. stateId = History.Adapter.extractEventData('state',event,extra) || false;
  1389. // Fetch State
  1390. if ( stateId ) {
  1391. // Vanilla: Back/forward button was used
  1392. newState = History.getStateById(stateId);
  1393. }
  1394. else if ( History.expectedStateId ) {
  1395. // Vanilla: A new state was pushed, and popstate was called manually
  1396. newState = History.getStateById(History.expectedStateId);
  1397. }
  1398. else {
  1399. // Initial State
  1400. newState = History.extractState(document.location.href);
  1401. }
  1402. // The State did not exist in our store
  1403. if ( !newState ) {
  1404. // Regenerate the State
  1405. newState = History.createStateObject(null,null,document.location.href);
  1406. }
  1407. // Clean
  1408. History.expectedStateId = false;
  1409. // Check if we are the same state
  1410. if ( History.isLastSavedState(newState) ) {
  1411. // There has been no change (just the page's hash has finally propagated)
  1412. //History.debug('History.onPopState: no change', newState, History.savedStates);
  1413. History.busy(false);
  1414. return false;
  1415. }
  1416. // Store the State
  1417. History.storeState(newState);
  1418. History.saveState(newState);
  1419. // Force update of the title
  1420. History.setTitle(newState);
  1421. // Fire Our Event
  1422. History.Adapter.trigger(window,'statechange');
  1423. History.busy(false);
  1424. // Return true
  1425. return true;
  1426. };
  1427. History.Adapter.bind(window,'popstate',History.onPopState);
  1428. /**
  1429. * History.pushState(data,title,url)
  1430. * Add a new State to the history object, become it, and trigger onpopstate
  1431. * We have to trigger for HTML4 compatibility
  1432. * @param {object} data
  1433. * @param {string} title
  1434. * @param {string} url
  1435. * @return {true}
  1436. */
  1437. History.pushState = function(data,title,url,queue){
  1438. //History.debug('History.pushState: called', arguments);
  1439. // Check the State
  1440. if ( History.getHashByUrl(url) && History.emulated.pushState ) {
  1441. throw new Error('History.js does not support states with fragement-identifiers (hashes/anchors).');
  1442. }
  1443. // Handle Queueing
  1444. if ( queue !== false && History.busy() ) {
  1445. // Wait + Push to Queue
  1446. //History.debug('History.pushState: we must wait', arguments);
  1447. History.pushQueue({
  1448. scope: History,
  1449. callback: History.pushState,
  1450. args: arguments,
  1451. queue: queue
  1452. });
  1453. return false;
  1454. }
  1455. // Make Busy + Continue
  1456. History.busy(true);
  1457. // Create the newState
  1458. var newState = History.createStateObject(data,title,url);
  1459. // Check it
  1460. if ( History.isLastSavedState(newState) ) {
  1461. // Won't be a change
  1462. History.busy(false);
  1463. }
  1464. else {
  1465. // Store the newState
  1466. History.storeState(newState);
  1467. History.expectedStateId = newState.id;
  1468. // Push the newState
  1469. history.pushState(newState.id,newState.title,newState.url);
  1470. // Fire HTML5 Event
  1471. History.Adapter.trigger(window,'popstate');
  1472. }
  1473. // End pushState closure
  1474. return true;
  1475. };
  1476. /**
  1477. * History.replaceState(data,title,url)
  1478. * Replace the State and trigger onpopstate
  1479. * We have to trigger for HTML4 compatibility
  1480. * @param {object} data
  1481. * @param {string} title
  1482. * @param {string} url
  1483. * @return {true}
  1484. */
  1485. History.replaceState = function(data,title,url,queue){
  1486. //History.debug('History.replaceState: called', arguments);
  1487. // Check the State
  1488. if ( History.getHashByUrl(url) && History.emulated.pushState ) {
  1489. throw new Error('History.js does not support states with fragement-identifiers (hashes/anchors).');
  1490. }
  1491. // Handle Queueing
  1492. if ( queue !== false && History.busy() ) {
  1493. // Wait + Push to Queue
  1494. //History.debug('History.replaceState: we must wait', arguments);
  1495. History.pushQueue({
  1496. scope: History,
  1497. callback: History.replaceState,
  1498. args: arguments,
  1499. queue: queue
  1500. });
  1501. return false;
  1502. }
  1503. // Make Busy + Continue
  1504. History.busy(true);
  1505. // Create the newState
  1506. var newState = History.createStateObject(data,title,url);
  1507. // Check it
  1508. if ( History.isLastSavedState(newState) ) {
  1509. // Won't be a change
  1510. History.busy(false);
  1511. }
  1512. else {
  1513. // Store the newState
  1514. History.storeState(newState);
  1515. History.expectedStateId = newState.id;
  1516. // Push the newState
  1517. history.replaceState(newState.id,newState.title,newState.url);
  1518. // Fire HTML5 Event
  1519. History.Adapter.trigger(window,'popstate');
  1520. }
  1521. // End replaceState closure
  1522. return true;
  1523. };
  1524. } // !History.emulated.pushState
  1525. // ====================================================================
  1526. // Initialise
  1527. /**
  1528. * Load the Store
  1529. */
  1530. if ( sessionStorage ) {
  1531. // Fetch
  1532. try {
  1533. History.store = JSON.parse(sessionStorage.getItem('History.store'))||{};
  1534. }
  1535. catch ( err ) {
  1536. History.store = {};
  1537. }
  1538. // Normalize
  1539. History.normalizeStore();
  1540. }
  1541. else {
  1542. // Default Load
  1543. History.store = {};
  1544. History.normalizeStore();
  1545. }
  1546. /**
  1547. * Clear Intervals on exit to prevent memory leaks
  1548. */
  1549. History.Adapter.bind(window,"beforeunload",History.clearAllIntervals);
  1550. History.Adapter.bind(window,"unload",History.clearAllIntervals);
  1551. /**
  1552. * Create the initial State
  1553. */
  1554. History.saveState(History.storeState(History.extractState(document.location.href,true)));
  1555. /**
  1556. * Bind for Saving Store
  1557. */
  1558. if ( sessionStorage ) {
  1559. // When the page is closed
  1560. History.onUnload = function(){
  1561. // Prepare
  1562. var currentStore, item;
  1563. // Fetch
  1564. try {
  1565. currentStore = JSON.parse(sessionStorage.getItem('History.store'))||{};
  1566. }
  1567. catch ( err ) {
  1568. currentStore = {};
  1569. }
  1570. // Ensure
  1571. currentStore.idToState = currentStore.idToState || {};
  1572. currentStore.urlToId = currentStore.urlToId || {};
  1573. currentStore.stateToId = currentStore.stateToId || {};
  1574. // Sync
  1575. for ( item in History.idToState ) {
  1576. if ( !History.idToState.hasOwnProperty(item) ) {
  1577. continue;
  1578. }
  1579. currentStore.idToState[item] = History.idToState[item];
  1580. }
  1581. for ( item in History.urlToId ) {
  1582. if ( !History.urlToId.hasOwnProperty(item) ) {
  1583. continue;
  1584. }
  1585. currentStore.urlToId[item] = History.urlToId[item];
  1586. }
  1587. for ( item in History.stateToId ) {
  1588. if ( !History.stateToId.hasOwnProperty(item) ) {
  1589. continue;
  1590. }
  1591. currentStore.stateToId[item] = History.stateToId[item];
  1592. }
  1593. // Update
  1594. History.store = currentStore;
  1595. History.normalizeStore();
  1596. // Store
  1597. sessionStorage.setItem('History.store',JSON.stringify(currentStore));
  1598. };
  1599. // For Internet Explorer
  1600. History.intervalList.push(setInterval(History.onUnload,History.options.storeInterval));
  1601. // For Other Browsers
  1602. History.Adapter.bind(window,'beforeunload',History.onUnload);
  1603. History.Adapter.bind(window,'unload',History.onUnload);
  1604. // Both are enabled for consistency
  1605. }
  1606. // Non-Native pushState Implementation
  1607. if ( !History.emulated.pushState ) {
  1608. // Be aware, the following is only for native pushState implementations
  1609. // If you are wanting to include something for all browsers
  1610. // Then include it above this if block
  1611. /**
  1612. * Setup Safari Fix
  1613. */
  1614. if ( History.bugs.safariPoll ) {
  1615. History.intervalList.push(setInterval(History.safariStatePoll, History.options.safariPollInterval));
  1616. }
  1617. /**
  1618. * Ensure Cross Browser Compatibility
  1619. */
  1620. if ( navigator.vendor === 'Apple Computer, Inc.' || (navigator.appCodeName||'') === 'Mozilla' ) {
  1621. /**
  1622. * Fix Safari HashChange Issue
  1623. */
  1624. // Setup Alias
  1625. History.Adapter.bind(window,'hashchange',function(){
  1626. History.Adapter.trigger(window,'popstate');
  1627. });
  1628. // Initialise Alias
  1629. if ( History.getHash() ) {
  1630. History.Adapter.onDomLoad(function(){
  1631. History.Adapter.trigger(window,'hashchange');
  1632. });
  1633. }
  1634. }
  1635. } // !History.emulated.pushState
  1636. }; // History.initCore
  1637. // Try and Initialise History
  1638. History.init();
  1639. })(window);