Commit 246b6cf1 authored by Kevin Abel's avatar Kevin Abel
Browse files

Move the local script stuff into separate template

The local script can now be loaded with requirejs without relying on any
globals!
parent 41215ab6
...@@ -44,27 +44,14 @@ if (!$isEmbed) { ...@@ -44,27 +44,14 @@ if (!$isEmbed) {
} }
$page->doctitle = '<title>Search | University of Nebraska&ndash;Lincoln</title>'; $page->doctitle = '<title>Search | University of Nebraska&ndash;Lincoln</title>';
$page->head = '<link rel="home" href="./" />';
$page->pagetitle = ''; $page->pagetitle = '';
$page->breadcrumbs = renderTemplate('templates/breadcrumbs.tpl.php'); $page->breadcrumbs = renderTemplate('templates/breadcrumbs.tpl.php');
} }
$localResults = ''; $localResults = '';
$inlineJS = ''; $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;
}
$page->addScript(htmlspecialchars('//www.google.com/jsapi?' . http_build_query($params)));
$page->addStyleSheet('css/search.css'); $page->addStyleSheet('css/search.css');
//u is referring site //u is referring site
...@@ -110,9 +97,7 @@ if (isset($_GET['u']) && $scanned = UNL_Search::getScannedPage($_GET['u'])) { ...@@ -110,9 +97,7 @@ if (isset($_GET['u']) && $scanned = UNL_Search::getScannedPage($_GET['u'])) {
// Auto-build a custom search engine // Auto-build a custom search engine
$context = array('crefUrl' => UNL_Search::getLinkedCSEUrl($_GET['u'])); $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( $localResults = renderTemplate('templates/google-results.tpl.php', array(
'title' => $page->titlegraphic, 'title' => $page->titlegraphic,
'id' => 'local_results', 'id' => 'local_results',
...@@ -122,12 +107,6 @@ if (isset($_GET['u']) && $scanned = UNL_Search::getScannedPage($_GET['u'])) { ...@@ -122,12 +107,6 @@ if (isset($_GET['u']) && $scanned = UNL_Search::getScannedPage($_GET['u'])) {
loadDefaultSections($page); loadDefaultSections($page);
} }
if (isset($_GET['q'])) {
$q = json_encode($_GET['q']);
$inlineJS .= "var INITIAL_QUERY = {$q};\n";
}
$page->addScriptDeclaration($inlineJS);
$maincontent = ''; $maincontent = '';
if (!$isEmbed) { if (!$isEmbed) {
...@@ -139,6 +118,32 @@ $maincontent .= renderTemplate('templates/search-results.tpl.php', array( ...@@ -139,6 +118,32 @@ $maincontent .= renderTemplate('templates/search-results.tpl.php', array(
'local_results' => $localResults '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) { if (!$isEmbed) {
$page->maincontentarea = $maincontent; $page->maincontentarea = $maincontent;
echo $page; echo $page;
......
<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>
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment