|
| 1 | +/* Copyright 2021 Tecnativa - David Vidal |
| 2 | + License LGPL-3.0 or later (https://www.gnu.org/licenses/lgpl). */ |
| 3 | + |
| 4 | +import publicWidget from "@web/legacy/js/public/public_widget"; |
| 5 | + |
| 6 | +publicWidget.registry.PortalRmaSale = publicWidget.Widget.extend({ |
| 7 | + selector: "#form-request-rma", |
| 8 | + events: { |
| 9 | + "change .rma-operation": "_onChangeOperationId", |
| 10 | + "change #delivery-rma-qty input": "_onChangeQty", |
| 11 | + "click .o_rma_portal_shipping_card": "_onChangeShippingAddress", |
| 12 | + }, |
| 13 | + |
| 14 | + /** |
| 15 | + * @override |
| 16 | + */ |
| 17 | + start: function () { |
| 18 | + const ids = this.$("[name*='-operation_id']") |
| 19 | + .map(function () { |
| 20 | + return this.name.replace("-operation_id", ""); |
| 21 | + }) |
| 22 | + .get(); |
| 23 | + this.$submit = $("#form-request-rma button[type='submit']"); |
| 24 | + this.rows_ids = ids; |
| 25 | + // We'll build an object that will ease the form check. It could be further |
| 26 | + // extended with additional checks. |
| 27 | + this.rows = {}; |
| 28 | + $.each(ids, (id) => { |
| 29 | + this.rows[id] = { |
| 30 | + $comment: this.$(`#comment-${id}`), |
| 31 | + $comment_input: this.$(`[name='${id}-description']`), |
| 32 | + $operation: this.$(`[name='${id}-operation_id']`), |
| 33 | + $qty: this.$(`[name='${id}-quantity']`), |
| 34 | + }; |
| 35 | + }); |
| 36 | + this._checkCanSubmit(); |
| 37 | + }, |
| 38 | + /** |
| 39 | + * @private |
| 40 | + * @param {Object} row: the form row structure |
| 41 | + */ |
| 42 | + _show_comment: function (row) { |
| 43 | + if (row.$comment) { |
| 44 | + row.$comment.addClass("show"); |
| 45 | + if (row.$comment_input) { |
| 46 | + row.$comment_input.focus(); |
| 47 | + } |
| 48 | + } |
| 49 | + }, |
| 50 | + /** |
| 51 | + * @private |
| 52 | + * @param {Object} row: the form row structure |
| 53 | + */ |
| 54 | + _hide_comment: function (row) { |
| 55 | + if (row.$comment) { |
| 56 | + row.$comment.removeClass("show"); |
| 57 | + } |
| 58 | + }, |
| 59 | + /** |
| 60 | + * We should be able to submit only when an operation is selected and a |
| 61 | + * quantity entered in a row at least. |
| 62 | + * @private |
| 63 | + */ |
| 64 | + _canSubmit: function () { |
| 65 | + var can_submit = false; |
| 66 | + for (const id of this.rows_ids) { |
| 67 | + const row = this.rows[id]; |
| 68 | + if ( |
| 69 | + row && |
| 70 | + // Qty greater than 0 |
| 71 | + row.$qty && |
| 72 | + row.$qty.val() && |
| 73 | + Number(row.$qty.val()) && |
| 74 | + // An operation is defined |
| 75 | + row.$operation && |
| 76 | + row.$operation.val() |
| 77 | + ) { |
| 78 | + can_submit = true; |
| 79 | + break; |
| 80 | + } |
| 81 | + } |
| 82 | + return can_submit; |
| 83 | + }, |
| 84 | + /** |
| 85 | + * Checked every time we change the quantity or the operation and at start |
| 86 | + * |
| 87 | + * @private |
| 88 | + * @param {Object} row: the form row structure |
| 89 | + */ |
| 90 | + _checkCanSubmit: function () { |
| 91 | + this.$submit.prop("disabled", !this._canSubmit()); |
| 92 | + }, |
| 93 | + /** |
| 94 | + * @private |
| 95 | + * @param {InputEvent} ev |
| 96 | + */ |
| 97 | + _onChangeOperationId: function (ev) { |
| 98 | + // Toggle comment on or off if an operation is requested |
| 99 | + const id = ev.currentTarget.name.replace("-operation_id", ""); |
| 100 | + var row = this.rows[id]; |
| 101 | + if (row && row.$operation && row.$operation.val()) { |
| 102 | + this._show_comment(row); |
| 103 | + } else { |
| 104 | + this._hide_comment(row); |
| 105 | + } |
| 106 | + this._checkCanSubmit(); |
| 107 | + }, |
| 108 | + /** |
| 109 | + * @private |
| 110 | + */ |
| 111 | + _onChangeQty: function () { |
| 112 | + this._checkCanSubmit(); |
| 113 | + }, |
| 114 | + _onChangeShippingAddress: function (ev) { |
| 115 | + const $address_container = $(ev.currentTarget.parentElement); |
| 116 | + $address_container.find("input").removeAttr("checked"); |
| 117 | + $address_container |
| 118 | + .find(".o_rma_portal_shipping_card") |
| 119 | + .removeClass("bg-primary") |
| 120 | + .removeClass("text-primary"); |
| 121 | + $(ev.currentTarget).find("input").attr("checked", "checked"); |
| 122 | + $(ev.currentTarget).addClass("bg-primary").addClass("text-primary"); |
| 123 | + }, |
| 124 | +}); |
0 commit comments