Adept Software Development

Adept: (A)pplication (D)evelopment (E)nterprise to (P)ersonal (T)ransition. It is a system I am developing to leverage Enterprise developer skills to produce stand-alone software for other market segments. This is a general software development blog discussing issues about project, architecture, design and development. The emphasis will be in Java, but many of the issues will be more general. Almost all will be technical.

http://marringtons.com

Tuesday, September 20, 2005

JavaScript Events - Part 4 - Event Library Source

As promised, here it is. Refer to the earlier articles if you want to know the hows and whys.
/*
 * Created on 5/11/4
 *
 * Copyright 2004 Paul Marrington
 * All rights reserved - http://marringtons.com
 * PROPRIETARY/CONFIDENTIAL
 * Use is subject to license terms - paul@marrington.net
 */

/**
 * Add all the events in an event object
 * inside the group given.
 */
Events.addEvents = function( element, group)
  {
    for (var name in group.events)
      Events.add(
        element, name, group.events[name]);
  }

Events.addSameEvent = function(
  name, action, elements)
  {
    for (var i = 2;  i < arguments.length;  i++)
      Events.add( arguments[i], name, action);
  }

Events.add = function( element, name, action)
  {
    name = name.toLowerCase();
    var on = "on" + name;

    if (! element.events)
      element.events = new Object();
    
    var list = element.events[name];
    if (! list)
      {
        list = element.events[name] = new Array();
        if (element[on]) list.push( element[on]);
        /*
         * This is an interesting bit of obscure code.
         * If there were any other way, I would not do
         * it, but IE makes it essential. The event handler
         * is inline so that it keeps a handle to the surrounding
         * function/object. This way we can set event.owner
         * to the same as the calling element when add
         * function was called - even though the event
         * handler is called asynchronously later,
         * long after the add function is dead and buried.
         * Nasty. It also means we are hanging on to an
         * unknown amount of stuff. The W3C DOM uses
         * evt.currentTarget, but this is the ONLY way
         * we can get IE to know the owner. This is
         * because evt.target is the actual element
         * clicked on or whatever, while the owner 
         * of the event can be further up the DOM tree.
         */
        element[on] = function( evt)
          {
            /*
             * Normalise the event object between
             & browsers and retrieve the list
             * of actions to take.
             */
            evt = evt || event;
            if (! evt.target)
              evt.target = evt.srcElement;
            /*
             * We get element from the owner object.
             */
            evt.owner = element;

            var list = this.events[evt.type];
            /*
             * Run each event in the list until
             * one returns false. This breaks the chain.
             * Events are run from the most
             * recently added to the oldest.
             */
            for (var i = list.length - 1;  i >= 0;  i--)
              {
                evt.action = list[i];
                if (! evt.action( evt))
                  return false;
              }
            return true;
          }
      }
    
    list.push( action);
  }

Events.newList = function( name)
  {
    var newList = function( evt)
      {
        for (var j = evt.action.events.length - 1; 
          j >= 0;  j--)
            if (! evt.action.events[j]( evt))
              return false;
        return true;
      }
    newList.eventName = name;
    newList.events = new Array();
    newList.add = function( event)
      {
        this.events.push( event);
      }
    return newList;
  }

 

7 Comments:

Blogger incription said...

Its pleasure to visit your blog and i am really in love with it.We have something regarding hosting too.We have our own iTalks Community.

iTalks Community hopes to become a great place for Web Designers, Web Masters, Graphics designers and Freelancers Hosting companies etc. to meet, discuss, and learn new techniques! We are planning to have a large database of tutorials to cover all the topics. We hope to encourage a free and open marketplace for trading upon the internet of designs, web hosting, and general internet solutions.

http://www.italks.net

We hope that you will enjoy your stay here and make this forum an active and happening place.

Regards,
vijay srivastava.

Friday, October 07, 2005 5:00:00 pm  
Blogger Nicole said...

wow. my dad's name is paul marrington. ha.
-a visitor.

Wednesday, May 31, 2006 12:26:00 pm  
Anonymous Anonymous said...

I've yet to actually use this code, but if it even does half of what you describe -- thank you. This is an issue I've been wrestling with for a while but have yet to come up with an elegant solution.

Again, thank you. Keep up the good work.

Saturday, December 16, 2006 11:40:00 am  
Anonymous Anonymous said...

hi,
its very nice blog.
& good place to get info about web development, webdesign, java scripts.

Thursday, July 05, 2007 9:33:00 pm  
Anonymous Anonymous said...

Great Tips on Software Development:

Software Development

Tuesday, August 21, 2007 2:51:00 pm  
Anonymous Anonymous said...

Good blog related java script and software development.

Tuesday, October 02, 2007 2:26:00 pm  
Anonymous Anonymous said...

nice post, thanks for sharing!

Monday, February 23, 2009 9:09:00 pm  

Post a Comment

<< Home