From 852e2c557e3449db2f367fca69d9c6a778996ae4 Mon Sep 17 00:00:00 2001 From: Pierre Masereel Date: Mon, 10 Oct 2016 16:51:58 +0200 Subject: [FIX] barcodes: set_quantity traceback on mobile When we are trying to set the quantity on a mobile device, we get a traceback saying that we cannot call the 'find' function on the elements returned by '_get_records'. This '_get_records' function returns a 'Collection' when called on a tree view and an 'Array' when called on a kanban view. When we are working on a mobile device, most of the one2many are displayed as kanban. The thing is when we work with the object returned by '_get_records' we call a 'find' method on it, which is implemented by Odoo's Collection class. So when we call 'find' on an 'Array' we actually call 'Array.prototype.find' which is implemented on most desktop browser and on some mobile browser [1]. This is what caused the traceback on Chrome in Android. To fix this issue, we enforce '_get_records' to always return an 'Array' and we ensure that, when working on this 'Array', we use '_.find'. [1] https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Global_Objects/Array/find --- addons/barcodes/static/src/js/form_view_barcode_handler.js | 11 ++++++++--- 1 file changed, 8 insertions(+), 3 deletions(-) (limited to 'addons/barcodes/static/src/js/form_view_barcode_handler.js') 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 b00bce51c5e..ffc6dfc7383 100644 --- a/addons/barcodes/static/src/js/form_view_barcode_handler.js +++ b/addons/barcodes/static/src/js/form_view_barcode_handler.js @@ -81,7 +81,7 @@ var FormViewBarcodeHandler = common.AbstractField.extend(BarcodeHandlerMixin, { if (this.last_scanned_barcode) { var new_qty = window.prompt(_t('Set quantity'), character) || "0"; new_qty = new_qty.replace(',', '.'); - var record = this._get_records(field).find(function(record) { + var record = _.find(this._get_records(field), function (record) { return record.get('product_barcode') === self.last_scanned_barcode; }); if (record) { @@ -159,8 +159,13 @@ var FormViewBarcodeHandler = common.AbstractField.extend(BarcodeHandlerMixin, { }, _get_records: function(field) { - return field.viewmanager.active_view.controller.records || // tree view - field.viewmanager.active_view.controller.widgets; // kanban view + var active_view = field.viewmanager.active_view; + if (active_view.type === "kanban") { + return active_view.controller.widgets; + } else { + // tree view case + return active_view.controller.records.records; + } }, }); -- cgit v1.2.3 From 57c7b1d3a1dc874113ec262905672fd63f52db74 Mon Sep 17 00:00:00 2001 From: Pierre Masereel Date: Mon, 10 Oct 2016 17:50:03 +0200 Subject: [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. --- .../static/src/js/form_view_barcode_handler.js | 43 +++++++++++++++------- 1 file changed, 30 insertions(+), 13 deletions(-) (limited to 'addons/barcodes/static/src/js/form_view_barcode_handler.js') 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(); } -- cgit v1.2.3