historyjs-rails 1.0.0
Sign up to get free protection for your applications and to get access to all the features.
- data/.gitignore +2 -0
- data/.rspec +3 -0
- data/Gemfile +3 -0
- data/LICENSE +9 -0
- data/README.md +84 -0
- data/Rakefile +5 -0
- data/historyjs-rails.gemspec +28 -0
- data/lib/generators/historyjs/install/install_generator.rb +72 -0
- data/lib/historyjs-rails.rb +1 -0
- data/lib/historyjs/rails.rb +10 -0
- data/lib/historyjs/rails/engine.rb +6 -0
- data/lib/historyjs/rails/railtie.rb +6 -0
- data/lib/historyjs/rails/version.rb +6 -0
- data/spec/historyjs/rails/version_spec.rb +6 -0
- data/spec/spec_helper.rb +3 -0
- data/vendor/assets/javascripts/history_adapter_jquery.js +77 -0
- data/vendor/assets/javascripts/history_adapter_jquery.min.js +1 -0
- data/vendor/assets/javascripts/history_adapter_mootools.js +84 -0
- data/vendor/assets/javascripts/history_adapter_mootools.min.js +1 -0
- data/vendor/assets/javascripts/history_adapter_native.js +121 -0
- data/vendor/assets/javascripts/history_adapter_native.min.js +1 -0
- data/vendor/assets/javascripts/history_adapter_right.js +78 -0
- data/vendor/assets/javascripts/history_adapter_right.min.js +1 -0
- data/vendor/assets/javascripts/history_adapter_zepto.js +74 -0
- data/vendor/assets/javascripts/history_adapter_zepto.min.js +1 -0
- data/vendor/assets/javascripts/history_core.js +1943 -0
- data/vendor/assets/javascripts/history_core.min.js +1 -0
- data/vendor/assets/javascripts/history_html4.js +621 -0
- data/vendor/assets/javascripts/history_html4.min.js +1 -0
- data/vendor/assets/javascripts/history_jquery.js +4 -0
- data/vendor/assets/javascripts/history_jquery.min.js +4 -0
- data/vendor/assets/javascripts/history_jquery_html5.js +3 -0
- data/vendor/assets/javascripts/history_jquery_html5.min.js +3 -0
- data/vendor/assets/javascripts/history_mootools.js +4 -0
- data/vendor/assets/javascripts/history_mootools.min.js +4 -0
- data/vendor/assets/javascripts/history_mootools_html5.js +3 -0
- data/vendor/assets/javascripts/history_mootools_html5.min.js +3 -0
- data/vendor/assets/javascripts/history_native.js +4 -0
- data/vendor/assets/javascripts/history_native.min.js +4 -0
- data/vendor/assets/javascripts/history_native_html5.js +3 -0
- data/vendor/assets/javascripts/history_native_html5.min.js +3 -0
- data/vendor/assets/javascripts/history_right.js +4 -0
- data/vendor/assets/javascripts/history_right.min.js +4 -0
- data/vendor/assets/javascripts/history_right_html5.js +3 -0
- data/vendor/assets/javascripts/history_right_html5.min.js +3 -0
- data/vendor/assets/javascripts/history_zepto.js +4 -0
- data/vendor/assets/javascripts/history_zepto.min.js +4 -0
- data/vendor/assets/javascripts/history_zepto_html5.js +3 -0
- data/vendor/assets/javascripts/history_zepto_html5.min.js +3 -0
- data/vendor/assets/javascripts/json2.js +480 -0
- data/vendor/assets/javascripts/json2.min.js +1 -0
- metadata +117 -0
@@ -0,0 +1 @@
|
|
1
|
+
(function(a,b){"use strict";var c=a.console||b,d=a.document,e=a.navigator,f=a.sessionStorage||!1,g=a.setTimeout,h=a.clearTimeout,i=a.setInterval,j=a.clearInterval,k=a.JSON,l=a.alert,m=a.History=a.History||{},n=a.history;k.stringify=k.stringify||k.encode,k.parse=k.parse||k.decode;if(typeof m.init!="undefined")throw new Error("History.js Core has already been loaded...");m.init=function(){return typeof m.Adapter=="undefined"?!1:(typeof m.initCore!="undefined"&&m.initCore(),typeof m.initHtml4!="undefined"&&m.initHtml4(),!0)},m.initCore=function(){if(typeof m.initCore.initialized!="undefined")return!1;m.initCore.initialized=!0,m.options=m.options||{},m.options.hashChangeInterval=m.options.hashChangeInterval||100,m.options.safariPollInterval=m.options.safariPollInterval||500,m.options.doubleCheckInterval=m.options.doubleCheckInterval||500,m.options.storeInterval=m.options.storeInterval||1e3,m.options.busyDelay=m.options.busyDelay||250,m.options.debug=m.options.debug||!1,m.options.initialTitle=m.options.initialTitle||d.title,m.intervalList=[],m.clearAllIntervals=function(){var a,b=m.intervalList;if(typeof b!="undefined"&&b!==null){for(a=0;a<b.length;a++)j(b[a]);m.intervalList=null}},m.debug=function(){(m.options.debug||!1)&&m.log.apply(m,arguments)},m.log=function(){var a=typeof c!="undefined"&&typeof c.log!="undefined"&&typeof c.log.apply!="undefined",b=d.getElementById("log"),e,f,g,h,i;a?(h=Array.prototype.slice.call(arguments),e=h.shift(),typeof c.debug!="undefined"?c.debug.apply(c,[e,h]):c.log.apply(c,[e,h])):e="\n"+arguments[0]+"\n";for(f=1,g=arguments.length;f<g;++f){i=arguments[f];if(typeof i=="object"&&typeof k!="undefined")try{i=k.stringify(i)}catch(j){}e+="\n"+i+"\n"}return b?(b.value+=e+"\n-----\n",b.scrollTop=b.scrollHeight-b.clientHeight):a||l(e),!0},m.getInternetExplorerMajorVersion=function(){var a=m.getInternetExplorerMajorVersion.cached=typeof m.getInternetExplorerMajorVersion.cached!="undefined"?m.getInternetExplorerMajorVersion.cached:function(){var a=3,b=d.createElement("div"),c=b.getElementsByTagName("i");while((b.innerHTML="<!--[if gt IE "+ ++a+"]><i></i><![endif]-->")&&c[0]);return a>4?a:!1}();return a},m.isInternetExplorer=function(){var a=m.isInternetExplorer.cached=typeof m.isInternetExplorer.cached!="undefined"?m.isInternetExplorer.cached:Boolean(m.getInternetExplorerMajorVersion());return a},m.emulated={pushState:!Boolean(a.history&&a.history.pushState&&a.history.replaceState&&!/ Mobile\/([1-7][a-z]|(8([abcde]|f(1[0-8]))))/i.test(e.userAgent)&&!/AppleWebKit\/5([0-2]|3[0-2])/i.test(e.userAgent)),hashChange:Boolean(!("onhashchange"in a||"onhashchange"in d)||m.isInternetExplorer()&&m.getInternetExplorerMajorVersion()<8)},m.enabled=!m.emulated.pushState,m.bugs={setHash:Boolean(!m.emulated.pushState&&e.vendor==="Apple Computer, Inc."&&/AppleWebKit\/5([0-2]|3[0-3])/.test(e.userAgent)),safariPoll:Boolean(!m.emulated.pushState&&e.vendor==="Apple Computer, Inc."&&/AppleWebKit\/5([0-2]|3[0-3])/.test(e.userAgent)),ieDoubleCheck:Boolean(m.isInternetExplorer()&&m.getInternetExplorerMajorVersion()<8),hashEscape:Boolean(m.isInternetExplorer()&&m.getInternetExplorerMajorVersion()<7)},m.isEmptyObject=function(a){for(var b in a)return!1;return!0},m.cloneObject=function(a){var b,c;return a?(b=k.stringify(a),c=k.parse(b)):c={},c},m.getRootUrl=function(){var a=d.location.protocol+"//"+(d.location.hostname||d.location.host);if(d.location.port||!1)a+=":"+d.location.port;return a+="/",a},m.getBaseHref=function(){var a=d.getElementsByTagName("base"),b=null,c="";return a.length===1&&(b=a[0],c=b.href.replace(/[^\/]+$/,"")),c=c.replace(/\/+$/,""),c&&(c+="/"),c},m.getBaseUrl=function(){var a=m.getBaseHref()||m.getBasePageUrl()||m.getRootUrl();return a},m.getPageUrl=function(){var a=m.getState(!1,!1),b=(a||{}).url||d.location.href,c;return c=b.replace(/\/+$/,"").replace(/[^\/]+$/,function(a,b,c){return/\./.test(a)?a:a+"/"}),c},m.getBasePageUrl=function(){var a=d.location.href.replace(/[#\?].*/,"").replace(/[^\/]+$/,function(a,b,c){return/[^\/]$/.test(a)?"":a}).replace(/\/+$/,"")+"/";return a},m.getFullUrl=function(a,b){var c=a,d=a.substring(0,1);return b=typeof b=="undefined"?!0:b,/[a-z]+\:\/\//.test(a)||(d==="/"?c=m.getRootUrl()+a.replace(/^\/+/,""):d==="#"?c=m.getPageUrl().replace(/#.*/,"")+a:d==="?"?c=m.getPageUrl().replace(/[\?#].*/,"")+a:b?c=m.getBaseUrl()+a.replace(/^(\.\/)+/,""):c=m.getBasePageUrl()+a.replace(/^(\.\/)+/,"")),c.replace(/\#$/,"")},m.getShortUrl=function(a){var b=a,c=m.getBaseUrl(),d=m.getRootUrl();return m.emulated.pushState&&(b=b.replace(c,"")),b=b.replace(d,"/"),m.isTraditionalAnchor(b)&&(b="./"+b),b=b.replace(/^(\.\/)+/g,"./").replace(/\#$/,""),b},m.store={},m.idToState=m.idToState||{},m.stateToId=m.stateToId||{},m.urlToId=m.urlToId||{},m.storedStates=m.storedStates||[],m.savedStates=m.savedStates||[],m.normalizeStore=function(){m.store.idToState=m.store.idToState||{},m.store.urlToId=m.store.urlToId||{},m.store.stateToId=m.store.stateToId||{}},m.getState=function(a,b){typeof a=="undefined"&&(a=!0),typeof b=="undefined"&&(b=!0);var c=m.getLastSavedState();return!c&&b&&(c=m.createStateObject()),a&&(c=m.cloneObject(c),c.url=c.cleanUrl||c.url),c},m.getIdByState=function(a){var b=m.extractId(a.url),c;if(!b){c=m.getStateString(a);if(typeof m.stateToId[c]!="undefined")b=m.stateToId[c];else if(typeof m.store.stateToId[c]!="undefined")b=m.store.stateToId[c];else{for(;;){b=(new Date).getTime()+String(Math.random()).replace(/\D/g,"");if(typeof m.idToState[b]=="undefined"&&typeof m.store.idToState[b]=="undefined")break}m.stateToId[c]=b,m.idToState[b]=a}}return b},m.normalizeState=function(a){var b,c;if(!a||typeof a!="object")a={};if(typeof a.normalized!="undefined")return a;if(!a.data||typeof a.data!="object")a.data={};b={},b.normalized=!0,b.title=a.title||"",b.url=m.getFullUrl(m.unescapeString(a.url||d.location.href)),b.hash=m.getShortUrl(b.url),b.data=m.cloneObject(a.data),b.id=m.getIdByState(b),b.cleanUrl=b.url.replace(/\??\&_suid.*/,""),b.url=b.cleanUrl,c=!m.isEmptyObject(b.data);if(b.title||c)b.hash=m.getShortUrl(b.url).replace(/\??\&_suid.*/,""),/\?/.test(b.hash)||(b.hash+="?"),b.hash+="&_suid="+b.id;return b.hashedUrl=m.getFullUrl(b.hash),(m.emulated.pushState||m.bugs.safariPoll)&&m.hasUrlDuplicate(b)&&(b.url=b.hashedUrl),b},m.createStateObject=function(a,b,c){var d={data:a,title:b,url:c};return d=m.normalizeState(d),d},m.getStateById=function(a){a=String(a);var c=m.idToState[a]||m.store.idToState[a]||b;return c},m.getStateString=function(a){var b,c,d;return b=m.normalizeState(a),c={data:b.data,title:a.title,url:a.url},d=k.stringify(c),d},m.getStateId=function(a){var b,c;return b=m.normalizeState(a),c=b.id,c},m.getHashByState=function(a){var b,c;return b=m.normalizeState(a),c=b.hash,c},m.extractId=function(a){var b,c,d;return c=/(.*)\&_suid=([0-9]+)$/.exec(a),d=c?c[1]||a:a,b=c?String(c[2]||""):"",b||!1},m.isTraditionalAnchor=function(a){var b=!/[\/\?\.]/.test(a);return b},m.extractState=function(a,b){var c=null,d,e;return b=b||!1,d=m.extractId(a),d&&(c=m.getStateById(d)),c||(e=m.getFullUrl(a),d=m.getIdByUrl(e)||!1,d&&(c=m.getStateById(d)),!c&&b&&!m.isTraditionalAnchor(a)&&(c=m.createStateObject(null,null,e))),c},m.getIdByUrl=function(a){var c=m.urlToId[a]||m.store.urlToId[a]||b;return c},m.getLastSavedState=function(){return m.savedStates[m.savedStates.length-1]||b},m.getLastStoredState=function(){return m.storedStates[m.storedStates.length-1]||b},m.hasUrlDuplicate=function(a){var b=!1,c;return c=m.extractState(a.url),b=c&&c.id!==a.id,b},m.storeState=function(a){return m.urlToId[a.url]=a.id,m.storedStates.push(m.cloneObject(a)),a},m.isLastSavedState=function(a){var b=!1,c,d,e;return m.savedStates.length&&(c=a.id,d=m.getLastSavedState(),e=d.id,b=c===e),b},m.saveState=function(a){return m.isLastSavedState(a)?!1:(m.savedStates.push(m.cloneObject(a)),!0)},m.getStateByIndex=function(a){var b=null;return typeof a=="undefined"?b=m.savedStates[m.savedStates.length-1]:a<0?b=m.savedStates[m.savedStates.length+a]:b=m.savedStates[a],b},m.getHash=function(){var a=m.unescapeHash(d.location.hash);return a},m.unescapeString=function(b){var c=b,d;for(;;){d=a.unescape(c);if(d===c)break;c=d}return c},m.unescapeHash=function(a){var b=m.normalizeHash(a);return b=m.unescapeString(b),b},m.normalizeHash=function(a){var b=a.replace(/[^#]*#/,"").replace(/#.*/,"");return b},m.setHash=function(a,b){var c,e,f;return b!==!1&&m.busy()?(m.pushQueue({scope:m,callback:m.setHash,args:arguments,queue:b}),!1):(c=m.escapeHash(a),m.busy(!0),e=m.extractState(a,!0),e&&!m.emulated.pushState?m.pushState(e.data,e.title,e.url,!1):d.location.hash!==c&&(m.bugs.setHash?(f=m.getPageUrl(),m.pushState(null,null,f+"#"+c,!1)):d.location.hash=c),m)},m.escapeHash=function(b){var c=m.normalizeHash(b);return c=a.escape(c),m.bugs.hashEscape||(c=c.replace(/\%21/g,"!").replace(/\%26/g,"&").replace(/\%3D/g,"=").replace(/\%3F/g,"?")),c},m.getHashByUrl=function(a){var b=String(a).replace(/([^#]*)#?([^#]*)#?(.*)/,"$2");return b=m.unescapeHash(b),b},m.setTitle=function(a){var b=a.title,c;b||(c=m.getStateByIndex(0),c&&c.url===a.url&&(b=c.title||m.options.initialTitle));try{d.getElementsByTagName("title")[0].innerHTML=b.replace("<","<").replace(">",">").replace(" & "," & ")}catch(e){}return d.title=b,m},m.queues=[],m.busy=function(a){typeof a!="undefined"?m.busy.flag=a:typeof m.busy.flag=="undefined"&&(m.busy.flag=!1);if(!m.busy.flag){h(m.busy.timeout);var b=function(){var a,c,d;if(m.busy.flag)return;for(a=m.queues.length-1;a>=0;--a){c=m.queues[a];if(c.length===0)continue;d=c.shift(),m.fireQueueItem(d),m.busy.timeout=g(b,m.options.busyDelay)}};m.busy.timeout=g(b,m.options.busyDelay)}return m.busy.flag},m.busy.flag=!1,m.fireQueueItem=function(a){return a.callback.apply(a.scope||m,a.args||[])},m.pushQueue=function(a){return m.queues[a.queue||0]=m.queues[a.queue||0]||[],m.queues[a.queue||0].push(a),m},m.queue=function(a,b){return typeof a=="function"&&(a={callback:a}),typeof b!="undefined"&&(a.queue=b),m.busy()?m.pushQueue(a):m.fireQueueItem(a),m},m.clearQueue=function(){return m.busy.flag=!1,m.queues=[],m},m.stateChanged=!1,m.doubleChecker=!1,m.doubleCheckComplete=function(){return m.stateChanged=!0,m.doubleCheckClear(),m},m.doubleCheckClear=function(){return m.doubleChecker&&(h(m.doubleChecker),m.doubleChecker=!1),m},m.doubleCheck=function(a){return m.stateChanged=!1,m.doubleCheckClear(),m.bugs.ieDoubleCheck&&(m.doubleChecker=g(function(){return m.doubleCheckClear(),m.stateChanged||a(),!0},m.options.doubleCheckInterval)),m},m.safariStatePoll=function(){var b=m.extractState(d.location.href),c;if(!m.isLastSavedState(b))c=b;else return;return c||(c=m.createStateObject()),m.Adapter.trigger(a,"popstate"),m},m.back=function(a){return a!==!1&&m.busy()?(m.pushQueue({scope:m,callback:m.back,args:arguments,queue:a}),!1):(m.busy(!0),m.doubleCheck(function(){m.back(!1)}),n.go(-1),!0)},m.forward=function(a){return a!==!1&&m.busy()?(m.pushQueue({scope:m,callback:m.forward,args:arguments,queue:a}),!1):(m.busy(!0),m.doubleCheck(function(){m.forward(!1)}),n.go(1),!0)},m.go=function(a,b){var c;if(a>0)for(c=1;c<=a;++c)m.forward(b);else{if(!(a<0))throw new Error("History.go: History.go requires a positive or negative integer passed.");for(c=-1;c>=a;--c)m.back(b)}return m};if(m.emulated.pushState){var o=function(){};m.pushState=m.pushState||o,m.replaceState=m.replaceState||o}else m.onPopState=function(b,c){var e=!1,f=!1,g,h;return m.doubleCheckComplete(),g=m.getHash(),g?(h=m.extractState(g||d.location.href,!0),h?m.replaceState(h.data,h.title,h.url,!1):(m.Adapter.trigger(a,"anchorchange"),m.busy(!1)),m.expectedStateId=!1,!1):(e=m.Adapter.extractEventData("state",b,c)||!1,e?f=m.getStateById(e):m.expectedStateId?f=m.getStateById(m.expectedStateId):f=m.extractState(d.location.href),f||(f=m.createStateObject(null,null,d.location.href)),m.expectedStateId=!1,m.isLastSavedState(f)?(m.busy(!1),!1):(m.storeState(f),m.saveState(f),m.setTitle(f),m.Adapter.trigger(a,"statechange"),m.busy(!1),!0))},m.Adapter.bind(a,"popstate",m.onPopState),m.pushState=function(b,c,d,e){if(m.getHashByUrl(d)&&m.emulated.pushState)throw new Error("History.js does not support states with fragement-identifiers (hashes/anchors).");if(e!==!1&&m.busy())return m.pushQueue({scope:m,callback:m.pushState,args:arguments,queue:e}),!1;m.busy(!0);var f=m.createStateObject(b,c,d);return m.isLastSavedState(f)?m.busy(!1):(m.storeState(f),m.expectedStateId=f.id,n.pushState(f.id,f.title,f.url),m.Adapter.trigger(a,"popstate")),!0},m.replaceState=function(b,c,d,e){if(m.getHashByUrl(d)&&m.emulated.pushState)throw new Error("History.js does not support states with fragement-identifiers (hashes/anchors).");if(e!==!1&&m.busy())return m.pushQueue({scope:m,callback:m.replaceState,args:arguments,queue:e}),!1;m.busy(!0);var f=m.createStateObject(b,c,d);return m.isLastSavedState(f)?m.busy(!1):(m.storeState(f),m.expectedStateId=f.id,n.replaceState(f.id,f.title,f.url),m.Adapter.trigger(a,"popstate")),!0};if(f){try{m.store=k.parse(f.getItem("History.store"))||{}}catch(p){m.store={}}m.normalizeStore()}else m.store={},m.normalizeStore();m.Adapter.bind(a,"beforeunload",m.clearAllIntervals),m.Adapter.bind(a,"unload",m.clearAllIntervals),m.saveState(m.storeState(m.extractState(d.location.href,!0))),f&&(m.onUnload=function(){var a,b;try{a=k.parse(f.getItem("History.store"))||{}}catch(c){a={}}a.idToState=a.idToState||{},a.urlToId=a.urlToId||{},a.stateToId=a.stateToId||{};for(b in m.idToState){if(!m.idToState.hasOwnProperty(b))continue;a.idToState[b]=m.idToState[b]}for(b in m.urlToId){if(!m.urlToId.hasOwnProperty(b))continue;a.urlToId[b]=m.urlToId[b]}for(b in m.stateToId){if(!m.stateToId.hasOwnProperty(b))continue;a.stateToId[b]=m.stateToId[b]}m.store=a,m.normalizeStore(),f.setItem("History.store",k.stringify(a))},m.intervalList.push(i(m.onUnload,m.options.storeInterval)),m.Adapter.bind(a,"beforeunload",m.onUnload),m.Adapter.bind(a,"unload",m.onUnload));if(!m.emulated.pushState){m.bugs.safariPoll&&m.intervalList.push(i(m.safariStatePoll,m.options.safariPollInterval));if(e.vendor==="Apple Computer, Inc."||(e.appCodeName||"")==="Mozilla")m.Adapter.bind(a,"hashchange",function(){m.Adapter.trigger(a,"popstate")}),m.getHash()&&m.Adapter.onDomLoad(function(){m.Adapter.trigger(a,"hashchange")})}},m.init()})(window)
|
@@ -0,0 +1,621 @@
|
|
1
|
+
/**
|
2
|
+
* History.js HTML4 Support
|
3
|
+
* Depends on the HTML5 Support
|
4
|
+
* @author Benjamin Arthur Lupton <contact@balupton.com>
|
5
|
+
* @copyright 2010-2011 Benjamin Arthur Lupton <contact@balupton.com>
|
6
|
+
* @license New BSD License <http://creativecommons.org/licenses/BSD/>
|
7
|
+
*/
|
8
|
+
|
9
|
+
(function(window,undefined){
|
10
|
+
"use strict";
|
11
|
+
|
12
|
+
// ========================================================================
|
13
|
+
// Initialise
|
14
|
+
|
15
|
+
// Localise Globals
|
16
|
+
var
|
17
|
+
document = window.document, // Make sure we are using the correct document
|
18
|
+
setTimeout = window.setTimeout||setTimeout,
|
19
|
+
clearTimeout = window.clearTimeout||clearTimeout,
|
20
|
+
setInterval = window.setInterval||setInterval,
|
21
|
+
History = window.History = window.History||{}; // Public History Object
|
22
|
+
|
23
|
+
// Check Existence
|
24
|
+
if ( typeof History.initHtml4 !== 'undefined' ) {
|
25
|
+
throw new Error('History.js HTML4 Support has already been loaded...');
|
26
|
+
}
|
27
|
+
|
28
|
+
|
29
|
+
// ========================================================================
|
30
|
+
// Initialise HTML4 Support
|
31
|
+
|
32
|
+
// Initialise HTML4 Support
|
33
|
+
History.initHtml4 = function(){
|
34
|
+
// Initialise
|
35
|
+
if ( typeof History.initHtml4.initialized !== 'undefined' ) {
|
36
|
+
// Already Loaded
|
37
|
+
return false;
|
38
|
+
}
|
39
|
+
else {
|
40
|
+
History.initHtml4.initialized = true;
|
41
|
+
}
|
42
|
+
|
43
|
+
|
44
|
+
// ====================================================================
|
45
|
+
// Properties
|
46
|
+
|
47
|
+
/**
|
48
|
+
* History.enabled
|
49
|
+
* Is History enabled?
|
50
|
+
*/
|
51
|
+
History.enabled = true;
|
52
|
+
|
53
|
+
|
54
|
+
// ====================================================================
|
55
|
+
// Hash Storage
|
56
|
+
|
57
|
+
/**
|
58
|
+
* History.savedHashes
|
59
|
+
* Store the hashes in an array
|
60
|
+
*/
|
61
|
+
History.savedHashes = [];
|
62
|
+
|
63
|
+
/**
|
64
|
+
* History.isLastHash(newHash)
|
65
|
+
* Checks if the hash is the last hash
|
66
|
+
* @param {string} newHash
|
67
|
+
* @return {boolean} true
|
68
|
+
*/
|
69
|
+
History.isLastHash = function(newHash){
|
70
|
+
// Prepare
|
71
|
+
var oldHash = History.getHashByIndex(),
|
72
|
+
isLast;
|
73
|
+
|
74
|
+
// Check
|
75
|
+
isLast = newHash === oldHash;
|
76
|
+
|
77
|
+
// Return isLast
|
78
|
+
return isLast;
|
79
|
+
};
|
80
|
+
|
81
|
+
/**
|
82
|
+
* History.saveHash(newHash)
|
83
|
+
* Push a Hash
|
84
|
+
* @param {string} newHash
|
85
|
+
* @return {boolean} true
|
86
|
+
*/
|
87
|
+
History.saveHash = function(newHash){
|
88
|
+
// Check Hash
|
89
|
+
if ( History.isLastHash(newHash) ) {
|
90
|
+
return false;
|
91
|
+
}
|
92
|
+
|
93
|
+
// Push the Hash
|
94
|
+
History.savedHashes.push(newHash);
|
95
|
+
|
96
|
+
// Return true
|
97
|
+
return true;
|
98
|
+
};
|
99
|
+
|
100
|
+
/**
|
101
|
+
* History.getHashByIndex()
|
102
|
+
* Gets a hash by the index
|
103
|
+
* @param {integer} index
|
104
|
+
* @return {string}
|
105
|
+
*/
|
106
|
+
History.getHashByIndex = function(index){
|
107
|
+
// Prepare
|
108
|
+
var hash = null;
|
109
|
+
|
110
|
+
// Handle
|
111
|
+
if ( typeof index === 'undefined' ) {
|
112
|
+
// Get the last inserted
|
113
|
+
hash = History.savedHashes[History.savedHashes.length-1];
|
114
|
+
}
|
115
|
+
else if ( index < 0 ) {
|
116
|
+
// Get from the end
|
117
|
+
hash = History.savedHashes[History.savedHashes.length+index];
|
118
|
+
}
|
119
|
+
else {
|
120
|
+
// Get from the beginning
|
121
|
+
hash = History.savedHashes[index];
|
122
|
+
}
|
123
|
+
|
124
|
+
// Return hash
|
125
|
+
return hash;
|
126
|
+
};
|
127
|
+
|
128
|
+
|
129
|
+
// ====================================================================
|
130
|
+
// Discarded States
|
131
|
+
|
132
|
+
/**
|
133
|
+
* History.discardedHashes
|
134
|
+
* A hashed array of discarded hashes
|
135
|
+
*/
|
136
|
+
History.discardedHashes = {};
|
137
|
+
|
138
|
+
/**
|
139
|
+
* History.discardedStates
|
140
|
+
* A hashed array of discarded states
|
141
|
+
*/
|
142
|
+
History.discardedStates = {};
|
143
|
+
|
144
|
+
/**
|
145
|
+
* History.discardState(State)
|
146
|
+
* Discards the state by ignoring it through History
|
147
|
+
* @param {object} State
|
148
|
+
* @return {true}
|
149
|
+
*/
|
150
|
+
History.discardState = function(discardedState,forwardState,backState){
|
151
|
+
//History.debug('History.discardState', arguments);
|
152
|
+
// Prepare
|
153
|
+
var discardedStateHash = History.getHashByState(discardedState),
|
154
|
+
discardObject;
|
155
|
+
|
156
|
+
// Create Discard Object
|
157
|
+
discardObject = {
|
158
|
+
'discardedState': discardedState,
|
159
|
+
'backState': backState,
|
160
|
+
'forwardState': forwardState
|
161
|
+
};
|
162
|
+
|
163
|
+
// Add to DiscardedStates
|
164
|
+
History.discardedStates[discardedStateHash] = discardObject;
|
165
|
+
|
166
|
+
// Return true
|
167
|
+
return true;
|
168
|
+
};
|
169
|
+
|
170
|
+
/**
|
171
|
+
* History.discardHash(hash)
|
172
|
+
* Discards the hash by ignoring it through History
|
173
|
+
* @param {string} hash
|
174
|
+
* @return {true}
|
175
|
+
*/
|
176
|
+
History.discardHash = function(discardedHash,forwardState,backState){
|
177
|
+
//History.debug('History.discardState', arguments);
|
178
|
+
// Create Discard Object
|
179
|
+
var discardObject = {
|
180
|
+
'discardedHash': discardedHash,
|
181
|
+
'backState': backState,
|
182
|
+
'forwardState': forwardState
|
183
|
+
};
|
184
|
+
|
185
|
+
// Add to discardedHash
|
186
|
+
History.discardedHashes[discardedHash] = discardObject;
|
187
|
+
|
188
|
+
// Return true
|
189
|
+
return true;
|
190
|
+
};
|
191
|
+
|
192
|
+
/**
|
193
|
+
* History.discardState(State)
|
194
|
+
* Checks to see if the state is discarded
|
195
|
+
* @param {object} State
|
196
|
+
* @return {bool}
|
197
|
+
*/
|
198
|
+
History.discardedState = function(State){
|
199
|
+
// Prepare
|
200
|
+
var StateHash = History.getHashByState(State),
|
201
|
+
discarded;
|
202
|
+
|
203
|
+
// Check
|
204
|
+
discarded = History.discardedStates[StateHash]||false;
|
205
|
+
|
206
|
+
// Return true
|
207
|
+
return discarded;
|
208
|
+
};
|
209
|
+
|
210
|
+
/**
|
211
|
+
* History.discardedHash(hash)
|
212
|
+
* Checks to see if the state is discarded
|
213
|
+
* @param {string} State
|
214
|
+
* @return {bool}
|
215
|
+
*/
|
216
|
+
History.discardedHash = function(hash){
|
217
|
+
// Check
|
218
|
+
var discarded = History.discardedHashes[hash]||false;
|
219
|
+
|
220
|
+
// Return true
|
221
|
+
return discarded;
|
222
|
+
};
|
223
|
+
|
224
|
+
/**
|
225
|
+
* History.recycleState(State)
|
226
|
+
* Allows a discarded state to be used again
|
227
|
+
* @param {object} data
|
228
|
+
* @param {string} title
|
229
|
+
* @param {string} url
|
230
|
+
* @return {true}
|
231
|
+
*/
|
232
|
+
History.recycleState = function(State){
|
233
|
+
//History.debug('History.recycleState', arguments);
|
234
|
+
// Prepare
|
235
|
+
var StateHash = History.getHashByState(State);
|
236
|
+
|
237
|
+
// Remove from DiscardedStates
|
238
|
+
if ( History.discardedState(State) ) {
|
239
|
+
delete History.discardedStates[StateHash];
|
240
|
+
}
|
241
|
+
|
242
|
+
// Return true
|
243
|
+
return true;
|
244
|
+
};
|
245
|
+
|
246
|
+
|
247
|
+
// ====================================================================
|
248
|
+
// HTML4 HashChange Support
|
249
|
+
|
250
|
+
if ( History.emulated.hashChange ) {
|
251
|
+
/*
|
252
|
+
* We must emulate the HTML4 HashChange Support by manually checking for hash changes
|
253
|
+
*/
|
254
|
+
|
255
|
+
/**
|
256
|
+
* History.hashChangeInit()
|
257
|
+
* Init the HashChange Emulation
|
258
|
+
*/
|
259
|
+
History.hashChangeInit = function(){
|
260
|
+
// Define our Checker Function
|
261
|
+
History.checkerFunction = null;
|
262
|
+
|
263
|
+
// Define some variables that will help in our checker function
|
264
|
+
var lastDocumentHash = '',
|
265
|
+
iframeId, iframe,
|
266
|
+
lastIframeHash, checkerRunning;
|
267
|
+
|
268
|
+
// Handle depending on the browser
|
269
|
+
if ( History.isInternetExplorer() ) {
|
270
|
+
// IE6 and IE7
|
271
|
+
// We need to use an iframe to emulate the back and forward buttons
|
272
|
+
|
273
|
+
// Create iFrame
|
274
|
+
iframeId = 'historyjs-iframe';
|
275
|
+
iframe = document.createElement('iframe');
|
276
|
+
|
277
|
+
// Adjust iFarme
|
278
|
+
iframe.setAttribute('id', iframeId);
|
279
|
+
iframe.style.display = 'none';
|
280
|
+
|
281
|
+
// Append iFrame
|
282
|
+
document.body.appendChild(iframe);
|
283
|
+
|
284
|
+
// Create initial history entry
|
285
|
+
iframe.contentWindow.document.open();
|
286
|
+
iframe.contentWindow.document.close();
|
287
|
+
|
288
|
+
// Define some variables that will help in our checker function
|
289
|
+
lastIframeHash = '';
|
290
|
+
checkerRunning = false;
|
291
|
+
|
292
|
+
// Define the checker function
|
293
|
+
History.checkerFunction = function(){
|
294
|
+
// Check Running
|
295
|
+
if ( checkerRunning ) {
|
296
|
+
return false;
|
297
|
+
}
|
298
|
+
|
299
|
+
// Update Running
|
300
|
+
checkerRunning = true;
|
301
|
+
|
302
|
+
// Fetch
|
303
|
+
var documentHash = History.getHash()||'',
|
304
|
+
iframeHash = History.unescapeHash(iframe.contentWindow.document.location.hash)||'';
|
305
|
+
|
306
|
+
// The Document Hash has changed (application caused)
|
307
|
+
if ( documentHash !== lastDocumentHash ) {
|
308
|
+
// Equalise
|
309
|
+
lastDocumentHash = documentHash;
|
310
|
+
|
311
|
+
// Create a history entry in the iframe
|
312
|
+
if ( iframeHash !== documentHash ) {
|
313
|
+
//History.debug('hashchange.checker: iframe hash change', 'documentHash (new):', documentHash, 'iframeHash (old):', iframeHash);
|
314
|
+
|
315
|
+
// Equalise
|
316
|
+
lastIframeHash = iframeHash = documentHash;
|
317
|
+
|
318
|
+
// Create History Entry
|
319
|
+
iframe.contentWindow.document.open();
|
320
|
+
iframe.contentWindow.document.close();
|
321
|
+
|
322
|
+
// Update the iframe's hash
|
323
|
+
iframe.contentWindow.document.location.hash = History.escapeHash(documentHash);
|
324
|
+
}
|
325
|
+
|
326
|
+
// Trigger Hashchange Event
|
327
|
+
History.Adapter.trigger(window,'hashchange');
|
328
|
+
}
|
329
|
+
|
330
|
+
// The iFrame Hash has changed (back button caused)
|
331
|
+
else if ( iframeHash !== lastIframeHash ) {
|
332
|
+
//History.debug('hashchange.checker: iframe hash out of sync', 'iframeHash (new):', iframeHash, 'documentHash (old):', documentHash);
|
333
|
+
|
334
|
+
// Equalise
|
335
|
+
lastIframeHash = iframeHash;
|
336
|
+
|
337
|
+
// Update the Hash
|
338
|
+
History.setHash(iframeHash,false);
|
339
|
+
}
|
340
|
+
|
341
|
+
// Reset Running
|
342
|
+
checkerRunning = false;
|
343
|
+
|
344
|
+
// Return true
|
345
|
+
return true;
|
346
|
+
};
|
347
|
+
}
|
348
|
+
else {
|
349
|
+
// We are not IE
|
350
|
+
// Firefox 1 or 2, Opera
|
351
|
+
|
352
|
+
// Define the checker function
|
353
|
+
History.checkerFunction = function(){
|
354
|
+
// Prepare
|
355
|
+
var documentHash = History.getHash();
|
356
|
+
|
357
|
+
// The Document Hash has changed (application caused)
|
358
|
+
if ( documentHash !== lastDocumentHash ) {
|
359
|
+
// Equalise
|
360
|
+
lastDocumentHash = documentHash;
|
361
|
+
|
362
|
+
// Trigger Hashchange Event
|
363
|
+
History.Adapter.trigger(window,'hashchange');
|
364
|
+
}
|
365
|
+
|
366
|
+
// Return true
|
367
|
+
return true;
|
368
|
+
};
|
369
|
+
}
|
370
|
+
|
371
|
+
// Apply the checker function
|
372
|
+
History.intervalList.push(setInterval(History.checkerFunction, History.options.hashChangeInterval));
|
373
|
+
|
374
|
+
// Done
|
375
|
+
return true;
|
376
|
+
}; // History.hashChangeInit
|
377
|
+
|
378
|
+
// Bind hashChangeInit
|
379
|
+
History.Adapter.onDomLoad(History.hashChangeInit);
|
380
|
+
|
381
|
+
} // History.emulated.hashChange
|
382
|
+
|
383
|
+
|
384
|
+
// ====================================================================
|
385
|
+
// HTML5 State Support
|
386
|
+
|
387
|
+
// Non-Native pushState Implementation
|
388
|
+
if ( History.emulated.pushState ) {
|
389
|
+
/*
|
390
|
+
* We must emulate the HTML5 State Management by using HTML4 HashChange
|
391
|
+
*/
|
392
|
+
|
393
|
+
/**
|
394
|
+
* History.onHashChange(event)
|
395
|
+
* Trigger HTML5's window.onpopstate via HTML4 HashChange Support
|
396
|
+
*/
|
397
|
+
History.onHashChange = function(event){
|
398
|
+
//History.debug('History.onHashChange', arguments);
|
399
|
+
|
400
|
+
// Prepare
|
401
|
+
var currentUrl = ((event && event.newURL) || document.location.href),
|
402
|
+
currentHash = History.getHashByUrl(currentUrl),
|
403
|
+
currentState = null,
|
404
|
+
currentStateHash = null,
|
405
|
+
currentStateHashExits = null,
|
406
|
+
discardObject;
|
407
|
+
|
408
|
+
// Check if we are the same state
|
409
|
+
if ( History.isLastHash(currentHash) ) {
|
410
|
+
// There has been no change (just the page's hash has finally propagated)
|
411
|
+
//History.debug('History.onHashChange: no change');
|
412
|
+
History.busy(false);
|
413
|
+
return false;
|
414
|
+
}
|
415
|
+
|
416
|
+
// Reset the double check
|
417
|
+
History.doubleCheckComplete();
|
418
|
+
|
419
|
+
// Store our location for use in detecting back/forward direction
|
420
|
+
History.saveHash(currentHash);
|
421
|
+
|
422
|
+
// Expand Hash
|
423
|
+
if ( currentHash && History.isTraditionalAnchor(currentHash) ) {
|
424
|
+
//History.debug('History.onHashChange: traditional anchor', currentHash);
|
425
|
+
// Traditional Anchor Hash
|
426
|
+
History.Adapter.trigger(window,'anchorchange');
|
427
|
+
History.busy(false);
|
428
|
+
return false;
|
429
|
+
}
|
430
|
+
|
431
|
+
// Create State
|
432
|
+
currentState = History.extractState(History.getFullUrl(currentHash||document.location.href,false),true);
|
433
|
+
|
434
|
+
// Check if we are the same state
|
435
|
+
if ( History.isLastSavedState(currentState) ) {
|
436
|
+
//History.debug('History.onHashChange: no change');
|
437
|
+
// There has been no change (just the page's hash has finally propagated)
|
438
|
+
History.busy(false);
|
439
|
+
return false;
|
440
|
+
}
|
441
|
+
|
442
|
+
// Create the state Hash
|
443
|
+
currentStateHash = History.getHashByState(currentState);
|
444
|
+
|
445
|
+
// Check if we are DiscardedState
|
446
|
+
discardObject = History.discardedState(currentState);
|
447
|
+
if ( discardObject ) {
|
448
|
+
// Ignore this state as it has been discarded and go back to the state before it
|
449
|
+
if ( History.getHashByIndex(-2) === History.getHashByState(discardObject.forwardState) ) {
|
450
|
+
// We are going backwards
|
451
|
+
//History.debug('History.onHashChange: go backwards');
|
452
|
+
History.back(false);
|
453
|
+
} else {
|
454
|
+
// We are going forwards
|
455
|
+
//History.debug('History.onHashChange: go forwards');
|
456
|
+
History.forward(false);
|
457
|
+
}
|
458
|
+
return false;
|
459
|
+
}
|
460
|
+
|
461
|
+
// Push the new HTML5 State
|
462
|
+
//History.debug('History.onHashChange: success hashchange');
|
463
|
+
History.pushState(currentState.data,currentState.title,currentState.url,false);
|
464
|
+
|
465
|
+
// End onHashChange closure
|
466
|
+
return true;
|
467
|
+
};
|
468
|
+
History.Adapter.bind(window,'hashchange',History.onHashChange);
|
469
|
+
|
470
|
+
/**
|
471
|
+
* History.pushState(data,title,url)
|
472
|
+
* Add a new State to the history object, become it, and trigger onpopstate
|
473
|
+
* We have to trigger for HTML4 compatibility
|
474
|
+
* @param {object} data
|
475
|
+
* @param {string} title
|
476
|
+
* @param {string} url
|
477
|
+
* @return {true}
|
478
|
+
*/
|
479
|
+
History.pushState = function(data,title,url,queue){
|
480
|
+
//History.debug('History.pushState: called', arguments);
|
481
|
+
|
482
|
+
// Check the State
|
483
|
+
if ( History.getHashByUrl(url) ) {
|
484
|
+
throw new Error('History.js does not support states with fragement-identifiers (hashes/anchors).');
|
485
|
+
}
|
486
|
+
|
487
|
+
// Handle Queueing
|
488
|
+
if ( queue !== false && History.busy() ) {
|
489
|
+
// Wait + Push to Queue
|
490
|
+
//History.debug('History.pushState: we must wait', arguments);
|
491
|
+
History.pushQueue({
|
492
|
+
scope: History,
|
493
|
+
callback: History.pushState,
|
494
|
+
args: arguments,
|
495
|
+
queue: queue
|
496
|
+
});
|
497
|
+
return false;
|
498
|
+
}
|
499
|
+
|
500
|
+
// Make Busy
|
501
|
+
History.busy(true);
|
502
|
+
|
503
|
+
// Fetch the State Object
|
504
|
+
var newState = History.createStateObject(data,title,url),
|
505
|
+
newStateHash = History.getHashByState(newState),
|
506
|
+
oldState = History.getState(false),
|
507
|
+
oldStateHash = History.getHashByState(oldState),
|
508
|
+
html4Hash = History.getHash();
|
509
|
+
|
510
|
+
// Store the newState
|
511
|
+
History.storeState(newState);
|
512
|
+
History.expectedStateId = newState.id;
|
513
|
+
|
514
|
+
// Recycle the State
|
515
|
+
History.recycleState(newState);
|
516
|
+
|
517
|
+
// Force update of the title
|
518
|
+
History.setTitle(newState);
|
519
|
+
|
520
|
+
// Check if we are the same State
|
521
|
+
if ( newStateHash === oldStateHash ) {
|
522
|
+
//History.debug('History.pushState: no change', newStateHash);
|
523
|
+
History.busy(false);
|
524
|
+
return false;
|
525
|
+
}
|
526
|
+
|
527
|
+
// Update HTML4 Hash
|
528
|
+
if ( newStateHash !== html4Hash && newStateHash !== History.getShortUrl(document.location.href) ) {
|
529
|
+
//History.debug('History.pushState: update hash', newStateHash, html4Hash);
|
530
|
+
History.setHash(newStateHash,false);
|
531
|
+
return false;
|
532
|
+
}
|
533
|
+
|
534
|
+
// Update HTML5 State
|
535
|
+
History.saveState(newState);
|
536
|
+
|
537
|
+
// Fire HTML5 Event
|
538
|
+
//History.debug('History.pushState: trigger popstate');
|
539
|
+
History.Adapter.trigger(window,'statechange');
|
540
|
+
History.busy(false);
|
541
|
+
|
542
|
+
// End pushState closure
|
543
|
+
return true;
|
544
|
+
};
|
545
|
+
|
546
|
+
/**
|
547
|
+
* History.replaceState(data,title,url)
|
548
|
+
* Replace the State and trigger onpopstate
|
549
|
+
* We have to trigger for HTML4 compatibility
|
550
|
+
* @param {object} data
|
551
|
+
* @param {string} title
|
552
|
+
* @param {string} url
|
553
|
+
* @return {true}
|
554
|
+
*/
|
555
|
+
History.replaceState = function(data,title,url,queue){
|
556
|
+
//History.debug('History.replaceState: called', arguments);
|
557
|
+
|
558
|
+
// Check the State
|
559
|
+
if ( History.getHashByUrl(url) ) {
|
560
|
+
throw new Error('History.js does not support states with fragement-identifiers (hashes/anchors).');
|
561
|
+
}
|
562
|
+
|
563
|
+
// Handle Queueing
|
564
|
+
if ( queue !== false && History.busy() ) {
|
565
|
+
// Wait + Push to Queue
|
566
|
+
//History.debug('History.replaceState: we must wait', arguments);
|
567
|
+
History.pushQueue({
|
568
|
+
scope: History,
|
569
|
+
callback: History.replaceState,
|
570
|
+
args: arguments,
|
571
|
+
queue: queue
|
572
|
+
});
|
573
|
+
return false;
|
574
|
+
}
|
575
|
+
|
576
|
+
// Make Busy
|
577
|
+
History.busy(true);
|
578
|
+
|
579
|
+
// Fetch the State Objects
|
580
|
+
var newState = History.createStateObject(data,title,url),
|
581
|
+
oldState = History.getState(false),
|
582
|
+
previousState = History.getStateByIndex(-2);
|
583
|
+
|
584
|
+
// Discard Old State
|
585
|
+
History.discardState(oldState,newState,previousState);
|
586
|
+
|
587
|
+
// Alias to PushState
|
588
|
+
History.pushState(newState.data,newState.title,newState.url,false);
|
589
|
+
|
590
|
+
// End replaceState closure
|
591
|
+
return true;
|
592
|
+
};
|
593
|
+
|
594
|
+
} // History.emulated.pushState
|
595
|
+
|
596
|
+
|
597
|
+
|
598
|
+
// ====================================================================
|
599
|
+
// Initialise
|
600
|
+
|
601
|
+
// Non-Native pushState Implementation
|
602
|
+
if ( History.emulated.pushState ) {
|
603
|
+
/**
|
604
|
+
* Ensure initial state is handled correctly
|
605
|
+
*/
|
606
|
+
if ( History.getHash() && !History.emulated.hashChange ) {
|
607
|
+
History.Adapter.onDomLoad(function(){
|
608
|
+
History.Adapter.trigger(window,'hashchange');
|
609
|
+
});
|
610
|
+
}
|
611
|
+
|
612
|
+
} // History.emulated.pushState
|
613
|
+
|
614
|
+
}; // History.initHtml4
|
615
|
+
|
616
|
+
// Try and Initialise History
|
617
|
+
if ( typeof History.init !== 'undefined' ) {
|
618
|
+
History.init();
|
619
|
+
}
|
620
|
+
|
621
|
+
})(window);
|