| 1 |
/** @odoo-module */ |
| 2 |
|
| 3 |
import { Component, useState, xml } from "@odoo/owl"; |
| 4 |
import { FILTER_SCHEMA } from "../core/config"; |
| 5 |
import { createUrlFromId } from "../core/url"; |
| 6 |
import { ensureArray, INCLUDE_LEVEL } from "../hoot_utils"; |
| 7 |
|
| 8 |
/** |
| 9 |
* @typedef {{ |
| 10 |
* class?: string; |
| 11 |
* ids?: Record<import("../core/config").SearchFilter, string[]>; |
| 12 |
* onClick?: (event: PointerEvent) => any; |
| 13 |
* options?: import("../core/url").CreateUrlFromIdOptions; |
| 14 |
* slots: { default: any }; |
| 15 |
* style?: string; |
| 16 |
* target?: string; |
| 17 |
* title?: string; |
| 18 |
* }} HootLinkProps |
| 19 |
*/ |
| 20 |
|
| 21 |
//----------------------------------------------------------------------------- |
| 22 |
// Global |
| 23 |
//----------------------------------------------------------------------------- |
| 24 |
|
| 25 |
const { |
| 26 |
Object: { entries: $entries }, |
| 27 |
} = globalThis; |
| 28 |
|
| 29 |
//----------------------------------------------------------------------------- |
| 30 |
// Exports |
| 31 |
//----------------------------------------------------------------------------- |
| 32 |
|
| 33 |
/** |
| 34 |
* Link component which computes its href lazily (i.e. on focus or pointerenter). |
| 35 |
* |
| 36 |
* @extends {Component<HootLinkProps, import("../hoot").Environment>} |
| 37 |
*/ |
| 38 |
export class HootLink extends Component { |
| 39 |
static template = xml` |
| 40 |
<a |
| 41 |
t-att-class="props.class" |
| 42 |
t-att-href="state.href" |
| 43 |
t-att-target="props.target" |
| 44 |
t-att-title="props.title" |
| 45 |
t-att-style="props.style" |
| 46 |
t-on-click.stop="onClick" |
| 47 |
t-on-focus="updateHref" |
| 48 |
t-on-pointerenter="updateHref" |
| 49 |
> |
| 50 |
<t t-slot="default" /> |
| 51 |
</a> |
| 52 |
`; |
| 53 |
static props = { |
| 54 |
class: { type: String, optional: true }, |
| 55 |
ids: { |
| 56 |
type: Object, |
| 57 |
values: [String, { type: Array, element: String }], |
| 58 |
optional: true, |
| 59 |
}, |
| 60 |
options: { |
| 61 |
type: Object, |
| 62 |
shape: { |
| 63 |
debug: { type: Boolean, optional: true }, |
| 64 |
ignore: { type: Boolean, optional: true }, |
| 65 |
}, |
| 66 |
optional: true, |
| 67 |
}, |
| 68 |
slots: { |
| 69 |
type: Object, |
| 70 |
shape: { |
| 71 |
default: { type: Object, optional: true }, |
| 72 |
}, |
| 73 |
}, |
| 74 |
style: { type: String, optional: true }, |
| 75 |
target: { type: String, optional: true }, |
| 76 |
title: { type: String, optional: true }, |
| 77 |
}; |
| 78 |
|
| 79 |
setup() { |
| 80 |
this.state = useState({ href: "#" }); |
| 81 |
} |
| 82 |
|
| 83 |
/** |
| 84 |
* @param {PointerEvent} ev |
| 85 |
*/ |
| 86 |
onClick(ev) { |
| 87 |
const { ids, options } = this.props; |
| 88 |
if (ids && ev.altKey) { |
| 89 |
const { includeSpecs } = this.env.runner.state; |
| 90 |
let appliedFilter = false; |
| 91 |
for (const [type, idOrIds] of $entries(ids)) { |
| 92 |
if (!(type in FILTER_SCHEMA)) { |
| 93 |
continue; |
| 94 |
} |
| 95 |
const targetValue = options?.ignore ? -INCLUDE_LEVEL.url : +INCLUDE_LEVEL.url; |
| 96 |
for (const id of ensureArray(idOrIds)) { |
| 97 |
const finalValue = includeSpecs[type][id] === targetValue ? 0 : targetValue; |
| 98 |
this.env.runner.include(type, id, finalValue); |
| 99 |
appliedFilter = true; |
| 100 |
} |
| 101 |
} |
| 102 |
|
| 103 |
if (appliedFilter) { |
| 104 |
ev.preventDefault(); |
| 105 |
} |
| 106 |
} else { |
| 107 |
this.props.onClick?.(ev); |
| 108 |
} |
| 109 |
} |
| 110 |
|
| 111 |
updateHref() { |
| 112 |
const { ids, options } = this.props; |
| 113 |
const simplifiedIds = this.env.runner.simplifyUrlIds(ids); |
| 114 |
this.state.href = createUrlFromId(simplifiedIds, options); |
| 115 |
} |
| 116 |
} |