Posted By

6e on 06/17/08


Tagged

ajax javascript textmate prototype


Versions (?)

State Controller


 / Published in: Other
 

  1. document.observe('dom:loaded', function(){
  2. // instance the class...it is available via 'controller'
  3. controller = new StateController();
  4. });
  5.  
  6. var StateController = Class.create({
  7.  
  8. initialize: function(){
  9. util.trace('Control this.');
  10.  
  11. this.lastHash = '';
  12. this.ready = true;
  13.  
  14. Event.observe(window, 'load', this.loaded.bind(this));
  15. document.observe('receiver:stateChange', this.stateChange.bind(this));
  16. //document.observe('receiver:hashChange', this.hashChange.bind(this));
  17. },
  18.  
  19. loaded: function(){
  20. util.trace('Got loaded');
  21. //observe for hash changes
  22. this.observeHash();
  23. },
  24.  
  25. stateChange: function($event){
  26. // when a state change is invoked, change the hash
  27. util.trace('change hash '+ window.location.hash);
  28. // add slash for aesthetics...looks like swfaddress
  29. this.setHash($event.memo.destination);
  30. },
  31.  
  32. observeHash: function(){
  33. //when hash changes fire receiver:hashChanged
  34. new PeriodicalExecuter(function(pe){
  35. if (window.location.hash == this.lastHash) {
  36. // hash hasn't changed, bail
  37. return;
  38. }
  39. // hash has changed, take note and fire receiver:stateChange
  40. this.lastHash = window.location.hash;
  41. util.trace('New hash');
  42. document.body.fire('receiver:hashChange', {destination: window.location.hash});
  43.  
  44. }.bind(this), .1);
  45. },
  46.  
  47. fireEvent: function($destination){
  48. //method for flash to send destination
  49. document.body.fire('receiver:stateChange', {destination: $destination});
  50. },
  51.  
  52. getHash: function(){
  53. // returns the hash stripped of #/
  54. return window.location.hash.sub('#/', '')
  55. },
  56.  
  57. setHash: function($destination){
  58. // formats hash to #/destination
  59. window.location.hash = '/'+$destination;
  60. },
  61.  
  62. getRelativeURL: function($url){
  63. // pulls protocol, host and path from url to return location
  64. var absolute = window.location.protocol+'//'+window.location.host+window.location.pathname;
  65. util.trace(absolute+ ' ' + $url);
  66. return $url.sub(absolute, '');
  67. },
  68.  
  69. ready: function(){
  70. return this.ready;
  71. },
  72.  
  73. sitePageTitle: function(destination, delimiter){
  74. var pathArray = destination.split('/').invoke('capitalize');
  75. pathArray.unshift(document.title.split(' '+delimiter+' ').last());
  76. pathArray.reverse();
  77. return pathArray.join(' '+delimiter+' ');
  78. }
  79.  
  80. });

Report this snippet  

You need to login to post a comment.