(function($) { 'use strict'; const MomentryPortal = { apiRoot: momentryApi.root, nonce: momentryApi.nonce, init: function() { this.bindEvents(); this.loadIdentities(); }, bindEvents: function() { $(document).on('click', '.identity-card', this.showIdentityDetail.bind(this)); $(document).on('click', '.angle-coverage-btn', this.showAngleCoverage.bind(this)); $(document).on('click', '.body-actions-btn', this.showBodyActions.bind(this)); }, request: function(endpoint, method, data) { return $.ajax({ url: this.apiRoot + endpoint, method: method, data: data, beforeSend: function(xhr) { xhr.setRequestHeader('X-WP-Nonce', momentryApi.nonce); }, }); }, loadIdentities: function(page = 1) { const self = this; this.request('momentry/v1/identities', 'GET', {page: page, per_page: 20}) .done(function(response) { if (response.success) { self.renderIdentityList(response.data); self.renderPagination(response.pagination); } }) .fail(function(error) { console.error('Failed to load identities:', error); $('.identity-list').html('

Failed to load identities. Please try again.

'); }); }, renderIdentityList: function(identities) { const html = identities.map(function(identity) { return `

${identity.name}

${identity.source} ${identity.total_references} vectors
${((identity.quality_avg || 0) * 100).toFixed(1)}%
${identity.trace_duration ? `
${identity.trace_duration}s ${(identity.trace_confidence * 100).toFixed(1)}% confidence
` : ''}
`; }).join(''); $('.identity-list').html(html); }, renderPagination: function(pagination) { const html = ` `; $('.pagination-container').html(html); }, showIdentityDetail: function(e) { const uuid = $(e.currentTarget).data('uuid'); const self = this; this.request(`momentry/v1/identities/${uuid}`, 'GET') .done(function(response) { if (response.success) { self.renderIdentityDetail(response.data); } }) .fail(function(error) { console.error('Failed to load identity detail:', error); }); }, renderIdentityDetail: function(identity) { const html = `

${identity.name}

Reference Vectors

Total: ${identity.reference_vectors.total}

Angles: ${identity.reference_vectors.angles.join(', ') || 'None'}

Quality Avg: ${((identity.reference_vectors.quality_avg || 0) * 100).toFixed(1)}%

${identity.trace_stats ? `

Trace Statistics

Duration: ${identity.trace_stats.duration_seconds}s

Confidence: ${(identity.trace_stats.avg_confidence * 100).toFixed(1)}%

Appearances: ${identity.trace_stats.total_appearances} frames

` : ''}
`; $('.modal-container').html(html).show(); }, showAngleCoverage: function(e) { e.stopPropagation(); const uuid = $(e.currentTarget).data('uuid'); const self = this; this.request(`momentry/v1/identities/${uuid}/angle-coverage`, 'GET') .done(function(response) { if (response.success) { self.renderAngleCoverage(response.data); } }) .fail(function(error) { console.error('Failed to load angle coverage:', error); }); }, renderAngleCoverage: function(data) { const angles = Object.entries(data.angles).map(function([angle, stats]) { const statusIcon = stats.status === 'dominant' ? '✅' : stats.status === 'present' ? '✅' : '⚠️'; const count = stats.count > 0 ? `${stats.count} frames` : '0 frames'; const quality = stats.quality_avg ? `(${(stats.quality_avg * 100).toFixed(1)}%)` : ''; return `
${statusIcon} ${angle.replace('_', ' ')} ${count} ${quality}
`; }).join(''); const html = `

Angle Coverage

Coverage Score: ${data.score}%
${angles}

${data.recommendation}

`; $('.modal-container').html(html).show(); }, showBodyActions: function(e) { e.stopPropagation(); const uuid = $(e.currentTarget).data('uuid'); const self = this; this.request(`momentry/v1/identities/${uuid}/body-actions`, 'GET') .done(function(response) { if (response.success) { self.renderBodyActions(response.data); } }) .fail(function(error) { console.error('Failed to load body actions:', error); }); }, renderBodyActions: function(data) { const actions = Object.entries(data.actions || {}).map(function([category, items]) { const itemsHtml = items.map(function(item) { return `${item.action}: ${item.count}`; }).join(''); return `

${category}

${itemsHtml}
`; }).join(''); const html = `

Body Actions: ${data.name}

${actions || '

No body actions detected

'}
`; $('.modal-container').html(html).show(); }, }; $(document).ready(function() { MomentryPortal.init(); $(document).on('click', '.close-modal', function() { $('.modal-container').hide(); }); $(document).on('click', '.pagination .prev:not([disabled])', function() { const page = parseInt($('.page-info').text().match(/Page (\d+)/)[1]); MomentryPortal.loadIdentities(page - 1); }); $(document).on('click', '.pagination .next:not([disabled])', function() { const page = parseInt($('.page-info').text().match(/Page (\d+)/)[1]); MomentryPortal.loadIdentities(page + 1); }); }); })(jQuery);