Allow user to change language without explicitly clicking "Set"
Allow user to change language without explicitly clicking "Set"

This uses javascript to submit the form as soon as the user changes the
language selection box, rather than having to also click "Set". It
removes the Set button also. Since this is done in the jQuery ready
event, there is a bit of time while the page is loading where the
onchange won't work and the set button will still be visible, then it'll
"disappear" when the page finishes loading. There's no good way around
that though if we want the page to degrade properly without javascript.

file:b/js/lang.js (new)
--- /dev/null
+++ b/js/lang.js
@@ -1,1 +1,22 @@
+/*
+ * GitPHP Javascript language selector
+ * 
+ * Changes the language as soon as it's selected,
+ * rather than requiring a submit
+ * 
+ * @author Christopher Han <xiphux@gmail.com>
+ * @copyright Copyright (c) 2010 Christopher Han
+ * @package GitPHP
+ */
 
+function initLangSelector() {
+	$("#selLang").change(function() {
+		$("#frmLangSelect").submit();
+	});
+	$("#btnLangSet").remove();
+}
+
+$(document).ready(function() {
+	initLangSelector();
+});
+

--- a/templates/header.tpl
+++ b/templates/header.tpl
@@ -32,6 +32,7 @@
     <script language="javascript" src="js/ext/jquery-1.4.2.min.js"></script>
     <script language="javascript" src="js/ext/jquery.qtip.min.js"></script>
     <script language="javascript" src="js/tooltips.js"></script>
+    <script language="javascript" src="js/lang.js"></script>
     {foreach from=$extrascripts item=script}
     <script language="javascript" src="{$script}"></script>
     {/foreach}
@@ -45,17 +46,17 @@
       </a>
       {if $supportedlocales}
       <div class="lang_select">
-        <form action="{$SCRIPT_NAME}" method="get">
+        <form action="{$SCRIPT_NAME}" method="get" id="frmLangSelect">
 	{foreach from=$requestvars key=var item=val}
 	<input type="hidden" name="{$var}" value="{$val}" />
 	{/foreach}
 	<label for="l">language:</label>
-	<select name="l">
+	<select name="l" id="selLang">
 	  {foreach from=$supportedlocales key=locale item=language}
 	    <option {if $locale == $currentlocale}selected="selected"{/if} value="{$locale}">{$language}</option>
 	  {/foreach}
 	</select>
-	<input type="submit" value="Set" />
+	<input type="submit" value="Set" id="btnLangSet" />
 	</form>
       </div>
       {/if}

comments