|
| 1 | +/** @odoo-module **/ |
| 2 | + |
| 3 | +import {Component, onMounted, onPatched, useRef} from "@odoo/owl"; |
| 4 | +import {_t} from "@web/core/l10n/translation"; |
| 5 | +import {registry} from "@web/core/registry"; |
| 6 | +import {standardFieldProps} from "@web/views/fields/standard_field_props"; |
| 7 | + |
| 8 | +/** |
| 9 | + * Custom widget that renders HTML search results and handles row clicks. |
| 10 | + * When a row with class "o_cr_search_result" is clicked, it writes the |
| 11 | + * partner ID to the _selected_partner_id bridge field, which triggers |
| 12 | + * a server-side onchange to set registrant_id. |
| 13 | + */ |
| 14 | +export class CrSearchResultsField extends Component { |
| 15 | + static template = "spp_change_request_v2.CrSearchResultsField"; |
| 16 | + static props = {...standardFieldProps}; |
| 17 | + |
| 18 | + setup() { |
| 19 | + this.containerRef = useRef("container"); |
| 20 | + onMounted(() => this._attachClickHandler()); |
| 21 | + onPatched(() => this._attachClickHandler()); |
| 22 | + } |
| 23 | + |
| 24 | + get htmlContent() { |
| 25 | + return this.props.record.data[this.props.name] || ""; |
| 26 | + } |
| 27 | + |
| 28 | + _attachClickHandler() { |
| 29 | + const el = this.containerRef.el; |
| 30 | + if (!el) return; |
| 31 | + // Row selection |
| 32 | + el.querySelectorAll(".o_cr_search_result").forEach((row) => { |
| 33 | + row.onclick = (ev) => { |
| 34 | + ev.preventDefault(); |
| 35 | + ev.stopPropagation(); |
| 36 | + const partnerId = parseInt(row.dataset.partnerId); |
| 37 | + if (partnerId) { |
| 38 | + this.props.record.update({_selected_partner_id: partnerId}); |
| 39 | + } |
| 40 | + }; |
| 41 | + }); |
| 42 | + // Pagination |
| 43 | + el.querySelectorAll(".o_cr_page_prev, .o_cr_page_next").forEach((link) => { |
| 44 | + link.onclick = (ev) => { |
| 45 | + ev.preventDefault(); |
| 46 | + ev.stopPropagation(); |
| 47 | + const page = parseInt(link.dataset.page); |
| 48 | + if (!isNaN(page) && page >= 0) { |
| 49 | + this.props.record.update({_search_page: page}); |
| 50 | + } |
| 51 | + }; |
| 52 | + }); |
| 53 | + } |
| 54 | +} |
| 55 | + |
| 56 | +export const crSearchResultsField = { |
| 57 | + component: CrSearchResultsField, |
| 58 | + displayName: _t("CR Search Results"), |
| 59 | + supportedTypes: ["html"], |
| 60 | +}; |
| 61 | + |
| 62 | +registry.category("fields").add("cr_search_results", crSearchResultsField); |
0 commit comments