Sha256: bcf68caea80c5a842de974d6a416b0f52e28bd3c3172ecd109219846717b023b

Contents?: true

Size: 2 KB

Versions: 7

Compression:

Stored size: 2 KB

Contents

/*
---

name: History

description: History Management via popstate or hashchange.

authors: Christoph Pojer (@cpojer)

license: MIT-style license.

requires: [Core/Events, Core/Element.Event]

provides: History

...
*/

(function(){

var events = Element.NativeEvents,
	location = window.location,
	base = location.pathname,
	history = window.history,
	hasPushState = ('pushState' in history),
	event = hasPushState ? 'popstate' : 'hashchange';

this.History = new new Class({

	Implements: [Events],

	initialize: hasPushState ? function(){
		events[event] = 2;
		window.addEvent(event, this.pop.bind(this));
	} : function(){
		events[event] = 1;
		window.addEvent(event, this.pop.bind(this));

		this.hash = location.hash;
		var hashchange = ('onhashchange' in window);
		if (!(hashchange && (document.documentMode === undefined || document.documentMode > 7)))
			this.timer = this.check.periodical(200, this);
	},

	push: hasPushState ? function(url, title, state){
		if (base && base != url) base = null;
		
		history.pushState(state || null, title || null, url);
		this.onChange(url, state);
	} : function(url){
		location.hash = url;
	},

	replace: hasPushState ? function(url, title, state){
		history.replaceState(state || null, title || null, url);
	} : function(url){
		this.hash = '#' + url;
		this.push(url);
	},

	pop: hasPushState ? function(event){
		var url = location.pathname;
		if (url == base){
			base = null;
			return;
		}
		this.onChange(url, event.event.state);
	} : function(){
		var hash = location.hash;
		if (this.hash == hash) return;

		this.hash = hash;
		this.onChange(hash.substr(1));
	},

	onChange: function(url, state){
		this.fireEvent('change', [url, state || {}]);
	},

	back: function(){
		history.back();
	},

	forward: function(){
		history.forward();
	},
	
	getPath: function(){
		return hasPushState ? location.pathname : location.hash.substr(1);
	},

	hasPushState: function(){
		return hasPushState;
	},

	check: function(){
		if (this.hash != location.hash) this.pop();
	}

});

}).call(this);

Version data entries

7 entries across 7 versions & 1 rubygems

Version Path
lsd_rails-0.1.6 Packages/mootools-history/Source/History.js
lsd_rails-0.1.5 Packages/mootools-history/Source/History.js
lsd_rails-0.1.4 Packages/mootools-history/Source/History.js
lsd_rails-0.1.3 Packages/mootools-history/Source/History.js
lsd_rails-0.1.2 Packages/mootools-history/Source/History.js
lsd_rails-0.1.1 Packages/mootools-history/Source/History.js
lsd_rails-0.1 Packages/mootools-history/Source/History.js