I generalized it a bit more:
function IndexOf(arr, szMatch)
{
for (var i in arr)
{
if (arr[i] == szMatch)
{
return i;
}
}
return null;
}
var arrEventScripts = new Array();
function AddEventScript(EventObject, szEventType, szName,
szScriptBody)
{
if (IndexOf(arrEventScripts, szName) == null);
{
//add this
arrEventScripts.push(szName);
//add the script to the "AddScript" block since it
doesn't yet exist;
document.all("AddScript").text += "\nfunction " +
szName + "(evt)\n{ " + szScriptBody + "}";
}
//add a listener for the script to the object;
eval("EventObject.addEventListener('" + szEventType + "'," +
szName + ", false );");
}
--- In [email protected], "Geoff Swenson"
<[EMAIL PROTECTED]> wrote:
>
> This is what I just discovered, after tearing my hair out for a
couple of
> days.
>
> This is an utter hack, so if there is and easier way, I am all ears.
>
> //add a stub script to your doc.(the dummy script is probably not
necessary):
>
> <script language="javascript" id="AddScript">function dummy() {
return
> null;}</script>
>
> //inside your label class, when you add the event:
>
> //create a unique function name:
> var szOnClickName = this.DrawingObjectID + "_Click";
>
> //create the function with the desired objects actually specified by
> //appending it to the stub script text. (InnerHTML does not work).
>
> document.all("AddScript").text += "\nfunction " + szOnClickName +
> "(evt)\n{ var svgDoc = OpenSVG('" + this.DrawingObjectID + "');\n
> svgOnClick(svgDoc, evt);}";
>
> //use eval to add the listener with the function you just created.
> eval("objBackgroundRect.addEventListener('click'," + szOnClickName
+ ",
> false );");
>
> function svgOnClick(theObj, theEvent)
> {
> alert(theObj + "," + theEvent);
> }
>
> function OpenSVG(szContainerID)
> {
> try
> {
> return document.all(szContainerID).getSVGDocument();
> }
> catch (e)
> {
> //a_lert(e);
> var answer = confirm("You probably don't have the SVG
plugin.\nDo you
> want to install it now?")
> if (answer)
> {
> try
> {
> window.location.href =
> "http://download.adobe.com/pub/adobe/magic/svgviewer/win/3.x/3.03/en
/SVGView.exe";
> }
> catch(e)
> {
> alert("Could not open up plugin
website");
> return false;
> }
> }
> return false;
> }
> }
>
> I just needed the parent embed object, but I think this would
> work with any object you need. But you do have to make sure that
you run
> this sort of thing ONLY once when the object is created.
>
> Geoff Swenson
> [EMAIL PROTECTED]
>
>
> > I have 2 objects: one for the map (zoom/pan, ...), one for
displaying
> > labels (more objects to come :)). The purpose of the labels
class is
> > displaying a label of a map element like icons for restaurants,
> > streetnames, rivers, ... . In the SVG map I use
> > onclick="ShowLabel(evt)". How do I get this event
handled by
> > the Label
> > class.
> >
> > This is the code for my labels (in a normal script file, not a
js class
> > yet):
> >
> > /**
> > * OnClick
> > */
> > function ShowLabel(evt) {
> > var STROKEWIDTH = 2;
> > var FONTSIZE = 15;
> > var SPACEX = 5;
> > var OPACITY = 1;
> > var RX = 3;
> > var RY = 3;
> > var LINELENGTH = 10;
> >
> > var group = svgdoc.createElement("g");
> > group.setAttribute("id", "label" +
labelCount);
> > labelCount++;
> > group.setAttribute("onclick","OnClose(evt)
");
> >
> > var textNode =
> > svgdoc.createTextNode(evt.target.getAttribute
("fme:NAME"));
> >
> > var text = svgdoc.createElement("text");
> >
> > text.appendChild(textNode);
> > text.setAttribute("x", evt.clientX -
> > text.getComputedTextLength() /
> > 2 - text.getComputedTextLength() * 0.25);
> > text.setAttribute("y", evt.clientY - FONTSIZE + 1);
> > text.setAttribute("fill","rgb(0,0,0)");
> > text.setAttribute("opacity",1);
> > text.setAttribute("font-size", FONTSIZE);
> > text.setAttribute("pointer-
events","none");
> >
> > var rect = svgdoc.createElement("rect");
> > rect.setAttribute("x", evt.clientX -
> > text.getComputedTextLength() /
> > 2 - SPACEX);
> > rect.setAttribute("y", evt.clientY - FONTSIZE *
1.2 -
> > LINELENGTH);
> > rect.setAttribute("width",
text.getComputedTextLength() +
> > SPACEX * 2);
> > rect.setAttribute("height", FONTSIZE +
FONTSIZE*0.2);
> > rect.setAttribute("opacity", OPACITY);
> > rect.setAttribute("fill","rgb(255,255,255)
");
> > rect.setAttribute("stroke","rgb(0,0,0)");
> > rect.setAttribute("stroke-width",STROKEWIDTH);
> > rect.setAttribute("rx",RX);
> > rect.setAttribute("ry",RY);
> >
> > var line = svgdoc.createElement("line");
> > line.setAttribute("x1", evt.clientX);
> > line.setAttribute("y1", evt.clientY);
> > line.setAttribute("x2", evt.clientX);
> > line.setAttribute("y2", evt.clientY - LINELENGTH);
> > line.setAttribute("stroke", "rgb(0,0,0)
");
> > line.setAttribute("stroke-width", STROKEWIDTH);
> >
> > var circle = svgdoc.createElement("circle");
> > circle.setAttribute("cx", evt.clientX);
> > circle.setAttribute("cy", evt.clientY);
> > circle.setAttribute("r", 3);
> > circle.setAttribute("stroke", "rgb(0,0,0)
");
> > circle.setAttribute("fill", "rgb(255,255,255)
");
> > circle.setAttribute("stroke-width", 2);
> >
> > group.appendChild(rect);
> > group.appendChild(line);
> > group.appendChild(text);
> > group.appendChild(circle);
> >
> > svgdoc.getElementById("Names").appendChild(group);
> > }
> >
> > /**
> > * OnClose
> > */
> > function OnClose(evt) {
> > try {
> >
> > svgdoc.getElementById("Names").removeChild
(svgdoc.getElementById(evt.currentTarget.getAttribute
("id")));
> > labelCount--;
> > } catch (exception) {
> > alert(exception);
> > }
> > }
> >
> >
> >
>
------------------------ Yahoo! Groups Sponsor --------------------~-->
Most low income households are not online. Help bridge the digital divide today!
http://us.click.yahoo.com/I258zB/QnQLAA/TtwFAA/1U_rlB/TM
--------------------------------------------------------------------~->
-----
To unsubscribe send a message to: [EMAIL PROTECTED]
-or-
visit http://groups.yahoo.com/group/svg-developers and click "edit my
membership"
----
Yahoo! Groups Links
<*> To visit your group on the web, go to:
http://groups.yahoo.com/group/svg-developers/
<*> To unsubscribe from this group, send an email to:
[EMAIL PROTECTED]
<*> Your use of Yahoo! Groups is subject to:
http://docs.yahoo.com/info/terms/