Snippets

Piotr Szrajber Smart M.App - add asynchronously loaded item to the tooltip

Created by Piotr Szrajber last modified
/*
 * Add a cell to the tooltip that has a "Loading..." value and launches an asynchronous process that replaces this "Loading..." with a real value
 * Usage: to use with reverse geocoding
 * Piotr Szrajber <piotr.szrajber@hexagongeospatial.com>
 */
var i = 0;
gsp.ready("v1.0", function(gsp) {
    gsp.bi.stage.findWidgets({
        descriptors: [{
            chartM: {
                chart: "choropleth"
            }
        }]
    }, function(widgets) {
        if (!widgets || !widgets[0]) {
            console.error("No choropleth found.");
        }
        var geochart = widgets[0].chart;
        var orig = geochart.tooltipProvider().update;
        geochart.tooltipProvider().update = function() {
            var args = Array.prototype.slice.call(arguments);
            orig.apply(this, arguments);
            var ttipContent = this.getTooltipContent(args[1], args[2]);
            // add a new table row with a cell that is using feature ID
            var newContent = this.getTooltipContent(args[1], args[2]).replace(/<\/table>/, "<tr><td>NEW PARAMETER:</td><td id=\"Loading_tooltip_" + args[1].id + "\">Loading...</td></tr></table>");
            args[0].setContent(newContent);

            setTimeout(function() {
                var cell = document.querySelector("#Loading_tooltip_" + args[1].id);
                // if there is no such a cell, it means that the tooltip has been closed before so we can actuall cancel the asynchronous operation
                if (!cell) {
                    console.log("No cell for id=" + args[1].id);
                    return;
                }

                // TODO: launch Reverse geocoding and output it to that cell (with the ID) when it's done
                // TODO: cancel the request when the tooltip is abandoned
                cell.innerHTML = "Hello world " + i++ + "!";
            }, 3000);
        }
    });
});

Comments (0)

HTTPS SSH

You can clone a snippet to your computer for local editing. Learn more.