| 1 | [[PageOutline]] |
| 2 | |
| 3 | This page is one of the !MapGuide Community CodeSamples. Visit the CodeSamples page to view more! |
| 4 | |
| 5 | == Overview == |
| 6 | |
| 7 | This html page can automatically invoke a name viewer command on viewer startup based on the value of a {{{Cmd}}} query string property. This uses the {{{InitialTask}}} property of the Web Layout. |
| 8 | |
| 9 | For example setting this initial task pane url: |
| 10 | {{{ |
| 11 | http://path/to/autostart.html?Cmd=Measure |
| 12 | }}} |
| 13 | |
| 14 | Will launch the Measure command on viewer startup |
| 15 | |
| 16 | autostart.html |
| 17 | {{{ |
| 18 | <html> |
| 19 | <head> |
| 20 | <title>Auto-start command in task pane</title> |
| 21 | <script type="text/javascript"> |
| 22 | |
| 23 | var viewerFrame = parent.parent; |
| 24 | |
| 25 | function GetQueryStringValue(key) |
| 26 | { |
| 27 | key = key.replace(/[\[]/,"\\\[").replace(/[\]]/,"\\\]"); |
| 28 | var regex = new RegExp("[\\?&]"+key+"=([^&#]*)"); |
| 29 | var qs = regex.exec(window.location.href); |
| 30 | if(qs == null) |
| 31 | return ""; |
| 32 | else |
| 33 | return qs[1]; |
| 34 | } |
| 35 | |
| 36 | function GetCommandIndex(name) |
| 37 | { |
| 38 | var cmds = viewerFrame.GetCommands(); |
| 39 | for(var i = 0; i < cmds.length; i++) |
| 40 | { |
| 41 | //This command array is not sequential, there may be holes |
| 42 | //so we have to test element existence before testing name equality |
| 43 | if (cmds[i] && cmds[i].name == name) |
| 44 | return i; |
| 45 | } |
| 46 | return -1; |
| 47 | } |
| 48 | |
| 49 | function OnMapInitialized() |
| 50 | { |
| 51 | var cmdName = GetQueryStringValue("Cmd"); |
| 52 | var i = GetCommandIndex(cmdName); |
| 53 | if (i < 0) |
| 54 | alert("Command not found: " + cmdName); |
| 55 | else |
| 56 | viewerFrame.ExecuteCommand(i); |
| 57 | } |
| 58 | |
| 59 | window.onload = function() { |
| 60 | //Timer variable |
| 61 | var timer; |
| 62 | |
| 63 | // This is our "watch" function. What we are doing here is |
| 64 | // repeatedly checking the mapInit value of the Map Frame. When |
| 65 | // mapInit is true, then the map has been loaded. |
| 66 | var watch = function() { |
| 67 | |
| 68 | // What we are doing here is attempting to access the mapInit |
| 69 | // variable of the Map frame. The reason the code is in a try-catch |
| 70 | // is because chances are that this code access the DOM |
| 71 | // of the map frame before its DOM has been initialized. |
| 72 | try { |
| 73 | var mapFrame = viewerFrame.mapFrame; |
| 74 | if(mapFrame.mapInit) { |
| 75 | |
| 76 | // Remove the timer so this watch function stops executing |
| 77 | clearInterval(timer); |
| 78 | |
| 79 | // Call our custom handler |
| 80 | OnMapInitialized(); |
| 81 | } |
| 82 | } |
| 83 | catch(e) { |
| 84 | } |
| 85 | }; |
| 86 | |
| 87 | // Start the "watching" process. We basically invoke the watch function |
| 88 | // every 200ms until the map has been loaded. |
| 89 | timer = setInterval(watch, 200); |
| 90 | |
| 91 | }; |
| 92 | |
| 93 | </script> |
| 94 | </head> |
| 95 | <body> |
| 96 | </body> |
| 97 | </html> |
| 98 | }}} |