Skip to content
Snippets Groups Projects
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
No related branches found
No related tags found
No related merge requests found
......@@ -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;
......
<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>
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment