From 825729f6455b8f92307176f36e448b9d1f2f52ef Mon Sep 17 00:00:00 2001
From: Olav Morken <olav.morken@uninett.no>
Date: Thu, 12 Jun 2008 13:27:52 +0000
Subject: [PATCH] Add autofocus helper script with support from default
 template. Also change previous autofocus implementations to use this script.

git-svn-id: https://simplesamlphp.googlecode.com/svn/trunk@646 44740490-163a-0410-bde0-09ae8108e29a
---
 templates/default/httpredirect-debug.php |  4 +---
 templates/default/includes/header.php    | 12 +++++++++++-
 templates/default/login.php              |  7 +------
 templates/default/post-debug.php         |  5 +----
 www/resources/script.js                  | 11 +++++++++++
 5 files changed, 25 insertions(+), 14 deletions(-)
 create mode 100644 www/resources/script.js

diff --git a/templates/default/httpredirect-debug.php b/templates/default/httpredirect-debug.php
index 7b921779c..78cb0e661 100644
--- a/templates/default/httpredirect-debug.php
+++ b/templates/default/httpredirect-debug.php
@@ -1,5 +1,6 @@
 <?php 
 	$this->data['icon'] = 'debug.png';
+	$this->data['autofocus'] = 'sendlink';
 	$this->includeAtTemplateBase('includes/header.php'); 
 ?>
 
@@ -14,9 +15,6 @@
 		<pre style="overflow: scroll; border: 1px solid #eee"><?php echo $this->data['message']; ?></pre>
 		
 		<p>[ <a id="sendlink" href="<?php echo htmlentities($this->data['url']); ?>">send SAML message</a> ]</p>
-		<script type="text/javascript">
-			document.getElementById('sendlink').focus();
-		</script>
 
 		<h2>Debug mode</h2>
 		
diff --git a/templates/default/includes/header.php b/templates/default/includes/header.php
index a8403227b..f11fdd583 100644
--- a/templates/default/includes/header.php
+++ b/templates/default/includes/header.php
@@ -12,8 +12,18 @@ if(array_key_exists('header', $this->data)) {
 
 	<link rel="stylesheet" type="text/css" href="/<?php echo $this->data['baseurlpath']; ?>resources/default.css" />
 	<link rel="icon" type="image/icon" href="/<?php echo $this->data['baseurlpath']; ?>resources/icons/favicon.ico" />
+	<script type="text/javascript" src="/<?php echo $this->data['baseurlpath']; ?>resources/script.js" />
 </head>
-<body>
+<?php
+$onLoad = '';
+if(array_key_exists('autofocus', $this->data)) {
+	$onLoad .= 'SimpleSAML_focus(\'' . $this->data['autofocus'] . '\');';
+}
+if($onLoad !== '') {
+	$onLoad = ' onload="' . $onLoad . '"';
+}
+?>
+<body<?php echo $onLoad; ?>>
 
 <div id="wrap">
 	
diff --git a/templates/default/login.php b/templates/default/login.php
index 6dc9a8c38..4b6aade06 100644
--- a/templates/default/login.php
+++ b/templates/default/login.php
@@ -1,13 +1,8 @@
 <?php 
 	if (!array_key_exists('icon', $this->data)) $this->data['icon'] = 'lock.png';
+	$this->data['autofocus'] = 'username';
 	$this->includeAtTemplateBase('includes/header.php'); 
 ?>
-	<script type="text/javascript">
-		function focusit() {
-			document.getElementById('username').focus();
-		}
-		window.onload = focusit;
-	</script>
 <div id="content">
 
 	<?php if (isset($this->data['error'])) { ?>
diff --git a/templates/default/post-debug.php b/templates/default/post-debug.php
index 79d4cac72..ad37b6185 100644
--- a/templates/default/post-debug.php
+++ b/templates/default/post-debug.php
@@ -1,5 +1,6 @@
 <?php 
 	$this->data['icon'] = 'debug.png';
+	$this->data['autofocus'] = 'sendbutton';
 	$this->includeAtTemplateBase('includes/header.php'); 
 ?>
 
@@ -19,10 +20,6 @@
 			<input type="submit" value="Submit the response to the service" id="sendbutton" />
 		</form>
 
-		<script type="text/javascript">
-			document.getElementById('sendbutton').focus();
-		</script>
-
 		<h2>Debug mode</h2>
 		
 		<p>As you are in debug mode you are lucky to see the content of the response you are sending:</p>
diff --git a/www/resources/script.js b/www/resources/script.js
new file mode 100644
index 000000000..1d1fa03b4
--- /dev/null
+++ b/www/resources/script.js
@@ -0,0 +1,11 @@
+/**
+ * Set focus to the element with the given id.
+ *
+ * @param id  The id of the element which should receive focus.
+ */
+function SimpleSAML_focus(id) {
+  element = document.getElementById(id);
+  if(element != null) {
+    element.focus();
+  }
+}
-- 
GitLab