← Back to team overview

launchpad-reviewers team mailing list archive

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

 

Richard Harding has proposed merging lp:~rharding/launchpad/fix_lp.js into lp:launchpad.

Requested reviews:
  Launchpad code reviewers (launchpad-reviewers)

For more details, see:
https://code.launchpad.net/~rharding/launchpad/fix_lp.js/+merge/88316

= 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. 

-- 
https://code.launchpad.net/~rharding/launchpad/fix_lp.js/+merge/88316
Your team Launchpad code reviewers is requested to review the proposed merge of lp:~rharding/launchpad/fix_lp.js into lp:launchpad.
=== 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-12 08:56:28 +0000
@@ -92,145 +92,3 @@
         return query;
     };
 }, "0.1", {"requires":["cookie", "lp.app.widgets.expander"]});
-
-
-// Lint-safe scripting URL.
-var VOID_URL = '_:void(0);'.replace('_', 'javascript');
-
-
-function registerLaunchpadFunction(func) {
-    // registers a function to fire onload.
-    // Use this for initilaizing any javascript that should fire once the page
-    // has been loaded.
-    LPS.use('node', function(Y) {
-        Y.on('load', function(e) {
-            func();
-        }, window);
-    });
-}
-
-// Enable or disable the beta.launchpad.net redirect
-function setBetaRedirect(enable) {
-    var expire = new Date();
-    if (enable) {
-        expire.setTime(expire.getTime() + 1000);
-        document.cookie = ('inhibit_beta_redirect=0; Expires=' +
-                           expire.toGMTString() + cookie_scope);
-        alert('Redirection to the beta site has been enabled');
-    } else {
-        expire.setTime(expire.getTime() + 2 * 60 * 60 * 1000);
-        document.cookie = ('inhibit_beta_redirect=1; Expires=' +
-                           expire.toGMTString() + cookie_scope);
-        alert('You will not be redirected to the beta site for 2 hours');
-    }
-    return false;
-}
-
-function setFocusByName(name) {
-    // Focus the first element matching the given name which can be focused.
-    var nodes = document.getElementsByName(name);
-    var i, node;
-    for (i = 0; i < nodes.length; i++) {
-        node = nodes[i];
-        if (node.focus) {
-            try {
-                // Trying to focus a hidden element throws an error in IE8.
-                if (node.offsetHeight !== 0) {
-                    node.focus();
-                }
-            } catch (e) {
-                LPS.use('console', function(Y) {
-                    Y.log('In setFocusByName(<' +
-                        node.tagName + ' type=' + node.type + '>): ' + e);
-                });
-            }
-            break;
-        }
-    }
-}
-
-function popup_window(url, name, width, height) {
-    var iframe = document.getElementById('popup_iframe_' + name);
-    if (!iframe.src || iframe.src === VOID_URL) {
-        // The first time this handler runs the window may not have been
-        // set up yet; sort that out.
-        iframe.style.width = width + 'px';
-        iframe.style.height = height + 'px';
-        iframe.style.position = 'absolute';
-        iframe.style.background = 'white';
-        iframe.src = url;
-    }
-    iframe.style.display = 'inline';
-    // I haven't found a way of making the search form focus again when
-    // the popup window is redisplayed. I tried doing an
-    //    iframe.contentDocument.searchform.search.focus()
-    // but nothing happens.. -- kiko, 2007-03-12
-}
-
-function selectWidget(widget_name, event) {
-  if (event && (event.keyCode === 9 || event.keyCode === 13)) {
-      // Avoid firing if user is tabbing through or simply pressing
-      // enter to submit the form.
-      return;
-  }
-  document.getElementById(widget_name).checked = true;
-}
-
-function switchBugBranchFormAndWhiteboard(id) {
-    var div = document.getElementById('bugbranch' + id);
-    var wb = document.getElementById('bugbranch' + id + '-wb');
-
-    if (div.style.display === "none") {
-        /* Expanding the form */
-        if (wb !== null) {
-            wb.style.display = "none";
-        }
-        div.style.display = "block";
-        /* Use two focus calls to get the browser to scroll to the end of the
-         * form first, then focus back to the first field of the form.
-         */
-        document.getElementById('field'+id+'.actions.update').focus();
-        document.getElementById('field'+id+'.whiteboard').focus();
-    } else {
-        if (wb !== null) {
-            wb.style.display = "block";
-        }
-        div.style.display = "none";
-    }
-    return false;
-}
-
-function switchSpecBranchFormAndSummary(id) {
-    /* The document has two identifiable elements for each
-     * spec-branch link:
-     *    'specbranchX' which is the div containing the edit form
-     *    'specbranchX-summary' which is the div contining the sumary
-     * where X is the database id of the link.
-     */
-    var div = document.getElementById('specbranch' + id);
-    var wb = document.getElementById('specbranch' + id + '-summary');
-
-    if (div.style.display === "none") {
-        /* Expanding the form */
-        if (wb !== null) {
-            wb.style.display = "none";
-        }
-        div.style.display = "block";
-        /* Use two focus calls to get the browser to scroll to the end of the
-         * form first, then focus back to the first field of the form.
-         */
-        document.getElementById('field' + id + '.actions.change').focus();
-        document.getElementById('field' + id + '.summary').focus();
-    } else {
-        if (wb !== null) {
-            wb.style.display = "block";
-        }
-        div.style.display = "none";
-    }
-    return false;
-}
-
-function updateField(field, enabled)
-{
-    field.disabled = !enabled;
-}

