Debian Codegraph @

dojox/app/controllers/HistoryHash.js

raw symbols SCIP
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 // module:
4 // dojox/app/controllers/HistoryHash
5 // summary:
6 // Bind "app-domNode" event on dojox/app application instance,
7 // Bind "startTransition" event on dojox/app application domNode,
8 // Bind "/dojo/hashchange" event on window object.
9 // Maintain history by history hash.
10
11 return declare("dojox.app.controllers.HistoryHash", Controller, {
12
13 constructor: function(app){
14 // summary:
15 // Bind "app-domNode" event on dojox/app application instance,
16 // Bind "startTransition" event on dojox/app application domNode,
17 // subscribe "/dojo/hashchange" event.
18 //
19 // app:
20 // dojox/app application instance.
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 = []; // application history stack
32 this._historyLen = 0; // current window.history length
33 this._historyDiff = 0; // the diff of window.history stack and application history stack
34 this._current = null; // current history item in application history stack
35 this._next = null; // next history item in application history stack
36 this._previous = null; // previous history item in application history stack
37 this._index = 0; // identify current history item's index in application history stack
38 this._oldHistoryLen = 0;// window.history stack length before hash change
39 this._newHistoryLen = 0;// window.history stack length after hash change
40 this._addToHistoryStack = false;
41 this._startTransitionEvent = false;
42
43 // push the default page to the history stack
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 // get the diff of window.history and application history
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 // summary:
70 // Response to dojox/app "startTransition" event.
71 //
72 // example:
73 // Use "dojox/mobile/TransitionEvent" to trigger "startTransition" event, and this function will response the event. For example:
74 // | var transOpts = {
75 // | title:"List",
76 // | target:"items,list",
77 // | url: "#items,list"
78 // | params: {"param1":"p1value"}
79 // | };
80 // | new TransitionEvent(domNode, transOpts, e).dispatch();
81 //
82 // evt: Object
83 // transition options parameter
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 // pushState on iOS will not change location bar hash because of security.
99 // window.history.pushState(evt.detail, title, currentHash);
100
101 // history.length will be changed by set location hash
102 // change url hash, to workaround iOS pushState not change address bar issue.
103 window.location.hash = currentHash;
104
105 // The operation above will trigger hashchange.
106 // Use _addToHistoryStack flag to indicate the _onHashChange method should add this hash to history stack.
107 // When add hash to history stack, this flag should be set to false, we do this in _addHistory.
108 this._addToHistoryStack = true;
109 //set startTransition event flag to true if the hash change from startTransition event.
110 this._startTransitionEvent = true;
111 },
112
113 _addHistory: function(hash){
114 // summary:
115 // Add hash to application history stack, update history management flags.
116 //
117 // hash:
118 // new hash should be added to _historyStack.
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 // In order to make sure _addToHistoryStack flag invalid after add hash to history stack,
135 // we set this flag to false in every addHistory operation even if it's already false.
136 this._addToHistoryStack = false;
137 },
138
139 _onHashChange: function(currentHash){
140 // summary:
141 // subscribe /dojo/hashchange and do add history, back, forward and go operation.
142 //
143 // currentHash:
144 // the new url hash when /dojo/hashchange is triggered.
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 // Application history stack asynchronized with window.history, refresh application history stack.
153 if(this._oldHistoryLen > this._newHistoryLen){
154 //console.log("need to refresh _historyStack, oldLen:"+this._oldHistoryLen+", newLen: "+this._newHistoryLen+", diff:"+this._historyDiff);
155 this._historyStack.splice((this._newHistoryLen - this._historyDiff - 1), (this._historyStack.length - 1));
156
157 // Reset _historyLen to make sure this._historyLen<window.history.length, so it will push this hash to history stack.
158 this._historyLen = this._historyStack.length;
159
160 // Reset this._oldHistoryLen, so it can avoid refresh history stack again in some situation,
161 // because by doing this, this._oldHistoryLen !== this._newHistoryLen
162 this._oldHistoryLen = 0;
163 }
164
165 // this._oldHistoryLen === this._newHistoryLen, it maybe need to refresh history stack or do history go, back and forward,
166 // so we use _addToHistoryStack to identify the refresh operation.
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 // It's a refresh operation, so that's no need to check history go, back or forward, just return.
172 return;
173 }
174
175 //window.history.length increase, add hash to application history stack.
176 if(this._historyLen < window.history.length){
177 this._addHistory(currentHash);
178 if(!this._startTransitionEvent){
179 // transition to the target view
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 // console.log("do nothing.");
188 }else if(currentHash === this._previous){ // back
189 this._back(currentHash, this._historyStack[this._index]["detail"]);
190 }else if(currentHash === this._next){ //forward
191 this._forward(currentHash, this._historyStack[this._index]["detail"]);
192 }else{ // go
193 //search in "back" first, then "forward"
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 //search in "forward"
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 // set startTransition event flag to false
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 // publish history back event
237 topic.publish("/app/history/back", {"viewId": target, "detail": detail});
238
239 // transition to the target view
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 // publish history forward event
260 topic.publish("/app/history/forward", {"viewId": target, "detail": detail});
261
262 // transition to the target view
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 // publish history go event
282 topic.publish("/app/history/go", {"viewId": target, "step": step, "detail": this._historyStack[index]["detail"]});
283
284 // transition to the target view
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 });