From d2a69c086af237c6493b06f8962b5fee31e33323 Mon Sep 17 00:00:00 2001
From: Shane Tomlinson <stomlinson@mozilla.com>
Date: Thu, 19 Jan 2012 13:12:15 +0000
Subject: [PATCH] Adding the code check to the main site.

* Update code_check to take the file_name_prefix (dialog vs production)
* Update start.js main site and dialog to use code_check with correct file_name_prefix.
* Introduce the moduleManager to the main site to take care of creating/loading  modules.
* Change the order of the hiding/displaying of elements so there is no flash while the code version is being checked.

close #917
---
 resources/static/dialog/start.js              |   2 +-
 resources/static/pages/start.js               | 111 ++++++++++--------
 resources/static/shared/modules/code_check.js |   8 +-
 .../shared/modules/code_check_unit_test.js    |   1 +
 resources/views/index.ejs                     |   2 +-
 resources/views/layout.ejs                    |   1 +
 scripts/compress.sh                           |   2 +-
 7 files changed, 74 insertions(+), 53 deletions(-)

diff --git a/resources/static/dialog/start.js b/resources/static/dialog/start.js
index 77834cc71..459cee2b0 100644
--- a/resources/static/dialog/start.js
+++ b/resources/static/dialog/start.js
@@ -15,7 +15,7 @@
   moduleManager.register("code_check", modules.CodeCheck);
 
   moduleManager.start("code_check", {
-    environment: "__BROWSERID_ENVIRONMENT__",
+    file_name_prefix: "dialog",
     code_ver: "__BROWSERID_CODE_VERSION__",
     ready: function(status) {
       // if status is false, that means the javascript is out of date and we
diff --git a/resources/static/pages/start.js b/resources/static/pages/start.js
index d64a447b6..d06630849 100644
--- a/resources/static/pages/start.js
+++ b/resources/static/pages/start.js
@@ -14,67 +14,84 @@ $(function() {
       pageHelpers = bid.PageHelpers,
       network = bid.Network,
       user = bid.User,
+      dom = bid.DOM,
       token = pageHelpers.getParameterByName("token"),
       path = document.location.pathname,
-      XHRDelay = bid.Modules.XHRDelay,
-      XHRDisableForm = bid.Modules.XHRDisableForm;
+      moduleManager = bid.module,
+      modules = bid.Modules,
+      CodeCheck = modules.CodeCheck,
+      XHRDelay = modules.XHRDelay,
+      XHRDisableForm = modules.XHRDisableForm;
 
   network.init({ time_until_delay: 10 * 1000 });
-  var xhrDelay = XHRDelay.create({});
-  xhrDelay.start();
-  var xhrDisableForm = XHRDisableForm.create({});
-  xhrDisableForm.start();
 
-  if (!path || path === "/") {
-    bid.index();
-  }
-  else if (path === "/signin") {
-    var module = bid.signIn.create();
-    module.start({});
-  }
-  else if (path === "/signup") {
-    bid.signUp();
-  }
-  else if (path === "/forgot") {
-    bid.forgot();
-  }
-  else if (path === "/add_email_address") {
-    var module = bid.addEmailAddress.create();
-    module.start({
-      token: token
-    });
-  }
-  else if(token && path === "/verify_email_address") {
-    bid.verifyEmailAddress(token);
-  }
+  moduleManager.register("code_check", CodeCheck);
 
+  $(".display_always,.display_auth,.display_nonauth").hide();
   if ($('#vAlign').length) {
     $(window).bind('resize', function() { $('#vAlign').css({'height' : $(window).height() }); }).trigger('resize');
   }
 
-  $("a.signOut").click(function(event) {
-    event.preventDefault();
-    event.stopPropagation();
 
-    user.logoutUser(function() {
-      document.location = "/";
-    }, pageHelpers.getFailure(bid.Errors.logout));
-  });
+  moduleManager.start("code_check", {
+    file_name_prefix: "browserid",
+    code_ver: "__BROWSERID_CODE_VERSION",
+    ready: function(status) {
+      if(!status) return;
 
-  $(".display_always,.display_auth,.display_nonauth").hide();
+      dom.addClass("body", "ready");
 
-  var ANIMATION_TIME = 500;
-  user.checkAuthentication(function(authenticated) {
-    $(".display_always").fadeIn(ANIMATION_TIME);
-
-    if (authenticated) {
-      $(".display_auth").fadeIn(ANIMATION_TIME);
-      if ($('#emailList').length) {
-        bid.manageAccount();
+      moduleManager.register("xhr_delay", XHRDelay);
+      moduleManager.register("xhr_disable_form", XHRDisableForm);
+      if (!path || path === "/") {
+        bid.index();
       }
-    }
-    else {
-      $(".display_nonauth").fadeIn(ANIMATION_TIME);
+      else if (path === "/signin") {
+        var module = bid.signIn.create();
+        module.start({});
+      }
+      else if (path === "/signup") {
+        bid.signUp();
+      }
+      else if (path === "/forgot") {
+        bid.forgot();
+      }
+      else if (path === "/add_email_address") {
+        var module = bid.addEmailAddress.create();
+        module.start({
+          token: token
+        });
+      }
+      else if(token && path === "/verify_email_address") {
+        bid.verifyEmailAddress(token);
+      }
+
+      $("a.signOut").click(function(event) {
+        event.preventDefault();
+        event.stopPropagation();
+
+        user.logoutUser(function() {
+          document.location = "/";
+        }, pageHelpers.getFailure(bid.Errors.logout));
+      });
+
+      var ANIMATION_TIME = 500;
+      user.checkAuthentication(function(authenticated) {
+        $(".display_always").fadeIn(ANIMATION_TIME);
+
+        dom.addClass("body", authenticated ? "authenticated" : "not_authenticated");
+        if (authenticated) {
+          $(".display_auth").fadeIn(ANIMATION_TIME);
+          if ($('#emailList').length) {
+            bid.manageAccount();
+          }
+        }
+        else {
+          $(".display_nonauth").fadeIn(ANIMATION_TIME);
+        }
+      });
+
+
     }
   });
 
diff --git a/resources/static/shared/modules/code_check.js b/resources/static/shared/modules/code_check.js
index 99798ba77..80856c524 100644
--- a/resources/static/shared/modules/code_check.js
+++ b/resources/static/shared/modules/code_check.js
@@ -11,7 +11,8 @@ BrowserID.Modules.CodeCheck = (function() {
   var bid = BrowserID,
       dom = bid.DOM,
       sc,
-      expectedCodeVer;
+      expectedCodeVer,
+      fileNamePrefix;
 
   function getMostRecentCodeVersion(oncomplete) {
     bid.Network.codeVersion(oncomplete, this.getErrorDialog(bid.Errors.checkScriptVersion, oncomplete));
@@ -35,7 +36,7 @@ BrowserID.Modules.CodeCheck = (function() {
   function loadScript(version, oncomplete) {
     var script = document.createElement("script");
     script.type = "text/javascript";
-    script.src = "https://browserid.org/production/dialog_v" + version + ".js";
+    script.src = "https://browserid.org/production/" + fileNamePrefix + "_v" + version + ".js";
     document.head.appendChild(script);
 
     oncomplete();
@@ -51,8 +52,9 @@ BrowserID.Modules.CodeCheck = (function() {
 
         data = data || {};
 
-        self.checkRequired(data, "code_ver");
+        self.checkRequired(data, "code_ver", "file_name_prefix");
         expectedCodeVer = data.code_ver;
+        fileNamePrefix = data.file_name_prefix;
 
         getMostRecentCodeVersion.call(self, function(version) {
           if(version) {
diff --git a/resources/static/test/qunit/shared/modules/code_check_unit_test.js b/resources/static/test/qunit/shared/modules/code_check_unit_test.js
index 90c2beb8c..c0701039d 100644
--- a/resources/static/test/qunit/shared/modules/code_check_unit_test.js
+++ b/resources/static/test/qunit/shared/modules/code_check_unit_test.js
@@ -13,6 +13,7 @@
 
   function createController(config) {
     var config = $.extend({
+      file_name_prefix: "dialog",
       code_ver: "ABC123"
     }, config);
 
diff --git a/resources/views/index.ejs b/resources/views/index.ejs
index 45054b83c..96672f13c 100644
--- a/resources/views/index.ejs
+++ b/resources/views/index.ejs
@@ -2,7 +2,7 @@
    - License, v. 2.0. If a copy of the MPL was not distributed with this
    - file, You can obtain one at http://mozilla.org/MPL/2.0/. -->
 
-  <div id="content" style="display:none" class="display_auth">
+  <div id="content" class="display_auth">
       <div id="newuser">
         New to BrowserID? <a href="/about">Learn more</a>
       </div>
diff --git a/resources/views/layout.ejs b/resources/views/layout.ejs
index 2d1cdd797..a3738e197 100644
--- a/resources/views/layout.ejs
+++ b/resources/views/layout.ejs
@@ -26,6 +26,7 @@
     <script src="/shared/javascript-extensions.js"></script>
     <script src="/shared/browserid.js"></script>
     <script src="/lib/dom-jquery.js"></script>
+    <script src="/lib/module.js"></script>
     <script src="/lib/jschannel.js"></script>
     <script src="/lib/winchan.js"></script>
     <script src="/lib/hub.js"></script>
diff --git a/scripts/compress.sh b/scripts/compress.sh
index 7e86abf6b..1acfba260 100755
--- a/scripts/compress.sh
+++ b/scripts/compress.sh
@@ -74,7 +74,7 @@ echo '****Building BrowserID.org HTML, CSS, and JS****'
 echo ''
 
 #produce the main site js
-cat lib/vepbundle.js lib/jquery-1.7.1.min.js lib/underscore-min.js lib/ejs.js shared/javascript-extensions.js shared/browserid.js lib/dom-jquery.js lib/jschannel.js lib/winchan.js lib/hub.js $BUILD_PATH/templates.js shared/renderer.js shared/error-display.js shared/screens.js shared/error-messages.js shared/wait-messages.js shared/mediator.js shared/storage.js shared/network.js shared/provisioning.js shared/user.js shared/tooltip.js shared/validation.js shared/helpers.js shared/class.js shared/modules/page_module.js shared/modules/xhr_delay.js shared/modules/xhr_disable_form.js shared/modules/code_check.js pages/page_helpers.js pages/start.js pages/index.js pages/add_email_address.js pages/verify_email_address.js pages/forgot.js pages/manage_account.js pages/signin.js pages/signup.js > $BUILD_PATH/browserid.uncompressed.js
+cat lib/vepbundle.js lib/jquery-1.7.1.min.js lib/underscore-min.js lib/ejs.js shared/javascript-extensions.js shared/browserid.js lib/dom-jquery.js lib/module.js lib/jschannel.js lib/winchan.js lib/hub.js $BUILD_PATH/templates.js shared/renderer.js shared/error-display.js shared/screens.js shared/error-messages.js shared/wait-messages.js shared/mediator.js shared/storage.js shared/network.js shared/provisioning.js shared/user.js shared/tooltip.js shared/validation.js shared/helpers.js shared/class.js shared/modules/page_module.js shared/modules/xhr_delay.js shared/modules/xhr_disable_form.js shared/modules/code_check.js pages/page_helpers.js pages/start.js pages/index.js pages/add_email_address.js pages/verify_email_address.js pages/forgot.js pages/manage_account.js pages/signin.js pages/signup.js > $BUILD_PATH/browserid.uncompressed.js
 
 # produce the main site css
 cat css/common.css css/style.css css/m.css > $BUILD_PATH/browserid.uncompressed.css
-- 
GitLab