Merge lp:~rharding/launchpad/fix_lp.js into lp:launchpad

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
Reviewer Review Type Date Requested Status
Ian Booth (community) code Approve
Review via email: mp+88316@code.launchpad.net

Commit message

[r=wallyworld][no-qa] Move the global JS code from the lp.js into the base layout macro.

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.
Revision history for this message
Ian Booth (wallyworld) wrote :

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.

review: Approve (code)

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