Browse files

https://issues.apache.org/jira/browse/MYFACES-3653 adding a universal…

… solution which works on all recent browsers

git-svn-id: https://svn.apache.org/repos/asf/myfaces/core/trunk@1412831 13f79535-47bb-0310-9956-ffa450edef68
  • Loading branch information...
1 parent db92fb6 commit 8c15e2f56a13bc794b127fb22ab0ea93dbd04c5d @weblets weblets committed Nov 23, 2012
Showing with 65 additions and 0 deletions.
  1. +65 −0 api/src/main/javascript/META-INF/resources/myfaces/_impl/_util/_Dom.js
View
65 api/src/main/javascript/META-INF/resources/myfaces/_impl/_util/_Dom.js
@@ -542,7 +542,50 @@ _MF_SINGLTN(_PFX_UTIL + "_Dom", Object, /** @lends myfaces._impl._util._Dom.prot
//most probably dataset properties are not supported
}
},
+ //from
+ // http://blog.vishalon.net/index.php/javascript-getting-and-setting-caret-position-in-textarea/
+ getCaretPosition:function (ctrl) {
+ var caretPos = 0;
+ try {
+
+ // other browsers make it simpler by simply having a selection start element
+ if (ctrl.selectionStart || ctrl.selectionStart == '0')
+ caretPos = ctrl.selectionStart;
+ // ie 5 quirks mode as second option because
+ // this option is flakey in conjunction with text areas
+ // TODO move this into the quirks class
+ else if (document.selection) {
+ ctrl.focus();
+ var selection = document.selection.createRange();
+ //the selection now is start zero
+ selection.moveStart('character', -ctrl.value.length);
+ //the caretposition is the selection start
+ caretPos = selection.text.length;
+ }
+ } catch (e) {
+ //now this is ugly, but not supported input types throw errors for selectionStart
+ //this way we are future proof by having not to define every selection enabled
+ //input in an if (which will be a lot in the near future with html5)
+ }
+ return caretPos;
+ },
+
+ setCaretPosition:function (ctrl, pos) {
+ if (ctrl.createTextRange) {
+ var range = ctrl.createTextRange();
+ range.collapse(true);
+ range.moveEnd('character', pos);
+ range.moveStart('character', pos);
+ range.select();
+ }
+ //IE quirks mode again, TODO move this into the quirks class
+ else if (ctrl.setSelectionRange) {
+ ctrl.focus();
+ //the selection range is our caret position
+ ctrl.setSelectionRange(pos, pos);
+ }
+ },
/**
* outerHTML replacement which works cross browserlike
@@ -553,6 +596,9 @@ _MF_SINGLTN(_PFX_UTIL + "_Dom", Object, /** @lends myfaces._impl._util._Dom.prot
*/
outerHTML : function(item, markup) {
this._assertStdParams(item, markup, "outerHTML");
+ // we can work on a single element in a cross browser fashion
+ // regarding the focus thanks to the
+ // icefaces team for providing the code
if (item.nodeName.toLowerCase() === 'input') {
var replacingInput = this._buildEvalNodes(item, markup)[0];
this.cloneAttributes(item, replacingInput);
@@ -562,6 +608,12 @@ _MF_SINGLTN(_PFX_UTIL + "_Dom", Object, /** @lends myfaces._impl._util._Dom.prot
if (markup !== "") {
var ret = null;
+ var focusElementId = null;
+ var caretPosition = 0;
+ if ('undefined' != typeof document.activeElement) {
+ focusElementId = (document.activeElement) ? document.activeElement.id : null;
+ caretPosition = this.getCaretPosition(document.activeElement);
+ }
// we try to determine the browsers compatibility
// level to standards dom level 2 via various methods
if (this.isDomCompliant()) {
@@ -570,6 +622,19 @@ _MF_SINGLTN(_PFX_UTIL + "_Dom", Object, /** @lends myfaces._impl._util._Dom.prot
//call into abstract method
ret = this._outerHTMLNonCompliant(item, markup);
}
+ if (focusElementId) {
+ var newFocusElement = this.byId(focusElementId);
+ if (newFocusElement && newFocusElement.nodeName.toLowerCase() === 'input') {
+ //just in case the replacement element is not focusable anymore
+ if ("undefined" != typeof newFocusElement.focus) {
+ newFocusElement.focus();
+ }
+ }
+ if (caretPosition) {
+ //zero caret position is set automatically on focus
+ this.setCaretPosition(newFocusElement, caretPosition);
+ }
+ }
// and remove the old item
//first we have to save the node newly insert for easier access in our eval part

0 comments on commit 8c15e2f

Please sign in to comment.