From 8c8db5d491d79767ddb1e4f423e21823052de89d Mon Sep 17 00:00:00 2001 From: Guy Halse <guy@tenet.ac.za> Date: Wed, 20 Mar 2019 11:12:06 +0200 Subject: [PATCH] Remove the inline javascript from discopower --- modules/discopower/lib/PowerIdPDisco.php | 16 +++----- modules/discopower/templates/disco.tpl.php | 3 +- modules/discopower/templates/disco.twig | 2 +- modules/discopower/www/assets/js/tablist.js | 13 +++++++ modules/discopower/www/tablist.php | 41 +++++++++++++++++++++ 5 files changed, 63 insertions(+), 12 deletions(-) create mode 100644 modules/discopower/www/assets/js/tablist.js create mode 100644 modules/discopower/www/tablist.php diff --git a/modules/discopower/lib/PowerIdPDisco.php b/modules/discopower/lib/PowerIdPDisco.php index c5ce0a954..887871cdb 100644 --- a/modules/discopower/lib/PowerIdPDisco.php +++ b/modules/discopower/lib/PowerIdPDisco.php @@ -268,6 +268,7 @@ class PowerIdPDisco extends \SimpleSAML\XHTML\IdPDisco $t->data['returnIDParam'] = $this->returnIdParam; $t->data['entityID'] = $this->spEntityId; $t->data['defaulttab'] = $this->discoconfig->getValue('defaulttab', 0); + $t->data['pageid'] = 'discopower'; $idpList = $this->processMetadata($t, $idpList, $preferredIdP); @@ -283,17 +284,12 @@ class PowerIdPDisco extends \SimpleSAML\XHTML\IdPDisco $t->data['autofocus'] = 'favouritesubmit'; } - $search = '<script type="text/javascript"> - $(document).ready(function() { - $("#tabdiv").tabs({ selected: '.$t->data['defaulttab'].' });'; - $i = 0; - foreach ($idpList as $tab => $slist) { - $search .= "\n".'$("#query_'.$tab.'").liveUpdate("#list_'.$tab.'")'. - (($i++ == 0) && (empty($this->data['faventry'])) ? '.focus()' : '').';'; - } - $search .= "});\n</script>"; + /* store the tab list in the session */ + $session = \SimpleSAML\Session::getSessionFromRequest(); + $session->setData('discopower:tabList', 'faventry', $this->data['faventry']); + $session->setData('discopower:tabList', 'tabs', array_keys($idpList)); + $session->setData('discopower:tabList', 'defaulttab', $t->data['defaulttab']); - $t->data['search'] = $search; $t->data['score'] = $this->discoconfig->getValue('score', 'quicksilver'); $t->data['tabNames'] = $discoPowerTabs; $t->data['preferredidp'] = $preferredIdP; diff --git a/modules/discopower/templates/disco.tpl.php b/modules/discopower/templates/disco.tpl.php index 3472a6e30..581b81927 100644 --- a/modules/discopower/templates/disco.tpl.php +++ b/modules/discopower/templates/disco.tpl.php @@ -10,7 +10,8 @@ $this->data['head'] .= '<script type="text/javascript" src="'. SimpleSAML\Module::getModuleURL('discopower/assets/js/jquery.livesearch.js').'"></script>'."\n"; $this->data['head'] .= '<script type="text/javascript" src="'. SimpleSAML\Module::getModuleURL('discopower/assets/js/'.$this->data['score'].'.js').'"></script>'."\n"; -$this->data['head'] .= $this->data['search']; +$this->data['head'] .= '<script type="text/javascript" src="'. + SimpleSAML\Module::getModuleURL('discopower/assets/js/tablist.js').'"></script>'."\n"; if (!empty($this->data['faventry'])) { $this->data['autofocus'] = 'favouritesubmit'; diff --git a/modules/discopower/templates/disco.twig b/modules/discopower/templates/disco.twig index bd7248393..27febae5b 100644 --- a/modules/discopower/templates/disco.twig +++ b/modules/discopower/templates/disco.twig @@ -9,7 +9,7 @@ <script src="/{{ baseurlpath }}resources/jquery-ui-1.8.js"></script> <script src="/{{ baseurlpath }}module.php/discopower/js/jquery.livesearch.js"></script> <script src="/{{ baseurlpath }}module.php/discopower/js/{{ score }}.js"></script> - {{ search|raw }} + <script src="/{{ baseurlpath }}module.php/discopower/js/tablist.js"></script> {% endblock %} {% block content %} diff --git a/modules/discopower/www/assets/js/tablist.js b/modules/discopower/www/assets/js/tablist.js new file mode 100644 index 000000000..789fb5b28 --- /dev/null +++ b/modules/discopower/www/assets/js/tablist.js @@ -0,0 +1,13 @@ +$(document).ready(function() { + $("#tabdiv").tabs(); + $.getJSON("tablist.php", function(data) { + $("#tabdiv").select(data["default"]); + for (var i = 0; i < data["tabs"].length; i++) { + var tab = data["tabs"][i]; + $("#query_"+tab).liveUpdate("#list_"+tab); + if (data["faventry"] == null && i == 0) { + $("#query_"+tab).focus(); + } + } + }); +}); diff --git a/modules/discopower/www/tablist.php b/modules/discopower/www/tablist.php new file mode 100644 index 000000000..1ffdffd71 --- /dev/null +++ b/modules/discopower/www/tablist.php @@ -0,0 +1,41 @@ +<?php +/** + * An AJAX handler to retrieve a list of disco tabs from the session. + * This allows us to dynamically update the tab list without inline javascript. + * + * @author Guy Halse, http://orcid.org/0000-0002-9388-8592 + * @package SimpleSAMLphp + */ +$session = \SimpleSAML\Session::getSessionFromRequest(); +$tabs = $session->getData('discopower:tabList', 'tabs'); +$faventry = $session->getData('discopower:tabList', 'faventry'); +$defaulttab = $session->getData('discopower:tabList', 'defaulttab'); + +if (!is_array($tabs)) { + throw new \SimpleSAML\Error\Exception('Could not get tab list from session'); +} + +// handle JSON vs JSONP requests +if (isset($_REQUEST['callback'])) { + if (!preg_match('/^[a-z0-9_]+$/i', $_REQUEST['callback'])) { + throw new \SimpleSAML\Error\Exception('Unsafe JSONP callback function name "'.$_REQUEST['callback'].'"'); + } + $jsonp = true; + header('Content-Type: application/javascript'); + print addslashes($_REQUEST['callback']) . '('; +} else { + $jsonp = false; + header('Content-Type: application/json'); +} + +print json_encode( + [ + 'faventry' => $faventry, + 'default' => $defaulttab, + 'tabs' => $tabs, + ] +); + +if ($jsonp) { + print ');'; +} -- GitLab