Skip to content

3 - Setting availability on demand

This example shows how to set the availability of the seats on demand. The seat availability of a block is not set until its seats are seen for the first time.

Map Viewer

Code

// ---- LOADING MODULE ----
DVM.loadModule("map_viewer", {
    container: "viewer-container", // Container where the viewer will be appended
    // Callbacks subscribed to events emited by the viewer
    callbacks: {
        // "first_seen" will be called the first time a section is painted on the map at level 1 (close enough to show seats)
        first_seen: onFirstSeen
    }
})
    .then(function(viewer) {
        console.log(`Module '${viewer.getModuleName()}' initialized:`, viewer);
        start(viewer);

    })
    .catch(function(err) {
        console.error(err);
    });


function start(viewer) {
    // ---- LOADING MAP ----
    viewer.loadMap({
        venue_id: "eu-es-00008-default" // Venue to be loaded. 3ddigitalvenue will provide these IDs
    })
        .then(function (obj) {
            // Successfully loaded

            // Get sections availability
            var available_sections = getSectionAvailability();
            // Apply sections availability
            viewer.setAvailability("section", available_sections);

            console.log("LOADED!");
        })
        .catch(function (err) {
            // Error while loading
            console.error(err);
        });


    // ---- AVAILABILITY FUNCTIONS ----
    // Get sections availability. For the purpose, we generate a RANDOM availability.
    function getSectionAvailability() {
        var sections = viewer.getNodesByType("section");
        var available_sections = [];

        for (var i = 0; i < sections.length; ++i) {
            var section = sections[i];
            if (Math.random() < 0.7) {
                available_sections.push(section.id);
            }
        }

        return available_sections;
    }

}

// ---- VIEWER CALLBACKS ----
// Called when the viewer emits "first_seen"
function onFirstSeen(obj) {
    var viewer = obj.instance;
    // Get the node or nodes affected
    var nodes = obj.nodes;

    for (var i = 0; i < nodes.length; ++i) {
        var node = nodes[i];

        console.log("first seen:", node.id);

        // Check if the node is available and is a section
        if (node && node.state === "available" && node.type === "section") {
            // Get all seats with this section as a parent
            var seats = viewer.getNodesByParent(node);
            var available = [];
            // SET RANDOM SEAT AVAILABILITY
            for (var j = 0; j < seats.length; ++j) {
                var seat = seats[j];
                if (Math.random() < 0.6) {
                    available.push(seat.id);
                }
            }

            delayAvailability(available, node.id);
        }
    }

    // Add a random timeout to see the effect on the map. This also simulates getting availability asynchronously on demand.
    function delayAvailability(availability, node_id) {
        setTimeout(function () {
            viewer.setAvailability("seat", availability, node_id);
        }, rand(250, 1000));
    }
}

// ---- HELPERS ----
function rand(min, max) {
    return Math.floor(Math.random() * (max - min + 1) + min);
}