function getTreeItem()

in web-app-pix2info-python/src/frontend/scripts.js [774:813]


function getTreeItem(key, value) {
    if (typeof key !== 'string') {
        console.error(`## ${key} / Key type is not a string: ${typeof key}`)
        return null
    }

    const item = document.createElement('sl-tree-item')
    switch (typeof value) {
        case 'object':
            const isArray = Array.isArray(value)
            item.textContent = isArray ? `${key}[]` : `${key}`
            if (!isArray && !Object.entries(value).length) {
                // Empty object (e.g. a protobuf message with default fields)
                break
            }
            if (LAZY_LOAD_TREE) {
                item.value = value
                item.setAttribute('lazy', true)
                item.addEventListener('sl-lazy-load', lazyLoadTreeItem)
            } else {
                fillDocumentTree(item, value)
            }
            break
        case 'string':
            if (MAX_TREE_ITEM_LEN < value.length)
                value = `${value.slice(0, MAX_TREE_ITEM_LEN)}…`
            value = value.replaceAll('\n', '\\n')
            item.textContent = `${key}: "${value}"`
            break
        case 'number':
        case 'bigint':
            item.textContent = `${key}: ${value}`
            break
        default:
            console.error(`## ${key} / Unhandled value type: ${typeof value}`)
            return null
    }

    return item
}