2007-08-04 07:20:45 +00:00
|
|
|
if (!window.Event) var Event = { };
|
2007-01-18 22:24:27 +00:00
|
|
|
|
|
|
|
Object.extend(Event, {
|
|
|
|
KEY_BACKSPACE: 8,
|
|
|
|
KEY_TAB: 9,
|
|
|
|
KEY_RETURN: 13,
|
|
|
|
KEY_ESC: 27,
|
|
|
|
KEY_LEFT: 37,
|
|
|
|
KEY_UP: 38,
|
|
|
|
KEY_RIGHT: 39,
|
|
|
|
KEY_DOWN: 40,
|
|
|
|
KEY_DELETE: 46,
|
|
|
|
KEY_HOME: 36,
|
|
|
|
KEY_END: 35,
|
|
|
|
KEY_PAGEUP: 33,
|
|
|
|
KEY_PAGEDOWN: 34,
|
2007-08-04 07:20:45 +00:00
|
|
|
KEY_INSERT: 45,
|
|
|
|
|
2007-08-09 23:45:59 +00:00
|
|
|
cache: { },
|
2007-08-04 07:20:45 +00:00
|
|
|
|
|
|
|
relatedTarget: function(event) {
|
|
|
|
var element;
|
|
|
|
switch(event.type) {
|
|
|
|
case 'mouseover': element = event.fromElement; break;
|
|
|
|
case 'mouseout': element = event.toElement; break;
|
|
|
|
default: return null;
|
|
|
|
}
|
|
|
|
return Element.extend(element);
|
|
|
|
}
|
|
|
|
});
|
2007-01-18 22:24:27 +00:00
|
|
|
|
2007-10-16 03:19:38 +00:00
|
|
|
Event.Methods = (function() {
|
2007-10-17 13:26:49 +00:00
|
|
|
var isButton;
|
|
|
|
|
2007-10-16 03:19:38 +00:00
|
|
|
if (Prototype.Browser.IE) {
|
2007-10-17 13:26:49 +00:00
|
|
|
var buttonMap = { 0: 1, 1: 4, 2: 2 };
|
|
|
|
isButton = function(event, code) {
|
|
|
|
return event.button == buttonMap[code];
|
|
|
|
};
|
2007-10-16 03:36:58 +00:00
|
|
|
|
2007-10-16 03:19:38 +00:00
|
|
|
} else if (Prototype.Browser.WebKit) {
|
2007-10-17 13:26:49 +00:00
|
|
|
isButton = function(event, code) {
|
2007-10-16 03:19:38 +00:00
|
|
|
switch (code) {
|
|
|
|
case 0: return event.which == 1 && !event.metaKey;
|
|
|
|
case 1: return event.which == 1 && event.metaKey;
|
|
|
|
default: return false;
|
|
|
|
}
|
2007-10-17 13:26:49 +00:00
|
|
|
};
|
2007-10-16 03:36:58 +00:00
|
|
|
|
2007-10-16 03:19:38 +00:00
|
|
|
} else {
|
2007-10-17 13:26:49 +00:00
|
|
|
isButton = function(event, code) {
|
2007-10-16 03:19:38 +00:00
|
|
|
return event.which ? (event.which === code + 1) : (event.button === code);
|
2007-10-17 13:26:49 +00:00
|
|
|
};
|
2007-10-16 03:19:38 +00:00
|
|
|
}
|
2007-08-04 07:20:45 +00:00
|
|
|
|
2007-10-16 03:19:38 +00:00
|
|
|
return {
|
|
|
|
isLeftClick: function(event) { return isButton(event, 0) },
|
|
|
|
isMiddleClick: function(event) { return isButton(event, 1) },
|
|
|
|
isRightClick: function(event) { return isButton(event, 2) },
|
|
|
|
|
|
|
|
element: function(event) {
|
2008-03-28 18:13:16 +00:00
|
|
|
event = Event.extend(event);
|
|
|
|
|
2008-04-04 17:55:59 +00:00
|
|
|
var node = event.target, type = event.type;
|
2008-03-28 18:13:16 +00:00
|
|
|
|
2008-04-28 22:54:51 +00:00
|
|
|
if (event.currentTarget && event.currentTarget.tagName) {
|
2008-03-28 18:13:16 +00:00
|
|
|
// Firefox screws up the "click" event when moving between radio buttons
|
|
|
|
// via arrow keys. It also screws up the "load" and "error" events on images,
|
|
|
|
// reporting the document as the target instead of the original image.
|
|
|
|
var currentTarget = event.currentTarget;
|
|
|
|
var tagName = currentTarget.tagName.toUpperCase();
|
|
|
|
if (['load', 'error'].include(type) ||
|
|
|
|
(tagName === 'INPUT' && currentTarget.type === 'radio' && type === 'click'))
|
|
|
|
node = currentTarget;
|
|
|
|
}
|
|
|
|
|
|
|
|
return Element.extend(node && node.nodeType == Node.TEXT_NODE ?
|
|
|
|
node.parentNode : node);
|
2007-10-16 03:19:38 +00:00
|
|
|
},
|
2007-01-18 22:24:27 +00:00
|
|
|
|
2007-10-16 03:19:38 +00:00
|
|
|
findElement: function(event, expression) {
|
|
|
|
var element = Event.element(event);
|
2007-11-13 21:48:09 +00:00
|
|
|
if (!expression) return element;
|
|
|
|
var elements = [element].concat(element.ancestors());
|
|
|
|
return Selector.findElement(elements, expression, 0);
|
2007-10-16 03:19:38 +00:00
|
|
|
},
|
2007-01-18 22:24:27 +00:00
|
|
|
|
2007-10-16 03:19:38 +00:00
|
|
|
pointer: function(event) {
|
2008-03-19 07:01:49 +00:00
|
|
|
var docElement = document.documentElement, body = document.body;
|
2007-10-16 03:19:38 +00:00
|
|
|
return {
|
|
|
|
x: event.pageX || (event.clientX +
|
2008-03-19 07:01:49 +00:00
|
|
|
(docElement.scrollLeft || body.scrollLeft) -
|
|
|
|
(docElement.clientLeft || 0)),
|
2007-10-16 03:19:38 +00:00
|
|
|
y: event.pageY || (event.clientY +
|
2008-03-19 07:01:49 +00:00
|
|
|
(docElement.scrollTop || body.scrollTop) -
|
|
|
|
(docElement.clientTop || 0))
|
2007-10-16 03:19:38 +00:00
|
|
|
};
|
|
|
|
},
|
2007-01-18 22:24:27 +00:00
|
|
|
|
2007-10-16 03:19:38 +00:00
|
|
|
pointerX: function(event) { return Event.pointer(event).x },
|
|
|
|
pointerY: function(event) { return Event.pointer(event).y },
|
2007-01-18 22:24:27 +00:00
|
|
|
|
2007-10-16 03:19:38 +00:00
|
|
|
stop: function(event) {
|
2007-10-16 05:11:42 +00:00
|
|
|
Event.extend(event);
|
2007-10-26 19:15:35 +00:00
|
|
|
event.preventDefault();
|
|
|
|
event.stopPropagation();
|
|
|
|
event.stopped = true;
|
2007-10-16 03:19:38 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
})();
|
2007-01-18 22:24:27 +00:00
|
|
|
|
2007-08-04 07:20:45 +00:00
|
|
|
Event.extend = (function() {
|
|
|
|
var methods = Object.keys(Event.Methods).inject({ }, function(m, name) {
|
|
|
|
m[name] = Event.Methods[name].methodize();
|
|
|
|
return m;
|
|
|
|
});
|
2007-01-18 22:24:27 +00:00
|
|
|
|
2007-08-04 07:20:45 +00:00
|
|
|
if (Prototype.Browser.IE) {
|
|
|
|
Object.extend(methods, {
|
|
|
|
stopPropagation: function() { this.cancelBubble = true },
|
|
|
|
preventDefault: function() { this.returnValue = false },
|
|
|
|
inspect: function() { return "[object Event]" }
|
|
|
|
});
|
2007-01-18 22:24:27 +00:00
|
|
|
|
2007-08-04 07:20:45 +00:00
|
|
|
return function(event) {
|
|
|
|
if (!event) return false;
|
|
|
|
if (event._extendedByPrototype) return event;
|
|
|
|
|
|
|
|
event._extendedByPrototype = Prototype.emptyFunction;
|
|
|
|
var pointer = Event.pointer(event);
|
|
|
|
Object.extend(event, {
|
|
|
|
target: event.srcElement,
|
|
|
|
relatedTarget: Event.relatedTarget(event),
|
|
|
|
pageX: pointer.x,
|
|
|
|
pageY: pointer.y
|
|
|
|
});
|
|
|
|
return Object.extend(event, methods);
|
|
|
|
};
|
2007-01-18 22:24:27 +00:00
|
|
|
|
2007-08-04 07:20:45 +00:00
|
|
|
} else {
|
2007-08-07 19:33:37 +00:00
|
|
|
Event.prototype = Event.prototype || document.createEvent("HTMLEvents").__proto__;
|
2007-08-04 07:20:45 +00:00
|
|
|
Object.extend(Event.prototype, methods);
|
|
|
|
return Prototype.K;
|
|
|
|
}
|
|
|
|
})();
|
2007-01-18 22:24:27 +00:00
|
|
|
|
2007-08-04 07:20:45 +00:00
|
|
|
Object.extend(Event, (function() {
|
2007-08-09 23:45:59 +00:00
|
|
|
var cache = Event.cache;
|
2008-01-25 18:49:40 +00:00
|
|
|
|
2007-08-04 07:20:45 +00:00
|
|
|
function getEventID(element) {
|
2008-01-25 18:49:40 +00:00
|
|
|
if (element._prototypeEventID) return element._prototypeEventID[0];
|
2007-08-04 07:20:45 +00:00
|
|
|
arguments.callee.id = arguments.callee.id || 1;
|
2008-01-25 18:49:40 +00:00
|
|
|
return element._prototypeEventID = [++arguments.callee.id];
|
2007-08-04 07:20:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
function getDOMEventName(eventName) {
|
2007-10-22 22:16:32 +00:00
|
|
|
if (eventName && eventName.include(':')) return "dataavailable";
|
2007-10-16 03:36:58 +00:00
|
|
|
return eventName;
|
2007-08-04 07:20:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
function getCacheForID(id) {
|
|
|
|
return cache[id] = cache[id] || { };
|
|
|
|
}
|
|
|
|
|
|
|
|
function getWrappersForEventName(id, eventName) {
|
|
|
|
var c = getCacheForID(id);
|
|
|
|
return c[eventName] = c[eventName] || [];
|
|
|
|
}
|
|
|
|
|
2007-08-17 16:17:26 +00:00
|
|
|
function createWrapper(element, eventName, handler) {
|
|
|
|
var id = getEventID(element);
|
2007-08-04 07:20:45 +00:00
|
|
|
var c = getWrappersForEventName(id, eventName);
|
|
|
|
if (c.pluck("handler").include(handler)) return false;
|
2007-01-18 22:24:27 +00:00
|
|
|
|
2007-08-04 07:20:45 +00:00
|
|
|
var wrapper = function(event) {
|
2007-10-22 01:04:17 +00:00
|
|
|
if (!Event || !Event.extend ||
|
|
|
|
(event.eventName && event.eventName != eventName))
|
|
|
|
return false;
|
2007-08-04 07:20:45 +00:00
|
|
|
|
|
|
|
Event.extend(event);
|
2007-12-31 16:16:14 +00:00
|
|
|
handler.call(element, event);
|
2007-08-04 07:20:45 +00:00
|
|
|
};
|
2007-01-18 22:24:27 +00:00
|
|
|
|
2007-08-04 07:20:45 +00:00
|
|
|
wrapper.handler = handler;
|
|
|
|
c.push(wrapper);
|
|
|
|
return wrapper;
|
|
|
|
}
|
|
|
|
|
|
|
|
function findWrapper(id, eventName, handler) {
|
|
|
|
var c = getWrappersForEventName(id, eventName);
|
|
|
|
return c.find(function(wrapper) { return wrapper.handler == handler });
|
|
|
|
}
|
|
|
|
|
|
|
|
function destroyWrapper(id, eventName, handler) {
|
2007-08-09 23:45:59 +00:00
|
|
|
var c = getCacheForID(id);
|
|
|
|
if (!c[eventName]) return false;
|
|
|
|
c[eventName] = c[eventName].without(findWrapper(id, eventName, handler));
|
2007-08-04 07:20:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
function destroyCache() {
|
|
|
|
for (var id in cache)
|
|
|
|
for (var eventName in cache[id])
|
|
|
|
cache[id][eventName] = null;
|
|
|
|
}
|
|
|
|
|
2008-03-28 17:48:42 +00:00
|
|
|
|
|
|
|
// Internet Explorer needs to remove event handlers on page unload
|
|
|
|
// in order to avoid memory leaks.
|
2007-08-04 07:20:45 +00:00
|
|
|
if (window.attachEvent) {
|
|
|
|
window.attachEvent("onunload", destroyCache);
|
|
|
|
}
|
|
|
|
|
2008-03-28 17:48:42 +00:00
|
|
|
// Safari has a dummy event handler on page unload so that it won't
|
|
|
|
// use its bfcache. Safari <= 3.1 has an issue with restoring the "document"
|
|
|
|
// object when page is returned to via the back button using its bfcache.
|
|
|
|
if (Prototype.Browser.WebKit) {
|
|
|
|
window.addEventListener('unload', Prototype.emptyFunction, false);
|
|
|
|
}
|
|
|
|
|
2007-08-04 07:20:45 +00:00
|
|
|
return {
|
|
|
|
observe: function(element, eventName, handler) {
|
|
|
|
element = $(element);
|
2007-08-17 16:17:26 +00:00
|
|
|
var name = getDOMEventName(eventName);
|
2007-08-04 07:20:45 +00:00
|
|
|
|
2007-08-17 16:17:26 +00:00
|
|
|
var wrapper = createWrapper(element, eventName, handler);
|
2007-08-11 19:53:39 +00:00
|
|
|
if (!wrapper) return element;
|
2007-08-04 07:20:45 +00:00
|
|
|
|
|
|
|
if (element.addEventListener) {
|
|
|
|
element.addEventListener(name, wrapper, false);
|
|
|
|
} else {
|
|
|
|
element.attachEvent("on" + name, wrapper);
|
|
|
|
}
|
2007-08-11 18:46:02 +00:00
|
|
|
|
|
|
|
return element;
|
2007-08-04 07:20:45 +00:00
|
|
|
},
|
|
|
|
|
|
|
|
stopObserving: function(element, eventName, handler) {
|
|
|
|
element = $(element);
|
|
|
|
var id = getEventID(element), name = getDOMEventName(eventName);
|
|
|
|
|
|
|
|
if (!handler && eventName) {
|
2007-08-11 19:07:21 +00:00
|
|
|
getWrappersForEventName(id, eventName).each(function(wrapper) {
|
2007-08-04 07:20:45 +00:00
|
|
|
element.stopObserving(eventName, wrapper.handler);
|
2007-08-11 19:07:21 +00:00
|
|
|
});
|
|
|
|
return element;
|
2007-08-04 07:20:45 +00:00
|
|
|
|
|
|
|
} else if (!eventName) {
|
2007-08-11 19:07:21 +00:00
|
|
|
Object.keys(getCacheForID(id)).each(function(eventName) {
|
2007-08-04 07:20:45 +00:00
|
|
|
element.stopObserving(eventName);
|
2007-08-11 19:07:21 +00:00
|
|
|
});
|
|
|
|
return element;
|
2007-08-04 07:20:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
var wrapper = findWrapper(id, eventName, handler);
|
2007-08-11 19:07:21 +00:00
|
|
|
if (!wrapper) return element;
|
2007-08-04 07:20:45 +00:00
|
|
|
|
|
|
|
if (element.removeEventListener) {
|
|
|
|
element.removeEventListener(name, wrapper, false);
|
|
|
|
} else {
|
|
|
|
element.detachEvent("on" + name, wrapper);
|
|
|
|
}
|
2007-08-09 23:45:59 +00:00
|
|
|
|
|
|
|
destroyWrapper(id, eventName, handler);
|
2007-08-11 18:46:02 +00:00
|
|
|
|
|
|
|
return element;
|
2007-08-04 07:20:45 +00:00
|
|
|
},
|
|
|
|
|
|
|
|
fire: function(element, eventName, memo) {
|
|
|
|
element = $(element);
|
|
|
|
if (element == document && document.createEvent && !element.dispatchEvent)
|
|
|
|
element = document.documentElement;
|
|
|
|
|
2007-12-20 13:10:42 +00:00
|
|
|
var event;
|
2007-08-04 07:20:45 +00:00
|
|
|
if (document.createEvent) {
|
2007-12-20 13:10:42 +00:00
|
|
|
event = document.createEvent("HTMLEvents");
|
2007-08-04 07:20:45 +00:00
|
|
|
event.initEvent("dataavailable", true, true);
|
|
|
|
} else {
|
2007-12-20 13:10:42 +00:00
|
|
|
event = document.createEventObject();
|
2007-08-04 07:20:45 +00:00
|
|
|
event.eventType = "ondataavailable";
|
|
|
|
}
|
|
|
|
|
|
|
|
event.eventName = eventName;
|
2007-08-08 23:19:45 +00:00
|
|
|
event.memo = memo || { };
|
2007-08-04 07:20:45 +00:00
|
|
|
|
|
|
|
if (document.createEvent) {
|
|
|
|
element.dispatchEvent(event);
|
|
|
|
} else {
|
|
|
|
element.fireEvent(event.eventType, event);
|
|
|
|
}
|
|
|
|
|
2007-11-13 21:48:09 +00:00
|
|
|
return Event.extend(event);
|
2007-01-18 22:24:27 +00:00
|
|
|
}
|
2007-08-04 07:20:45 +00:00
|
|
|
};
|
|
|
|
})());
|
|
|
|
|
|
|
|
Object.extend(Event, Event.Methods);
|
|
|
|
|
|
|
|
Element.addMethods({
|
2007-08-11 18:46:02 +00:00
|
|
|
fire: Event.fire,
|
|
|
|
observe: Event.observe,
|
|
|
|
stopObserving: Event.stopObserving
|
2007-01-18 22:24:27 +00:00
|
|
|
});
|
|
|
|
|
2007-08-04 07:20:45 +00:00
|
|
|
Object.extend(document, {
|
|
|
|
fire: Element.Methods.fire.methodize(),
|
|
|
|
observe: Element.Methods.observe.methodize(),
|
2008-01-04 00:30:53 +00:00
|
|
|
stopObserving: Element.Methods.stopObserving.methodize(),
|
|
|
|
loaded: false
|
2007-08-04 07:20:45 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
(function() {
|
|
|
|
/* Support for the DOMContentLoaded event is based on work by Dan Webb,
|
|
|
|
Matthias Miller, Dean Edwards and John Resig. */
|
|
|
|
|
2008-01-04 00:30:53 +00:00
|
|
|
var timer;
|
2007-08-04 07:20:45 +00:00
|
|
|
|
|
|
|
function fireContentLoadedEvent() {
|
2008-01-04 00:30:53 +00:00
|
|
|
if (document.loaded) return;
|
2007-08-04 07:20:45 +00:00
|
|
|
if (timer) window.clearInterval(timer);
|
2007-10-11 06:00:45 +00:00
|
|
|
document.fire("dom:loaded");
|
2008-01-04 00:30:53 +00:00
|
|
|
document.loaded = true;
|
2007-08-04 07:20:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (document.addEventListener) {
|
|
|
|
if (Prototype.Browser.WebKit) {
|
|
|
|
timer = window.setInterval(function() {
|
|
|
|
if (/loaded|complete/.test(document.readyState))
|
|
|
|
fireContentLoadedEvent();
|
|
|
|
}, 0);
|
|
|
|
|
|
|
|
Event.observe(window, "load", fireContentLoadedEvent);
|
|
|
|
|
|
|
|
} else {
|
2007-08-11 18:46:02 +00:00
|
|
|
document.addEventListener("DOMContentLoaded",
|
|
|
|
fireContentLoadedEvent, false);
|
2007-08-04 07:20:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
} else {
|
2007-10-08 22:37:15 +00:00
|
|
|
document.write("<script id=__onDOMContentLoaded defer src=//:><\/script>");
|
2007-08-04 07:20:45 +00:00
|
|
|
$("__onDOMContentLoaded").onreadystatechange = function() {
|
|
|
|
if (this.readyState == "complete") {
|
|
|
|
this.onreadystatechange = null;
|
|
|
|
fireContentLoadedEvent();
|
|
|
|
}
|
|
|
|
};
|
|
|
|
}
|
|
|
|
})();
|