Class o2.Event


static class o2.Event

A cross-browser event handling and event utilities class.

Defined in event.constants

Function Summary
static addEventListener (DomNode node, String evt, Function fn)

Adds a new event listener to the DOM Node.

Usage example:

 o2.Event.addEventListener('container', 'click', function(evt) {
      doClickHandling();
 });
 
static addEventListeners (Object collection, String eventName, Function handler)

Adds a set of event handlers the the eventName of the given collection.

Usage example:

 o2.Event.addEventListeners(['elm1', 'elm2'], 'click', function(evt) {
      handleClickEvent();
 });
 
static getEventObject (Event evt)

Gets the actual event object.

Usage example:

 o2.Event.addEventListener('container', 'click', function(evt) {
      var e = o2.Event.getEventObject(evt);
 });
 
static getKeyCode (Event evt)

Gets the key code of the key-related event (keydown, keyup, keypress etc.).

Usage example:

 o2.Event.addEventListener('container', 'keydown', function(evt) {
      var code = o2.Event.getKeyCode(evt);
 });
 
static getMouseCoordinates (Event evt)

Gets the current mouse coordinates.

Usage example:

 o2.Event.addEventListener('container', 'mousemove', function(evt) {
      var dimensions = o2.Event.getMouseCoordinates(evt);
 });
 
static getTarget (Event evt)

Gets the originating source of the event.

Usage example:

 o2.Event.addEventListener('container', 'click', function(evt) {
      var src = o2.Event.getTarget(evt);
 });
 
static isArrowKey (Event evt)

Checks whether the pressed key is an arrow key.

Usage example:

 o2.Event.addEventListener('container', 'keydown', function(evt) {
      var isArrowKey = o2.Event.isArrowKey(evt);
 });
 
static isBackspaceKey (Event evt)

Checks whether the pressed key is the backspace (DEL) key.

Usage example:

 o2.Event.addEventListener('container', 'keydown', function(evt) {
      var isBackspaceKey = o2.Event.isBackspaceKey(evt);
 });
 

Checks whether the character in a onkeypress event actually produces a printable char.

The thing you have to remember is that you can't reliably tell anything at all about any character that may be typed in a onkeydown or onkeyup event: The printable key is determined only in the onkeypress handler.

Usage example:

 o2.Event.addEventListener('container', 'keypress', function(evt) {
      var isCharKeypress = o2.Event.isCharacterKeypressEvent(evt);
 });
 
static isEnterKey (Event evt)

Checks whether the pressed key is the enter (return) key.

Usage example:

 o2.Event.addEventListener('container', 'keypress', function(evt) {
      var isEnterKey = o2.Event.isEnterKey(evt);
 });
 
static isEscapeKey (Event evt)

Checks whether the pressed key is the escape (ESC) key.

Usage example:

 o2.Event.addEventListener('container', 'keypress', function(evt) {
      var isEscapeKey = o2.Event.isEscapeKey(evt);
 });
 
static isRightClick (Event evt)

Checks whether or not the curent action is a right click action.

Usage example:

 o2.Event.addEventListener('container', 'click', function(evt) {
      var isRightClick = o2.Event.isRightClick(evt);
 });
 
static isTabKey (Event evt)

Checks whether the pressed key is the tab key.

Usage example:

 o2.Event.addEventListener('container', 'keypress', function(evt) {
      var isTabKey = o2.Event.isTabKey(evt);
 });
 
static preventDefault (Event evt)

Prevents the default action. When this method is called inside an even handling callback, the default action associated with that event is not triggered. Like, if it is an onclick event on a link, then the browser does not go to the href of that link.

Usage example:

 o2.Event.addEventListener('container', 'click', function(evt) {
      o2.Event.preventDefault(evt);
 });
 
static removeEventListener (DomNode node, String evt, Function fn)

Removes an already-added new event listener from the DOM Node.

Usage example:

 o2.Event.removeEventListener('container', 'click', container_click);
 
