JSEventTargetBase.h [plain text]
#ifndef JSEventTargetBase_h
#define JSEventTargetBase_h
#include "Document.h"
#include "Event.h"
#include "EventNames.h"
#include "JSEvent.h"
#include "kjs_events.h"
#include "kjs_window.h"
namespace KJS {
extern const struct HashTable JSEventTargetPropertiesTable;
extern const struct HashTable JSEventTargetPrototypeTable;
}
namespace WebCore {
using namespace EventNames;
class AtomicString;
class EventTarget;
struct JSEventTargetProperties {
enum {
AddEventListener, RemoveEventListener, DispatchEvent,
OnAbort, OnBlur, OnChange, OnClick, OnContextMenu, OnDblClick, OnError,
OnDragEnter, OnDragOver, OnDragLeave, OnDrop, OnDragStart, OnDrag, OnDragEnd,
OnBeforeCut, OnCut, OnBeforeCopy, OnCopy, OnBeforePaste, OnPaste, OnSelectStart,
OnFocus, OnInput, OnKeyDown, OnKeyPress, OnKeyUp, OnLoad, OnMouseDown,
OnMouseMove, OnMouseOut, OnMouseOver, OnMouseUp, OnMouseWheel, OnReset,
OnResize, OnScroll, OnSearch, OnSelect, OnSubmit, OnUnload
};
};
bool retrieveEventTargetAndCorrespondingNode(KJS::ExecState*, KJS::JSObject* thisObj, Node*&, EventTarget*&);
KJS::JSValue* jsEventTargetAddEventListener(KJS::ExecState*, KJS::JSObject*, const KJS::List&);
KJS::JSValue* jsEventTargetRemoveEventListener(KJS::ExecState*, KJS::JSObject*, const KJS::List&);
KJS::JSValue* jsEventTargetDispatchEvent(KJS::ExecState*, KJS::JSObject*, const KJS::List&);
AtomicString eventNameForPropertyToken(int token);
template<class JSEventTarget>
class JSEventTargetBase {
public:
JSEventTargetBase() { }
KJS::JSValue* getValueProperty(const JSEventTarget* owner, KJS::ExecState* exec, int token) const
{
AtomicString eventName = eventNameForPropertyToken(token);
if (!eventName.isEmpty())
return owner->getListener(eventName);
return KJS::jsUndefined();
}
void putValueProperty(const JSEventTarget* owner, KJS::ExecState* exec, int token, KJS::JSValue* value, int attr)
{
AtomicString eventName = eventNameForPropertyToken(token);
if (!eventName.isEmpty())
owner->setListener(exec, eventName, value);
}
private:
friend class JSEventTargetNode;
friend class JSEventTargetSVGElementInstance;
template<class JSParent>
bool getOwnPropertySlot(JSEventTarget* owner, KJS::ExecState* exec, const KJS::Identifier& propertyName, KJS::PropertySlot& slot)
{
return KJS::getStaticValueSlot<JSEventTarget, JSParent>(exec, &KJS::JSEventTargetPropertiesTable, owner, propertyName, slot);
}
template<class JSParent>
void put(JSEventTarget* owner, KJS::ExecState* exec, const KJS::Identifier& propertyName, KJS::JSValue* value, int attr)
{
KJS::lookupPut<JSEventTarget, JSParent>(exec, propertyName, value, attr, &KJS::JSEventTargetPropertiesTable, owner);
}
};
template<class JSEventTargetPrototypeParent, class JSEventTargetPrototypeInformation>
class JSEventTargetPrototype : public KJS::JSObject {
public:
JSEventTargetPrototype(KJS::ExecState* exec)
: KJS::JSObject(JSEventTargetPrototypeParent::self(exec))
{
}
static KJS::JSObject* self(KJS::ExecState* exec)
{
static KJS::Identifier* prototypeName = new KJS::Identifier(JSEventTargetPrototypeInformation::prototypeClassName());
KJS::JSGlobalObject* globalObject = exec->lexicalGlobalObject();
if (KJS::JSValue* objectValue = globalObject->getDirect(*prototypeName)) {
ASSERT(objectValue->isObject());
return static_cast<KJS::JSObject*>(objectValue);
}
KJS::JSObject* newObject = new JSEventTargetPrototype<JSEventTargetPrototypeParent, JSEventTargetPrototypeInformation>(exec);
globalObject->putDirect(*prototypeName, newObject, KJS::Internal | KJS::DontEnum);
return newObject;
}
bool getOwnPropertySlot(KJS::ExecState* exec, const KJS::Identifier& propertyName, KJS::PropertySlot& slot)
{
return KJS::getStaticFunctionSlot<KJS::JSObject>(exec, &KJS::JSEventTargetPrototypeTable, this, propertyName, slot);
}
virtual const KJS::ClassInfo* classInfo() const
{
static const KJS::ClassInfo s_classInfo = { JSEventTargetPrototypeInformation::prototypeClassName(), 0, &KJS::JSEventTargetPrototypeTable };
return &s_classInfo;
}
};
}
#endif // JSEventTargetBase_h