Opened 14 years ago

Closed 14 years ago

#654 closed enhancement (fixed)

reduce useless test in event handlers

Reported by: mokhet Owned by: gogo
Priority: normal Milestone:
Component: Xinha Core Version: trunk
Severity: normal Keywords: event
Cc:

Description

HTMLArea._addEvent = function(el, evname, func) {
  if (HTMLArea.is_ie) {
    el.attachEvent("on" + evname, func);
  } else {
    el.addEventListener(evname, func, true);
  }
  HTMLArea._eventFlushers.push([el, evname, func]);
};

In this piece of code, we can see the test (HTMLArea.is_ie) is run at every event added.

I think it would be better to test this once and create a different function for each handled case. This is not a lot of save you can think, but hey, why not avoid some tests when we can easily avoid them.

if (HTMLArea.is_ie) {
  HTMLArea._addEvent = function(el, evname, func) {
   el.attachEvent("on" + evname, func);
   HTMLArea._eventFlushers.push([el, evname, func]);
  };
} else {
  HTMLArea._addEvent = function(el, evname, func) {
    el.addEventListener(evname, func, true);
    HTMLArea._eventFlushers.push([el, evname, func]);
  };
}

But i dont like the use of HTMLArea.is_ie, it is very dirty IMO. So i propose this change :

if (document.addEventListener) {
  HTMLArea._addEvent = function(el, evname, func) {
    el.addEventListener(evname, func, true);
    HTMLArea._eventFlushers.push([el, evname, func]);
  };
} else if (document.attachEvent) {
  HTMLArea._addEvent = function(el, evname, func) {
   el.attachEvent("on" + evname, func);
   HTMLArea._eventFlushers.push([el, evname, func]);
  };
} else {
  HTMLArea._addEvent = function(el, evname, func) {
    alert('_addEvent is not supported');
  };
}

The same concept can be used for removeEvent and stopEvent

if (document.addEventListener) {
  HTMLArea._addEvent = function(...
  HTMLArea._removeEvent = function(...
  HTMLArea._stopEvent = function(...
} else if (document.attachEvent) {
  HTMLArea._addEvent = function(...
  HTMLArea._removeEvent = function(...
  HTMLArea._stopEvent = function(...
} else {
  HTMLArea._addEvent = function(...
  HTMLArea._removeEvent = function(...
  HTMLArea._stopEvent = function(...
}

I will upload a diff patch in a moment.

Attachments (1)

events.patch (2.3 KB) - added by mokhet 14 years ago.

Download all attachments as: .zip

Change History (3)

Changed 14 years ago by mokhet

comment:1 Changed 14 years ago by gogo

Looks good, you have commit access, so go right ahead.

comment:2 Changed 14 years ago by mokhet

  • Resolution set to fixed
  • Status changed from new to closed

patch applied in changeset:449

Well, i did apply the patch but i made a few update in the braces to satisfy the BSD-Style Bracing as suggered in comments.

Note: See TracTickets for help on using tickets.