From 57c7b1d3a1dc874113ec262905672fd63f52db74 Mon Sep 17 00:00:00 2001 From: Pierre Masereel Date: Mon, 10 Oct 2016 17:50:03 +0200 Subject: [PATCH] [FIX] barcodes: _set_quantity_listener Before, the '_set_quantity_listener' function was opening a 'window.prompt' which had different comportment depending the navigator. For example, the focus was not allways on the input field, the enter key 'Cancel' the prompt or 'validate' it. So to improve the usability and the consistancy we used a dialog instead a prompt, to manage the focus and the 'Enter' key pressed when we want to set a quantity. --- .../src/js/form_view_barcode_handler.js | 43 +++++++++++++------ 1 file changed, 30 insertions(+), 13 deletions(-) diff --git a/addons/barcodes/static/src/js/form_view_barcode_handler.js b/addons/barcodes/static/src/js/form_view_barcode_handler.js index ffc6dfc7383..7f2614f0ef1 100644 --- a/addons/barcodes/static/src/js/form_view_barcode_handler.js +++ b/addons/barcodes/static/src/js/form_view_barcode_handler.js @@ -7,6 +7,7 @@ var common = require('web.form_common'); var BarcodeEvents = require('barcodes.BarcodeEvents'); var BarcodeHandlerMixin = require('barcodes.BarcodeHandlerMixin'); var KanbanRecord = require('web_kanban.Record'); +var Dialog = require('web.Dialog'); var _t = core._t; @@ -77,22 +78,38 @@ var FormViewBarcodeHandler = common.AbstractField.extend(BarcodeHandlerMixin, { } else { var field = this.form_view.fields[this.m2x_field]; var view = field.viewmanager.active_view; + var $content = $('
').append($('', {type: 'text', class: 'o_set_qty_input'})); if (this.last_scanned_barcode) { - var new_qty = window.prompt(_t('Set quantity'), character) || "0"; - new_qty = new_qty.replace(',', '.'); - var record = _.find(this._get_records(field), function (record) { - return record.get('product_barcode') === self.last_scanned_barcode; + this.dialog = new Dialog(this, { + title: _t('Set quantity'), + buttons: [{text: _t('Select'), classes: 'btn-primary', close: true, click: function () { + var new_qty = this.$content.find('.o_set_qty_input').val(); + var record = _.find(self._get_records(field), function (record) { + return record.get('product_barcode') === self.last_scanned_barcode; + }); + if (record) { + var values = {}; + values[self.quantity_field] = parseFloat(new_qty); + field.data_update(record.get('id'), values).then(function () { + view.controller.reload_record(record); + }); + } else { + self._display_no_last_scanned_warning(); + } + }}, {text: _t('Discard'), close: true}], + $content: $content, + }).open(); + // This line set the value of the key which triggered the _set_quantity in the input + this.dialog.$content.find('.o_set_qty_input').focus().val(character); + + var $selectBtn = this.dialog.$footer.find('.btn-primary'); + core.bus.on('keypress', this.dialog, function(event){ + if (event.which === 13) { + event.preventDefault(); + $selectBtn.click(); + } }); - if (record) { - var values = {}; - values[this.quantity_field] = parseFloat(new_qty); - field.data_update(record.get('id'), values).then(function () { - view.controller.reload_record(record); - }); - } else { - this._display_no_last_scanned_warning(); - } } else { this._display_no_last_scanned_warning(); }