<commit_before>/* Copyright 2021 Tecnativa - <NAME>
   License LGPL-3.0 or later (https://www.gnu.org/licenses/lgpl). */

odoo.define("rma_sale.animation", function (require) {
    "use strict";

    var sAnimation = require("website.content.snippets.animation");

    // In the customer portal when a RMA operation is selected show the comments
    // selector so the user doesn't miss the chance to add his comments
    sAnimation.registry.rma_operation_portal = sAnimation.Class.extend({
        selector: ".rma-operation",
        start: function () {
            this.id = this.el.name.replace("-operation_id", "");
            this.$comment = $("#comment-" + this.id);
            this.$comment_input = $("[name='" + this.id + "-description']");
            var _this = this;
            this.$el.on("change", function () {
                _this._onChangeOperationId();
            });
        },
        _show_comment: function () {
            if (this.$comment) {
                this.$comment.removeClass("show");
                this.$comment.addClass("show");
                if (this.$comment_input) {
                    this.$comment_input.focus();
                }
            }
        },
        _hide_comment: function () {
            if (this.$comment) {
                this.$comment.removeClass("show");
            }
        },
        _onChangeOperationId: function () {
            // Toggle comment on or off if an operation is requested
            if (this.$el && this.$el.val()) {
                this._show_comment();
            } else {
                this._hide_comment();
            }
        },
    });
});
<commit_msg>[IMP] rma_sale: avoid submit form in portal if no info<commit_after>/* Copyright 2021 Tecnativa - <NAME>
   License LGPL-3.0 or later (https://www.gnu.org/licenses/lgpl). */

odoo.define("rma_sale.animation", function (require) {
    "use strict";

    const publicWidget = require("web.public.widget");

    /**
     * Adds some machinery to the customer portal RMA form:
     *
     * - Avoid submitting the form if no qty or operation is reported.
     * - Show automatically the observations field when the operation is selected
     *   and hide it back with no operation selected.
     */
    publicWidget.registry.PortalRmaSale = publicWidget.Widget.extend({
        selector: "#form-request-rma",
        events: {
            "change .rma-operation": "_onChangeOperationId",
            "change #delivery-rma-qty input": "_onChangeQty",
        },

        /**
         * @override
         */
        start: function () {
            const ids = this.$("[name*='-operation_id']")
                .map(function () {
                    return this.name.replace("-operation_id", "");
                })
                .get();
            this.$submit = $("#form-request-rma button[type='submit']");
            this.rows_ids = ids;
            // We'll build an object that will ease the form check. It could be further
            // extended with additional checks.
            this.rows = {};
            _.each(ids, (id) => {
                this.rows[id] = {
                    $comment: this.$(`#comment-${id}`),
                    $comment_input: this.$(`[name='${id}-description']`),
                    $operation: this.$(`[name='${id}-operation_id']`),
                    $qty: this.$(`[name='${id}-quantity']`),
                };
            });
            this._checkCanSubmit();
        },
        /**
         * @private
         * @param {Object} row: the form row structure
         */
        _show_comment: function (row) {
            if (row.$comment) {
                row.$comment.addClass("show");
                if (row.$comment_input) {
                    row.$comment_input.focus();
                }
            }
        },
        /**
         * @private
         * @param {Object} row: the form row structure
         */
        _hide_comment: function (row) {
            if (row.$comment) {
                row.$comment.removeClass("show");
            }
        },
        /**
         * We should be able to submit only when an operation is selected and a
         * quantity entered in a row at least.
         * @private
         */
        _canSubmit: function () {
            var can_submit = false;
            for (const id of this.rows_ids) {
                const row = this.rows[id];
                if (
                    row &&
                    // Qty greater than 0
                    row.$qty &&
                    row.$qty.val() &&
                    Number(row.$qty.val()) &&
                    // An operation is defined
                    row.$operation &&
                    row.$operation.val()
                ) {
                    can_submit = true;
                    break;
                }
            }
            return can_submit;
        },
        /**
         * Checked every time we change the quantity or the operation and at start
         *
         * @private
         * @param {Object} row: the form row structure
         */
        _checkCanSubmit: function () {
            this.$submit.prop("disabled", !this._canSubmit());
        },
        /**
         * @private
         * @param {InputEvent} ev
         */
        _onChangeOperationId: function (ev) {
            // Toggle comment on or off if an operation is requested
            const id = ev.currentTarget.name.replace("-operation_id", "");
            var row = this.rows[id];
            if (row && row.$operation && row.$operation.val()) {
                this._show_comment(row);
            } else {
                this._hide_comment(row);
            }
            this._checkCanSubmit();
        },
        /**
         * @private
         */
        _onChangeQty: function () {
            this._checkCanSubmit();
        },
    });
});

{'fraction_non_alphanumeric': 0.13025710880299676, 'fraction_numerical': 0.0022135194959986377, 'mean_word_length': 1.3858651502843218, 'pattern_counts': {'":': 2, '<': 5, '<?xml version=': 0, '>': 6, 'https://': 2, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5757480', 'n_tokens_mistral': 1554, 'n_tokens_neox': 1505, 'n_words': 365}