native.history.js 52 KB

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