history.js 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. define("dojox/app/module/history", ["dojo/_base/kernel","dojo/_base/lang", "dojo/_base/declare", "dojo/on"],function(dojo,dlang,declare,listen){
  2. return declare(null, {
  3. postCreate: function(params,node){
  4. this.inherited(arguments);
  5. var hash=window.location.hash;
  6. this._startView= ((hash && hash.charAt(0)=="#")?hash.substr(1):hash)||this.defaultView;
  7. listen(this.domNode, "startTransition", dojo.hitch(this, "onStartTransition"));
  8. listen(window,"popstate", dojo.hitch(this, "onPopState"));
  9. },
  10. startup: function(){
  11. this.inherited(arguments);
  12. },
  13. onStartTransition: function(evt){
  14. console.log("onStartTransition", evt.detail.href, history.state);
  15. if (evt.preventDefault){
  16. evt.preventDefault();
  17. }
  18. var target = evt.detail.target;
  19. var regex = /#(.+)/;
  20. if(!target && regex.test(evt.detail.href)){
  21. target = evt.detail.href.match(regex)[1];
  22. }
  23. //prevent event from bubbling to window and being
  24. //processed by dojox/mobile/ViewController
  25. evt.cancelBubble = true;
  26. if(evt.stopPropagation){
  27. evt.stopPropagation();
  28. }
  29. dojo.when(this.transition(target, dojo.mixin({reverse: false},evt.detail)), dojo.hitch(this, function(){
  30. history.pushState(evt.detail,evt.detail.href, evt.detail.url);
  31. }))
  32. },
  33. /*
  34. onHashChange: function(evt){
  35. var target = window.location.hash.substr(1);;
  36. var evt = {target: window.location.hash, url: "#" + target,title:null};
  37. //this.onStartTransition(evt);
  38. },
  39. */
  40. onPopState: function(evt){
  41. // Check application status, if application status not STARTED, do nothing.
  42. // when clean browser's cache then refresh the current page, it will trigger popState event.
  43. // but the application not start, it will throw an error.
  44. if(this.getStatus() !== this.lifecycle.STARTED ){
  45. return;
  46. }
  47. var state = evt.state;
  48. if (!state){
  49. if(!this._startView && window.location.hash){
  50. state={
  51. target: (location.hash && location.hash.charAt(0)=="#")?location.hash.substr(1):location.hash,
  52. url: location.hash
  53. }
  54. }else{
  55. state={};
  56. }
  57. }
  58. var target = state.target || this._startView || this.defaultView;
  59. if (this._startView){
  60. this._startView=null;
  61. }
  62. var title = state.title||null;
  63. var href = state.url || null;
  64. if (evt._sim) {
  65. history.replaceState(state, title, href );
  66. }
  67. /*
  68. dojo.when(this.transition(window.history.state, {rev: true}), dojo.hitch(this, function(){
  69. console.log('done transition from onPopState');
  70. }))
  71. */
  72. var currentState = history.state;
  73. this.transition(target, dojo.mixin({reverse: true},state));
  74. }
  75. });
  76. });