diff options
| author | stephanchrst <stephanchrst@gmail.com> | 2022-05-10 21:51:50 +0700 |
|---|---|---|
| committer | stephanchrst <stephanchrst@gmail.com> | 2022-05-10 21:51:50 +0700 |
| commit | 3751379f1e9a4c215fb6eb898b4ccc67659b9ace (patch) | |
| tree | a44932296ef4a9b71d5f010906253d8c53727726 /addons/purchase_product_matrix/static/src | |
| parent | 0a15094050bfde69a06d6eff798e9a8ddf2b8c21 (diff) | |
initial commit 2
Diffstat (limited to 'addons/purchase_product_matrix/static/src')
| -rw-r--r-- | addons/purchase_product_matrix/static/src/js/product_matrix_configurator.js | 183 |
1 files changed, 183 insertions, 0 deletions
diff --git a/addons/purchase_product_matrix/static/src/js/product_matrix_configurator.js b/addons/purchase_product_matrix/static/src/js/product_matrix_configurator.js new file mode 100644 index 00000000..907b2d4b --- /dev/null +++ b/addons/purchase_product_matrix/static/src/js/product_matrix_configurator.js @@ -0,0 +1,183 @@ +odoo.define('purchase.product_matrix_configurator', function (require) { + +var relationalFields = require('web.relational_fields'); +var FieldsRegistry = require('web.field_registry'); +var core = require('web.core'); +var _t = core._t; + +/** + * The purchase.product_matrix_configurator widget is a widget extending FieldMany2One + * It triggers the opening of the matrix edition when the product has multiple variants. + * + * + * !!! WARNING !!! + * + * This widget is only designed for Purchase Order Lines. + * !!! It should only be used on a product_template field !!! + */ +var MatrixConfiguratorWidget = relationalFields.FieldMany2One.extend({ + events: _.extend({}, relationalFields.FieldMany2One.prototype.events, { + 'click .o_edit_product_configuration': '_onEditProductConfiguration' + }), + + /** + * @override + */ + _render: function () { + this._super.apply(this, arguments); + if (this.mode === 'edit' && this.value && + (this._isConfigurableProduct())) { + this._addProductLinkButton(); + this._addConfigurationEditButton(); + } else if (this.mode === 'edit' && this.value) { + this._addProductLinkButton(); + } else { + this.$('.o_edit_product_configuration').hide(); + } + }, + + /** + * Add button linking to product_id/product_template_id form. + */ + _addProductLinkButton: function () { + if (this.$('.o_external_button').length === 0) { + var $productLinkButton = $('<button>', { + type: 'button', + class: 'fa fa-external-link btn btn-secondary o_external_button', + tabindex: '-1', + draggable: false, + 'aria-label': _t('External Link'), + title: _t('External Link') + }); + + var $inputDropdown = this.$('.o_input_dropdown'); + $inputDropdown.after($productLinkButton); + } + }, + + /** + * If current product is configurable, + * Show edit button (in Edit Mode) after the product/product_template + */ + _addConfigurationEditButton: function () { + var $inputDropdown = this.$('.o_input_dropdown'); + + if ($inputDropdown.length !== 0 && + this.$('.o_edit_product_configuration').length === 0) { + var $editConfigurationButton = $('<button>', { + type: 'button', + class: 'fa fa-pencil btn btn-secondary o_edit_product_configuration', + tabindex: '-1', + draggable: false, + 'aria-label': _t('Edit Configuration'), + title: _t('Edit Configuration') + }); + + $inputDropdown.after($editConfigurationButton); + } + }, + + /** + * Hook to override with _onEditProductConfiguration + * to know if edit pencil button has to be put next to the field + * + * @private + */ + _isConfigurableProduct: function () { + return this.recordData.is_configurable_product; + }, + + /** + * Override catching changes on product_id or product_template_id. + * Calls _onTemplateChange in case of product_template change. + * Calls _onProductChange in case of product change. + * Shouldn't be overridden by product configurators + * or only to setup some data for further computation + * before calling super. + * + * @override + */ + reset: async function (record, ev) { + await this._super(...arguments); + if (ev && ev.target === this && ev.data.changes && ev.data.changes.product_template_id && record.data.product_template_id.data.id) { + this._onTemplateChange(record.data.product_template_id.data.id, ev.data.dataPointID); + } + }, + + /** + * Hook for product_template based configurators + * (product configurator, matrix, ...). + * + * @param {integer} productTemplateId + * @param {String} dataPointID + * + * @private + */ + _onTemplateChange: function (productTemplateId, dataPointId) { + var self = this; + this._rpc({ + model: 'product.template', + method: 'get_single_product_variant', + args: [ + productTemplateId + ] + }).then(function (result) { + if (result.product_id) { + self.trigger_up('field_changed', { + dataPointID: dataPointId, + changes: { + product_id: {id: result.product_id}, + }, + }); + } else { + self._openMatrix(productTemplateId, dataPointId, false); + } + }); + }, + + /** + * Hook for editing a configured line. + * The button triggering this function is only shown in Edit mode, + * when _isConfigurableProduct is True. + * + * @private + */ + _onEditProductConfiguration: function () { + if (this.recordData.is_configurable_product) { + this._openMatrix(this.recordData.product_template_id.data.id, this.dataPointID, true); + } + }, + + _openMatrix: function (productTemplateId, dataPointId, edit) { + var attribs = edit ? this._getPTAVS() : []; + this.trigger_up('open_matrix', { + product_template_id: productTemplateId, + model: 'purchase.order', + dataPointId: dataPointId, + edit: edit, + editedCellAttributes: attribs, + // used to focus the cell representing the line on which the pencil was clicked. + }); + }, + + /** + * Returns the list of attribute ids (product.template.attribute.value) + * from the current POLine. + */ + _getPTAVS: function () { + var PTAVSIDS = []; + _.each(this.recordData.product_no_variant_attribute_value_ids.res_ids, function (id) { + PTAVSIDS.push(id); + }); + _.each(this.recordData.product_template_attribute_value_ids.res_ids, function (id) { + PTAVSIDS.push(id); + }); + return PTAVSIDS.sort(function (a, b) {return a - b;}); + } +}); + +FieldsRegistry.add('matrix_configurator', MatrixConfiguratorWidget); + +return MatrixConfiguratorWidget; + +}); |
