39 lines
4.4 KiB
JavaScript
39 lines
4.4 KiB
JavaScript
/* Shared attribute palette. Consumers own records, filters, commands, drag payloads and preferences. */
|
|
(() => {
|
|
'use strict';
|
|
const node=(tag,text)=>{const n=document.createElement(tag);if(text!==undefined)n.textContent=text;return n;};
|
|
const control=(label,run)=>{const b=node('button',label);b.type='button';b.addEventListener('click',run);return b;};
|
|
function mount(host,options){
|
|
host.classList.add('pd-attributes-panel');host.replaceChildren();const rows=[];
|
|
for(const group of options.groups){
|
|
const section=node('details');section.dataset.attributeGroup=group.id;section.open=options.isExpanded?.(group.id,true)??true;
|
|
section.append(node('summary',group.label));section.addEventListener('toggle',()=>options.onExpansion?.(group.id,section.open));
|
|
if(group.editable===false)section.append(node('p','Filter only'));
|
|
const values=group.values,byParent=new Map(),byKey=new Map(values.map(v=>[v.key??v.value,v]));
|
|
for(const v of values){const parent=v.parentKey&&byKey.has(v.parentKey)?v.parentKey:null;if(!byParent.has(parent))byParent.set(parent,[]);byParent.get(parent).push(v);}
|
|
const visited=new Set();
|
|
function append(v,parent,depth){
|
|
const key=v.key??v.value;if(visited.has(key))return;visited.add(key);
|
|
const branch=node('div');branch.className='pd-attribute-branch';const row=node('div');row.className='pd-attribute-row '+(options.rowClass||'');row.dataset.attribute=group.id;row.dataset.value=v.value;row.style.setProperty('--attribute-depth',depth);row.dataset.assignable=String(group.editable!==false&&v.assignable!==false);
|
|
const descendants=byParent.get(key)||[],expansionKey=`${group.id}:${key}`;let opened=options.isExpanded?.(expansionKey,false)??false;
|
|
const expand=control('',()=>{opened=!opened;sync();options.onExpansion?.(expansionKey,opened);});expand.className='pd-attribute-expand';expand.hidden=!descendants.length;
|
|
const filter=control('',()=>options.onFilter(group,v));filter.className='pd-attribute-filter';filter.dataset.attributeFilter='';filter.title=v.filterHint||`${group.label}: ${v.label}${descendants.length?' (includes descendants)':''}`;
|
|
const image=options.icon(v);image.classList.add('pd-attribute-icon');const label=node('span',v.label);label.className='pd-attribute-label';const count=node('span','0');count.dataset.count='';count.className='pd-attribute-count';filter.append(image,label,count);row.append(filter,descendants.length?expand:Object.assign(node('span'),{className:'pd-attribute-expand'}));
|
|
if(group.editable!==false&&v.assignable!==false){const assign=control('Assign',()=>options.onAssign(group,v));assign.className='pd-attribute-assign';assign.dataset.assign='';assign.setAttribute('aria-label',`Assign ${group.label}: ${v.label} to selected ${options.recordLabel||'records'}`);row.append(assign);}
|
|
row.draggable=group.editable!==false&&v.assignable!==false&&options.canEdit();
|
|
let dragBlocked=false;row.addEventListener('pointerdown',e=>{dragBlocked=!!e.target.closest('[data-assign],button.pd-attribute-expand');});
|
|
row.addEventListener('dragstart',e=>{if(dragBlocked||e.target.closest('[data-assign],.pd-attribute-expand')||!row.draggable){e.preventDefault();return;}options.onDragStart?.(e,group,v);});
|
|
row.addEventListener('click',e=>{if(!e.target.closest('button'))filter.click();});
|
|
const children=node('div');children.className='pd-attribute-children';
|
|
function sync(){children.hidden=!opened;expand.textContent=opened?'▾':'▸';expand.setAttribute('aria-label',`${opened?'Collapse':'Expand'} ${v.label}`);expand.setAttribute('aria-expanded',String(opened));}
|
|
sync();branch.append(row,children);parent.append(branch);rows.push({row,filter,count,group,value:v});for(const child of descendants)append(child,children,depth+1);
|
|
}
|
|
for(const v of byParent.get(null)||[])append(v,section,0);for(const v of values)if(!visited.has(v.key??v.value))append(v,section,0);
|
|
host.append(section);
|
|
}
|
|
function update({counts=true}={}){for(const entry of rows){const {row,filter,count,group,value}=entry;const active=!!options.isActive(group,value);filter.setAttribute('aria-pressed',String(active));row.classList.toggle('is-filter-active',active);if(counts)count.textContent=String(options.count(group,value));const assign=row.querySelector('[data-assign]');if(assign)assign.disabled=!options.canEdit()||options.busy()||!options.hasSelection();row.draggable=!!assign&&options.canEdit()&&!options.busy();}}
|
|
update();return{update};
|
|
}
|
|
window.PlotDirectorAttributesPanel=Object.freeze({mount});
|
|
})();
|