[mapguide-users] OnSelectionChanged in AJAX viewer?
Kenneth, GEOGRAF A/S
ks at geograf.dk
Mon Apr 28 08:36:44 EDT 2008
You must safeguard against timing problems.
It is very likely that your frame will load completely before the iframe
does.
In that case, you cannot get the OnSelectionChanged method reference.
You can add an "onload" to the iframe, but it is still possible that it
will break, because the internal frames do not load in the expected order.
A simple fix is to wrap your "InitDocument" with a timer.
Also, using the "frames[0].frames[1]" is relying on the order of your
page, and the order of the viewer frames.
A more reliable method, is to use the ID if the frame element, and then
use the built-in method GetMapFrame().
I have applied the above to your example:
<!-- Javascript part -->
<script type="text/javascript">
var mapFrame = null; // A reference to the map frame
var originalMapFrameOnSelectionChanged = null // storage for the
original onSelectionChanged function
function OnSelectionChanged() // when something is selected on the map
{
alert('Your handler works perfectly');
originalMapFrameOnSelectionChanged();
}
function InitDocument()
{
var win = document.getElementById('viewerFrame');
if (win.contentWindow) //FF returns the actual tag, IE returns
the iframe
win = win.contentWindow;
mapFrameRef = win.GetMapFrame();
originalMapFrameOnSelectionChanged = mapFrameRef.parent.OnSelectionChanged;
mapFrameRef.parent.OnSelectionChanged =
OnSelectionChanged;
}
function RetryInit()
{
if (mapFrame != null)
return;
try
{
InitDocument();
}
catch (e)
{
mapFrame = null;
}
if (mapFrame == null)
window.setTimeout("RetryInit();", 500);
}
</script>
<!-- HTML part -->
<html>
<body>
<iframe id="viewerFrame" frameborder="yes" width="100%" height="60%"
src="
src="../mapviewernet/dwfviewer.aspx?SESSION=<%=sessionId%>&WEBLAYOUT=<%=webLayout%>""
onload="RetryInit()"></iframe>
</body>
</html>
Regards, Kenneth, GEOGRAF A/S
Kimmo Kotajärvi skrev:
>
> Here's my solution (simplified):
>
>
>
> <!-- Javascript part -->
>
>
>
> <script type="text/javascript">
>
> var mapFrame = null; // A reference to the map frame
>
> var originalMapFrameOnSelectionChanged = null // storage for the
> original onSelectionChanged function
>
>
>
> function OnSelectionChanged() // when something is selected on the map
>
> {
>
> alert('Your handler works perfectly');
>
> originalMapFrameOnSelectionChanged();
>
> }
>
>
>
> function InitDocument()
>
> {
>
> mapFrameRef = frames[0].frames[1];
>
> originalMapFrameOnSelectionChanged =
> mapFrameRef.parent.OnSelectionChanged;
>
> mapFrameRef.parent.OnSelectionChanged =
> OnSelectionChanged;
>
> }
>
> </script>
>
>
>
> <!-- HTML part -->
>
>
>
> <html>
>
> <body onload="javascript:InitDocument()">
>
> <iframe id="viewerFrame" frameborder="yes" width="100%"
> height="60%" src="
> src="../mapviewernet/dwfviewer.aspx?SESSION=<%=sessionId%>&WEBLAYOUT=<%=webLayout%>""></iframe>
>
> </body>
>
> </html>
>
>
>
>
>
> So, body onload calls InitDocument which registers the new
> OnSelectionChanged event handler. The new handler only pops up a
> dialog box and then calls the old function. The MG Viewer is embedded
> in an iframe.
>
>
>
>
>
> ------------------------------------------------------------------------
>
> *Lähettäjä:* mapguide-users-bounces at lists.osgeo.org
> [mailto:mapguide-users-bounces at lists.osgeo.org] *Puolesta *Tanja Suikkanen
> *Lähetetty:* 28. huhtikuuta 2008 14:10
> *Vastaanottaja:* mapguide-users at lists.osgeo.org
> *Aihe:* Re: [mapguide-users] OnSelectionChanged in AJAX viewer?
>
>
>
> My map is embedded in my own page.
>
> When a user selects one or more features on the map, I want my own
> function to be executed.
>
> But doesn't parent.parent.mapFrame still get me the map frame..?
>
> And this works but only when I click some button.
>
>
>
> Tanja
>
>
>
>
>
> *From:* Kenneth, GEOGRAF A/S [mailto:ks at geograf.dk]
> *Sent:* 28. huhtikuuta 2008 13:57
> *To:* tanja.suikkanen at dimenteq.fi; MapGuide Users Mail List
> *Subject:* SPAM-LOW: Re: [mapguide-users] OnSelectionChanged in AJAX
> viewer?
>
>
>
> Are you issuing the script from a command?
>
> If you want to do this all the time (regardless of what the user has
> done), you must load the script with the solution.
> If your map is embedded in another page, you can load the script
> there, and change "var map = parent.parent.mapFrame;" to point to the
> correct frame.
> If it is just the basic layout, you can try to put it in the page that
> loads in the taskpane (but beware that the document gets unloaded if
> the user navigates).
> As a last resort, you can embed the script in the viewerfiles.
>
> In case you can use the embedded map approach, you should place the
> following line after the "try {":
> map = document.getElementById('<insert iframe/frame id>').GetMapFrame()
>
> Regards, Kenneth, GEOGRAF A/S
>
>
>
> Tanja Suikkanen skrev:
>
> Ok, I sent my code snippet earlier today, but here it is again.
>
> I got this example from
> http://trac.osgeo.org/mapguide/wiki/CodeSamples/JavaScript/AJAXViewerEventHooking.
>
>
>
> var map = parent.parent.mapFrame;
>
>
>
> var origOnSelectionChanged = null;
>
>
>
> function MySelectionHandler()
>
> {
>
> origOnSelectionChanged();
>
>
>
> var selCount = map.GetSelectedCount();
>
> if(selCount > 0)
>
> {
>
> My function() {}
>
> }
>
> }
>
>
>
> window.onload = function() {
>
>
>
> var timer;
>
>
>
> var watch = function() {
>
>
>
> try {
>
> if(map.mapInit) {
>
>
>
> clearInterval(timer);
>
>
>
> origOnSelectionChanged = map.OnSelectionChanged;
>
>
>
> map.OnSelectionChanged = MySelectionHandler;
>
> }
>
> }
>
> catch(e) {
>
> }
>
> };
>
>
>
> timer = setInterval(watch, 200);
>
> };
>
>
>
> Window.onload function is executed only, if I press a button.
>
> I want to have the functionality I was telling you about without any
> button clicks.
>
> Please ask if I don't tell you enough.
>
>
>
> Tanja
>
>
>
>
>
> *From:* Ivan Milicevic [mailto:Ivan.Milicevic at supranet.hr]
> *Sent:* 28. huhtikuuta 2008 12:04
> *To:* tanja.suikkanen at dimenteq.fi
> <mailto:tanja.suikkanen at dimenteq.fi>; MapGuide Users Mail List
> *Subject:* RE: [mapguide-users] OnSelectionChanged in AJAX viewer?
>
>
>
> Feature doesn't get highlighted?
>
>
>
> Of course that you can add this functionality in your own code, the
> logic is same. Allwas call mapFrame functions (via JavaScript) or
> count selection from C# or PHP.
>
>
>
> If you want to get some help over here, be more specific.
>
>
>
> Ivan.
>
>
>
> *From:* mapguide-users-bounces at lists.osgeo.org
> <mailto:mapguide-users-bounces at lists.osgeo.org>
> [mailto:mapguide-users-bounces at lists.osgeo.org] *On Behalf Of *Tanja
> Suikkanen
> *Sent:* Monday, April 28, 2008 10:46 AM
> *To:* mapguide-users at lists.osgeo.org
> <mailto:mapguide-users at lists.osgeo.org>
> *Subject:* RE: [mapguide-users] OnSelectionChanged in AJAX viewer?
>
>
>
> Ok, now it worked but some functionality had been removed?
>
> When I first select a feature, everything goes fine but when I select
> another feature, that doesn't get highlighted...
>
> Also I wouldn't like to change the original files, just add my own
> functionality in my own code files.
>
> But thanks anyway!
>
>
>
> Tanja
>
> ------------------------------------------------------------------------
>
> _______________________________________________
> mapguide-users mailing list
> mapguide-users at lists.osgeo.org
> http://lists.osgeo.org/mailman/listinfo/mapguide-users
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://lists.osgeo.org/pipermail/mapguide-users/attachments/20080428/c5d8348c/attachment.html
More information about the mapguide-users
mailing list