diff options
| author | Christophe Simonis <chs@odoo.com> | 2017-08-31 12:55:41 +0200 |
|---|---|---|
| committer | Christophe Simonis <chs@odoo.com> | 2017-08-31 12:55:41 +0200 |
| commit | 4879ce040797fd4a47b6b8326c3aacd365c8ab91 (patch) | |
| tree | 45479b47c5dcfba0d0ced4f05d770b0852f31263 /addons/barcodes/static/src | |
| parent | dae6178a0a5629ce678b20fcc9226caa44a5b0ce (diff) | |
| parent | fe77df3ba480d40b3d6b25736397bd52c684ce03 (diff) | |
[MERGE] forward port branch saas-17 up to fe77df3ba48
Diffstat (limited to 'addons/barcodes/static/src')
| -rw-r--r-- | addons/barcodes/static/src/js/barcode_events.js | 22 | ||||
| -rw-r--r-- | addons/barcodes/static/src/js/barcode_field.js | 24 | ||||
| -rw-r--r-- | addons/barcodes/static/src/js/barcode_form_view.js | 118 |
3 files changed, 115 insertions, 49 deletions
diff --git a/addons/barcodes/static/src/js/barcode_events.js b/addons/barcodes/static/src/js/barcode_events.js index 04c03a41308..0f13c59fe66 100644 --- a/addons/barcodes/static/src/js/barcode_events.js +++ b/addons/barcodes/static/src/js/barcode_events.js @@ -77,13 +77,7 @@ var BarcodeEvents = core.Class.extend(mixins.PropertiesMixin, { 'bubbles': old_event.bubbles, 'cancelable': old_event.cancelable, }; - try { - new_event = new Event("keypress", params); - } catch(error) { - // For IE >= 9, use new CustomEvent(), instead of new Event() - new_event = new CustomEvent("keypress", params); - } - + new_event = $.Event('keypress', params); new_event.viewArg = old_event.viewArg; new_event.ctrl = old_event.ctrl; new_event.alt = old_event.alt; @@ -96,7 +90,7 @@ var BarcodeEvents = core.Class.extend(mixins.PropertiesMixin, { new_event.which = old_event.which; new_event.dispatched_by_barcode_reader = true; - old_event.target.dispatchEvent(new_event); + $(old_event.target).trigger(new_event); } } }, @@ -173,17 +167,17 @@ var BarcodeEvents = core.Class.extend(mixins.PropertiesMixin, { }, start: function(prevent_key_repeat){ - document.body.addEventListener('keypress', this.__handler, true); + $('body').bind("keypress", this.__handler); if (prevent_key_repeat === true) { - document.body.addEventListener('keydown', this.__keydown_handler, true); - document.body.addEventListener('keyup', this.__keyup_handler, true); + $('body').bind("keydown", this.__keydown_handler); + $('body').bind('keyup', this.__keyup_handler); } }, stop: function(){ - document.body.removeEventListener('keypress', this.__handler, true); - document.body.removeEventListener('keydown', this.__keydown_handler, true); - document.body.removeEventListener('keyup', this.__keyup_handler, true); + $('body').unbind("keypress", this.__handler); + $('body').unbind("keydown", this.__keydown_handler); + $('body').unbind('keyup', this.__keyup_handler); }, }); diff --git a/addons/barcodes/static/src/js/barcode_field.js b/addons/barcodes/static/src/js/barcode_field.js index aa31b4d8261..7b9b29aa4c8 100644 --- a/addons/barcodes/static/src/js/barcode_field.js +++ b/addons/barcodes/static/src/js/barcode_field.js @@ -8,12 +8,12 @@ var fieldRegistry = require('web.field_registry'); // Field in which the user can both type normally and scan barcodes var FieldFloatScannable = basicFields.FieldFloat.extend({ - events: { + events: _.extend({}, basicFields.FieldFloat.prototype.events, { // The barcode_events component intercepts keypresses and releases them when it // appears they are not part of a barcode. But since released keypresses don't // trigger native behaviour (like characters input), we must simulate it. 'keypress': '_onKeypress', - }, + }), //-------------------------------------------------------------------------- // Private @@ -40,9 +40,8 @@ var FieldFloatScannable = basicFields.FieldFloat.extend({ */ _onKeypress: function (e) { /* only simulate a keypress if it has been previously prevented */ - if (e.originalEvent.dispatched_by_barcode_reader !== true) { + if (e.dispatched_by_barcode_reader !== true) { e.preventDefault(); - this.$input.blur(); return; } var character = String.fromCharCode(e.which); @@ -52,23 +51,12 @@ var FieldFloatScannable = basicFields.FieldFloat.extend({ e.target.value = str_before_carret + character + str_after_carret; var new_carret_index = str_before_carret.length + character.length; e.target.setSelectionRange(new_carret_index, new_carret_index); - // Note: FieldChar (that FieldFloat extends) calls store_dom_value upon change - // event, which is triggered when the input loses focus and its internal dirty - // flag is set. But here, we directly modify the value property of the input, - // which doesn't set the dirty flag. So we could call store_dom_value upon blur. - // But we also want the DOM value to be stored when a barcode_event occurs and - // triggers an onchange. We could listen to barcode_event in order to call - // store_dom_value, but that would have to happen before the onchange is triggered. - // So the safest method is still to store the value each time it changes. - // This long explanation is here to avoid having to do the thinking all over agan - // in case this strategy doesn't work / breaks. - // TL;DR Safest way not to lose the value when a barcode scan triggers an onchange. - this._setValue(e.target.value); + // trigger an 'input' event to notify the widget that it's value changed + $(e.target).trigger('input'); }, }); // Field to use scan barcodes - var FormViewBarcodeHandler = AbstractField.extend({ /** * @override @@ -78,8 +66,6 @@ var FormViewBarcodeHandler = AbstractField.extend({ this.trigger_up('activeBarcode', { name: this.name, - fieldName: 'pack_operation_product_ids', - quantity: 'qty_done', commands: { barcode: '_barcodeAddX2MQuantity', } diff --git a/addons/barcodes/static/src/js/barcode_form_view.js b/addons/barcodes/static/src/js/barcode_form_view.js index 73df8a3c03f..da90b198352 100644 --- a/addons/barcodes/static/src/js/barcode_form_view.js +++ b/addons/barcodes/static/src/js/barcode_form_view.js @@ -26,19 +26,23 @@ FormController.include({ this.activeBarcode = { form_view: { commands: { - 'O-CMD.NEW': 'createRecord', - 'O-CMD.EDIT': 'toEditMode', - 'O-CMD.CANCEL': 'discardChange', - 'O-CMD.SAVE': function () { return this.saveRecord({reload: true}); }, - // 'O-CMD.PAGER-PREV': - // 'O-CMD.PAGER-NEXT': - } - } + 'O-CMD.EDIT': this._barcodeEdit.bind(this), + 'O-CMD.CANCEL': this._barcodeDiscard.bind(this), + 'O-CMD.SAVE': this._barcodeSave.bind(this), + 'O-CMD.PAGER-PREV': this._barcodePagerPrevious.bind(this), + 'O-CMD.PAGER-NEXT': this._barcodePagerNext.bind(this), + 'O-CMD.PAGER-FIRST': this._barcodePagerFirst.bind(this), + 'O-CMD.PAGER-LAST': this._barcodePagerLast.bind(this), + }, + }, }; this.barcodeMutex = new concurrency.Mutex(); this._barcodeStartListening(); }, + /** + * @override + */ destroy: function () { this._barcodeStopListening(); this._super(); @@ -71,6 +75,55 @@ FormController.include({ }, /** * @private + */ + _barcodeDiscard: function () { + return this.discardChanges(); + }, + /** + * @private + */ + _barcodeEdit: function () { + return this._setMode('edit'); + }, + /** + * @private + */ + _barcodePagerFirst: function () { + var self = this; + return this.mutex.exec(function () {}).then(function () { + self.pager.updateState({ + current_min: 1, + }, {notifyChange: true}); + }); + }, + /** + * @private + */ + _barcodePagerLast: function () { + var self = this; + return this.mutex.exec(function () {}).then(function () { + var state = self.model.get(self.handle, {raw: true}); + self.pager.updateState({ + current_min: state.count, + }, {notifyChange: true}); + }); + }, + /** + * @private + */ + _barcodePagerNext: function () { + return this.mutex.exec(function () {}).then(this.pager.next.bind(this.pager)); + }, + /** + * @private + */ + _barcodePagerPrevious: function () { + return this.mutex.exec(function () {}).then(this.pager.previous.bind(this.pager)); + }, + /** + * Returns true iff the given barcode matches the given record (candidate). + * + * @private * @param {Object} candidate: record in the x2m * @param {string} barcode sent by the scanner (string generate from keypress series) * @param {Object} activeBarcode: options sent by the field who use barcode features @@ -81,6 +134,12 @@ FormController.include({ }, /** * @private + */ + _barcodeSave: function () { + return this.saveRecord(); + }, + /** + * @private * @param {Object} candidate: record in the x2m * @param {Object} current record * @param {string} barcode sent by the scanner (string generate from keypress series) @@ -89,8 +148,14 @@ FormController.include({ */ _barcodeSelectedCandidate: function (candidate, record, barcode, activeBarcode) { var changes = {}; - changes[activeBarcode.quantity] = candidate.data[activeBarcode.quantity] + 1; - return this.model.notifyChanges(candidate.id, changes); + var candidateChanges = {}; + candidateChanges[activeBarcode.quantity] = candidate.data[activeBarcode.quantity] + 1; + changes[activeBarcode.fieldName] = { + operation: 'UPDATE', + id: candidate.id, + data: candidateChanges, + }; + return this.model.notifyChanges(this.handle, changes); }, /** * @private @@ -99,7 +164,6 @@ FormController.include({ core.bus.on('barcode_scanned', this, this._barcodeScanned); core.bus.on('keypress', this, this._quantityListener); }, - /** * @private */ @@ -128,7 +192,7 @@ FormController.include({ */ _getBarCodeRecord: function (record, barcode, activeBarcode) { var self = this; - if (!activeBarcode.fieldName) { + if (!activeBarcode.fieldName || !record.data[activeBarcode.fieldName]) { return; } return _.find(record.data[activeBarcode.fieldName].data, function (record) { @@ -167,6 +231,10 @@ FormController.include({ candidate: this.activeBarcode[name] && this.activeBarcode[name].handle === this.handle ? this.activeBarcode[name].candidate : null, }; + + // we want to disable autofocus when activating the barcode to avoid + // putting the scanned value in the focused field + this.disableAutofocus = true; }, /** * @private @@ -232,8 +300,13 @@ FormController.include({ if (prefixed && !hasCommand) { self.do_warn(_t('Error : Barcode command is undefined'), barcode); } - return $.when.apply($, defs).then(function () { - self.update({}, {reload: false}); + return self.alive($.when.apply($, defs)).then(function () { + if (!prefixed) { + // redraw the view if we scanned a real barcode (required if + // we manually apply the change in JS, e.g. incrementing the + // quantity) + self.update({}, {reload: false}); + } }); }); }, @@ -321,8 +394,6 @@ FormRenderer.include({ commands['O-BTN.' + node.attrs.barcode_trigger] = function () { if (!$button.hasClass('o_invisible_modifier')) { $button.click(); - } else { - this.do_warn(_t('Action currently unavailable')); } return $.when(); }; @@ -361,6 +432,21 @@ FormRenderer.include({ } return $button; }, + /** + * Add barcode event handler + * + * @override + * @private + * @param {Object} node + * @returns {jQueryElement} + */ + _renderTagButton: function (node) { + var $button = this._super.apply(this, arguments); + if (node.attrs.barcode_trigger) { + this._barcodeButtonHandler($button, node); + } + return $button; + } }); BarcodeEvents.ReservedBarcodePrefixes.push('O-BTN'); |
