Autofocus username field on login form
Autofocus username field on login form

--- a/js/common.js
+++ b/js/common.js
@@ -9,17 +9,19 @@
  * @subpackage Javascript
  */
 
-define(["jquery", "modules/getproject", "modules/lang", "modules/tooltip.snapshot", "modules/tooltip.commit", "modules/tooltip.tag"],
+define(["jquery", "modules/getproject", "modules/lang", "modules/tooltip.snapshot", "modules/tooltip.commit", "modules/tooltip.tag", 'modernizr'],
 	function($, project, lang, tooltipSnapshot, tooltipCommit, tooltipTag) {
 		$(function() {
 			lang($('div.lang_select'));
 			tooltipSnapshot($('a.snapshotTip'));
+      if (project) {
+        tooltipCommit($('a.commitTip'));
+        tooltipTag($('a.tagTip'));
+      }
+      if (!Modernizr.input.autofocus) {
+        $('input[autofocus]').filter(':first').focus();
+      }
 		});
-
-		if (project) {
-			tooltipCommit($('a.commitTip'));
-			tooltipTag($('a.tagTip'));
-		}
 	}
 );
 

--- a/js/ext/modernizr.custom.js
+++ b/js/ext/modernizr.custom.js
@@ -1,5 +1,5 @@
 /* Modernizr 2.6.2 (Custom Build) | MIT & BSD

- * Build: http://modernizr.com/download/#-addtest-teststyles

+ * Build: http://modernizr.com/download/#-input-addtest-teststyles

  */

 ;

 

@@ -18,7 +18,7 @@
     modElem = document.createElement(mod),

     mStyle = modElem.style,

 

-    inputElem  ,

+    inputElem  = document.createElement('input')  ,

 

 

     toString = {}.toString,    tests = {},

@@ -159,6 +159,17 @@
         }

         return false;

     }

+    function webforms() {

+                                            Modernizr['input'] = (function( props ) {

+            for ( var i = 0, len = props.length; i < len; i++ ) {

+                attrs[ props[i] ] = !!(props[i] in inputElem);

+            }

+            if (attrs.list){

+                                  attrs.list = !!(document.createElement('datalist') && window.HTMLDataListElement);

+            }

+            return attrs;

+        })('autocomplete autofocus list placeholder max min multiple pattern required step'.split(' '));

+        }

     for ( var feature in tests ) {

         if ( hasOwnProp(tests, feature) ) {

                                     featureName  = feature.toLowerCase();

@@ -168,6 +179,7 @@
         }

     }

 

+    Modernizr.input || webforms();

 

 

      Modernizr.addTest = function ( feature, test ) {


--- a/templates/login.tpl
+++ b/templates/login.tpl
@@ -22,7 +22,7 @@
   <form method="post" action="{geturl action=login}">
     <div class="field">
       <label for="username">{t}username:{/t}</label>
-      <input type="text" name="username" {if $username}value="{$username}"{/if} />
+      <input type="text" name="username" {if $username}value="{$username}"{/if} autofocus />
     </div>
     <div class="field">
       <label for="password">{t}password:{/t}</label>

comments