element.onclick = function( evt)
{
evt = evt || event;
if (! evt.target)
evt.target = evt.srcElement;
alert( "clicking " + evt.target
}
The problems grew:
Events.add( menu, "click",
function( event)
{
Panel.menuHover = false;
menu.style.display = "none";
Panel.hideShield( menu.panel);
return true;
});
This can still cause a tedious amount of coding when a lot of events need to be set for a single element. I use an addEvents() method for these cases. It still sets events on a single element, but now it picks up these events from the methods of an object called events. This makes for some clear self-documenting code.
Events.addEvents( tabBarTextNode, Panel.tabActions);
...
Panel.tabActions = {events : {}};
Panel.tabActions.events.click =
function( evt)
{
var panel
= Elements.getPanel( evt.target).panel;
Panel.setFocus( panel);
}
Panel.tabActions.events.contextmenu =
function( evt)
{
var panel = Elements.getPanel(
evt.target).panel;
var menu = Panel._contextMenu(
panel, Mouse.location( evt));
menu.style.top =
Panel.frame.height - menu.offsetHeight;
return false;
}
Less common - but just as important - are the situations where the same event and action need to be set on many elements. This following method has to use the parameters in a different order with the elements at the end, so that we can add as much as is needed.
Events.addSameEvent( "contextmenu",
function() {alert('not allowed');},
panel.titleBar, panel.resizeBox,
panel.shadowRight, panel.shadowBottom);
This last method is going to take some describing - as is always the case when objects are used. It's used if you need to add an event to multiple elements that can be used to call multiple actions. None of the methods above will do the job. We can add multiple events to a single element, but we can't add a new event and have it be available to more than one element. The object newList resolves this deficiency: it creates a method that's also an object, so it can have state as well as function. The state is used to point to a list of events, rather than attach them to the element as we do above.
// Called once to create event function objects.
Mouse.events = {};
Mouse.events.mousemove
= Events.newList( "mousemove");
Mouse.events.mouseup
= Events.newList( "mouseup");
Mouse.events.blur
= Events.newList( "blur");
...
// Called for each object we want
// to trigger the event list for.
// Note that the events added are
// function object, not just functions.
Mouse.initialise = function( w)
{ Events.addEvents( w, Mouse); }
...
// Elsewhere we add new events that will be
// called for each registered element
Mouse.events.mousemove.add( Panel.onmousemove);
Mouse.events.mouseup.add( Panel.onmouseup);
The mouse object here is the only occasion so far when I've used this more complex structure. Every window registers itself for Mouse events by calling Mouse.initialise(). Other modules that need to do something special on these actions register themselves with the action function objects using add(). To put it simply, Panel.onmousemove() will be called for each registered window.
Now, I suppose you want the library code. Well you can't have it yet. This article is already too long. I'll release it next week, so nyah.
--
Posted by Paul Marrington to Adept Software Development at 9/13/2005 04:53:00 PM