| 1 |
define(["dojo/_base/lang", "dojo/_base/declare", "dojo/topic", "dojo/on", "../Controller", "../utils/hash", "dojo/hash"], |
| 2 |
function(lang, declare, topic, on, Controller, hash){ |
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
return declare("dojox.app.controllers.HistoryHash", Controller, { |
| 12 |
|
| 13 |
constructor: function(app){ |
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
this.events = { |
| 22 |
"app-domNode": this.onDomNodeChange |
| 23 |
}; |
| 24 |
if(this.app.domNode){ |
| 25 |
this.onDomNodeChange({oldNode: null, newNode: this.app.domNode}); |
| 26 |
} |
| 27 |
topic.subscribe("/dojo/hashchange", lang.hitch(this, function(newhash){ |
| 28 |
this._onHashChange(newhash); |
| 29 |
})); |
| 30 |
|
| 31 |
this._historyStack = []; |
| 32 |
this._historyLen = 0; |
| 33 |
this._historyDiff = 0; |
| 34 |
this._current = null; |
| 35 |
this._next = null; |
| 36 |
this._previous = null; |
| 37 |
this._index = 0; |
| 38 |
this._oldHistoryLen = 0; |
| 39 |
this._newHistoryLen = 0; |
| 40 |
this._addToHistoryStack = false; |
| 41 |
this._startTransitionEvent = false; |
| 42 |
|
| 43 |
|
| 44 |
var currentHash = window.location.hash; |
| 45 |
if(currentHash && (currentHash.length > 1)){ |
| 46 |
currentHash = currentHash.substr(1); |
| 47 |
} |
| 48 |
this._historyStack.push({ |
| 49 |
"hash": currentHash, |
| 50 |
"url": window.location.href, |
| 51 |
"detail": {target:currentHash} |
| 52 |
}); |
| 53 |
this._historyLen = window.history.length; |
| 54 |
this._index = this._historyStack.length - 1; |
| 55 |
this._current = currentHash; |
| 56 |
|
| 57 |
|
| 58 |
this._historyDiff = window.history.length - this._historyStack.length; |
| 59 |
}, |
| 60 |
|
| 61 |
onDomNodeChange: function(evt){ |
| 62 |
if(evt.oldNode != null){ |
| 63 |
this.unbind(evt.oldNode, "startTransition"); |
| 64 |
} |
| 65 |
this.bind(evt.newNode, "startTransition", lang.hitch(this, this.onStartTransition)); |
| 66 |
}, |
| 67 |
|
| 68 |
onStartTransition: function(evt){ |
| 69 |
|
| 70 |
|
| 71 |
|
| 72 |
|
| 73 |
|
| 74 |
|
| 75 |
|
| 76 |
|
| 77 |
|
| 78 |
|
| 79 |
|
| 80 |
|
| 81 |
|
| 82 |
|
| 83 |
|
| 84 |
|
| 85 |
var target = evt.detail.target; |
| 86 |
var regex = /#(.+)/; |
| 87 |
if(!target && regex.test(evt.detail.href)){ |
| 88 |
target = evt.detail.href.match(regex)[1]; |
| 89 |
} |
| 90 |
|
| 91 |
var currentHash = evt.detail.url || "#" + target; |
| 92 |
|
| 93 |
if(evt.detail.params){ |
| 94 |
currentHash = hash.buildWithParams(currentHash, evt.detail.params); |
| 95 |
} |
| 96 |
|
| 97 |
this._oldHistoryLen = window.history.length; |
| 98 |
|
| 99 |
|
| 100 |
|
| 101 |
|
| 102 |
|
| 103 |
window.location.hash = currentHash; |
| 104 |
|
| 105 |
|
| 106 |
|
| 107 |
|
| 108 |
this._addToHistoryStack = true; |
| 109 |
|
| 110 |
this._startTransitionEvent = true; |
| 111 |
}, |
| 112 |
|
| 113 |
_addHistory: function(hash){ |
| 114 |
|
| 115 |
|
| 116 |
|
| 117 |
|
| 118 |
|
| 119 |
this._historyStack.push({ |
| 120 |
"hash": hash, |
| 121 |
"url": window.location.href, |
| 122 |
"detail": {target:hash} |
| 123 |
}); |
| 124 |
|
| 125 |
this._historyLen = window.history.length; |
| 126 |
this._index = this._historyStack.length - 1; |
| 127 |
|
| 128 |
this._previous = this._current; |
| 129 |
this._current = hash; |
| 130 |
this._next = null; |
| 131 |
|
| 132 |
this._historyDiff = window.history.length - this._historyStack.length; |
| 133 |
|
| 134 |
|
| 135 |
|
| 136 |
this._addToHistoryStack = false; |
| 137 |
}, |
| 138 |
|
| 139 |
_onHashChange: function(currentHash){ |
| 140 |
|
| 141 |
|
| 142 |
|
| 143 |
|
| 144 |
|
| 145 |
|
| 146 |
if(this._index < 0 || this._index > (window.history.length - 1)){ |
| 147 |
throw Error("Application history out of management."); |
| 148 |
} |
| 149 |
|
| 150 |
this._newHistoryLen = window.history.length; |
| 151 |
|
| 152 |
|
| 153 |
if(this._oldHistoryLen > this._newHistoryLen){ |
| 154 |
|
| 155 |
this._historyStack.splice((this._newHistoryLen - this._historyDiff - 1), (this._historyStack.length - 1)); |
| 156 |
|
| 157 |
|
| 158 |
this._historyLen = this._historyStack.length; |
| 159 |
|
| 160 |
|
| 161 |
|
| 162 |
this._oldHistoryLen = 0; |
| 163 |
} |
| 164 |
|
| 165 |
|
| 166 |
|
| 167 |
if(this._addToHistoryStack && (this._oldHistoryLen === this._newHistoryLen)){ |
| 168 |
this._historyStack.splice((this._newHistoryLen - this._historyDiff - 1), (this._historyStack.length - 1)); |
| 169 |
this._addHistory(currentHash); |
| 170 |
|
| 171 |
|
| 172 |
return; |
| 173 |
} |
| 174 |
|
| 175 |
|
| 176 |
if(this._historyLen < window.history.length){ |
| 177 |
this._addHistory(currentHash); |
| 178 |
if(!this._startTransitionEvent){ |
| 179 |
|
| 180 |
this.app.emit("app-transition", { |
| 181 |
viewId: hash.getTarget(currentHash), |
| 182 |
opts: { params: hash.getParams(currentHash) || {} } |
| 183 |
}); |
| 184 |
} |
| 185 |
}else{ |
| 186 |
if(currentHash == this._current){ |
| 187 |
|
| 188 |
}else if(currentHash === this._previous){ |
| 189 |
this._back(currentHash, this._historyStack[this._index]["detail"]); |
| 190 |
}else if(currentHash === this._next){ |
| 191 |
this._forward(currentHash, this._historyStack[this._index]["detail"]); |
| 192 |
}else{ |
| 193 |
|
| 194 |
var index = -1; |
| 195 |
for(var i = this._index; i > 0; i--){ |
| 196 |
if(currentHash === this._historyStack[i]["hash"]){ |
| 197 |
index = i; |
| 198 |
break; |
| 199 |
} |
| 200 |
} |
| 201 |
|
| 202 |
|
| 203 |
if(-1 === index){ |
| 204 |
for(var i = this._index; i < this._historyStack.length; i++){ |
| 205 |
if(currentHash === this._historyStack[i]["hash"]){ |
| 206 |
index = i; |
| 207 |
break; |
| 208 |
} |
| 209 |
} |
| 210 |
} |
| 211 |
|
| 212 |
if(0 < index < this._historyStack.length){ |
| 213 |
this._go(index, (index - this._index)); |
| 214 |
}else{ |
| 215 |
this.app.log("go error. index out of history stack."); |
| 216 |
} |
| 217 |
} |
| 218 |
} |
| 219 |
|
| 220 |
this._startTransitionEvent = false; |
| 221 |
}, |
| 222 |
|
| 223 |
_back: function(currentHash, detail){ |
| 224 |
this.app.log("back"); |
| 225 |
this._next = this._historyStack[this._index]["hash"]; |
| 226 |
this._index--; |
| 227 |
if(this._index > 0){ |
| 228 |
this._previous = this._historyStack[this._index - 1]["hash"]; |
| 229 |
}else{ |
| 230 |
this._previous = null; |
| 231 |
} |
| 232 |
this._current = currentHash; |
| 233 |
|
| 234 |
var target = hash.getTarget(currentHash, this.app.defaultView); |
| 235 |
|
| 236 |
|
| 237 |
topic.publish("/app/history/back", {"viewId": target, "detail": detail}); |
| 238 |
|
| 239 |
|
| 240 |
this.app.emit("app-transition", { |
| 241 |
viewId: target, |
| 242 |
opts: lang.mixin({reverse: true}, detail, {"params": hash.getParams(currentHash)}) |
| 243 |
}); |
| 244 |
}, |
| 245 |
|
| 246 |
_forward: function(currentHash, detail){ |
| 247 |
this.app.log("forward"); |
| 248 |
this._previous = this._historyStack[this._index]["hash"]; |
| 249 |
this._index++; |
| 250 |
if(this._index < this._historyStack.length - 1){ |
| 251 |
this._next = this._historyStack[this._index + 1]["hash"]; |
| 252 |
}else{ |
| 253 |
this._next = null; |
| 254 |
} |
| 255 |
this._current = currentHash; |
| 256 |
|
| 257 |
var target = hash.getTarget(currentHash, this.app.defaultView); |
| 258 |
|
| 259 |
|
| 260 |
topic.publish("/app/history/forward", {"viewId": target, "detail": detail}); |
| 261 |
|
| 262 |
|
| 263 |
this.app.emit("app-transition", { |
| 264 |
viewId: target, |
| 265 |
opts: lang.mixin({reverse: false}, detail, {"params": hash.getParams(currentHash)}) |
| 266 |
}); |
| 267 |
}, |
| 268 |
|
| 269 |
_go: function(index, step){ |
| 270 |
if(index < 0 || (index > window.history.length - 1)){ |
| 271 |
throw Error("Application history.go steps out of management, index: "+index+" length: "+window.history.length); |
| 272 |
} |
| 273 |
|
| 274 |
this._index = index; |
| 275 |
this._current = this._historyStack[index]["hash"]; |
| 276 |
this._previous = this._historyStack[index - 1] ? this._historyStack[index - 1]["hash"] : null; |
| 277 |
this._next = this._historyStack[index + 1] ? this._historyStack[index + 1]["hash"] : null; |
| 278 |
|
| 279 |
var target = hash.getTarget(this._current, this.app.defaultView); |
| 280 |
|
| 281 |
|
| 282 |
topic.publish("/app/history/go", {"viewId": target, "step": step, "detail": this._historyStack[index]["detail"]}); |
| 283 |
|
| 284 |
|
| 285 |
this.app.emit("app-transition", { |
| 286 |
viewId: target, |
| 287 |
opts: lang.mixin({reverse: (step <= 0)}, this._historyStack[index]["detail"], {"params": hash.getParams(this._current)}) |
| 288 |
}); |
| 289 |
} |
| 290 |
}); |
| 291 |
}); |