wiki:CodeSamples/JavaScript/InvokeCommandOnStartup

This page is one of the MapGuide Community CodeSamples. Visit the CodeSamples page to view more!

Overview

This html page can automatically invoke a named viewer command on viewer startup based on the value of a Cmd query string property. This uses the InitialTask property of the Web Layout.

For example setting this initial task pane url:

http://path/to/autostart.html?Cmd=Measure

Will launch the Measure command on viewer startup.

As you can see from the code below, we need to employ initialization checks just like in the event hooking example, to make sure we can start using the Viewer API when the frames have been fully initialized.

autostart.html

<html>
        <head>
                <title>Auto-start command in task pane</title>
                <script type="text/javascript">
                        
                        var viewerFrame = parent.parent;
                        
                        function GetQueryStringValue(key)
                        {
                                key = key.replace(/[\[]/,"\\\[").replace(/[\]]/,"\\\]");
                                var regex = new RegExp("[\\?&]"+key+"=([^&#]*)");
                                var qs = regex.exec(window.location.href);
                                if(qs == null)
                                        return "";
                                else
                                        return qs[1];
                        }
                        
                        function GetCommandIndex(name)
                        {
                                var cmds = viewerFrame.GetCommands();
                                for(var i = 0; i < cmds.length; i++)
                                {
                                        //This command array is not sequential, there may be holes
                                        //so we have to test element existence before testing name equality
                                        if (cmds[i] && cmds[i].name == name)
                                                return i;
                                }
                                return -1;
                        }
                        
                        function OnMapInitialized()
                        {
                                var cmdName = GetQueryStringValue("Cmd");
                                var i = GetCommandIndex(cmdName);
                                if (i < 0)
                                        alert("Command not found: " + cmdName);
                                else
                                        viewerFrame.ExecuteCommand(i);
                        }
                        
                        window.onload = function() {
                                //Timer variable
                                var timer; 

                                // This is our "watch" function. What we are doing here is 
                                // repeatedly checking the mapInit value of the Map Frame. When
                                // mapInit is true, then the map has been loaded.
                                var watch = function() {
                                        
                                        // What we are doing here is attempting to access the mapInit
                                        // variable of the Map frame. The reason the code is in a try-catch
                                        // is because chances are that this code access the DOM
                                        // of the map frame before its DOM has been initialized.
                                        try {
                                                var mapFrame = viewerFrame.mapFrame;
                                                if(mapFrame.mapInit) {
                         
                                                        // Remove the timer so this watch function stops executing
                                                        clearInterval(timer);
                                                         
                                                        // Call our custom handler
                                                        OnMapInitialized();
                                                }
                                        }
                                        catch(e) {
                                        }
                                };
                                
                                // Start the "watching" process. We basically invoke the watch function 
                                // every 200ms until the map has been loaded.
                                timer = setInterval(watch, 200);

                        };
                        
                </script>
        </head>
        <body>
        </body>
</html>
Last modified 5 years ago Last modified on Feb 6, 2013 1:08:10 PM