static stopPropagation (Event evt)

Stops the propagation of the event upwards in the DOM hierarchy.

Note that "change" event does not bubble.

Also, events: change, submit, reset, focus, blur do not bubble in Internet Explorer.

According to specification, "focus" and "blur" should not bubble, while "change", "submit", "reset" should.

This behavior implemented properly in all web browsers but IE.

See http://www.w3.org/TR/DOM-Level-2-Events/events.html#Events-flow for details.

Usage example:

 o2.Event.addEventListener('container', 'click', function(evt) {
      o2.Event.stopPropagation(evt);
 });
 

Function Details

function addEventListener

static addEventListener(DomNode node, String evt, Function fn)

Adds a new event listener to the DOM Node.

Usage example:

 o2.Event.addEventListener('container', 'click', function(evt) {
      doClickHandling();
 });
 
Parameters:
node - the DOM object (or its String id) the evet shall be attached.
evt - the name of the event (like "click", "mousemove"...)
fn - a reference to the on[event] callback action.
Throws:
exception - if fn callback is not defined.

function addEventListeners

static addEventListeners(Object collection, String eventName, Function handler)

Adds a set of event handlers the the eventName of the given collection.

Usage example:

 o2.Event.addEventListeners(['elm1', 'elm2'], 'click', function(evt) {
      handleClickEvent();
 });
 
Parameters:
collection - an Object or an Array of DOM nodes, or their ids.
eventName - the name of the event to attach to.
handler - the common event handling callback.
Throws:
Exception - if the handler callback is not defined.

function getEventObject

static getEventObject(Event evt)

Gets the actual event object.

Usage example:

 o2.Event.addEventListener('container', 'click', function(evt) {
      var e = o2.Event.getEventObject(evt);
 });
 
Parameters:
evt - the actual DOM Event object used internally in Event.addEventListener
Returns:
the actual DOM Event object.

function getKeyCode

static getKeyCode(Event evt)

Gets the key code of the key-related event (keydown, keyup, keypress etc.).

Usage example:

 o2.Event.addEventListener('container', 'keydown', function(evt) {
      var code = o2.Event.getKeyCode(evt);
 });
 
Parameters:
evt - the actual DOM Event object used internally in o2.Event.addEventListener
Returns:
the unicode key code associated with the event as an Integer, if found; 0 otherwise.

function getMouseCoordinates

static getMouseCoordinates(Event evt)

Gets the current mouse coordinates.

Usage example:

 o2.Event.addEventListener('container', 'mousemove', function(evt) {
      var dimensions = o2.Event.getMouseCoordinates(evt);
 });
 
Parameters:
evt - the actual DOM Event object used internally in o2.Event.addEventListener
Returns:
the coordinates in the form of {x: mouseX, y: mouseY} where x is the distance from the top of the screen, and y is the distance from the left of the screen.

function getTarget

static getTarget(Event evt)

Gets the originating source of the event.

Usage example:

 o2.Event.addEventListener('container', 'click', function(evt) {
      var src = o2.Event.getTarget(evt);
 });
 
Parameters:
evt - the actual DOM Event object used internally in o2.Event.addEventListener
Returns:
the actual DOM target of the event object.

function isArrowKey

static isArrowKey(Event evt)

Checks whether the pressed key is an arrow key.

Usage example:

 o2.Event.addEventListener('container', 'keydown', function(evt) {
      var isArrowKey = o2.Event.isArrowKey(evt);
 });
 
Parameters:
evt - the actual DOM Event object used internally in o2.Event.addEventListener
Returns:
the true if the pressed key is an arrow key, false otherwise.

function isBackspaceKey

static isBackspaceKey(Event evt)

Checks whether the pressed key is the backspace (DEL) key.

Usage example:

 o2.Event.addEventListener('container', 'keydown', function(evt) {
      var isBackspaceKey = o2.Event.isBackspaceKey(evt);
 });
 
