Web Images Videos Maps News Shopping Gmail more »
Recently Visited Groups | Help | Sign in
Google Groups Home
JavaScript Events - Part 3 - An Event Library
There are currently too many topics in this group that display first. To make this topic appear first, remove this option from another topic.
There was an error processing your request. Please try again.
flag
  1 message - Collapse all  -  Translate all to Translated (View all originals)
The group you are posting to is a Usenet group. Messages posted to this group will make your email address visible to anyone on the Internet.
Your reply message has not been sent.
Your post will appear after it is approved by moderators
 
From:
To:
Cc:
Followup To:
Add Cc | Add Followup-to | Edit Subject
Subject:
Validation:
For verification purposes please type the characters you see in the picture below or the numbers you hear by clicking the accessibility icon. Listen and type the numbers you hear
 
Paul Marrington  
View profile  
 More options Sep 13 2005, 2:54 am
From: Paul Marrington <pa...@askowl.com.au>
Date: Mon, 12 Sep 2005 23:54:18 -0700 (PDT)
Local: Tues, Sep 13 2005 2:54 am
Subject: [Adept Software Development] JavaScript Events - Part 3 - An Event Library
My first attempt at an event library allowed a single event to be attached to multiple elements, as well as multiple event methods to a single event on a single element. This is very useful when you want to track the mouse since you need to register the same even for multiple windows or frames. However, it did require the creation of a special event object and then to have that object attached. Consequently - except for the special mouse tracking case - most of my events were set in the time honoured way:
element.onclick = function( evt)
  {
    evt = evt || event;
    if (! evt.target)
      evt.target = evt.srcElement;
    alert( "clicking " + evt.target
  }

 

The problems grew:
  1. Every event function had to have the first two lines to ensure basic data conformity between browsers.
  2. If more than one piece of independent code wanted to set the same event, they had to know about each other or the event call was lost.
  3. The target field pointed to the element that triggered the event, not necessarily the element that owned the event. The latter isn't available from the event object for IE.
I wanted to create a library that:
  1. Could be called to set for an element event that already had others functions attached - either by HTML or due to earlier calls. Events needed to be called from most recent to oldest set.
  2. Would provide an event object already normalised for browser differences.
  3. Would provide an owner field that worked across all browsers.
  4. Could ease coding by setting multiple events for a single element, and multiple elements with the same event.

Usage

The core method is called, surprisingly enough, Events.add(). It requires a reference to the element, the event name and a function to run when the event is triggered.
  • The event name, as with the DOM equivalent, does not start with "on". In other words, use "click", not "onclick".
  • It will call multiple action methods for a single event.
  • If the HTML had an event set, this event will be added as well.
  • The action function has a normalised event object as its one parameter.
  • It includes valid target and owner fields.
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


    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
End of messages
« Back to Discussions « Newer topic     Older topic »

Google Groups - Google Home - Terms of Service - Privacy Policy
©2009 Google