Merge lp:~rharding/launchpad/fix_lp.js into lp:launchpad
- fix_lp.js
- Merge into devel
Proposed by
Richard Harding
Status: | Merged |
---|---|
Approved by: | Ian Booth |
Approved revision: | no longer in the source branch. |
Merged at revision: | 14675 |
Proposed branch: | lp:~rharding/launchpad/fix_lp.js |
Merge into: | lp:launchpad |
Diff against target: |
357 lines (+165/-168) 2 files modified
lib/lp/app/javascript/lp.js (+0/-142) lib/lp/app/templates/base-layout-macros.pt (+165/-26) |
To merge this branch: | bzr merge lp:~rharding/launchpad/fix_lp.js |
Related bugs: |
Reviewer | Review Type | Date Requested | Status |
---|---|---|---|
Ian Booth (community) | code | Approve | |
Review via email: mp+88316@code.launchpad.net |
Commit message
[r=wallyworld]
Description of the change
= Summary =
There is global JS code in the lp.js which is a YUI module. This should be made more obvious and not associated with a YUI module.
== Proposed Fix ==
Move the global code into the JS block of the base layout macro outside of any YUI block.
To post a comment you must log in.
Preview Diff
[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
1 | === modified file 'lib/lp/app/javascript/lp.js' | |||
2 | --- lib/lp/app/javascript/lp.js 2011-10-27 11:36:13 +0000 | |||
3 | +++ lib/lp/app/javascript/lp.js 2012-01-13 09:22:27 +0000 | |||
4 | @@ -92,145 +92,3 @@ | |||
5 | 92 | return query; | 92 | return query; |
6 | 93 | }; | 93 | }; |
7 | 94 | }, "0.1", {"requires":["cookie", "lp.app.widgets.expander"]}); | 94 | }, "0.1", {"requires":["cookie", "lp.app.widgets.expander"]}); |
8 | 95 | |||
9 | 96 | |||
10 | 97 | // Lint-safe scripting URL. | ||
11 | 98 | var VOID_URL = '_:void(0);'.replace('_', 'javascript'); | ||
12 | 99 | |||
13 | 100 | |||
14 | 101 | function registerLaunchpadFunction(func) { | ||
15 | 102 | // registers a function to fire onload. | ||
16 | 103 | // Use this for initilaizing any javascript that should fire once the page | ||
17 | 104 | // has been loaded. | ||
18 | 105 | LPS.use('node', function(Y) { | ||
19 | 106 | Y.on('load', function(e) { | ||
20 | 107 | func(); | ||
21 | 108 | }, window); | ||
22 | 109 | }); | ||
23 | 110 | } | ||
24 | 111 | |||
25 | 112 | // Enable or disable the beta.launchpad.net redirect | ||
26 | 113 | function setBetaRedirect(enable) { | ||
27 | 114 | var expire = new Date(); | ||
28 | 115 | if (enable) { | ||
29 | 116 | expire.setTime(expire.getTime() + 1000); | ||
30 | 117 | document.cookie = ('inhibit_beta_redirect=0; Expires=' + | ||
31 | 118 | expire.toGMTString() + cookie_scope); | ||
32 | 119 | alert('Redirection to the beta site has been enabled'); | ||
33 | 120 | } else { | ||
34 | 121 | expire.setTime(expire.getTime() + 2 * 60 * 60 * 1000); | ||
35 | 122 | document.cookie = ('inhibit_beta_redirect=1; Expires=' + | ||
36 | 123 | expire.toGMTString() + cookie_scope); | ||
37 | 124 | alert('You will not be redirected to the beta site for 2 hours'); | ||
38 | 125 | } | ||
39 | 126 | return false; | ||
40 | 127 | } | ||
41 | 128 | |||
42 | 129 | function setFocusByName(name) { | ||
43 | 130 | // Focus the first element matching the given name which can be focused. | ||
44 | 131 | var nodes = document.getElementsByName(name); | ||
45 | 132 | var i, node; | ||
46 | 133 | for (i = 0; i < nodes.length; i++) { | ||
47 | 134 | node = nodes[i]; | ||
48 | 135 | if (node.focus) { | ||
49 | 136 | try { | ||
50 | 137 | // Trying to focus a hidden element throws an error in IE8. | ||
51 | 138 | if (node.offsetHeight !== 0) { | ||
52 | 139 | node.focus(); | ||
53 | 140 | } | ||
54 | 141 | } catch (e) { | ||
55 | 142 | LPS.use('console', function(Y) { | ||
56 | 143 | Y.log('In setFocusByName(<' + | ||
57 | 144 | node.tagName + ' type=' + node.type + '>): ' + e); | ||
58 | 145 | }); | ||
59 | 146 | } | ||
60 | 147 | break; | ||
61 | 148 | } | ||
62 | 149 | } | ||
63 | 150 | } | ||
64 | 151 | |||
65 | 152 | function popup_window(url, name, width, height) { | ||
66 | 153 | var iframe = document.getElementById('popup_iframe_' + name); | ||
67 | 154 | if (!iframe.src || iframe.src === VOID_URL) { | ||
68 | 155 | // The first time this handler runs the window may not have been | ||
69 | 156 | // set up yet; sort that out. | ||
70 | 157 | iframe.style.width = width + 'px'; | ||
71 | 158 | iframe.style.height = height + 'px'; | ||
72 | 159 | iframe.style.position = 'absolute'; | ||
73 | 160 | iframe.style.background = 'white'; | ||
74 | 161 | iframe.src = url; | ||
75 | 162 | } | ||
76 | 163 | iframe.style.display = 'inline'; | ||
77 | 164 | // I haven't found a way of making the search form focus again when | ||
78 | 165 | // the popup window is redisplayed. I tried doing an | ||
79 | 166 | // iframe.contentDocument.searchform.search.focus() | ||
80 | 167 | // but nothing happens.. -- kiko, 2007-03-12 | ||
81 | 168 | } | ||
82 | 169 | |||
83 | 170 | function selectWidget(widget_name, event) { | ||
84 | 171 | if (event && (event.keyCode === 9 || event.keyCode === 13)) { | ||
85 | 172 | // Avoid firing if user is tabbing through or simply pressing | ||
86 | 173 | // enter to submit the form. | ||
87 | 174 | return; | ||
88 | 175 | } | ||
89 | 176 | document.getElementById(widget_name).checked = true; | ||
90 | 177 | } | ||
91 | 178 | |||
92 | 179 | function switchBugBranchFormAndWhiteboard(id) { | ||
93 | 180 | var div = document.getElementById('bugbranch' + id); | ||
94 | 181 | var wb = document.getElementById('bugbranch' + id + '-wb'); | ||
95 | 182 | |||
96 | 183 | if (div.style.display === "none") { | ||
97 | 184 | /* Expanding the form */ | ||
98 | 185 | if (wb !== null) { | ||
99 | 186 | wb.style.display = "none"; | ||
100 | 187 | } | ||
101 | 188 | div.style.display = "block"; | ||
102 | 189 | /* Use two focus calls to get the browser to scroll to the end of the | ||
103 | 190 | * form first, then focus back to the first field of the form. | ||
104 | 191 | */ | ||
105 | 192 | document.getElementById('field'+id+'.actions.update').focus(); | ||
106 | 193 | document.getElementById('field'+id+'.whiteboard').focus(); | ||
107 | 194 | } else { | ||
108 | 195 | if (wb !== null) { | ||
109 | 196 | wb.style.display = "block"; | ||
110 | 197 | } | ||
111 | 198 | div.style.display = "none"; | ||
112 | 199 | } | ||
113 | 200 | return false; | ||
114 | 201 | } | ||
115 | 202 | |||
116 | 203 | function switchSpecBranchFormAndSummary(id) { | ||
117 | 204 | /* The document has two identifiable elements for each | ||
118 | 205 | * spec-branch link: | ||
119 | 206 | * 'specbranchX' which is the div containing the edit form | ||
120 | 207 | * 'specbranchX-summary' which is the div contining the sumary | ||
121 | 208 | * where X is the database id of the link. | ||
122 | 209 | */ | ||
123 | 210 | var div = document.getElementById('specbranch' + id); | ||
124 | 211 | var wb = document.getElementById('specbranch' + id + '-summary'); | ||
125 | 212 | |||
126 | 213 | if (div.style.display === "none") { | ||
127 | 214 | /* Expanding the form */ | ||
128 | 215 | if (wb !== null) { | ||
129 | 216 | wb.style.display = "none"; | ||
130 | 217 | } | ||
131 | 218 | div.style.display = "block"; | ||
132 | 219 | /* Use two focus calls to get the browser to scroll to the end of the | ||
133 | 220 | * form first, then focus back to the first field of the form. | ||
134 | 221 | */ | ||
135 | 222 | document.getElementById('field' + id + '.actions.change').focus(); | ||
136 | 223 | document.getElementById('field' + id + '.summary').focus(); | ||
137 | 224 | } else { | ||
138 | 225 | if (wb !== null) { | ||
139 | 226 | wb.style.display = "block"; | ||
140 | 227 | } | ||
141 | 228 | div.style.display = "none"; | ||
142 | 229 | } | ||
143 | 230 | return false; | ||
144 | 231 | } | ||
145 | 232 | |||
146 | 233 | function updateField(field, enabled) | ||
147 | 234 | { | ||
148 | 235 | field.disabled = !enabled; | ||
149 | 236 | } | ||
150 | 237 | 95 | ||
151 | === modified file 'lib/lp/app/templates/base-layout-macros.pt' | |||
152 | --- lib/lp/app/templates/base-layout-macros.pt 2012-01-12 11:51:26 +0000 | |||
153 | +++ lib/lp/app/templates/base-layout-macros.pt 2012-01-13 09:22:27 +0000 | |||
154 | @@ -90,7 +90,7 @@ | |||
155 | 90 | 90 | ||
156 | 91 | <metal:load-lavascript use-macro="context/@@+base-layout-macros/load-javascript" /> | 91 | <metal:load-lavascript use-macro="context/@@+base-layout-macros/load-javascript" /> |
157 | 92 | 92 | ||
159 | 93 | <script type="text/javascript"> | 93 | <script id="base-layout-load-scripts" type="text/javascript"> |
160 | 94 | LPS.use('base', 'node', 'oop', 'event', 'lp.app.beta_features', | 94 | LPS.use('base', 'node', 'oop', 'event', 'lp.app.beta_features', |
161 | 95 | 'lp.bugs.bugtask_index', 'lp.bugs.subscribers', | 95 | 'lp.bugs.bugtask_index', 'lp.bugs.subscribers', |
162 | 96 | 'lp.code.branchmergeproposal.diff', 'lp.comments.hide', | 96 | 'lp.code.branchmergeproposal.diff', 'lp.comments.hide', |
163 | @@ -103,32 +103,171 @@ | |||
164 | 103 | Y.lp.app.beta_features.display_beta_notification(); | 103 | Y.lp.app.beta_features.display_beta_notification(); |
165 | 104 | }); | 104 | }); |
166 | 105 | }); | 105 | }); |
167 | 106 | |||
168 | 107 | LPS.use('node', 'event-delegate', 'lp', 'lp.app.foldables', 'lp.app.links', | ||
169 | 108 | 'lp.app.longpoll', 'lp.app.inlinehelp', 'lp.app.sorttable', function(Y) { | ||
170 | 109 | Y.on('load', function(e) { | ||
171 | 110 | Y.lp.app.sorttable.SortTable.init(); | ||
172 | 111 | Y.lp.app.inlinehelp.init_help(); | ||
173 | 112 | Y.lp.activate_collapsibles(); | ||
174 | 113 | Y.lp.app.foldables.activate(); | ||
175 | 114 | Y.lp.app.links.check_valid_lp_links(); | ||
176 | 115 | // Longpolling will only start if | ||
177 | 116 | // LP.cache.longpoll is populated. | ||
178 | 117 | // We use Y.later to work around a Safari/Chrome 'feature': | ||
179 | 118 | // The mouse cursor stays 'busy' until all the requests started during | ||
180 | 119 | // page load are finished. Hence we want the long poll request to start | ||
181 | 120 | // right *after* the page has loaded. | ||
182 | 121 | Y.later(0, Y.lp.app.longpoll, Y.lp.app.longpoll.setupLongPollManager); | ||
183 | 122 | }, window); | ||
184 | 123 | |||
185 | 124 | Y.on('lp:context:web_link:changed', function(e) { | ||
186 | 125 | window.location = e.new_value; | ||
187 | 126 | }); | ||
188 | 127 | }); | ||
189 | 128 | |||
190 | 129 | // This code is pulled from lp.js that needs to be available on every | ||
191 | 130 | // request. Pulling here to get it outside the scope of the YUI block. | ||
192 | 131 | // Lint-safe scripting URL. | ||
193 | 132 | var VOID_URL = '_:void(0);'.replace('_', 'javascript'); | ||
194 | 133 | |||
195 | 134 | function registerLaunchpadFunction(func) { | ||
196 | 135 | // registers a function to fire onload. | ||
197 | 136 | // Use this for initilaizing any javascript that should fire once the page | ||
198 | 137 | // has been loaded. | ||
199 | 138 | LPS.use('node', function(Y) { | ||
200 | 139 | Y.on('load', function(e) { | ||
201 | 140 | func(); | ||
202 | 141 | }, window); | ||
203 | 142 | }); | ||
204 | 143 | } | ||
205 | 144 | |||
206 | 145 | // Enable or disable the beta.launchpad.net redirect | ||
207 | 146 | function setBetaRedirect(enable) { | ||
208 | 147 | var expire = new Date(); | ||
209 | 148 | if (enable) { | ||
210 | 149 | expire.setTime(expire.getTime() + 1000); | ||
211 | 150 | document.cookie = ('inhibit_beta_redirect=0; Expires=' + | ||
212 | 151 | expire.toGMTString() + cookie_scope); | ||
213 | 152 | alert('Redirection to the beta site has been enabled'); | ||
214 | 153 | } else { | ||
215 | 154 | expire.setTime(expire.getTime() + 2 * 60 * 60 * 1000); | ||
216 | 155 | document.cookie = ('inhibit_beta_redirect=1; Expires=' + | ||
217 | 156 | expire.toGMTString() + cookie_scope); | ||
218 | 157 | alert('You will not be redirected to the beta site for 2 hours'); | ||
219 | 158 | } | ||
220 | 159 | return false; | ||
221 | 160 | } | ||
222 | 161 | |||
223 | 162 | function setFocusByName(name) { | ||
224 | 163 | // Focus the first element matching the given name which can be focused. | ||
225 | 164 | var nodes = document.getElementsByName(name); | ||
226 | 165 | var i, node; | ||
227 | 166 | for (i = 0; i < nodes.length; i++) { | ||
228 | 167 | node = nodes[i]; | ||
229 | 168 | if (node.focus) { | ||
230 | 169 | try { | ||
231 | 170 | // Trying to focus a hidden element throws an error in IE8. | ||
232 | 171 | if (node.offsetHeight !== 0) { | ||
233 | 172 | node.focus(); | ||
234 | 173 | } | ||
235 | 174 | } catch (e) { | ||
236 | 175 | LPS.use('console', function(Y) { | ||
237 | 176 | Y.log('In setFocusByName(<' + | ||
238 | 177 | node.tagName + ' type=' + node.type + '>): ' + e); | ||
239 | 178 | }); | ||
240 | 179 | } | ||
241 | 180 | break; | ||
242 | 181 | } | ||
243 | 182 | } | ||
244 | 183 | } | ||
245 | 184 | |||
246 | 185 | function popup_window(url, name, width, height) { | ||
247 | 186 | var iframe = document.getElementById('popup_iframe_' + name); | ||
248 | 187 | if (!iframe.src || iframe.src === VOID_URL) { | ||
249 | 188 | // The first time this handler runs the window may not have been | ||
250 | 189 | // set up yet; sort that out. | ||
251 | 190 | iframe.style.width = width + 'px'; | ||
252 | 191 | iframe.style.height = height + 'px'; | ||
253 | 192 | iframe.style.position = 'absolute'; | ||
254 | 193 | iframe.style.background = 'white'; | ||
255 | 194 | iframe.src = url; | ||
256 | 195 | } | ||
257 | 196 | iframe.style.display = 'inline'; | ||
258 | 197 | // I haven't found a way of making the search form focus again when | ||
259 | 198 | // the popup window is redisplayed. I tried doing an | ||
260 | 199 | // iframe.contentDocument.searchform.search.focus() | ||
261 | 200 | // but nothing happens.. -- kiko, 2007-03-12 | ||
262 | 201 | } | ||
263 | 202 | |||
264 | 203 | function selectWidget(widget_name, event) { | ||
265 | 204 | if (event && (event.keyCode === 9 || event.keyCode === 13)) { | ||
266 | 205 | // Avoid firing if user is tabbing through or simply pressing | ||
267 | 206 | // enter to submit the form. | ||
268 | 207 | return; | ||
269 | 208 | } | ||
270 | 209 | document.getElementById(widget_name).checked = true; | ||
271 | 210 | } | ||
272 | 211 | |||
273 | 212 | function switchBugBranchFormAndWhiteboard(id) { | ||
274 | 213 | var div = document.getElementById('bugbranch' + id); | ||
275 | 214 | var wb = document.getElementById('bugbranch' + id + '-wb'); | ||
276 | 215 | |||
277 | 216 | if (div.style.display === "none") { | ||
278 | 217 | /* Expanding the form */ | ||
279 | 218 | if (wb !== null) { | ||
280 | 219 | wb.style.display = "none"; | ||
281 | 220 | } | ||
282 | 221 | div.style.display = "block"; | ||
283 | 222 | /* Use two focus calls to get the browser to scroll to the end of the | ||
284 | 223 | * form first, then focus back to the first field of the form. | ||
285 | 224 | */ | ||
286 | 225 | document.getElementById('field'+id+'.actions.update').focus(); | ||
287 | 226 | document.getElementById('field'+id+'.whiteboard').focus(); | ||
288 | 227 | } else { | ||
289 | 228 | if (wb !== null) { | ||
290 | 229 | wb.style.display = "block"; | ||
291 | 230 | } | ||
292 | 231 | div.style.display = "none"; | ||
293 | 232 | } | ||
294 | 233 | return false; | ||
295 | 234 | } | ||
296 | 235 | |||
297 | 236 | function switchSpecBranchFormAndSummary(id) { | ||
298 | 237 | /* The document has two identifiable elements for each | ||
299 | 238 | * spec-branch link: | ||
300 | 239 | * 'specbranchX' which is the div containing the edit form | ||
301 | 240 | * 'specbranchX-summary' which is the div contining the sumary | ||
302 | 241 | * where X is the database id of the link. | ||
303 | 242 | */ | ||
304 | 243 | var div = document.getElementById('specbranch' + id); | ||
305 | 244 | var wb = document.getElementById('specbranch' + id + '-summary'); | ||
306 | 245 | |||
307 | 246 | if (div.style.display === "none") { | ||
308 | 247 | /* Expanding the form */ | ||
309 | 248 | if (wb !== null) { | ||
310 | 249 | wb.style.display = "none"; | ||
311 | 250 | } | ||
312 | 251 | div.style.display = "block"; | ||
313 | 252 | /* Use two focus calls to get the browser to scroll to the end of the | ||
314 | 253 | * form first, then focus back to the first field of the form. | ||
315 | 254 | */ | ||
316 | 255 | document.getElementById('field' + id + '.actions.change').focus(); | ||
317 | 256 | document.getElementById('field' + id + '.summary').focus(); | ||
318 | 257 | } else { | ||
319 | 258 | if (wb !== null) { | ||
320 | 259 | wb.style.display = "block"; | ||
321 | 260 | } | ||
322 | 261 | div.style.display = "none"; | ||
323 | 262 | } | ||
324 | 263 | return false; | ||
325 | 264 | } | ||
326 | 265 | |||
327 | 266 | function updateField(field, enabled) | ||
328 | 267 | { | ||
329 | 268 | field.disabled = !enabled; | ||
330 | 269 | } | ||
331 | 106 | </script> | 270 | </script> |
332 | 107 | |||
333 | 108 | <script id="base-layout-load-scripts" type="text/javascript"> | ||
334 | 109 | LPS.use('node', 'event-delegate', 'lp', 'lp.app.foldables', | ||
335 | 110 | 'lp.app.links', 'lp.app.sorttable', 'lp.app.longpoll', | ||
336 | 111 | 'lp.app.inlinehelp', function(Y) { | ||
337 | 112 | Y.on('load', function(e) { | ||
338 | 113 | Y.lp.app.sorttable.SortTable.init(); | ||
339 | 114 | Y.lp.app.inlinehelp.init_help(); | ||
340 | 115 | Y.lp.activate_collapsibles(); | ||
341 | 116 | Y.lp.app.foldables.activate(); | ||
342 | 117 | Y.lp.app.links.check_valid_lp_links(); | ||
343 | 118 | // Longpolling will only start if | ||
344 | 119 | // LP.cache.longpoll is populated. | ||
345 | 120 | // We use Y.later to work around a Safari/Chrome 'feature': | ||
346 | 121 | // The mouse cursor stays 'busy' until all the requests started during | ||
347 | 122 | // page load are finished. Hence we want the long poll request to start | ||
348 | 123 | // right *after* the page has loaded. | ||
349 | 124 | Y.later(0, Y.lp.app.longpoll, Y.lp.app.longpoll.setupLongPollManager); | ||
350 | 125 | }, window); | ||
351 | 126 | |||
352 | 127 | Y.on('lp:context:web_link:changed', function(e) { | ||
353 | 128 | window.location = e.new_value; | ||
354 | 129 | }); | ||
355 | 130 | }); | ||
356 | 131 | </script> | ||
357 | 132 | </metal:page-javascript> | 271 | </metal:page-javascript> |
358 | 133 | 272 | ||
359 | 134 | 273 |
I feel dirty, and will hate myself in the morning, but +1 for now. So long as we commit to taking the next step to clean this up.