history.adapter.mootools.js 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. /**
  2. * History.js MooTools Adapter
  3. * @author Benjamin Arthur Lupton <contact@balupton.com>
  4. * @copyright 2010-2011 Benjamin Arthur Lupton <contact@balupton.com>
  5. * @license New BSD License <http://creativecommons.org/licenses/BSD/>
  6. */
  7. // Closure
  8. (function(window,undefined){
  9. "use strict";
  10. // Localise Globals
  11. var
  12. History = window.History = window.History||{},
  13. MooTools = window.MooTools,
  14. Element = window.Element;
  15. // Check Existence
  16. if ( typeof History.Adapter !== 'undefined' ) {
  17. throw new Error('History.js Adapter has already been loaded...');
  18. }
  19. // Make MooTools aware of History.js Events
  20. Object.append(Element.NativeEvents,{
  21. 'popstate':2,
  22. 'hashchange':2
  23. });
  24. // Add the Adapter
  25. History.Adapter = {
  26. /**
  27. * History.Adapter.bind(el,event,callback)
  28. * @param {Element|string} el
  29. * @param {string} event - custom and standard events
  30. * @param {function} callback
  31. * @return {void}
  32. */
  33. bind: function(el,event,callback){
  34. var El = typeof el === 'string' ? document.id(el) : el;
  35. El.addEvent(event,callback);
  36. },
  37. /**
  38. * History.Adapter.trigger(el,event)
  39. * @param {Element|string} el
  40. * @param {string} event - custom and standard events
  41. * @param {Object=} extra - a object of extra event data (optional)
  42. * @return void
  43. */
  44. trigger: function(el,event,extra){
  45. var El = typeof el === 'string' ? document.id(el) : el;
  46. El.fireEvent(event,extra);
  47. },
  48. /**
  49. * History.Adapter.extractEventData(key,event,extra)
  50. * @param {string} key - key for the event data to extract
  51. * @param {string} event - custom and standard events
  52. * @return {mixed}
  53. */
  54. extractEventData: function(key,event){
  55. // MooTools Native then MooTools Custom
  56. var result = (event && event.event && event.event[key]) || (event && event[key]) || undefined;
  57. // Return
  58. return result;
  59. },
  60. /**
  61. * History.Adapter.onDomLoad(callback)
  62. * @param {function} callback
  63. * @return {void}
  64. */
  65. onDomLoad: function(callback) {
  66. window.addEvent('domready',callback);
  67. }
  68. };
  69. // Try and Initialise History
  70. if ( typeof History.init !== 'undefined' ) {
  71. History.init();
  72. }
  73. })(window);