]>
Commit | Line | Data |
---|---|---|
6527f429 DM |
1 | /**\r |
2 | * The Router is an ordered set of {@link Ext.app.route.Route} definitions that decode a\r | |
3 | * url into a controller function to execute. Each `route` defines a type of url to match,\r | |
4 | * along with the controller function to call if it is matched. The Router uses the\r | |
5 | * {@link Ext.util.History} singleton to find out when the browser's url has changed.\r | |
6 | *\r | |
7 | * Routes are almost always defined inside a {@link Ext.app.Controller Controller}, as\r | |
8 | * opposed to on the Router itself. End-developers should not usually need to interact\r | |
9 | * directly with the Router as the Controllers manage everything automatically. See the\r | |
10 | * {@link Ext.app.Controller Controller documentation} for more information on specifying\r | |
11 | * routes.\r | |
12 | *\r | |
13 | * @private\r | |
14 | */\r | |
15 | Ext.define('Ext.app.route.Router', {\r | |
16 | singleton : true,\r | |
17 | \r | |
18 | requires : [\r | |
19 | 'Ext.app.route.Queue',\r | |
20 | 'Ext.app.route.Route',\r | |
21 | 'Ext.util.History'\r | |
22 | ],\r | |
23 | \r | |
24 | /**\r | |
25 | * @property {String} [multipleToken=|] The token to split the routes to support multiple routes.\r | |
26 | */\r | |
27 | multipleToken: '|',\r | |
28 | \r | |
29 | /**\r | |
30 | * @property {Boolean} queueRoutes True to queue routes to be executed one after the\r | |
31 | * other, false to execute routes immediately.\r | |
32 | */\r | |
33 | queueRoutes: true,\r | |
34 | \r | |
35 | /**\r | |
36 | * @property {Ext.app.route.Route[]} routes The connected {@link Ext.app.route.Route}\r | |
37 | * instances.\r | |
38 | */\r | |
39 | \r | |
40 | constructor : function () {\r | |
41 | var History = Ext.util.History;\r | |
42 | \r | |
43 | if (!History.ready) {\r | |
44 | History.init();\r | |
45 | }\r | |
46 | \r | |
47 | History.on('change', this.onStateChange, this);\r | |
48 | this.clear();\r | |
49 | },\r | |
50 | \r | |
51 | /**\r | |
52 | * React to a token\r | |
53 | *\r | |
54 | * @private\r | |
55 | * @param {String} token The token to react to.\r | |
56 | */\r | |
57 | onStateChange : function (token) {\r | |
58 | var me = this,\r | |
59 | app = me.application,\r | |
60 | routes = me.routes,\r | |
61 | len = routes.length,\r | |
62 | queueRoutes = me.queueRoutes,\r | |
63 | tokens = token.split(me.multipleToken),\r | |
64 | t = 0,\r | |
65 | length = tokens.length,\r | |
66 | i, queue, route, args, matched;\r | |
67 | \r | |
68 | for (; t < length; t++) {\r | |
69 | token = tokens[t];\r | |
70 | matched = false;\r | |
71 | \r | |
72 | if (queueRoutes) {\r | |
73 | //create a queue\r | |
74 | queue = new Ext.app.route.Queue({\r | |
75 | token : token\r | |
76 | });\r | |
77 | }\r | |
78 | \r | |
79 | for (i = 0; i < len; i++) {\r | |
80 | route = routes[i];\r | |
81 | args = route.recognize(token);\r | |
82 | \r | |
83 | if (args) {\r | |
84 | matched = true;\r | |
85 | if (queueRoutes) {\r | |
86 | queue.queueAction(route, args);\r | |
87 | } else {\r | |
88 | route.execute(token, args);\r | |
89 | }\r | |
90 | }\r | |
91 | }\r | |
92 | \r | |
93 | if (queueRoutes) {\r | |
94 | //run the queue\r | |
95 | queue.runQueue();\r | |
96 | }\r | |
97 | \r | |
98 | if (!matched && app) {\r | |
99 | app.fireEvent('unmatchedroute', token);\r | |
100 | }\r | |
101 | }\r | |
102 | },\r | |
103 | \r | |
104 | /**\r | |
105 | * Create the {@link Ext.app.route.Route} instance and connect to the\r | |
106 | * {@link Ext.app.route.Router} singleton.\r | |
107 | *\r | |
108 | * @param {String} url The url to recognize.\r | |
109 | * @param {String} action The action on the controller to execute when the url is\r | |
110 | * matched.\r | |
111 | * @param {Ext.app.Controller} controller The controller associated with the\r | |
112 | * {@link Ext.app.route.Route}\r | |
113 | */\r | |
114 | connect : function (url, action, controller) {\r | |
115 | var config = {\r | |
116 | url : url,\r | |
117 | action : action,\r | |
118 | controller : controller\r | |
119 | };\r | |
120 | \r | |
121 | if (Ext.isObject(action)) {\r | |
122 | Ext.merge(config, action);\r | |
123 | }\r | |
124 | this.routes.push(new Ext.app.route.Route(config));\r | |
125 | },\r | |
126 | \r | |
127 | /**\r | |
128 | * Disconnects all routes for a controller.\r | |
129 | * @param {Ext.app.Controller} controller The controller to disconnect routes from.\r | |
130 | */\r | |
131 | disconnectAll: function(controller) {\r | |
132 | var routes = this.routes,\r | |
133 | len = routes.length,\r | |
134 | newRoutes = [],\r | |
135 | i, route;\r | |
136 | \r | |
137 | for (i = 0; i < len; ++i) {\r | |
138 | route = routes[i];\r | |
139 | if (route.controller !== controller) {\r | |
140 | newRoutes.push(route);\r | |
141 | }\r | |
142 | }\r | |
143 | this.routes = newRoutes;\r | |
144 | },\r | |
145 | \r | |
146 | /**\r | |
147 | * Recognizes a url string connected to the Router, return the controller/action pair\r | |
148 | * plus any additional config associated with it.\r | |
149 | *\r | |
150 | * @param {String} url The url to recognize.\r | |
151 | * @return {Object/Boolean} If the url was recognized, the controller and action to\r | |
152 | * call, else `false`.\r | |
153 | */\r | |
154 | recognize : function(url) {\r | |
155 | var routes = this.routes || [],\r | |
156 | i = 0,\r | |
157 | len = routes.length,\r | |
158 | route, args;\r | |
159 | \r | |
160 | for (; i < len; i++) {\r | |
161 | route = routes[i];\r | |
162 | args = route.recognize(url);\r | |
163 | \r | |
164 | if (args) {\r | |
165 | //route is recognized, return it and the arguments recognized if any\r | |
166 | return {\r | |
167 | route : route,\r | |
168 | args : args\r | |
169 | };\r | |
170 | }\r | |
171 | }\r | |
172 | \r | |
173 | return false;\r | |
174 | },\r | |
175 | \r | |
176 | /**\r | |
177 | * Convenience method which just calls the supplied function with the\r | |
178 | * {@link Ext.app.route.Router} singleton. Example usage:\r | |
179 | *\r | |
180 | * Ext.app.route.Router.draw(function(map) {\r | |
181 | * map.connect('activate/:token', {controller: 'users', action: 'activate'});\r | |
182 | * map.connect('home', {controller: 'index', action: 'home'});\r | |
183 | * });\r | |
184 | *\r | |
185 | * @param {Function} fn The function to call\r | |
186 | */\r | |
187 | draw : function(fn) {\r | |
188 | fn.call(this, this);\r | |
189 | },\r | |
190 | \r | |
191 | /**\r | |
192 | * Clear all the recognized routes.\r | |
193 | */\r | |
194 | clear : function() {\r | |
195 | this.routes = [];\r | |
196 | }\r | |
197 | });\r |