From ac5dc58367d9bed5a576baaa7b803a67b5d00d07 Mon Sep 17 00:00:00 2001 From: Xavier Morel Date: Thu, 19 Jul 2012 11:57:42 +0200 Subject: [PATCH] [IMP] focus the clicked field when starting a row edition by clicking on a cell bzr revid: xmo@openerp.com-20120719095742-lps9xjsrvhn5fnlh --- .../web/static/src/js/view_list_editable.js | 54 +++++++++++++------ doc/list-view.rst | 22 +++++++- 2 files changed, 58 insertions(+), 18 deletions(-) diff --git a/addons/web/static/src/js/view_list_editable.js b/addons/web/static/src/js/view_list_editable.js index 052a471f0f3..56df125c98b 100644 --- a/addons/web/static/src/js/view_list_editable.js +++ b/addons/web/static/src/js/view_list_editable.js @@ -165,9 +165,11 @@ openerp.web.list_editable = function (instance) { * Set up the edition of a record of the list view "inline" * * @param {instance.web.list.Record} [record] record to edit, leave empty to create a new record + * @param {Object} [options] + * @param {String} [options.focus_field] field to focus at start of edition * @return {jQuery.Deferred} */ - start_edition: function (record) { + start_edition: function (record, options) { var self = this; var item = false; if (record) { @@ -194,14 +196,14 @@ openerp.web.list_editable = function (instance) { return self.editor.edit(item, function (field_name, field) { var cell = cells[field_name]; if (!cell || field.get('effective_readonly')) { - // Readonly fields can just remain the list's, form's - // usually don't have backgrounds &al + // Readonly fields can just remain the list's, + // form's usually don't have backgrounds &al field.set({invisible: true}); return; } self.fields_for_resize.push({field: field, cell: cell}); - }).pipe(function () { + }, options).pipe(function () { $recordRow.addClass('oe_edition'); self.resize_fields(); return record.attributes; @@ -558,7 +560,33 @@ openerp.web.list_editable = function (instance) { throw new Error("is_editing's state filter must be either `new` or" + " `edit` if provided"); }, - edit: function (record, configureField) { + _focus_setup: function (focus_field) { + var form = this.form; + + var field; + // If a field to focus was specified + if (focus_field + // Is actually in the form + && (field = form.fields[focus_field]) + // And is visible + && field.$element.is(':visible')) { + // focus it + field.focus(); + return; + } + + _(form.fields_order).detect(function (name) { + // look for first visible field in fields_order, focus it + var field = form.fields[name]; + if (!field.$element.is(':visible')) { + return false; + } + field.focus(); + // Stop as soon as a field got focused + return true; + }); + }, + edit: function (record, configureField, options) { // TODO: specify sequence of edit calls var self = this; var form = self.form; @@ -573,16 +601,7 @@ openerp.web.list_editable = function (instance) { _(form.fields).each(function (field, name) { configureField(name, field); }); - // TODO: actually focus clicked field (if editable) - _(form.fields_order).detect(function (name) { - // look for first visible field in fields_order, focus it - var field = form.fields[name]; - if (!field.$element.is(':visible')) { - return false; - } - field.focus(); - return true; - }); + self._focus_setup(options && options.focus_field); return form; }); }, @@ -627,7 +646,10 @@ openerp.web.list_editable = function (instance) { return this._super.apply(this, arguments); } var record_id = $(event.currentTarget).data('id'); - this.view.start_edition(record_id ? this.records.get(record_id) : null); + this.view.start_edition( + record_id ? this.records.get(record_id) : null, { + focus_field: $(event.target).data('field') + }); }, /** * If a row mapping to the record (@data-id matching the record's id or diff --git a/doc/list-view.rst b/doc/list-view.rst index ebb5f0ffe4d..0d67c27f963 100644 --- a/doc/list-view.rst +++ b/doc/list-view.rst @@ -131,7 +131,7 @@ Interaction Methods rejected if a pending edition could not be saved (e.g. validation failure) -.. js:function:: openerp.web.ListView.start_edition([record]) +.. js:function:: openerp.web.ListView.start_edition([record][, options]) Starts editing the provided record inline, through an overlay form view of editable fields in the record. @@ -142,7 +142,9 @@ Interaction Methods This method resolves any pending edition when invoked, before starting a new edition. + :param record: record to edit, or null to create a new record :type record: :js:class:`~openerp.web.list.Record` + :param EditOptions options: :returns: delegate to the form used for the edition .. js:function:: openerp.web.ListView.save_edition @@ -303,7 +305,7 @@ formview, delegating instead to its :type record_state: String :rtype: Boolean - .. js:function:: openerp.web.list.Editor.edit(record, configureField) + .. js:function:: openerp.web.list.Editor.edit(record, configureField[, options]) Loads the provided record into the internal form view and displays the form view. @@ -320,6 +322,7 @@ formview, delegating instead to its method do some last-minute configuration of form fields. :type configureField: Function + :param EditOptions options: :returns: jQuery delegate to the form object .. js:function:: openerp.web.list.Editor.save @@ -391,6 +394,21 @@ formview, delegating instead to its dataset (instead of appended) :rtype: Boolean + +.. js:class:: EditOptions + + Options object optionally passed into a method starting an edition + to configure its setup and behavior + + .. js:attribute:: focus_field + + Name of the field to set focus on after setting up the edition + of the record. + + If this option is not provided, or the requested field can not + be focused (invisible, readonly or not in the view), the first + visible non-readonly field is focused. + Changes from 6.1 ----------------