history.adapter.jquery.js 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. /**
  2. * History.js jQuery 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. jQuery = window.jQuery;
  14. // Check Existence
  15. if ( typeof History.Adapter !== 'undefined' ) {
  16. throw new Error('History.js Adapter has already been loaded...');
  17. }
  18. // Add the Adapter
  19. History.Adapter = {
  20. /**
  21. * History.Adapter.bind(el,event,callback)
  22. * @param {Element|string} el
  23. * @param {string} event - custom and standard events
  24. * @param {function} callback
  25. * @return {void}
  26. */
  27. bind: function(el,event,callback){
  28. jQuery(el).bind(event,callback);
  29. },
  30. /**
  31. * History.Adapter.trigger(el,event)
  32. * @param {Element|string} el
  33. * @param {string} event - custom and standard events
  34. * @param {Object=} extra - a object of extra event data (optional)
  35. * @return {void}
  36. */
  37. trigger: function(el,event,extra){
  38. jQuery(el).trigger(event,extra);
  39. },
  40. /**
  41. * History.Adapter.extractEventData(key,event,extra)
  42. * @param {string} key - key for the event data to extract
  43. * @param {string} event - custom and standard events
  44. * @param {Object=} extra - a object of extra event data (optional)
  45. * @return {mixed}
  46. */
  47. extractEventData: function(key,event,extra){
  48. // jQuery Native then jQuery Custom
  49. var result = (event && event.originalEvent && event.originalEvent[key]) || (extra && extra[key]) || undefined;
  50. // Return
  51. return result;
  52. },
  53. /**
  54. * History.Adapter.onDomLoad(callback)
  55. * @param {function} callback
  56. * @return {void}
  57. */
  58. onDomLoad: function(callback) {
  59. jQuery(callback);
  60. }
  61. };
  62. // Try and Initialise History
  63. if ( typeof History.init !== 'undefined' ) {
  64. History.init();
  65. }
  66. })(window);