diff options
| author | Christophe Matthieu <chm@odoo.com> | 2017-08-24 15:27:44 +0200 |
|---|---|---|
| committer | Christophe Matthieu <chm@odoo.com> | 2017-08-30 14:39:17 +0200 |
| commit | 2728fe1ec1ac7e99819d0f30f29a1814b00c82a4 (patch) | |
| tree | 89b1325b1ba3e86bc6ac830462c4694bc99553d2 /addons/web/static/src/js/core/dom.js | |
| parent | 6cf98c4c87d644928897a76b9985947ab2ebc607 (diff) | |
[IMP] web: onchange x2many with virtual id in create command
When an onchange modifies an x2many field, the line on which the user
works remains in editable mode and the cursor is returned to the same
position.
The second argument of create command is a reference used to reactivate
the edited line.
Diffstat (limited to 'addons/web/static/src/js/core/dom.js')
| -rw-r--r-- | addons/web/static/src/js/core/dom.js | 42 |
1 files changed, 41 insertions, 1 deletions
diff --git a/addons/web/static/src/js/core/dom.js b/addons/web/static/src/js/core/dom.js index 6650022c486..7bae1bc29a5 100644 --- a/addons/web/static/src/js/core/dom.js +++ b/addons/web/static/src/js/core/dom.js @@ -153,10 +153,32 @@ return { return $to_detach.detach(); }, /** + * Returns the input or textarea cursor offset + * + * @param {Object} DOM item input or texteara + * @returns {Object} + */ + 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 }; + }, + /** * Returns the distance between a DOM element and the top-left corner of the * window * - * @param {element} [e] the DOM element + * @param {Object} node DOM element (input or texteara) * @return {Object} the left and top distances in pixels */ getPosition: function (e) { @@ -254,5 +276,23 @@ return { } return $container; }, + /** + * Moves the cursor position in a DOM element (input or texteara) + * + * @param {Object} node DOM element (input or textarea) + * @param {Int} pos cursor position + */ + 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); + } + }, }; }); |
