Skip to content

Commit

Permalink
Fix auto-focus script. The element count of the tab sections contents…
Browse files Browse the repository at this point in the history
… array is irrelevant. This implementation will focus on the first text or textarea input in the form, unless focus is already attributed to a form input.
  • Loading branch information
fbarthez committed Nov 11, 2015
1 parent 9c62945 commit 7e93f29
Showing 1 changed file with 13 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -123,14 +123,20 @@ public String tabScriptString() {
if (d2wContext().valueForKey(Keys.firstResponderKey) != null) {
return scriptForFirstResponderActivation();
} else {
String result = "";
String formName = ERXWOForm.formName(context(), "EditForm");
if (formName != null) {
int pos = tabSectionsContents().count() - 1;
result = "var pos=0;\n if (document." + formName + ".elements.length>" + pos +
") pos=" + pos + ";\n var elem = document." + formName + ".elements[" + pos +
"];\n if (elem!=null && (elem.type == 'text' || elem.type == 'area')) elem.focus();";
}
String result = "var focusedElement = document.querySelector('form[name=\"" + formName + "\"] input:focus');"
// only continue when no form element is focused, yet
+ "if (focusedElement == undefined) {"
+ " var focusableElements = document.querySelectorAll('form[name=\"" + formName + "\"] input');"
+ " var qualifiedTypes = ['text', 'textarea'];"
+ " for (i = 0; i < focusableElements.length; i++) { "
+ " var anElement = focusableElements[i];"
+ " if (qualifiedTypes.include(anElement.type.toLowerCase())) {"
+ " anElement.focus();"
+ " break;"
+ " }"
+ " }"
+ "}";
return result;
}
}
Expand Down

0 comments on commit 7e93f29

Please sign in to comment.