1 | define([ |
---|
2 | 'dojo/_base/declare', |
---|
3 | 'dojo/hash', |
---|
4 | 'dojo/io-query', |
---|
5 | 'dojo/topic', |
---|
6 | './Content', |
---|
7 | './Page' |
---|
8 | ],function(declare,hash,ioQuery,topic,Content,Page){ |
---|
9 | |
---|
10 | var Router = declare(null,{ |
---|
11 | _started: false, |
---|
12 | _routes: null, |
---|
13 | _previousHash: null, |
---|
14 | |
---|
15 | _paramMatch: /:(\w[\w\d]*)/g, |
---|
16 | _paramReplace: "([^\\/!]+)", |
---|
17 | |
---|
18 | constructor: function() { |
---|
19 | this._routes = []; |
---|
20 | }, |
---|
21 | startup: function() { |
---|
22 | if ( this._started ) { return; } |
---|
23 | if ( !Content._started ) { |
---|
24 | Content.startup(); |
---|
25 | } |
---|
26 | this._started = true; |
---|
27 | |
---|
28 | var self = this; |
---|
29 | |
---|
30 | if ( hash() === "" ) { |
---|
31 | hash("#!/"); |
---|
32 | } |
---|
33 | this._handlePathChange(hash()); |
---|
34 | topic.subscribe("/dojo/hashchange", function(){ |
---|
35 | self._handlePathChange.apply(self,arguments); |
---|
36 | }); |
---|
37 | }, |
---|
38 | register: function(route) { |
---|
39 | if ( this._started ) { |
---|
40 | console.warn('Registering routes after startup() is called is discouraged.'); |
---|
41 | } |
---|
42 | var self = this; |
---|
43 | var callback; |
---|
44 | if ( route.callback ) { |
---|
45 | callback = function(params){ |
---|
46 | Content.set(route.callback(params)); |
---|
47 | }; |
---|
48 | } else if ( route.redirect ) { |
---|
49 | callback = function(params){ |
---|
50 | self.go(route.redirect); |
---|
51 | }; |
---|
52 | } else if ( route.constructor ) { |
---|
53 | callback = function(params){ |
---|
54 | Content.set( new route.constructor(params) ); |
---|
55 | }; |
---|
56 | } |
---|
57 | if ( callback ) { |
---|
58 | if ( route.path ) { |
---|
59 | this._routes.push(this._createRoute(route.path,callback)); |
---|
60 | } else { |
---|
61 | this._defaultCallback = callback; |
---|
62 | } |
---|
63 | } else { |
---|
64 | console.warn("Route "+(route.path||"default")+" has no action."); |
---|
65 | } |
---|
66 | }, |
---|
67 | _createRoute: function(path,callback) { |
---|
68 | var match, route = {}; |
---|
69 | route.callback = callback; |
---|
70 | route.paramNames = []; |
---|
71 | while((match = this._paramMatch.exec(path)) !== null){ |
---|
72 | route.paramNames.push(match[1]); |
---|
73 | } |
---|
74 | path = path.replace(this._paramMatch, this._paramReplace); |
---|
75 | route.regexp = new RegExp('^!'+path+'(!(.*))?$'); |
---|
76 | return route; |
---|
77 | }, |
---|
78 | _handlePathChange: function(newHash) { |
---|
79 | if ( this._previousHash === newHash ) { return; } |
---|
80 | this._previousHash = newHash; |
---|
81 | |
---|
82 | for (var i = this._routes.length-1; i >= 0; i--) { |
---|
83 | var result; |
---|
84 | var route = this._routes[i]; |
---|
85 | |
---|
86 | if ((result = route.regexp.exec(newHash)) !== null) { |
---|
87 | var numParams = route.paramNames.length; |
---|
88 | |
---|
89 | var params = {}; |
---|
90 | for (var j = 0; j < numParams; j++) { |
---|
91 | params[route.paramNames[j]] = result[j+1]; |
---|
92 | } |
---|
93 | |
---|
94 | if ( result.length > numParams+1 && result[numParams+2] ) { |
---|
95 | params.options = ioQuery.queryToObject(result[numParams+2]); |
---|
96 | } |
---|
97 | |
---|
98 | try { |
---|
99 | route.callback(params); |
---|
100 | } catch(err) { |
---|
101 | console.warn("Page change failed.",err); |
---|
102 | } |
---|
103 | return; |
---|
104 | } |
---|
105 | } |
---|
106 | |
---|
107 | try { |
---|
108 | this._defaultCallback(); |
---|
109 | } catch(err) { |
---|
110 | console.warn("Default page failed.",err); |
---|
111 | } |
---|
112 | }, |
---|
113 | go: function(path,args) { |
---|
114 | if ( !this._started ) { return; } |
---|
115 | var newHash = this._pathToHash(path,args); |
---|
116 | this._handlePathChange(newHash); |
---|
117 | hash(newHash); |
---|
118 | }, |
---|
119 | _pathToHash: function(path,args) { |
---|
120 | var hash = '!'+path; |
---|
121 | if ( args ) { |
---|
122 | hash += '!'+ioQuery.objectToQuery(args); |
---|
123 | } |
---|
124 | return hash; |
---|
125 | }, |
---|
126 | _defaultCallback: function() { |
---|
127 | Content.set(new Page({ |
---|
128 | templateString: "<div>Requested page not found. Go <a href=\"#!/\">home</a>.</div>" |
---|
129 | })); |
---|
130 | } |
---|
131 | }); |
---|
132 | |
---|
133 | return new Router(); |
---|
134 | }); |
---|