history.adapter.extjs.js 2.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. /**
  2. * History.js ExtJS Adapter
  3. * @author Sean Adkinson <sean.adkinson@gmail.com>
  4. * @copyright 2012 Sean Adkinson <sean.adkinson@gmail.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. Ext = window.Ext;
  14. window.JSON = {
  15. stringify: Ext.JSON.encode,
  16. parse: Ext.JSON.decode
  17. };
  18. // Check Existence
  19. if ( typeof History.Adapter !== 'undefined' ) {
  20. throw new Error('History.js Adapter has already been loaded...');
  21. }
  22. // Add the Adapter
  23. History.Adapter = {
  24. observables: {},
  25. /**
  26. * History.Adapter.bind(el,event,callback)
  27. * @param {Element|string} el
  28. * @param {string} event - custom and standard events
  29. * @param {function} callback
  30. * @param {Object} scope
  31. * @return {void}
  32. */
  33. bind: function(element,eventName,callback,scope){
  34. Ext.EventManager.addListener(element, eventName, callback, scope);
  35. //bind an observable to the element that will let us "trigger" events on it
  36. var id = Ext.id(element, 'history-'), observable = this.observables[id];
  37. if (!observable) {
  38. observable = Ext.create('Ext.util.Observable');
  39. this.observables[id] = observable;
  40. }
  41. observable.on(eventName, callback, scope);
  42. },
  43. /**
  44. * History.Adapter.trigger(el,event)
  45. * @param {Element|string} el
  46. * @param {string} event - custom and standard events
  47. * @param {Object=} extra - a object of extra event data (optional)
  48. * @return {void}
  49. */
  50. trigger: function(element,eventName,extra){
  51. var id = Ext.id(element, 'history-'), observable = this.observables[id];
  52. if (observable) {
  53. observable.fireEvent(eventName, extra);
  54. }
  55. },
  56. /**
  57. * History.Adapter.extractEventData(key,event,extra)
  58. * @param {string} key - key for the event data to extract
  59. * @param {string} event - custom and standard events
  60. * @param {Object=} extra - a object of extra event data (optional)
  61. * @return {mixed}
  62. */
  63. extractEventData: function(key,event,extra){
  64. var result = (event && event.browserEvent && event.browserEvent[key]) || (extra && extra[key]) || undefined;
  65. return result;
  66. },
  67. /**
  68. * History.Adapter.onDomLoad(callback)
  69. * @param {function} callback
  70. * @return {void}
  71. */
  72. onDomLoad: function(callback) {
  73. Ext.onReady(callback);
  74. }
  75. };
  76. // Try and Initialise History
  77. if ( typeof History.init !== 'undefined' ) {
  78. History.init();
  79. }
  80. })(window);