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
=== modified file 'lib/lp/app/javascript/lp.js'
--- lib/lp/app/javascript/lp.js 2011-10-27 11:36:13 +0000
+++ lib/lp/app/javascript/lp.js 2012-01-13 09:22:27 +0000
@@ -92,145 +92,3 @@
92 return query;92 return query;
93 };93 };
94}, "0.1", {"requires":["cookie", "lp.app.widgets.expander"]});94}, "0.1", {"requires":["cookie", "lp.app.widgets.expander"]});
95
96
97// Lint-safe scripting URL.
98var VOID_URL = '_:void(0);'.replace('_', 'javascript');
99
100
101function registerLaunchpadFunction(func) {
102 // registers a function to fire onload.
103 // Use this for initilaizing any javascript that should fire once the page
104 // has been loaded.
105 LPS.use('node', function(Y) {
106 Y.on('load', function(e) {
107 func();
108 }, window);
109 });
110}
111
112// Enable or disable the beta.launchpad.net redirect
113function setBetaRedirect(enable) {
114 var expire = new Date();
115 if (enable) {
116 expire.setTime(expire.getTime() + 1000);
117 document.cookie = ('inhibit_beta_redirect=0; Expires=' +
118 expire.toGMTString() + cookie_scope);
119 alert('Redirection to the beta site has been enabled');
120 } else {
121 expire.setTime(expire.getTime() + 2 * 60 * 60 * 1000);
122 document.cookie = ('inhibit_beta_redirect=1; Expires=' +
123 expire.toGMTString() + cookie_scope);
124 alert('You will not be redirected to the beta site for 2 hours');
125 }
126 return false;
127}
128
129function setFocusByName(name) {
130 // Focus the first element matching the given name which can be focused.
131 var nodes = document.getElementsByName(name);
132 var i, node;
133 for (i = 0; i < nodes.length; i++) {
134 node = nodes[i];
135 if (node.focus) {
136 try {
137 // Trying to focus a hidden element throws an error in IE8.
138 if (node.offsetHeight !== 0) {
139 node.focus();
140 }
141 } catch (e) {
142 LPS.use('console', function(Y) {
143 Y.log('In setFocusByName(<' +
144 node.tagName + ' type=' + node.type + '>): ' + e);
145 });
146 }
147 break;
148 }
149 }
150}
151
152function popup_window(url, name, width, height) {
153 var iframe = document.getElementById('popup_iframe_' + name);
154 if (!iframe.src || iframe.src === VOID_URL) {
155 // The first time this handler runs the window may not have been
156 // set up yet; sort that out.
157 iframe.style.width = width + 'px';
158 iframe.style.height = height + 'px';
159 iframe.style.position = 'absolute';
160 iframe.style.background = 'white';
161 iframe.src = url;
162 }
163 iframe.style.display = 'inline';
164 // I haven't found a way of making the search form focus again when
165 // the popup window is redisplayed. I tried doing an
166 // iframe.contentDocument.searchform.search.focus()
167 // but nothing happens.. -- kiko, 2007-03-12
168}
169
170function selectWidget(widget_name, event) {
171 if (event && (event.keyCode === 9 || event.keyCode === 13)) {
172 // Avoid firing if user is tabbing through or simply pressing
173 // enter to submit the form.
174 return;
175 }
176 document.getElementById(widget_name).checked = true;
177}
178
179function switchBugBranchFormAndWhiteboard(id) {
180 var div = document.getElementById('bugbranch' + id);
181 var wb = document.getElementById('bugbranch' + id + '-wb');
182
183 if (div.style.display === "none") {
184 /* Expanding the form */
185 if (wb !== null) {
186 wb.style.display = "none";
187 }
188 div.style.display = "block";
189 /* Use two focus calls to get the browser to scroll to the end of the
190 * form first, then focus back to the first field of the form.
191 */
192 document.getElementById('field'+id+'.actions.update').focus();
193 document.getElementById('field'+id+'.whiteboard').focus();
194 } else {
195 if (wb !== null) {
196 wb.style.display = "block";
197 }
198 div.style.display = "none";
199 }
200 return false;
201}
202
203function switchSpecBranchFormAndSummary(id) {
204 /* The document has two identifiable elements for each
205 * spec-branch link:
206 * 'specbranchX' which is the div containing the edit form
207 * 'specbranchX-summary' which is the div contining the sumary
208 * where X is the database id of the link.
209 */
210 var div = document.getElementById('specbranch' + id);
211 var wb = document.getElementById('specbranch' + id + '-summary');
212
213 if (div.style.display === "none") {
214 /* Expanding the form */
215 if (wb !== null) {
216 wb.style.display = "none";
217 }
218 div.style.display = "block";
219 /* Use two focus calls to get the browser to scroll to the end of the
220 * form first, then focus back to the first field of the form.
221 */
222 document.getElementById('field' + id + '.actions.change').focus();
223 document.getElementById('field' + id + '.summary').focus();
224 } else {
225 if (wb !== null) {
226 wb.style.display = "block";
227 }
228 div.style.display = "none";
229 }
230 return false;
231}
232
233function updateField(field, enabled)
234{
235 field.disabled = !enabled;
236}
23795
=== modified file 'lib/lp/app/templates/base-layout-macros.pt'
--- lib/lp/app/templates/base-layout-macros.pt 2012-01-12 11:51:26 +0000
+++ lib/lp/app/templates/base-layout-macros.pt 2012-01-13 09:22:27 +0000
@@ -90,7 +90,7 @@
9090
91 <metal:load-lavascript use-macro="context/@@+base-layout-macros/load-javascript" />91 <metal:load-lavascript use-macro="context/@@+base-layout-macros/load-javascript" />
9292
93 <script type="text/javascript">93 <script id="base-layout-load-scripts" type="text/javascript">
94 LPS.use('base', 'node', 'oop', 'event', 'lp.app.beta_features',94 LPS.use('base', 'node', 'oop', 'event', 'lp.app.beta_features',
95 'lp.bugs.bugtask_index', 'lp.bugs.subscribers',95 'lp.bugs.bugtask_index', 'lp.bugs.subscribers',
96 'lp.code.branchmergeproposal.diff', 'lp.comments.hide',96 'lp.code.branchmergeproposal.diff', 'lp.comments.hide',
@@ -103,32 +103,171 @@
103 Y.lp.app.beta_features.display_beta_notification();103 Y.lp.app.beta_features.display_beta_notification();
104 });104 });
105 });105 });
106
107 LPS.use('node', 'event-delegate', 'lp', 'lp.app.foldables', 'lp.app.links',
108 'lp.app.longpoll', 'lp.app.inlinehelp', 'lp.app.sorttable', function(Y) {
109 Y.on('load', function(e) {
110 Y.lp.app.sorttable.SortTable.init();
111 Y.lp.app.inlinehelp.init_help();
112 Y.lp.activate_collapsibles();
113 Y.lp.app.foldables.activate();
114 Y.lp.app.links.check_valid_lp_links();
115 // Longpolling will only start if
116 // LP.cache.longpoll is populated.
117 // We use Y.later to work around a Safari/Chrome 'feature':
118 // The mouse cursor stays 'busy' until all the requests started during
119 // page load are finished. Hence we want the long poll request to start
120 // right *after* the page has loaded.
121 Y.later(0, Y.lp.app.longpoll, Y.lp.app.longpoll.setupLongPollManager);
122 }, window);
123
124 Y.on('lp:context:web_link:changed', function(e) {
125 window.location = e.new_value;
126 });
127 });
128
129 // This code is pulled from lp.js that needs to be available on every
130 // request. Pulling here to get it outside the scope of the YUI block.
131 // Lint-safe scripting URL.
132 var VOID_URL = '_:void(0);'.replace('_', 'javascript');
133
134 function registerLaunchpadFunction(func) {
135 // registers a function to fire onload.
136 // Use this for initilaizing any javascript that should fire once the page
137 // has been loaded.
138 LPS.use('node', function(Y) {
139 Y.on('load', function(e) {
140 func();
141 }, window);
142 });
143 }
144
145 // Enable or disable the beta.launchpad.net redirect
146 function setBetaRedirect(enable) {
147 var expire = new Date();
148 if (enable) {
149 expire.setTime(expire.getTime() + 1000);
150 document.cookie = ('inhibit_beta_redirect=0; Expires=' +
151 expire.toGMTString() + cookie_scope);
152 alert('Redirection to the beta site has been enabled');
153 } else {
154 expire.setTime(expire.getTime() + 2 * 60 * 60 * 1000);
155 document.cookie = ('inhibit_beta_redirect=1; Expires=' +
156 expire.toGMTString() + cookie_scope);
157 alert('You will not be redirected to the beta site for 2 hours');
158 }
159 return false;
160 }
161
162 function setFocusByName(name) {
163 // Focus the first element matching the given name which can be focused.
164 var nodes = document.getElementsByName(name);
165 var i, node;
166 for (i = 0; i < nodes.length; i++) {
167 node = nodes[i];
168 if (node.focus) {
169 try {
170 // Trying to focus a hidden element throws an error in IE8.
171 if (node.offsetHeight !== 0) {
172 node.focus();
173 }
174 } catch (e) {
175 LPS.use('console', function(Y) {
176 Y.log('In setFocusByName(<' +
177 node.tagName + ' type=' + node.type + '>): ' + e);
178 });
179 }
180 break;
181 }
182 }
183 }
184
185 function popup_window(url, name, width, height) {
186 var iframe = document.getElementById('popup_iframe_' + name);
187 if (!iframe.src || iframe.src === VOID_URL) {
188 // The first time this handler runs the window may not have been
189 // set up yet; sort that out.
190 iframe.style.width = width + 'px';
191 iframe.style.height = height + 'px';
192 iframe.style.position = 'absolute';
193 iframe.style.background = 'white';
194 iframe.src = url;
195 }
196 iframe.style.display = 'inline';
197 // I haven't found a way of making the search form focus again when
198 // the popup window is redisplayed. I tried doing an
199 // iframe.contentDocument.searchform.search.focus()
200 // but nothing happens.. -- kiko, 2007-03-12
201 }
202
203 function selectWidget(widget_name, event) {
204 if (event && (event.keyCode === 9 || event.keyCode === 13)) {
205 // Avoid firing if user is tabbing through or simply pressing
206 // enter to submit the form.
207 return;
208 }
209 document.getElementById(widget_name).checked = true;
210 }
211
212 function switchBugBranchFormAndWhiteboard(id) {
213 var div = document.getElementById('bugbranch' + id);
214 var wb = document.getElementById('bugbranch' + id + '-wb');
215
216 if (div.style.display === "none") {
217 /* Expanding the form */
218 if (wb !== null) {
219 wb.style.display = "none";
220 }
221 div.style.display = "block";
222 /* Use two focus calls to get the browser to scroll to the end of the
223 * form first, then focus back to the first field of the form.
224 */
225 document.getElementById('field'+id+'.actions.update').focus();
226 document.getElementById('field'+id+'.whiteboard').focus();
227 } else {
228 if (wb !== null) {
229 wb.style.display = "block";
230 }
231 div.style.display = "none";
232 }
233 return false;
234 }
235
236 function switchSpecBranchFormAndSummary(id) {
237 /* The document has two identifiable elements for each
238 * spec-branch link:
239 * 'specbranchX' which is the div containing the edit form
240 * 'specbranchX-summary' which is the div contining the sumary
241 * where X is the database id of the link.
242 */
243 var div = document.getElementById('specbranch' + id);
244 var wb = document.getElementById('specbranch' + id + '-summary');
245
246 if (div.style.display === "none") {
247 /* Expanding the form */
248 if (wb !== null) {
249 wb.style.display = "none";
250 }
251 div.style.display = "block";
252 /* Use two focus calls to get the browser to scroll to the end of the
253 * form first, then focus back to the first field of the form.
254 */
255 document.getElementById('field' + id + '.actions.change').focus();
256 document.getElementById('field' + id + '.summary').focus();
257 } else {
258 if (wb !== null) {
259 wb.style.display = "block";
260 }
261 div.style.display = "none";
262 }
263 return false;
264 }
265
266 function updateField(field, enabled)
267 {
268 field.disabled = !enabled;
269 }
106 </script>270 </script>
107
108 <script id="base-layout-load-scripts" type="text/javascript">
109 LPS.use('node', 'event-delegate', 'lp', 'lp.app.foldables',
110 'lp.app.links', 'lp.app.sorttable', 'lp.app.longpoll',
111 'lp.app.inlinehelp', function(Y) {
112 Y.on('load', function(e) {
113 Y.lp.app.sorttable.SortTable.init();
114 Y.lp.app.inlinehelp.init_help();
115 Y.lp.activate_collapsibles();
116 Y.lp.app.foldables.activate();
117 Y.lp.app.links.check_valid_lp_links();
118 // Longpolling will only start if
119 // LP.cache.longpoll is populated.
120 // We use Y.later to work around a Safari/Chrome 'feature':
121 // The mouse cursor stays 'busy' until all the requests started during
122 // page load are finished. Hence we want the long poll request to start
123 // right *after* the page has loaded.
124 Y.later(0, Y.lp.app.longpoll, Y.lp.app.longpoll.setupLongPollManager);
125 }, window);
126
127 Y.on('lp:context:web_link:changed', function(e) {
128 window.location = e.new_value;
129 });
130 });
131 </script>
132</metal:page-javascript>271</metal:page-javascript>
133272
134273