1 /* Copyright (c) 2006 MetaCarta, Inc., published under the BSD license.
2 * See http://svn.openlayers.org/trunk/openlayers/license.txt for the full
3 * text of the license. */
4 OpenLayers.Events = Class.create();
6 OpenLayers.Events.prototype = {
7 // Array: supported events
9 "mouseover", "mouseout",
10 "mousedown", "mouseup", "mousemove",
12 "resize", "focus", "blur"
15 // hash of Array(Function): events listener functions
18 // Object: the code object issuing application events
21 // DOMElement: the DOM element receiving browser events
24 // Array: list of support application events
28 * @param {OpenLayers.Map} map
29 * @param {DOMElement} div
31 initialize: function (object, div, eventTypes) {
35 this.eventTypes = eventTypes;
37 for (var i = 0; i < this.eventTypes.length; i++) {
38 // create a listener list for every custom application event
39 this.listeners[ this.eventTypes[i] ] = [];
42 for (var i = 0; i < this.BROWSER_EVENTS.length; i++) {
43 var eventType = this.BROWSER_EVENTS[i];
45 // every browser event has a corresponding application event
46 // (whether it's listened for or not).
47 this.listeners[ eventType ] = [];
49 Event.observe(div, eventType,
50 this.handleBrowserEvent.bindAsEventListener(this));
52 // disable dragstart in IE so that mousedown/move/up works normally
53 Event.observe(div, "dragstart", Event.stop);
59 * @param {Function} func
61 register: function (type, obj, func) {
66 var listeners = this.listeners[type];
67 listeners.push( {obj: obj, func: func} );
70 unregister: function (type, obj, func) {
71 var listeners = this.listeners[type];
72 for (var i = 0; i < listeners.length; i++) {
73 if (listeners[i].obj == obj && listeners[i].type == type) {
74 listeners.splice(i, 1);
80 remove: function(type) {
81 this.listeners[type].pop();
87 handleBrowserEvent: function (evt) {
88 evt.xy = this.getMousePosition(evt);
89 this.triggerEvent(evt.type, evt)
95 * @return {OpenLayers.Pixel}
97 getMousePosition: function (evt) {
98 if (!this.div.offsets) {
99 this.div.offsets = Position.page(this.div);
101 return new OpenLayers.Pixel(
102 evt.clientX - this.div.offsets[0],
103 evt.clientY - this.div.offsets[1]);
110 triggerEvent: function (type, evt) {
114 evt.object = this.object;
117 var listeners = this.listeners[type];
118 for (var i = 0; i < listeners.length; i++) {
119 var callback = listeners[i];
120 var continueChain = callback.func.call(callback.obj, evt);
121 if (continueChain != null && !continueChain) break;