[BUGFIX] Keep cursor position in textfields on navigating by arrow keys 41/42241/7
authorAndreas Fernasndez <a.fernandez@scripting-base.de>
Tue, 4 Aug 2015 10:28:17 +0000 (12:28 +0200)
committerChristian Kuhn <lolli@schwarzbu.ch>
Tue, 4 Aug 2015 12:30:04 +0000 (14:30 +0200)
Do not reset the cursor position if one navigates in the textfields by
arrow keys.

Resolves: #68721
Releases: master
Change-Id: I20387b3d6b607c9c28d05e55a9c5bcf9c4c28bbc
Reviewed-on: http://review.typo3.org/42241
Reviewed-by: Wouter Wolters <typo3@wouterwolters.nl>
Tested-by: Wouter Wolters <typo3@wouterwolters.nl>
Reviewed-by: Christian Kuhn <lolli@schwarzbu.ch>
Tested-by: Christian Kuhn <lolli@schwarzbu.ch>
typo3/sysext/backend/Resources/Public/JavaScript/FormEngineValidation.js

index 4642bf7..1c6823e 100644 (file)
@@ -488,13 +488,34 @@ define('TYPO3/CMS/Backend/FormEngineValidation', ['jquery', 'TYPO3/CMS/Backend/F
                $(FormEngineValidation.rulesSelector).each(function() {
                        var $field = $(this);
                        var newValue = FormEngineValidation.validateField($field);
-                       if (newValue.length) {
+                       if (newValue.length && $field.val() !== newValue) {
                                $field.val(newValue);
+                               FormEngineValidation.setCaretPosition($field, 0);
                        }
                });
        };
 
        /**
+        * Set the caret position in a text field
+        */
+       FormEngineValidation.setCaretPosition = function($element, caretPos) {
+               var elem = $element.get(0);
+
+               if (elem.createTextRange) {
+                       var range = elem.createTextRange();
+                       range.move('character', caretPos);
+                       range.select();
+               } else {
+                       if (elem.selectionStart) {
+                               elem.focus();
+                               elem.setSelectionRange(caretPos, caretPos);
+                       } else {
+                               elem.focus();
+                       }
+               }
+       };
+
+       /**
         * Helper function to get clean trimmed array from comma list
         *
         * @param {string} delimiter