This example validates a selection using checkIsolatedNodes, checking that the
current selection doesn't leave any seat isolated.
On real time, it marks isolated seats with orange color as a warning.
Once the validation button is pressed, if there is none isolated seat, a last check is made, checking that the
selected seats are not separated between them, using the neighbors plugin
method validateNeighbors.
If the current selection is still invalid, the checkout shouldn't be allowed.
import{loadModule}from"@3ddv/dvm";loadModule("map_viewer",{container:"viewer-container",styles_by_groups:true,instanced_nodes:true,plugins:["neighbors"],}).then((viewer)=>{start(viewer);}).catch((err)=>{console.error(err);});functionstart(viewer){constbtn_validate=document.getElementById("btn-validate");btn_validate.addEventListener("click",()=>voidvalidate())// Just for the example purposeviewer.flags.scroll_with_mod_key=true;constvenue_id="eu-es-00059-valenciabasket";constmap_id="S_320";viewer.subscribe("load_success",(_obj)=>{customizeStyles();setRandomAvailability();});viewer.subscribe("select",()=>checkWarnings());viewer.subscribe("unselect",()=>checkWarnings());// Load the mapviewer.loadMap({venue_id,map_id}).then(()=>{// Successfully loadedconsole.log("LOADED!");}).catch((err)=>{// Error while loadingconsole.error(err);});functioncustomizeStyles(){conststyles=viewer.getStyles();styles[0].seat.available.normal.warning={fillStyle:"orange"};viewer.setStyles(styles);}functioncheckWarnings(){resetWarnings();constnodes=viewer.checkIsolatedNodes();viewer.addNodesToGroup(nodes,"warning");}functionresetWarnings(){viewer.removeNodesFromGroup(viewer.getNodesByGroups("seat","warning"),"warning");}asyncfunctionvalidate(){if(viewer.isLoaded()){constselection=viewer.getNodesByState("seat","selected");if(selection.length){constnodes=viewer.checkIsolatedNodes();if(nodes.length){alert(`Invalid selection! The following seats are isolated: ${nodes.map((node)=>node.id).join(", ")}`);}else{if(awaitviewer.neighbors.validateNeighbors(selection)){alert("Valid selection!");returntrue;}else{alert("No seats isolated, but the current selection is not together!")}}}else{alert("No seats selected!");}}returnfalse;}// Just for the purpose of having some random availability on the mapfunctionsetRandomAvailability(prob=0.6){if(viewer&&viewer.isLoaded()){viewer.getTypesList().forEach((type)=>{constavailability=viewer.getNodesByType(type).filter(()=>Math.random()<prob);viewer.setAvailability(type,availability);});}}}