Ver Fonte

2006-12-31 Igor Zelmanovich <[email protected]>

	* webform.js: WebForm_AutoFocus support for not focusable elements 
	by setting focus on first focusable child.

svn path=/trunk/mcs/; revision=70285
Igor Zelmanovich há 19 anos atrás
pai
commit
f1984593a3

+ 5 - 0
mcs/class/System.Web/resources/ChangeLog

@@ -1,3 +1,8 @@
+2006-12-31 Igor Zelmanovich <[email protected]>
+
+	* webform.js: WebForm_AutoFocus support for not focusable elements 
+	by setting focus on first focusable child.
+
 2006-12-31 Igor Zelmanovich <[email protected]>
 
 	* callback.js: fixed: support IE7, encodeURIComponent is used to 

+ 46 - 2
mcs/class/System.Web/resources/webform.js

@@ -33,9 +33,53 @@ function WebForm_AutoFocus (id)
 	var x = document.getElementById ? document.getElementById (id) :
 					  ((document.all) ? document.all [id] : null);
 
-	if (typeof (x) != 'undefined') {
-		x.focus();
+	if (x && (!WebForm_CanFocus(x))) {
+		x = WebForm_FindFirstFocusableChild(x);
 	}
+	if (x) { x.focus(); }
+}
+
+function WebForm_CanFocus(element) {
+	if (!element || !(element.tagName) || element.disabled) {
+		return false;
+	}
+	if (element.type && element.type.toLowerCase() == "hidden") {
+		return false;
+	}
+	var tagName = element.tagName.toLowerCase();
+	return (tagName == "input" ||
+			tagName == "textarea" ||
+			tagName == "select" ||
+			tagName == "button" ||
+			tagName == "a");
+}
+
+function WebForm_FindFirstFocusableChild(element) {
+	if (!element || !(element.tagName)) {
+		return null;
+	}
+	var tagName = element.tagName.toLowerCase();
+	if (tagName == "undefined") {
+		return null;
+	}
+	var children = element.childNodes;
+	if (children) {
+		for (var i = 0; i < children.length; i++) {
+			try {
+				if (WebForm_CanFocus(children[i])) {
+					return children[i];
+				}
+				else {
+					var focused = WebForm_FindFirstFocusableChild(children[i]);
+					if (WebForm_CanFocus(focused)) {
+						return focused;
+					}
+				}
+			} catch (e) {
+			}
+		}
+	}
+	return null;
 }
 
 function wasControlEnabled (id)