@expofp/floorplan
Version:
Interactive floor plan library for expos and events
2 lines (1 loc) • 1.93 kB
JavaScript
import c from"../tools/track-event";export class ShortcutRegistry{constructor(){this.providers=new Map,this.activationOrder=new Map}register(t){if(!t?.id)throw new Error("Provider must have a valid id");this.providers.set(t.id,t)}unregister(t){this.providers.delete(t),this.activationOrder.delete(t)}getShortcuts(t){const e=this.providers.get(t);if(!e)return[];const r=e.createShortcuts(),i=this.activationOrder.get(t);return i?this.sortShortcuts(r,i):r}sortShortcuts(t,e){const r=new Map(e.map((i,s)=>[i,s]));return t.sort((i,s)=>{if(i.active!==s.active)return i.active?-1:1;if(i.active&&s.active){const o=r.get(i.id),n=r.get(s.id);return o!==void 0&&n!==void 0?o-n:(o!==void 0?-1:0)-(n!==void 0?-1:0)}return 0})}handleShortcutClick(t,e){const r=this.providers.get(t);if(!r)return;const i=r.createShortcuts().find(n=>n.id===e),s=i?.active??!1;r.handleShortcutClick(e);const o=this.isShortcutActive(r,e);this.reportClick(i,s,o),s!==o&&this.updateActivationOrder(t,e,s,o)}reportClick(t,e,r){if(!t||!(t.action==="custom"||!e&&r))return;const s=t.label||t.id;s&&c("sbtnview",s,"shortcut")}isShortcutActive(t,e){return t.createShortcuts().find(i=>i.id===e)?.active??!1}handleShortcutReset(t,e){const r=this.providers.get(t);if(!r?.handleShortcutReset)return;r.handleShortcutReset(e),this.activationOrder.get(t)?.includes(e)&&this.updateActivationOrder(t,e,!0,!1)}updateActivationOrder(t,e,r,i){const s=this.activationOrder.get(t)||[];let o;if(!r&&i)o=s.includes(e)?s:[...s,e];else if(r&&!i)o=s.filter(n=>n!==e);else return;o.length>0?this.activationOrder.set(t,o):this.activationOrder.delete(t)}updateActivationOrderFromState(t,e,r,i){this.updateActivationOrder(t,e,r,i)}shouldShowShortcuts(t){return this.providers.get(t)?.shouldShowShortcuts?.()??!0}getRegisteredProviders(){return Array.from(this.providers.keys())}isProviderRegistered(t){return this.providers.has(t)}clear(){this.providers.clear(),this.activationOrder.clear()}}