=== modified file 'lib/lp/app/templates/base-layout-macros.pt'
--- lib/lp/app/templates/base-layout-macros.pt	2012-01-11 13:59:39 +0000
+++ lib/lp/app/templates/base-layout-macros.pt	2012-01-12 08:56:28 +0000
@@ -90,7 +90,7 @@
 
   <metal:load-lavascript use-macro="context/@@+base-layout-macros/load-javascript" />
 
-    <script type="text/javascript">
+    <script id="base-layout-load-scripts" type="text/javascript">
       LPS.use('base', 'node', 'oop', 'event', 'lp.app.beta_features',
               'lp.bugs.bugtask_index', 'lp.bugs.subscribers',
                 'lp.code.branchmergeproposal.diff', 'lp.comments.hide',
@@ -103,31 +103,171 @@
               Y.lp.app.beta_features.display_beta_notification();
           });
       });
+
+      LPS.use('node', 'event-delegate', 'lp', 'lp.app.foldables', 'lp.app.links',
+          'lp.app.longpoll', 'lp.app.inlinehelp', function(Y) {
+          Y.on('load', function(e) {
+              sortables_init();
+              Y.lp.app.inlinehelp.init_help();
+              Y.lp.activate_collapsibles();
+              Y.lp.app.foldables.activate();
+              Y.lp.app.links.check_valid_lp_links();
+              // Longpolling will only start if
+              // LP.cache.longpoll is populated.
+              // We use Y.later to work around a Safari/Chrome 'feature':
+              // The mouse cursor stays 'busy' until all the requests started during
+              // page load are finished.  Hence we want the long poll request to start
+              // right *after* the page has loaded.
+              Y.later(0, Y.lp.app.longpoll, Y.lp.app.longpoll.setupLongPollManager);
+          }, window);
+
+          Y.on('lp:context:web_link:changed', function(e) {
+              window.location = e.new_value;
+          });
+      });
+
+      // This code is pulled from lp.js that needs to be available on every
+      // request. Pulling here to get it outside the scope of the YUI block.
+      // Lint-safe scripting URL.
+      var VOID_URL = '_:void(0);'.replace('_', 'javascript');
+
+      function registerLaunchpadFunction(func) {
+          // registers a function to fire onload.
+          // Use this for initilaizing any javascript that should fire once the page
+          // has been loaded.
+          LPS.use('node', function(Y) {
+              Y.on('load', function(e) {
+                  func();
+              }, window);
+          });
+      }
+
+      // Enable or disable the beta.launchpad.net redirect
+      function setBetaRedirect(enable) {
+          var expire = new Date();
+          if (enable) {
+              expire.setTime(expire.getTime() + 1000);
+              document.cookie = ('inhibit_beta_redirect=0; Expires=' +
+                                 expire.toGMTString() + cookie_scope);
+              alert('Redirection to the beta site has been enabled');
+          } else {
+              expire.setTime(expire.getTime() + 2 * 60 * 60 * 1000);
+              document.cookie = ('inhibit_beta_redirect=1; Expires=' +
+                                 expire.toGMTString() + cookie_scope);
+              alert('You will not be redirected to the beta site for 2 hours');
+          }
+          return false;
+      }
+
+      function setFocusByName(name) {
+          // Focus the first element matching the given name which can be focused.
+          var nodes = document.getElementsByName(name);
+          var i, node;
+          for (i = 0; i < nodes.length; i++) {
+              node = nodes[i];
+              if (node.focus) {
+                  try {
+                      // Trying to focus a hidden element throws an error in IE8.
+                      if (node.offsetHeight !== 0) {
+                          node.focus();
+                      }
+                  } catch (e) {
+                      LPS.use('console', function(Y) {
+                          Y.log('In setFocusByName(<' +
+                              node.tagName + ' type=' + node.type + '>): ' + e);
+                      });
+                  }
+                  break;
+              }
+          }
+      }
+
+      function popup_window(url, name, width, height) {
+          var iframe = document.getElementById('popup_iframe_' + name);
+          if (!iframe.src || iframe.src === VOID_URL) {
+              // The first time this handler runs the window may not have been
+              // set up yet; sort that out.
+              iframe.style.width = width + 'px';
+              iframe.style.height = height + 'px';
+              iframe.style.position = 'absolute';
+              iframe.style.background = 'white';
+              iframe.src = url;
+          }
+          iframe.style.display = 'inline';
+          // I haven't found a way of making the search form focus again when
+          // the popup window is redisplayed. I tried doing an
+          //    iframe.contentDocument.searchform.search.focus()
+          // but nothing happens.. -- kiko, 2007-03-12
+      }
+
+      function selectWidget(widget_name, event) {
+        if (event && (event.keyCode === 9 || event.keyCode === 13)) {
+            // Avoid firing if user is tabbing through or simply pressing
+            // enter to submit the form.
+            return;
+        }
+        document.getElementById(widget_name).checked = true;
+      }
+
+      function switchBugBranchFormAndWhiteboard(id) {
+          var div = document.getElementById('bugbranch' + id);
+          var wb = document.getElementById('bugbranch' + id + '-wb');
+
+          if (div.style.display === "none") {
+              /* Expanding the form */
+              if (wb !== null) {
+                  wb.style.display = "none";
+              }
+              div.style.display = "block";
+              /* Use two focus calls to get the browser to scroll to the end of the
+               * form first, then focus back to the first field of the form.
+               */
+              document.getElementById('field'+id+'.actions.update').focus();
+              document.getElementById('field'+id+'.whiteboard').focus();
+          } else {
+              if (wb !== null) {
+                  wb.style.display = "block";
+              }
+              div.style.display = "none";
+          }
+          return false;
+      }
+
+      function switchSpecBranchFormAndSummary(id) {
+          /* The document has two identifiable elements for each
+           * spec-branch link:
+           *    'specbranchX' which is the div containing the edit form
+           *    'specbranchX-summary' which is the div contining the sumary
+           * where X is the database id of the link.
+           */
+          var div = document.getElementById('specbranch' + id);
+          var wb = document.getElementById('specbranch' + id + '-summary');
+
+          if (div.style.display === "none") {
+              /* Expanding the form */
+              if (wb !== null) {
+                  wb.style.display = "none";
+              }
+              div.style.display = "block";
+              /* Use two focus calls to get the browser to scroll to the end of the
+               * form first, then focus back to the first field of the form.
+               */
+              document.getElementById('field' + id + '.actions.change').focus();
+              document.getElementById('field' + id + '.summary').focus();
+          } else {
+              if (wb !== null) {
+                  wb.style.display = "block";
+              }
+              div.style.display = "none";
+          }
+          return false;
+      }
+
+      function updateField(field, enabled)
+      {
+          field.disabled = !enabled;
+      }
     </script>
-
-  <script id="base-layout-load-scripts" type="text/javascript">
-    LPS.use('node', 'event-delegate', 'lp', 'lp.app.foldables', 'lp.app.links',
-        'lp.app.longpoll', 'lp.app.inlinehelp', function(Y) {
-        Y.on('load', function(e) {
-            sortables_init();
-            Y.lp.app.inlinehelp.init_help();
-            Y.lp.activate_collapsibles();
-            Y.lp.app.foldables.activate();
-            Y.lp.app.links.check_valid_lp_links();
-            // Longpolling will only start if
-            // LP.cache.longpoll is populated.
-            // We use Y.later to work around a Safari/Chrome 'feature':
-            // The mouse cursor stays 'busy' until all the requests started during
-            // page load are finished.  Hence we want the long poll request to start
-            // right *after* the page has loaded.
-            Y.later(0, Y.lp.app.longpoll, Y.lp.app.longpoll.setupLongPollManager);
-        }, window);
-
-        Y.on('lp:context:web_link:changed', function(e) {
-            window.location = e.new_value;
-        });
-    });
-  </script>
 </metal:page-javascript>