Event

To provide the ability of working with DOM events in a cross-browser way, RightJS like many other JavaScript frameworks provides some extensions to the events system.

If you attach your event listeners via the RightJS interfaces, then all the events your listeners receive will be automatically extended, otherwise, you might need to call the ext method on your events.

Internet Explorer Fixes

Instead of providing some mediative interface for the DOM events, RightJS fixes IE events by creating W3C style attributes for the events in order for you to work with them in IE as if it were a normal browser.

The list of fake attributes is:

  • which - which mouse button was pressed (1, 2, 3)
  • target - target element reference
  • currentTarget - the bounding (listener) element reference
  • relatedTarget - the related element for the over and out mouse events
  • pageX, pageY - the cursor position relative to the document

Methods

behave, delegate, ext, include, position, preventDefault, stop, stopPropagation

top ↑behave

Event.behave(String css_rule, String event, Function callback) -> Object events
Event.behave(String css_rule, String event, String method[, arg, ..]) -> Object events
Event.behave(String css_rule, Object event_handlers) -> Object events

Description

Attaches a document level events delegation handler.

Returns a hash that can be used to switch the handlers off in the document.stopObserving method.

Example

// with usual functions
Event.behave("#todos li", "click", function() { this.remove(); });

// with a call by name
Event.behave("#todos li", "click", "addClass", "marked");

// with a hash of handlers
Event.behave("#todos li", {
mouseover: function() { this.addClass('hovered'); },
mouseout: ['removeClass', 'hovered'],
click: 'remove'
});

// turning handlers off
var handlers = Event.behave(.....);

document.stopObserving(handlers);

top ↑delegate

Event.delegate(Object description) -> Function delegation

Description

Creates a function that will delegate the actual event handler depending on the element that triggered the event

NOTE: the actual callback function will be called in the context of the element that triggered the event

Example

var delegation = Event.delegate({
"div.red": function() { alert('You have clicked the red block'); },
"div.green": function() { alert('You have clicked the green block'); }
});

document.onClick(delegation);

// you also can use references by-name
var delegation = Event.delegate({
".red": ['addClass', 'was-red'],
".green": ['addClass', 'was-green'],
".hide-me": 'hide'
});

top ↑ext

Event.ext(Event event) -> Event extended

Description

Extends the event object with additional methods.

Example

$('element').attachEvent('onclick', function(event) {
Event.ext(event);

event.stop();
});

top ↑include

Event.include(Object methods[, boolean dont_rewrite])

Description

Registers additional methods for the DOM events.

Example

Event.include({
myMethod: function() {....}
});

$('my_element').onClick(function(event) {
event.myMethod()
});

top ↑position

position() -> {x: NNN, y: NNN}

Description

Returns the current event position.

Example

$('element').onMouseover(function(event) {
$('nice-looking-title').show().moveTo(event.position());
});

top ↑preventDefault

preventDefault() -> void

Description

Prevents the event defaults.

top ↑stop

stop() -> Event self

Description

Stops the event from further bubbling and cancels default handling.

Example

$('element').onContextmenu(function(e) {
e.stop();
$('context-menu').moveTo(e.position()).show();
});

top ↑stopPropagation

stopPropagation() -> void

Description

Stops the event propagation.