| 1 |
/** @odoo-module */ |
| 2 |
|
| 3 |
import { |
| 4 |
Component, |
| 5 |
onWillRender, |
| 6 |
onWillUpdateProps, |
| 7 |
xml as owlXml, |
| 8 |
toRaw, |
| 9 |
useState, |
| 10 |
} from "@odoo/owl"; |
| 11 |
import { isNode, toSelector } from "@web/../lib/hoot-dom/helpers/dom"; |
| 12 |
import { isInstanceOf, isIterable } from "@web/../lib/hoot-dom/hoot_dom_utils"; |
| 13 |
import { logger } from "../core/logger"; |
| 14 |
import { |
| 15 |
getTypeOf, |
| 16 |
isSafe, |
| 17 |
Markup, |
| 18 |
S_ANY, |
| 19 |
S_CIRCULAR, |
| 20 |
S_NONE, |
| 21 |
stringify, |
| 22 |
toExplicitString, |
| 23 |
} from "../hoot_utils"; |
| 24 |
|
| 25 |
/** |
| 26 |
* @typedef {{ |
| 27 |
* value?: any; |
| 28 |
* }} TechnicalValueProps |
| 29 |
*/ |
| 30 |
|
| 31 |
//----------------------------------------------------------------------------- |
| 32 |
// Global |
| 33 |
//----------------------------------------------------------------------------- |
| 34 |
|
| 35 |
const { |
| 36 |
Object: { keys: $keys }, |
| 37 |
} = globalThis; |
| 38 |
|
| 39 |
//----------------------------------------------------------------------------- |
| 40 |
// Internal |
| 41 |
//----------------------------------------------------------------------------- |
| 42 |
|
| 43 |
/** |
| 44 |
* Compacted version of {@link owlXml} removing all whitespace between tags. |
| 45 |
* |
| 46 |
* @type {typeof String.raw} |
| 47 |
*/ |
| 48 |
function xml(template, ...substitutions) { |
| 49 |
return owlXml({ |
| 50 |
raw: String.raw(template, ...substitutions) |
| 51 |
.replace(/>\s+/g, ">") |
| 52 |
.replace(/\s+</g, "<"), |
| 53 |
}); |
| 54 |
} |
| 55 |
|
| 56 |
const INVARIABLE_OBJECTS = [Promise, RegExp]; |
| 57 |
|
| 58 |
//----------------------------------------------------------------------------- |
| 59 |
// Exports |
| 60 |
//----------------------------------------------------------------------------- |
| 61 |
|
| 62 |
/** @extends {Component<TechnicalValueProps, import("../hoot").Environment>} */ |
| 63 |
export class HootTechnicalValue extends Component { |
| 64 |
static components = { HootTechnicalValue }; |
| 65 |
|
| 66 |
static props = { |
| 67 |
value: { optional: true }, |
| 68 |
}; |
| 69 |
|
| 70 |
static template = xml` |
| 71 |
<t t-if="isMarkup"> |
| 72 |
<t t-if="value.type === 'technical'"> |
| 73 |
<pre class="hoot-technical" t-att-class="value.className"> |
| 74 |
<t t-foreach="value.content" t-as="subValue" t-key="subValue_index"> |
| 75 |
<HootTechnicalValue value="subValue" /> |
| 76 |
</t> |
| 77 |
</pre> |
| 78 |
</t> |
| 79 |
<t t-else=""> |
| 80 |
<t t-if="value.tagName === 't'" t-esc="value.content" /> |
| 81 |
<t t-else="" t-tag="value.tagName" t-att-class="value.className" t-esc="value.content" /> |
| 82 |
</t> |
| 83 |
</t> |
| 84 |
<t t-elif="isNode(value)"> |
| 85 |
<t t-set="elParts" t-value="toSelector(value, { object: true })" /> |
| 86 |
<button |
| 87 |
class="hoot-html" |
| 88 |
t-on-click.stop="log" |
| 89 |
> |
| 90 |
<t><<t t-esc="elParts.tag" /></t> |
| 91 |
<t t-if="elParts.id"> |
| 92 |
<span class="hoot-html-id" t-esc="elParts.id" /> |
| 93 |
</t> |
| 94 |
<t t-if="elParts.class"> |
| 95 |
<span class="hoot-html-class" t-esc="elParts.class" /> |
| 96 |
</t> |
| 97 |
<t>/></t> |
| 98 |
</button> |
| 99 |
</t> |
| 100 |
<t t-elif="SPECIAL_SYMBOLS.includes(value)"> |
| 101 |
<span class="italic"> |
| 102 |
<<t t-esc="symbolValue(value)" />> |
| 103 |
</span> |
| 104 |
</t> |
| 105 |
<t t-elif="typeof value === 'symbol'"> |
| 106 |
<span> |
| 107 |
Symbol(<span class="hoot-string" t-esc="stringify(symbolValue(value))" />) |
| 108 |
</span> |
| 109 |
</t> |
| 110 |
<t t-elif="value and typeof value === 'object'"> |
| 111 |
<t t-set="labelSize" t-value="getLabelAndSize()" /> |
| 112 |
<pre class="hoot-technical"> |
| 113 |
<button |
| 114 |
class="hoot-object inline-flex items-center gap-1 me-1" |
| 115 |
t-on-click.stop="onClick" |
| 116 |
> |
| 117 |
<t t-if="labelSize[1] > 0"> |
| 118 |
<i |
| 119 |
class="fa fa-caret-right" |
| 120 |
t-att-class="{ 'rotate-90': state.open }" |
| 121 |
/> |
| 122 |
</t> |
| 123 |
<t t-esc="labelSize[0]" /> |
| 124 |
<t t-if="state.promiseState"> |
| 125 |
< |
| 126 |
<span class="text-gray" t-esc="state.promiseState[0]" /> |
| 127 |
<t t-if="state.promiseState[0] !== 'pending'"> |
| 128 |
: <HootTechnicalValue value="state.promiseState[1]" /> |
| 129 |
</t> |
| 130 |
> |
| 131 |
</t> |
| 132 |
<t t-elif="labelSize[1] !== null"> |
| 133 |
(<t t-esc="labelSize[1]" />) |
| 134 |
</t> |
| 135 |
</button> |
| 136 |
<t t-if="state.open and labelSize[1] > 0"> |
| 137 |
<t t-if="isIterable(value)"> |
| 138 |
<t>[</t> |
| 139 |
<ul class="ps-4"> |
| 140 |
<t t-foreach="value" t-as="subValue" t-key="subValue_index"> |
| 141 |
<li class="flex"> |
| 142 |
<HootTechnicalValue value="subValue" /> |
| 143 |
<t t-esc="displayComma(subValue)" /> |
| 144 |
</li> |
| 145 |
</t> |
| 146 |
</ul> |
| 147 |
<t>]</t> |
| 148 |
</t> |
| 149 |
<t t-else=""> |
| 150 |
<t>{</t> |
| 151 |
<ul class="ps-4"> |
| 152 |
<t t-foreach="value" t-as="key" t-key="key"> |
| 153 |
<li class="flex"> |
| 154 |
<span class="hoot-key" t-esc="key" /> |
| 155 |
<span class="me-1">:</span> |
| 156 |
<HootTechnicalValue value="value[key]" /> |
| 157 |
<t t-esc="displayComma(value[key])" /> |
| 158 |
</li> |
| 159 |
</t> |
| 160 |
</ul> |
| 161 |
<t>}</t> |
| 162 |
</t> |
| 163 |
</t> |
| 164 |
</pre> |
| 165 |
</t> |
| 166 |
<t t-else=""> |
| 167 |
<span t-attf-class="hoot-{{ getTypeOf(value) }}"> |
| 168 |
<t t-esc="typeof value === 'string' ? stringify(explicitValue) : explicitValue" /> |
| 169 |
</span> |
| 170 |
</t> |
| 171 |
`; |
| 172 |
|
| 173 |
getTypeOf = getTypeOf; |
| 174 |
isIterable = isIterable; |
| 175 |
isNode = isNode; |
| 176 |
stringify = stringify; |
| 177 |
toSelector = toSelector; |
| 178 |
|
| 179 |
SPECIAL_SYMBOLS = [S_ANY, S_CIRCULAR, S_NONE]; |
| 180 |
|
| 181 |
get explicitValue() { |
| 182 |
return toExplicitString(this.value); |
| 183 |
} |
| 184 |
|
| 185 |
setup() { |
| 186 |
this.logged = false; |
| 187 |
this.state = useState({ |
| 188 |
open: false, |
| 189 |
promiseState: null, |
| 190 |
}); |
| 191 |
this.wrapPromiseValue(this.props.value); |
| 192 |
|
| 193 |
onWillRender(() => { |
| 194 |
this.isMarkup = Markup.isMarkup(this.props.value); |
| 195 |
this.value = toRaw(this.props.value); |
| 196 |
this.isSafe = isSafe(this.value); |
| 197 |
}); |
| 198 |
onWillUpdateProps((nextProps) => { |
| 199 |
this.state.open = false; |
| 200 |
this.wrapPromiseValue(nextProps.value); |
| 201 |
}); |
| 202 |
} |
| 203 |
|
| 204 |
onClick() { |
| 205 |
this.log(this.value); |
| 206 |
this.state.open = !this.state.open; |
| 207 |
} |
| 208 |
|
| 209 |
getLabelAndSize() { |
| 210 |
if (isInstanceOf(this.value, Date)) { |
| 211 |
return [this.value.toISOString(), null]; |
| 212 |
} |
| 213 |
if (isInstanceOf(this.value, RegExp)) { |
| 214 |
return [String(this.value), null]; |
| 215 |
} |
| 216 |
return [this.value.constructor.name, this.getSize()]; |
| 217 |
} |
| 218 |
|
| 219 |
getSize() { |
| 220 |
for (const Class of INVARIABLE_OBJECTS) { |
| 221 |
if (isInstanceOf(this.value, Class)) { |
| 222 |
return null; |
| 223 |
} |
| 224 |
} |
| 225 |
if (!this.isSafe) { |
| 226 |
return 0; |
| 227 |
} |
| 228 |
const values = isIterable(this.value) ? [...this.value] : $keys(this.value); |
| 229 |
return values.length; |
| 230 |
} |
| 231 |
|
| 232 |
displayComma(value) { |
| 233 |
return value && typeof value === "object" ? "" : ","; |
| 234 |
} |
| 235 |
|
| 236 |
log() { |
| 237 |
if (this.logged) { |
| 238 |
return; |
| 239 |
} |
| 240 |
this.logged = true; |
| 241 |
logger.debug(this.value); |
| 242 |
} |
| 243 |
|
| 244 |
/** |
| 245 |
* @param {Symbol} symbol |
| 246 |
*/ |
| 247 |
symbolValue(symbol) { |
| 248 |
return symbol.toString().slice(7, -1); |
| 249 |
} |
| 250 |
|
| 251 |
wrapPromiseValue(promise) { |
| 252 |
if (!isInstanceOf(promise, Promise)) { |
| 253 |
return; |
| 254 |
} |
| 255 |
this.state.promiseState = ["pending", null]; |
| 256 |
Promise.resolve(promise).then( |
| 257 |
(value) => { |
| 258 |
this.state.promiseState = ["fulfilled", value]; |
| 259 |
return value; |
| 260 |
}, |
| 261 |
(reason) => { |
| 262 |
this.state.promiseState = ["rejected", reason]; |
| 263 |
throw reason; |
| 264 |
} |
| 265 |
); |
| 266 |
} |
| 267 |
} |