| 1 |
define(["dojo/_base/lang", "dojo/_base/declare", "dojo/on", "../Controller", "../utils/hash", "dojo/topic"], |
| 2 |
function(lang, declare, on, Controller, hash, topic){ |
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
return declare("dojox.app.controllers.History", Controller, { |
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
_currentPosition: 0, |
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
currentState: {}, |
| 21 |
|
| 22 |
constructor: function(){ |
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
this.events = { |
| 30 |
"app-domNode": this.onDomNodeChange |
| 31 |
}; |
| 32 |
if(this.app.domNode){ |
| 33 |
this.onDomNodeChange({oldNode: null, newNode: this.app.domNode}); |
| 34 |
} |
| 35 |
this.bind(window, "popstate", lang.hitch(this, this.onPopState)); |
| 36 |
}, |
| 37 |
|
| 38 |
onDomNodeChange: function(evt){ |
| 39 |
if(evt.oldNode != null){ |
| 40 |
this.unbind(evt.oldNode, "startTransition"); |
| 41 |
} |
| 42 |
this.bind(evt.newNode, "startTransition", lang.hitch(this, this.onStartTransition)); |
| 43 |
}, |
| 44 |
|
| 45 |
onStartTransition: function(evt){ |
| 46 |
|
| 47 |
|
| 48 |
|
| 49 |
|
| 50 |
|
| 51 |
|
| 52 |
|
| 53 |
|
| 54 |
|
| 55 |
|
| 56 |
|
| 57 |
|
| 58 |
|
| 59 |
|
| 60 |
|
| 61 |
var currentHash = window.location.hash; |
| 62 |
var currentView = hash.getTarget(currentHash, this.app.defaultView); |
| 63 |
var currentParams = hash.getParams(currentHash); |
| 64 |
var _detail = lang.clone(evt.detail); |
| 65 |
_detail.target = _detail.title = currentView; |
| 66 |
_detail.url = currentHash; |
| 67 |
_detail.params = currentParams; |
| 68 |
_detail.id = this._currentPosition; |
| 69 |
|
| 70 |
|
| 71 |
if(history.length == 1){ |
| 72 |
history.pushState(_detail, _detail.href, currentHash); |
| 73 |
} |
| 74 |
|
| 75 |
|
| 76 |
_detail.bwdTransition = _detail.transition; |
| 77 |
lang.mixin(this.currentState, _detail); |
| 78 |
history.replaceState(this.currentState, this.currentState.href, currentHash); |
| 79 |
|
| 80 |
|
| 81 |
this._currentPosition += 1; |
| 82 |
evt.detail.id = this._currentPosition; |
| 83 |
|
| 84 |
var newHash = evt.detail.url || "#" + evt.detail.target; |
| 85 |
|
| 86 |
if(evt.detail.params){ |
| 87 |
newHash = hash.buildWithParams(newHash, evt.detail.params); |
| 88 |
} |
| 89 |
|
| 90 |
evt.detail.fwdTransition = evt.detail.transition; |
| 91 |
history.pushState(evt.detail, evt.detail.href, newHash); |
| 92 |
this.currentState = lang.clone(evt.detail); |
| 93 |
|
| 94 |
|
| 95 |
topic.publish("/app/history/pushState", evt.detail.target); |
| 96 |
}, |
| 97 |
|
| 98 |
onPopState: function(evt){ |
| 99 |
|
| 100 |
|
| 101 |
|
| 102 |
|
| 103 |
|
| 104 |
|
| 105 |
|
| 106 |
|
| 107 |
|
| 108 |
if((this.app.getStatus() !== this.app.lifecycle.STARTED) || !evt.state ){ |
| 109 |
return; |
| 110 |
} |
| 111 |
|
| 112 |
|
| 113 |
var backward = evt.state.id < this._currentPosition; |
| 114 |
backward ? this._currentPosition -= 1 : this._currentPosition += 1; |
| 115 |
|
| 116 |
|
| 117 |
|
| 118 |
var opts = lang.mixin({reverse: backward ? true : false}, evt.state); |
| 119 |
opts.transition = backward ? opts.bwdTransition : opts.fwdTransition; |
| 120 |
this.app.emit("app-transition", { |
| 121 |
viewId: evt.state.target, |
| 122 |
opts: opts |
| 123 |
}); |
| 124 |
topic.publish("/app/history/popState", evt.state.target); |
| 125 |
} |
| 126 |
}); |
| 127 |
}); |