From 246b6cf1746428bba0fab0d8a6f6e33af16cf612 Mon Sep 17 00:00:00 2001
From: Kevin Abel <kabel2@unl.edu>
Date: Mon, 11 Jan 2016 20:07:41 -0600
Subject: [PATCH] Move the local script stuff into separate template

The local script can now be loaded with requirejs without relying on any
globals!
---
 www/index.php                     | 55 +++++++++++++++++--------------
 www/templates/end-scripts.tpl.php | 26 +++++++++++++++
 2 files changed, 56 insertions(+), 25 deletions(-)
 create mode 100644 www/templates/end-scripts.tpl.php

diff --git a/www/index.php b/www/index.php
index 00872d7..6d94221 100644
--- a/www/index.php
+++ b/www/index.php
@@ -44,27 +44,14 @@ if (!$isEmbed) {
     }
 
     $page->doctitle = '<title>Search | University of Nebraska&ndash;Lincoln</title>';
+    $page->head = '<link rel="home" href="./" />';
     $page->pagetitle = '';
     $page->breadcrumbs = renderTemplate('templates/breadcrumbs.tpl.php');
 }
 
 $localResults = '';
-$inlineJS = '';
-$apiKey = UNL_Search::getJSAPIKey();
-$params = array(
-    'autoload' => json_encode(array('modules' => array(
-        array(
-            'name' => 'search',
-            'version' => '1.0',
-            'callback' => 'searchInit',
-            'style' => '//www.google.com/cse/style/look/v2/default.css'
-        ),
-    ))),
-);
-if (!empty($apiKey)) {
-    $params['key'] = $apiKey;
-}
-$page->addScript(htmlspecialchars('//www.google.com/jsapi?' . http_build_query($params)));
+$context = '';
+
 $page->addStyleSheet('css/search.css');
 
 //u is referring site
@@ -110,9 +97,7 @@ if (isset($_GET['u']) && $scanned = UNL_Search::getScannedPage($_GET['u'])) {
         // Auto-build a custom search engine
         $context = array('crefUrl' => UNL_Search::getLinkedCSEUrl($_GET['u']));
     }
-    $context = json_encode($context);
-    $inlineJS .= "var LOCAL_SEARCH_CONTEXT = {$context};\n";
-    
+
     $localResults = renderTemplate('templates/google-results.tpl.php', array(
         'title' => $page->titlegraphic,
         'id' => 'local_results',
@@ -122,12 +107,6 @@ if (isset($_GET['u']) && $scanned = UNL_Search::getScannedPage($_GET['u'])) {
     loadDefaultSections($page);
 }
 
-if (isset($_GET['q'])) {
-    $q = json_encode($_GET['q']);
-    $inlineJS .= "var INITIAL_QUERY = {$q};\n";
-}
-
-$page->addScriptDeclaration($inlineJS);
 
 $maincontent = '';
 if (!$isEmbed) {
@@ -139,6 +118,32 @@ $maincontent .= renderTemplate('templates/search-results.tpl.php', array(
     'local_results' => $localResults
 ));
 
+$initialQuery = json_encode(isset($_GET['q']) ? $_GET['q'] : '');
+$context = json_encode($context);
+
+$apiKey = UNL_Search::getJSAPIKey();
+$params = array(
+    'autoload' => json_encode(array('modules' => array(
+        array(
+            'name' => 'search',
+            'version' => '1.0',
+            'callback' => 'searchInit',
+            'style' => '//www.google.com/cse/style/look/v2/default.css'
+        ),
+    ))),
+);
+
+if (!empty($apiKey)) {
+    $params['key'] = $apiKey;
+}
+
+$maincontent .= renderTemplate('templates/end-scripts.tpl.php', array(
+    'localScriptUrl' => $localScriptUrl,
+    'googleLoaderUrl' => 'https://www.google.com/jsapi?' . http_build_query($params),
+    'initialQuery' => $initialQuery,
+    'localContext' => $context,
+));
+
 if (!$isEmbed) {
     $page->maincontentarea = $maincontent;
     echo $page;
diff --git a/www/templates/end-scripts.tpl.php b/www/templates/end-scripts.tpl.php
new file mode 100644
index 0000000..59d5ea0
--- /dev/null
+++ b/www/templates/end-scripts.tpl.php
@@ -0,0 +1,26 @@
+<script>
+require(['jquery', 'wdn', '<?php echo $localScriptUrl ?>'], function($, WDN, UNLSearch) {
+	var gSearchDefer = $.Deferred();
+	window.searchInit = function() {
+        window.searchInit = $.noop;
+        gSearchDefer.resolve(google);
+    };
+
+    $('<script>', {
+        src: '<?php echo $googleLoaderUrl ?>',
+        asycn: 'async',
+        defer: 'defer'
+    }).appendTo($('body'));
+
+    var $localCss = $('<link>', {
+        'href': './css/search-google.css'
+    });
+
+    gSearchDefer.done(function(google) {
+        // ensure this CSS is loaded AFTER google
+        require(['css!' + $localCss[0].href], function() {
+    	   UNLSearch.initialize(<?php echo $initialQuery ?>, <?php echo $localContext ?>)
+        });
+    });
+});
+</script>
-- 
GitLab