diff options
| author | Géry Debongnie <ged@odoo.com> | 2017-04-22 23:29:30 +0200 |
|---|---|---|
| committer | Géry Debongnie <ged@odoo.com> | 2017-04-24 10:19:18 +0200 |
| commit | e4bf1f1ccc522f1715dba82940d5bb647e5138e3 (patch) | |
| tree | 875555672a866da1abc89e736a215768f8270f7f /addons/web/static/src | |
| parent | d8261d1162086e5c299e9ee364b7ac0476ea6fee (diff) | |
[FIX] web: form view should focus fields if possible
When a form view switches to edit mode, it is supposed to give the focus
to the first field, or, to a field tagged 'default_focus'.
Diffstat (limited to 'addons/web/static/src')
| -rw-r--r-- | addons/web/static/src/js/fields/abstract_field.js | 13 | ||||
| -rw-r--r-- | addons/web/static/src/js/views/form/form_renderer.js | 11 |
2 files changed, 20 insertions, 4 deletions
diff --git a/addons/web/static/src/js/fields/abstract_field.js b/addons/web/static/src/js/fields/abstract_field.js index d2a317bd3b2..87b3bf3f512 100644 --- a/addons/web/static/src/js/fields/abstract_field.js +++ b/addons/web/static/src/js/fields/abstract_field.js @@ -161,17 +161,24 @@ var AbstractField = Widget.extend({ /** * Activates the field widget. By default, activation means focusing and - * selecting the associated focusable element. + * selecting (if possible) the associated focusable element. The selecting + * part can be disabled. In that case, note that the focused input/textarea + * will have the cursor at the very end. * + * @param {boolean} [noselect=false] if false and the input is of type text + * or textarea, the content will also be selected * @returns {boolean} true if the widget was activated, false if the * focusable element was not found or invisible */ - activate: function () { + activate: function (noselect) { var $focusable = this.getFocusableElement(); if ($focusable.length && $focusable.is(':visible')) { $focusable.focus(); if ($focusable.is('input[type="text"], textarea')) { - setTimeout($focusable.select.bind($focusable), 0); + $focusable[0].selectionStart = $focusable[0].selectionEnd = $focusable[0].value.length; + if (!noselect) { + setTimeout($focusable.select.bind($focusable), 0); + } } return true; } diff --git a/addons/web/static/src/js/views/form/form_renderer.js b/addons/web/static/src/js/views/form/form_renderer.js index 4f0a700eed0..53d5a49b68c 100644 --- a/addons/web/static/src/js/views/form/form_renderer.js +++ b/addons/web/static/src/js/views/form/form_renderer.js @@ -284,7 +284,9 @@ var FormRenderer = BasicRenderer.extend({ widget.$el.addClass(FIELD_CLASSES[record.fields[node.attrs.name].type]); this._addFieldClassNames(widget); this._handleAttributes(widget.$el, node); - + if (JSON.parse(node.attrs.default_focus || "0")) { + this.defaultFocusField = widget; + } return widget; }, /** @@ -700,13 +702,20 @@ var FormRenderer = BasicRenderer.extend({ self.$el.toggleClass('o_form_editable', self.mode === 'edit'); self.$el.toggleClass('o_form_readonly', self.mode === 'readonly'); // Attach the tooltips on the fields' label + var focusWidget = self.defaultFocusField; _.each(self.allFieldWidgets[self.state.id], function (widget) { + if (!focusWidget) { + focusWidget = widget; + } if (core.debug || widget.attrs.help || widget.field.help) { var idForLabel = self.idsForLabels[widget.name]; var $label = idForLabel ? self.$('label[for=' + idForLabel + ']') : $(); self._addFieldTooltip(widget, $label); } }); + if (focusWidget) { + focusWidget.activate(true); + } }); }, |
