Handler.js
4.12 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
/* Copyright (c) 2006 MetaCarta, Inc., published under the BSD license.
* See http://svn.openlayers.org/trunk/openlayers/release-license.txt
* for the full text of the license. */
/**
* Base class to construct a higher-level handler for event sequences.
* Handlers are created by controls, which ultimately have the responsibility
* of making changes to the map.
*
* @class
*/
OpenLayers.Handler = OpenLayers.Class.create();
OpenLayers.Handler.MOD_NONE = 0;
OpenLayers.Handler.MOD_SHIFT = 1;
OpenLayers.Handler.MOD_CTRL = 2;
OpenLayers.Handler.MOD_ALT = 4;
OpenLayers.Handler.prototype = {
/**
* @type String
* @private
*/
id: null,
/**
* The control that initialized this handler.
* @type OpenLayers.Control
* @private
*/
control: null,
/**
* @type OpenLayers.Map
* @private
*/
map: null,
/**
* @type integer
*/
// keyMask: OpenLayers.Handler.MOD_NONE,
keyMask: null,
/**
* @type Boolean
* @private
*/
active: false,
/**
* @constructor
*
* @param {OpenLayers.Control} control
* @param {Object} callbacks A hash of callback functions
* @param {Object} options
*/
initialize: function(control, callbacks, options) {
OpenLayers.Util.extend(this, options);
this.control = control;
this.callbacks = callbacks;
if (control.map) {
this.setMap(control.map);
}
OpenLayers.Util.extend(this, options);
this.id = OpenLayers.Util.createUniqueID(this.CLASS_NAME + "_");
},
setMap: function (map) {
this.map = map;
},
checkModifiers: function (evt) {
if(this.keyMask == null) {
return true;
}
/* calculate the keyboard modifier mask for this event */
var keyModifiers =
(evt.shiftKey ? OpenLayers.Handler.MOD_SHIFT : 0) |
(evt.ctrlKey ? OpenLayers.Handler.MOD_CTRL : 0) |
(evt.altKey ? OpenLayers.Handler.MOD_ALT : 0);
/* if it differs from the handler object's key mask,
bail out of the event handler */
return (keyModifiers == this.keyMask);
},
/**
* Turn on the handler. Returns false if the handler was already active.
*
* @type {Boolean}
*/
activate: function() {
if(this.active) {
return false;
}
// register for event handlers defined on this class.
var events = OpenLayers.Events.prototype.BROWSER_EVENTS;
for (var i = 0; i < events.length; i++) {
if (this[events[i]]) {
this.register(events[i], this[events[i]]);
}
}
this.active = true;
return true;
},
/**
* Turn off the handler. Returns false if the handler was already inactive.
*
* @type {Boolean}
*/
deactivate: function() {
if(!this.active) {
return false;
}
// unregister event handlers defined on this class.
var events = OpenLayers.Events.prototype.BROWSER_EVENTS;
for (var i = 0; i < events.length; i++) {
if (this[events[i]]) {
this.unregister(events[i], this[events[i]]);
}
}
this.active = false;
return true;
},
/**
* trigger the control's named callback with the given arguments
*/
callback: function (name, args) {
if (this.callbacks[name]) {
this.callbacks[name].apply(this.control, args);
}
},
/**
* register an event on the map
*/
register: function (name, method) {
// TODO: deal with registerPriority in 3.0
this.map.events.registerPriority(name, this, method);
},
/**
* unregister an event from the map
*/
unregister: function (name, method) {
this.map.events.unregister(name, this, method);
},
/**
*
*/
destroy: function () {
// eliminate circular references
this.control = this.map = null;
},
/** @final @type String */
CLASS_NAME: "OpenLayers.Handler"
};