extjs.history.js 52 KB

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