@qooxdoo/framework
Version:
The JS Framework for Coders
341 lines (250 loc) • 9.03 kB
JavaScript
/* ************************************************************************
qooxdoo - the new era of web development
http://qooxdoo.org
Copyright:
2004-2011 1&1 Internet AG, Germany, http://www.1und1.de
License:
MIT: https://opensource.org/licenses/MIT
See the LICENSE file in the project's top-level directory for details.
Authors:
* Tino Butz (tbtz)
======================================================================
This class contains code based on the following work:
* Unify Project
Homepage:
http://unify-project.org
Copyright:
2009-2010 Deutsche Telekom AG, Germany, http://telekom.com
License:
MIT: http://www.opensource.org/licenses/mit-license.php
************************************************************************ */
/**
* EXPERIMENTAL - NOT READY FOR PRODUCTION
*
* This class provides support for HTML5 transition and animation events.
* Currently only WebKit and Firefox are supported.
*
* NOTE: Instances of this class must be disposed of after use
*
*/
qx.Class.define("qx.event.handler.Transition",
{
extend : qx.core.Object,
implement : [ qx.event.IEventHandler, qx.core.IDisposable ],
/*
*****************************************************************************
CONSTRUCTOR
*****************************************************************************
*/
/**
* Create a new instance
*
* @param manager {qx.event.Manager} Event manager for the window to use
*/
construct : function(manager)
{
this.base(arguments);
this.__registeredEvents = {};
this.__onEventWrapper = qx.lang.Function.listener(this._onNative, this);
},
/*
*****************************************************************************
STATICS
*****************************************************************************
*/
statics :
{
/** @type {Integer} Priority of this handler */
PRIORITY : qx.event.Registration.PRIORITY_NORMAL,
/** @type {Map} Supported event types */
SUPPORTED_TYPES :
{
transitionEnd : 1,
animationEnd : 1,
animationStart : 1,
animationIteration : 1
},
/** @type {Integer} Which target check to use */
TARGET_CHECK : qx.event.IEventHandler.TARGET_DOMNODE,
/** @type {Integer} Whether the method "canHandleEvent" must be called */
IGNORE_CAN_HANDLE : true,
/** Mapping of supported event types to native event types */
TYPE_TO_NATIVE : null,
/** Mapping of native event types to supported event types */
NATIVE_TO_TYPE : null
},
/*
*****************************************************************************
MEMBERS
*****************************************************************************
*/
members:
{
__onEventWrapper : null,
__registeredEvents : null,
/*
---------------------------------------------------------------------------
EVENT HANDLER INTERFACE
---------------------------------------------------------------------------
*/
// interface implementation
canHandleEvent: function(target, type) {
// Nothing needs to be done here
},
// interface implementation
/**
* This method is called each time an event listener, for one of the
* supported events, is added using {@link qx.event.Manager#addListener}.
*
* @param target {var} The target to, which the event handler should
* be attached
* @param type {String} event type
* @param capture {Boolean} Whether to attach the event to the
* capturing phase or the bubbling phase of the event.
* @signature function(target, type, capture)
*/
registerEvent: qx.core.Environment.select("engine.name",
{
"webkit" : function(target, type, capture)
{
var hash = qx.core.ObjectRegistry.toHashCode(target) + type;
var nativeType = qx.event.handler.Transition.TYPE_TO_NATIVE[type];
this.__registeredEvents[hash] =
{
target:target,
type : nativeType
};
qx.bom.Event.addNativeListener(target, nativeType, this.__onEventWrapper);
},
"gecko" : function(target, type, capture)
{
var hash = qx.core.ObjectRegistry.toHashCode(target) + type;
var nativeType = qx.event.handler.Transition.TYPE_TO_NATIVE[type];
this.__registeredEvents[hash] =
{
target:target,
type : nativeType
};
qx.bom.Event.addNativeListener(target, nativeType, this.__onEventWrapper);
},
"mshtml" : function(target, type, capture)
{
var hash = qx.core.ObjectRegistry.toHashCode(target) + type;
var nativeType = qx.event.handler.Transition.TYPE_TO_NATIVE[type];
this.__registeredEvents[hash] =
{
target:target,
type : nativeType
};
qx.bom.Event.addNativeListener(target, nativeType, this.__onEventWrapper);
},
"default" : function() {}
}),
// interface implementation
/**
* This method is called each time an event listener, for one of the
* supported events, is removed by using {@link qx.event.Manager#removeListener}
* and no other event listener is listening on this type.
*
* @param target {var} The target from, which the event handler should
* be removed
* @param type {String} event type
* @param capture {Boolean} Whether to attach the event to the
* capturing phase or the bubbling phase of the event.
* @signature function(target, type, capture)
*/
unregisterEvent: qx.core.Environment.select("engine.name",
{
"webkit" : function(target, type, capture)
{
var events = this.__registeredEvents;
if (!events) {
return;
}
var hash = qx.core.ObjectRegistry.toHashCode(target) + type;
if (events[hash]) {
delete events[hash];
}
qx.bom.Event.removeNativeListener(target, qx.event.handler.Transition.TYPE_TO_NATIVE[type], this.__onEventWrapper);
},
"gecko" : function(target, type, capture)
{
var events = this.__registeredEvents;
if (!events) {
return;
}
var hash = qx.core.ObjectRegistry.toHashCode(target) + type;
if (events[hash]) {
delete events[hash];
}
qx.bom.Event.removeNativeListener(target, qx.event.handler.Transition.TYPE_TO_NATIVE[type], this.__onEventWrapper);
},
"mshtml" : function(target, type, capture)
{
var events = this.__registeredEvents;
if (!events) {
return;
}
var hash = qx.core.ObjectRegistry.toHashCode(target) + type;
if (events[hash]) {
delete events[hash];
}
qx.bom.Event.removeNativeListener(target, qx.event.handler.Transition.TYPE_TO_NATIVE[type], this.__onEventWrapper);
},
"default" : function() {}
}),
/*
---------------------------------------------------------------------------
EVENT-HANDLER
---------------------------------------------------------------------------
*/
/**
* Global handler for the transition event.
*
* @signature function(domEvent)
* @param domEvent {Event} DOM event
*/
_onNative : qx.event.GlobalError.observeMethod(function(nativeEvent) {
qx.event.Registration.fireEvent(nativeEvent.target, qx.event.handler.Transition.NATIVE_TO_TYPE[nativeEvent.type], qx.event.type.Event);
})
},
/*
*****************************************************************************
DESTRUCTOR
*****************************************************************************
*/
destruct : function()
{
var event;
var events = this.__registeredEvents;
for (var id in events)
{
event = events[id];
if (event.target) {
qx.bom.Event.removeNativeListener(event.target, event.type, this.__onEventWrapper);
}
}
this.__registeredEvents = this.__onEventWrapper = null;
},
/*
*****************************************************************************
DEFER
*****************************************************************************
*/
defer : function(statics) {
var aniEnv = qx.core.Environment.get("css.animation") || {};
var transEnv = qx.core.Environment.get("css.transition") || {};
var n2t = qx.event.handler.Transition.NATIVE_TO_TYPE = {};
var t2n = qx.event.handler.Transition.TYPE_TO_NATIVE = {
transitionEnd : transEnv["end-event"] || null,
animationStart : aniEnv["start-event"] || null,
animationEnd : aniEnv["end-event"] || null,
animationIteration : aniEnv["iteration-event"] || null
};
for (var type in t2n) {
var nate = t2n[type];
n2t[nate] = type;
}
qx.event.Registration.addHandler(statics);
}
});