/** @odoo-module */ import { Component, onWillRender, onWillUpdateProps, xml as owlXml, toRaw, useState, } from "@odoo/owl"; import { isNode, toSelector } from "@web/../lib/hoot-dom/helpers/dom"; import { isInstanceOf, isIterable } from "@web/../lib/hoot-dom/hoot_dom_utils"; import { logger } from "../core/logger"; import { getTypeOf, isSafe, Markup, S_ANY, S_CIRCULAR, S_NONE, stringify, toExplicitString, } from "../hoot_utils"; /** * @typedef {{ * value?: any; * }} TechnicalValueProps */ //----------------------------------------------------------------------------- // Global //----------------------------------------------------------------------------- const { Object: { keys: $keys }, } = globalThis; //----------------------------------------------------------------------------- // Internal //----------------------------------------------------------------------------- /** * Compacted version of {@link owlXml} removing all whitespace between tags. * * @type {typeof String.raw} */ function xml(template, ...substitutions) { return owlXml({ raw: String.raw(template, ...substitutions) .replace(/>\s+/g, ">") .replace(/\s+} */ export class HootTechnicalValue extends Component { static components = { HootTechnicalValue }; static props = { value: { optional: true }, }; static template = xml`
                    
                        
                    
                
<> Symbol()
                
                
                    
                        [
                        
]
{
  • :
}
`; getTypeOf = getTypeOf; isIterable = isIterable; isNode = isNode; stringify = stringify; toSelector = toSelector; SPECIAL_SYMBOLS = [S_ANY, S_CIRCULAR, S_NONE]; get explicitValue() { return toExplicitString(this.value); } setup() { this.logged = false; this.state = useState({ open: false, promiseState: null, }); this.wrapPromiseValue(this.props.value); onWillRender(() => { this.isMarkup = Markup.isMarkup(this.props.value); this.value = toRaw(this.props.value); this.isSafe = isSafe(this.value); }); onWillUpdateProps((nextProps) => { this.state.open = false; this.wrapPromiseValue(nextProps.value); }); } onClick() { this.log(this.value); this.state.open = !this.state.open; } getLabelAndSize() { if (isInstanceOf(this.value, Date)) { return [this.value.toISOString(), null]; } if (isInstanceOf(this.value, RegExp)) { return [String(this.value), null]; } return [this.value.constructor.name, this.getSize()]; } getSize() { for (const Class of INVARIABLE_OBJECTS) { if (isInstanceOf(this.value, Class)) { return null; } } if (!this.isSafe) { return 0; } const values = isIterable(this.value) ? [...this.value] : $keys(this.value); return values.length; } displayComma(value) { return value && typeof value === "object" ? "" : ","; } log() { if (this.logged) { return; } this.logged = true; logger.debug(this.value); } /** * @param {Symbol} symbol */ symbolValue(symbol) { return symbol.toString().slice(7, -1); } wrapPromiseValue(promise) { if (!isInstanceOf(promise, Promise)) { return; } this.state.promiseState = ["pending", null]; Promise.resolve(promise).then( (value) => { this.state.promiseState = ["fulfilled", value]; return value; }, (reason) => { this.state.promiseState = ["rejected", reason]; throw reason; } ); } }