Parameters:
evt - the actual DOM Event object used internally in o2.Event.addEventListener
Returns:
the true if the pressed key is the backspace key, false otherwise.

function isCharacterKeypressEvent

static isCharacterKeypressEvent()

Checks whether the character in a onkeypress event actually produces a printable char.

The thing you have to remember is that you can't reliably tell anything at all about any character that may be typed in a onkeydown or onkeyup event: The printable key is determined only in the onkeypress handler.

Usage example:

 o2.Event.addEventListener('container', 'keypress', function(evt) {
      var isCharKeypress = o2.Event.isCharacterKeypressEvent(evt);
 });
 
Returns:
true if the pressed key is a printable character; false otherwise.

function isEnterKey

static isEnterKey(Event evt)

Checks whether the pressed key is the enter (return) key.

Usage example:

 o2.Event.addEventListener('container', 'keypress', function(evt) {
      var isEnterKey = o2.Event.isEnterKey(evt);
 });
 
Parameters:
evt - the actual DOM Event object used internally in o2.Event.addEventListener
Returns:
the true if the pressed key is the enter key, false otherwise.

function isEscapeKey

static isEscapeKey(Event evt)

Checks whether the pressed key is the escape (ESC) key.

Usage example:

 o2.Event.addEventListener('container', 'keypress', function(evt) {
      var isEscapeKey = o2.Event.isEscapeKey(evt);
 });
 
Parameters:
evt - the actual DOM Event object used internally in o2.Event.addEventListener
Returns:
the true if the pressed key is the escape key, false otherwise.

function isRightClick

static isRightClick(Event evt)

Checks whether or not the curent action is a right click action.

Usage example:

 o2.Event.addEventListener('container', 'click', function(evt) {
      var isRightClick = o2.Event.isRightClick(evt);
 });
 
Parameters:
evt - the actual DOM Event object used internally in o2.Event.addEventListener.
Returns:
true if the event is a right click event, false otherwise.

function isTabKey

static isTabKey(Event evt)

Checks whether the pressed key is the tab key.

Usage example:

 o2.Event.addEventListener('container', 'keypress', function(evt) {
      var isTabKey = o2.Event.isTabKey(evt);
 });
 
Parameters:
evt - the actual DOM Event object used internally in o2.Event.addEventListener
Returns:
the true if the pressed key is the tab key, false otherwise.

function preventDefault

static preventDefault(Event evt)

Prevents the default action. When this method is called inside an even handling callback, the default action associated with that event is not triggered. Like, if it is an onclick event on a link, then the browser does not go to the href of that link.

Usage example:

 o2.Event.addEventListener('container', 'click', function(evt) {
      o2.Event.preventDefault(evt);
 });
 
Parameters:
evt - the actual DOM Event object used internally in Event.addEventListener

function removeEventListener

static removeEventListener(DomNode node, String evt, Function fn)

Removes an already-added new event listener from the DOM Node.

Usage example:

 o2.Event.removeEventListener('container', 'click', container_click);
 
Parameters:
node - the DOM object (or its String reference) the evet shall be removed.
evt - the name of the event (like "click", "mousemove"...)
fn - a reference to the on[event] callback action.
Throws:
Exception - if fn callback is not defined.

function stopPropagation

static stopPropagation(Event evt)

Stops the propagation of the event upwards in the DOM hierarchy.

Note that "change" event does not bubble.

Also, events: change, submit, reset, focus, blur do not bubble in Internet Explorer.

According to specification, "focus" and "blur" should not bubble, while "change", "submit", "reset" should.

This behavior implemented properly in all web browsers but IE.

See http://www.w3.org/TR/DOM-Level-2-Events/events.html#Events-flow for details.

Usage example:

 o2.Event.addEventListener('container', 'click', function(evt) {
      o2.Event.stopPropagation(evt);
 });
 
Parameters:
evt - the actual DOM Event object used internally in Event.addEventListener