From 3a715311e4bf5b94c7508af6a6417d470f3a49b2 Mon Sep 17 00:00:00 2001
From: Eric Rasmussen <ericrasmussen1@gmail.com>
Date: Thu, 9 Aug 2018 13:55:25 -0500
Subject: [PATCH] Split end-scripts.tpl.php into 4.1/5.0 versions because it
 contains CSS

---
 www/index.php                               |  2 +-
 www/templates/{ => 4.1}/end-scripts.tpl.php |  0
 www/templates/5.0/end-scripts.tpl.php       | 32 +++++++++++++++++++++
 3 files changed, 33 insertions(+), 1 deletion(-)
 rename www/templates/{ => 4.1}/end-scripts.tpl.php (100%)
 create mode 100644 www/templates/5.0/end-scripts.tpl.php

diff --git a/www/index.php b/www/index.php
index ae36a93..3477550 100644
--- a/www/index.php
+++ b/www/index.php
@@ -143,7 +143,7 @@ $maincontent .= renderTemplate($templatePath . 'search-results.tpl.php', array(
 $initialQuery = json_encode(isset($_GET['q']) ? $_GET['q'] : '');
 $context = json_encode($context);
 
-$maincontent .= renderTemplate('templates/end-scripts.tpl.php', array(
+$maincontent .= renderTemplate($templatePath . 'end-scripts.tpl.php', array(
     'localScriptUrl' => $localScriptUrl,
     'googleLoaderUrl' => 'https://cse.google.com/cse.js?cx=' . $search_engine_id,
     'initialQuery' => $initialQuery,
diff --git a/www/templates/end-scripts.tpl.php b/www/templates/4.1/end-scripts.tpl.php
similarity index 100%
rename from www/templates/end-scripts.tpl.php
rename to www/templates/4.1/end-scripts.tpl.php
diff --git a/www/templates/5.0/end-scripts.tpl.php b/www/templates/5.0/end-scripts.tpl.php
new file mode 100644
index 0000000..a8ae6a8
--- /dev/null
+++ b/www/templates/5.0/end-scripts.tpl.php
@@ -0,0 +1,32 @@
+<script>
+require(['jquery', '<?php echo $localScriptUrl ?>'], function($, UNLSearch) {
+    var gSearchDefer = $.Deferred();
+	window.searchInit = function() {
+        window.searchInit = $.noop;
+        gSearchDefer.resolve(google);
+    };
+
+    window.__gcse = {
+      parsetags: 'explicit',
+      callback: window.searchInit
+    };
+
+    $('<script>', {
+        src: '<?php echo $googleLoaderUrl ?>',
+        async: 'async',
+        defer: 'defer',
+        type: 'text/javascript'
+    }).appendTo($('body'));
+
+    var $localCss = $('<link>', {
+        'href': './css/search-google.css?v=20170404'
+    });
+
+    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