1 | define([ |
---|
2 | 'dojo/_base/declare', |
---|
3 | 'dojo/hash', |
---|
4 | 'dojo/topic', |
---|
5 | './Content', |
---|
6 | './Page', |
---|
7 | './Path' |
---|
8 | ],function(declare,hash,topic,Content,Page,Path){ |
---|
9 | |
---|
10 | var Router = declare(null,{ |
---|
11 | _started: false, |
---|
12 | _routes: null, |
---|
13 | _previousHash: null, |
---|
14 | |
---|
15 | constructor: function() { |
---|
16 | this._routes = []; |
---|
17 | }, |
---|
18 | startup: function() { |
---|
19 | if ( this._started ) { return; } |
---|
20 | if ( !Content._started ) { |
---|
21 | Content.startup(); |
---|
22 | } |
---|
23 | this._started = true; |
---|
24 | |
---|
25 | var self = this; |
---|
26 | |
---|
27 | if ( hash() === "" ) { |
---|
28 | hash(Path.getDefault()); |
---|
29 | } |
---|
30 | this._handlePathChange(hash()); |
---|
31 | topic.subscribe("/dojo/hashchange", function(){ |
---|
32 | self._handlePathChange.apply(self,arguments); |
---|
33 | }); |
---|
34 | }, |
---|
35 | register: function(route) { |
---|
36 | if ( this._started ) { |
---|
37 | console.warn('Registering routes after startup() is called is discouraged.'); |
---|
38 | } |
---|
39 | var callback = this._normalizeCallback(route); |
---|
40 | if ( callback ) { |
---|
41 | if ( route.path ) { |
---|
42 | route.callback = callback; |
---|
43 | route.path = new Path(route.path); |
---|
44 | this._routes.push(route); |
---|
45 | } else { |
---|
46 | this._defaultCallback = callback; |
---|
47 | } |
---|
48 | } else { |
---|
49 | console.warn("Route "+(route.path||"default")+" has no action."); |
---|
50 | } |
---|
51 | }, |
---|
52 | _normalizeCallback: function(route) { |
---|
53 | var self = this; |
---|
54 | var callback = null; |
---|
55 | if ( route.callback ) { |
---|
56 | callback = function(params){ |
---|
57 | Content.set(route.callback(params)); |
---|
58 | }; |
---|
59 | } else if ( route.redirect ) { |
---|
60 | callback = function(params){ |
---|
61 | self.go(route.redirect); |
---|
62 | }; |
---|
63 | } else if ( route.constructor ) { |
---|
64 | callback = function(params){ |
---|
65 | Content.set( new route.constructor(params) ); |
---|
66 | }; |
---|
67 | } |
---|
68 | return callback; |
---|
69 | }, |
---|
70 | _handlePathChange: function(newHash) { |
---|
71 | if ( this._previousHash === newHash ) { return; } |
---|
72 | this._previousHash = newHash; |
---|
73 | for (var i = this._routes.length-1; i >= 0; i--) { |
---|
74 | var route = this._routes[i]; |
---|
75 | var params = null; |
---|
76 | if ((params = route.path.match(newHash)) !== null) { |
---|
77 | try { |
---|
78 | route.callback(params); |
---|
79 | } catch(err) { |
---|
80 | console.error("Page change failed with",err,err.toString()); |
---|
81 | } |
---|
82 | return; |
---|
83 | } |
---|
84 | } |
---|
85 | try { |
---|
86 | this._defaultCallback(); |
---|
87 | } catch(err) { |
---|
88 | console.error("Default page failed.",err); |
---|
89 | } |
---|
90 | }, |
---|
91 | go: function(path,args) { |
---|
92 | if ( !this._started ) { return; } |
---|
93 | var newHash = Path.format(path,args); |
---|
94 | this._handlePathChange(newHash); |
---|
95 | hash(newHash); |
---|
96 | }, |
---|
97 | _defaultCallback: function() { |
---|
98 | Content.set(new Page({ |
---|
99 | templateString: "<div>Requested page not found. Go <a href=\"#"+Path.getDefault()+"\">home</a>.</div>" |
---|
100 | })); |
---|
101 | } |
---|
102 | }); |
---|
103 | |
---|
104 | return new Router(); |
---|
105 | }); |
---|