HEX
Server: nginx/1.18.0
System: Linux test-ipsremont 5.4.0-214-generic #234-Ubuntu SMP Fri Mar 14 23:50:27 UTC 2025 x86_64
User: ips (1000)
PHP: 8.0.30
Disabled: pcntl_alarm,pcntl_fork,pcntl_waitpid,pcntl_wait,pcntl_wifexited,pcntl_wifstopped,pcntl_wifsignaled,pcntl_wifcontinued,pcntl_wexitstatus,pcntl_wtermsig,pcntl_wstopsig,pcntl_signal,pcntl_signal_get_handler,pcntl_signal_dispatch,pcntl_get_last_error,pcntl_strerror,pcntl_sigprocmask,pcntl_sigwaitinfo,pcntl_sigtimedwait,pcntl_exec,pcntl_getpriority,pcntl_setpriority,pcntl_async_signals,pcntl_unshare,
Upload Files
File: //var/www/quadcode/node_modules/alpinejs/src/magics/$id.js
import { magic } from '../magics'
import { closestIdRoot, findAndIncrementId } from '../ids'
import { interceptClone } from '../clone'

magic('id', (el, { cleanup }) => (name, key = null) => {
    let cacheKey = `${name}${key ? `-${key}` : ''}`

    return cacheIdByNameOnElement(el, cacheKey, cleanup, () => {
        let root = closestIdRoot(el, name)

        let id = root
            ? root._x_ids[name]
            : findAndIncrementId(name)

        return key
            ? `${name}-${id}-${key}`
            : `${name}-${id}`
    })
})

interceptClone((from, to) => {
    // Transfer over existing ID registrations from
    // the existing dom tree over to the new one
    // so that there aren't ID mismatches...
    if (from._x_id) {
        to._x_id = from._x_id
    }
})

function cacheIdByNameOnElement(el, cacheKey, cleanup, callback)
{
    if (! el._x_id) el._x_id = {}

    // We only want $id to run once per an element's lifecycle...
    if (el._x_id[cacheKey]) return el._x_id[cacheKey]

    let output = callback()

    el._x_id[cacheKey] = output

    cleanup(() => {
        delete el._x_id[cacheKey]
    })

    return output
}