]> git.openstreetmap.org Git - rails.git/blob - public/lib/OpenLayers/Events.js
back out the registration of the gpx mime type, as it didn't seem work
[rails.git] / public / lib / OpenLayers / Events.js
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();
5
6 OpenLayers.Events.prototype = {
7     // Array: supported events
8     BROWSER_EVENTS: [
9         "mouseover", "mouseout",
10         "mousedown", "mouseup", "mousemove", 
11         "click", "dblclick",
12         "resize", "focus", "blur"
13     ],
14
15     // hash of Array(Function): events listener functions
16     listeners: null,
17
18     // Object: the code object issuing application events
19     object: null,
20
21     // DOMElement: the DOM element receiving browser events
22     div: null,
23
24     // Array: list of support application events
25     eventTypes: null,
26
27     /**
28     * @param {OpenLayers.Map} map
29     * @param {DOMElement} div
30     */
31     initialize: function (object, div, eventTypes) {
32         this.listeners  = {};
33         this.object     = object;
34         this.div        = div;
35         this.eventTypes = eventTypes;
36         if (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] ] = [];
40             }
41         }
42         for (var i = 0; i < this.BROWSER_EVENTS.length; i++) {
43             var eventType = this.BROWSER_EVENTS[i];
44
45             // every browser event has a corresponding application event 
46             // (whether it's listened for or not).
47             this.listeners[ eventType ] = [];
48
49             Event.observe(div, eventType, 
50                 this.handleBrowserEvent.bindAsEventListener(this));
51         }
52         // disable dragstart in IE so that mousedown/move/up works normally
53         Event.observe(div, "dragstart", Event.stop);
54     },
55
56     /**
57     * @param {str} type
58     * @param {Object} obj
59     * @param {Function} func
60     */
61     register: function (type, obj, func) {
62         if (func == null) {
63             obj = this.object;
64             func = obj;
65         }
66         var listeners = this.listeners[type];
67         listeners.push( {obj: obj, func: func} );
68     },
69     
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);
75                 break;
76             }
77         }
78     },
79
80     remove: function(type) {
81         this.listeners[type].pop();
82     },
83
84     /**
85     * @param {event} evt
86     */
87     handleBrowserEvent: function (evt) {
88         evt.xy = this.getMousePosition(evt); 
89         this.triggerEvent(evt.type, evt)
90     },
91
92     /**
93     * @param {event} evt
94     * 
95     * @return {OpenLayers.Pixel}
96     */
97     getMousePosition: function (evt) {
98         if (!this.div.offsets) {
99             this.div.offsets = Position.page(this.div);
100         }
101         return new OpenLayers.Pixel(
102                         evt.clientX - this.div.offsets[0], 
103                         evt.clientY - this.div.offsets[1]); 
104     },
105
106     /**
107     * @param {str} type
108     * @param {event} evt
109     */
110     triggerEvent: function (type, evt) {
111         if (evt == null) {
112             evt = {};
113         }
114         evt.object = this.object;
115         evt.div = this.div;
116
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;
122         }
123     }
124 };