2011-05-20 05:49:30 +02:00
|
|
|
/**
|
2011-12-13 10:34:25 +01:00
|
|
|
* History.js MooTools Adapter
|
2011-05-20 05:49:30 +02:00
|
|
|
* @author Benjamin Arthur Lupton <contact@balupton.com>
|
|
|
|
* @copyright 2010-2011 Benjamin Arthur Lupton <contact@balupton.com>
|
|
|
|
* @license New BSD License <http://creativecommons.org/licenses/BSD/>
|
|
|
|
*/
|
|
|
|
|
|
|
|
// Closure
|
|
|
|
(function(window,undefined){
|
2011-12-13 10:34:25 +01:00
|
|
|
"use strict";
|
|
|
|
|
2011-05-20 05:49:30 +02:00
|
|
|
// Localise Globals
|
|
|
|
var
|
|
|
|
History = window.History = window.History||{},
|
2011-12-13 10:34:25 +01:00
|
|
|
MooTools = window.MooTools,
|
|
|
|
Element = window.Element;
|
2011-05-20 05:49:30 +02:00
|
|
|
|
|
|
|
// Check Existence
|
|
|
|
if ( typeof History.Adapter !== 'undefined' ) {
|
|
|
|
throw new Error('History.js Adapter has already been loaded...');
|
|
|
|
}
|
|
|
|
|
|
|
|
// Make MooTools aware of History.js Events
|
|
|
|
Object.append(Element.NativeEvents,{
|
|
|
|
'popstate':2,
|
|
|
|
'hashchange':2
|
|
|
|
});
|
|
|
|
|
|
|
|
// Add the Adapter
|
|
|
|
History.Adapter = {
|
|
|
|
/**
|
|
|
|
* History.Adapter.bind(el,event,callback)
|
2011-12-13 10:34:25 +01:00
|
|
|
* @param {Element|string} el
|
|
|
|
* @param {string} event - custom and standard events
|
|
|
|
* @param {function} callback
|
|
|
|
* @return {void}
|
2011-05-20 05:49:30 +02:00
|
|
|
*/
|
|
|
|
bind: function(el,event,callback){
|
|
|
|
var El = typeof el === 'string' ? document.id(el) : el;
|
|
|
|
El.addEvent(event,callback);
|
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
|
|
|
* History.Adapter.trigger(el,event)
|
2011-12-13 10:34:25 +01:00
|
|
|
* @param {Element|string} el
|
|
|
|
* @param {string} event - custom and standard events
|
|
|
|
* @param {Object=} extra - a object of extra event data (optional)
|
|
|
|
* @return void
|
2011-05-20 05:49:30 +02:00
|
|
|
*/
|
2011-12-13 10:34:25 +01:00
|
|
|
trigger: function(el,event,extra){
|
2011-05-20 05:49:30 +02:00
|
|
|
var El = typeof el === 'string' ? document.id(el) : el;
|
2011-12-13 10:34:25 +01:00
|
|
|
El.fireEvent(event,extra);
|
2011-05-20 05:49:30 +02:00
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
2011-12-13 10:34:25 +01:00
|
|
|
* History.Adapter.extractEventData(key,event,extra)
|
|
|
|
* @param {string} key - key for the event data to extract
|
|
|
|
* @param {string} event - custom and standard events
|
|
|
|
* @return {mixed}
|
|
|
|
*/
|
|
|
|
extractEventData: function(key,event){
|
|
|
|
// MooTools Native then MooTools Custom
|
|
|
|
var result = (event && event.event && event.event[key]) || (event && event[key]) || undefined;
|
|
|
|
|
|
|
|
// Return
|
|
|
|
return result;
|
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
|
|
|
* History.Adapter.onDomLoad(callback)
|
|
|
|
* @param {function} callback
|
|
|
|
* @return {void}
|
2011-05-20 05:49:30 +02:00
|
|
|
*/
|
|
|
|
onDomLoad: function(callback) {
|
|
|
|
window.addEvent('domready',callback);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
// Try and Initialise History
|
|
|
|
if ( typeof History.init !== 'undefined' ) {
|
|
|
|
History.init();
|
|
|
|
}
|
|
|
|
|
|
|
|
})(window);
|