summaryrefslogtreecommitdiff
path: root/addons/web/static/src/js/core/dom.js
diff options
context:
space:
mode:
authorAaron Bohy <aab@odoo.com>2017-09-29 08:50:28 +0200
committerAaron Bohy <aab@odoo.com>2017-09-29 16:02:36 +0200
commit36b4d78a2678806b25aaa5650b64496f9da9d205 (patch)
treee5325edb2d1a26530f587325e249d2b76136951a /addons/web/static/src/js/core/dom.js
parentf9dc8cf76dc8cd9bd091c7e0c4bdf368a9e47b23 (diff)
[FIX] web,mail: reset focus after onchange on x2many
Rev. 2728fe1 introduced an attempt of restoring the focus on the currently focused field in an x2many list after an onchange returned, and the list has been updated. However, it was often wrong (e.g. leaving a field by pressing tab/shift+tab, clicking on another field...) and reset the focus on an unexpected field. In addition to fixing this issue, this rev. also unifies the helper functions to get and set the cursor positions in an input or textarea, as they were duplicated between mail and web.
Diffstat (limited to 'addons/web/static/src/js/core/dom.js')
-rw-r--r--addons/web/static/src/js/core/dom.js50
1 files changed, 21 insertions, 29 deletions
diff --git a/addons/web/static/src/js/core/dom.js b/addons/web/static/src/js/core/dom.js
index 6ca13f18e4e..b39f39cab9c 100644
--- a/addons/web/static/src/js/core/dom.js
+++ b/addons/web/static/src/js/core/dom.js
@@ -157,26 +157,18 @@ return {
return $to_detach.detach();
},
/**
- * Returns the input or textarea cursor offset
+ * Returns the selection range of an input or textarea
*
* @param {Object} DOM item input or texteara
- * @returns {Object}
+ * @returns {Object} range
+ * @returns {integer} range.start
+ * @returns {integer} range.end
*/
- getCursor: function (node) {
- node = $(node)[0];
- var offset = 0;
- var length = 0;
- if ('selectionStart' in node) {
- offset = node.selectionStart;
- length = node.selectionEnd - offset;
- } else if ('selection' in document) {
- node.focus();
- var sel = document.selection.createRange();
- length = document.selection.createRange().text.length;
- sel.moveStart('character', -node.value.length);
- offset = sel.text.length - length;
- }
- return { offset: offset, length: length };
+ getSelectionRange: function (node) {
+ return {
+ start: node.selectionStart,
+ end: node.selectionEnd,
+ };
},
/**
* Returns the distance between a DOM element and the top-left corner of the
@@ -281,21 +273,21 @@ return {
return $container;
},
/**
- * Moves the cursor position in a DOM element (input or texteara)
+ * Sets the selection range of a given input or textarea
*
* @param {Object} node DOM element (input or textarea)
- * @param {Int} pos cursor position
+ * @param {integer} range.start
+ * @param {integer} range.end
*/
- setCursor: function (node, pos) {
- node = $(node).first().focus()[0];
- if ('createTextRange' in node) {
- var textRange = node.createTextRange();
- textRange.collapse(true);
- textRange.moveEnd(pos);
- textRange.moveStart(pos);
- textRange.select();
- } else if ('setSelectionRange' in node) {
- node.setSelectionRange(pos, pos);
+ setSelectionRange: function (node, range) {
+ if (node.setSelectionRange){
+ node.setSelectionRange(range.start, range.end);
+ } else if (node.createTextRange){
+ node.createTextRange()
+ .collapse(true)
+ .moveEnd('character', range.start)
+ .moveStart('character', range.end)
+ .select();
}
},
};