Handler.js 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288
  1. /* Copyright (c) 2006-2011 by OpenLayers Contributors (see authors.txt for
  2. * full list of contributors). Published under the Clear BSD license.
  3. * See http://svn.openlayers.org/trunk/openlayers/license.txt for the
  4. * full text of the license. */
  5. /**
  6. * @requires OpenLayers/BaseTypes/Class.js
  7. * @requires OpenLayers/Events.js
  8. */
  9. /**
  10. * Class: OpenLayers.Handler
  11. * Base class to construct a higher-level handler for event sequences. All
  12. * handlers have activate and deactivate methods. In addition, they have
  13. * methods named like browser events. When a handler is activated, any
  14. * additional methods named like a browser event is registered as a
  15. * listener for the corresponding event. When a handler is deactivated,
  16. * those same methods are unregistered as event listeners.
  17. *
  18. * Handlers also typically have a callbacks object with keys named like
  19. * the abstracted events or event sequences that they are in charge of
  20. * handling. The controls that wrap handlers define the methods that
  21. * correspond to these abstract events - so instead of listening for
  22. * individual browser events, they only listen for the abstract events
  23. * defined by the handler.
  24. *
  25. * Handlers are created by controls, which ultimately have the responsibility
  26. * of making changes to the the state of the application. Handlers
  27. * themselves may make temporary changes, but in general are expected to
  28. * return the application in the same state that they found it.
  29. */
  30. OpenLayers.Handler = OpenLayers.Class({
  31. /**
  32. * Property: id
  33. * {String}
  34. */
  35. id: null,
  36. /**
  37. * APIProperty: control
  38. * {<OpenLayers.Control>}. The control that initialized this handler. The
  39. * control is assumed to have a valid map property - that map is used
  40. * in the handler's own setMap method.
  41. */
  42. control: null,
  43. /**
  44. * Property: map
  45. * {<OpenLayers.Map>}
  46. */
  47. map: null,
  48. /**
  49. * APIProperty: keyMask
  50. * {Integer} Use bitwise operators and one or more of the OpenLayers.Handler
  51. * constants to construct a keyMask. The keyMask is used by
  52. * <checkModifiers>. If the keyMask matches the combination of keys
  53. * down on an event, checkModifiers returns true.
  54. *
  55. * Example:
  56. * (code)
  57. * // handler only responds if the Shift key is down
  58. * handler.keyMask = OpenLayers.Handler.MOD_SHIFT;
  59. *
  60. * // handler only responds if Ctrl-Shift is down
  61. * handler.keyMask = OpenLayers.Handler.MOD_SHIFT |
  62. * OpenLayers.Handler.MOD_CTRL;
  63. * (end)
  64. */
  65. keyMask: null,
  66. /**
  67. * Property: active
  68. * {Boolean}
  69. */
  70. active: false,
  71. /**
  72. * Property: evt
  73. * {Event} This property references the last event handled by the handler.
  74. * Note that this property is not part of the stable API. Use of the
  75. * evt property should be restricted to controls in the library
  76. * or other applications that are willing to update with changes to
  77. * the OpenLayers code.
  78. */
  79. evt: null,
  80. /**
  81. * Constructor: OpenLayers.Handler
  82. * Construct a handler.
  83. *
  84. * Parameters:
  85. * control - {<OpenLayers.Control>} The control that initialized this
  86. * handler. The control is assumed to have a valid map property; that
  87. * map is used in the handler's own setMap method. If a map property
  88. * is present in the options argument it will be used instead.
  89. * callbacks - {Object} An object whose properties correspond to abstracted
  90. * events or sequences of browser events. The values for these
  91. * properties are functions defined by the control that get called by
  92. * the handler.
  93. * options - {Object} An optional object whose properties will be set on
  94. * the handler.
  95. */
  96. initialize: function(control, callbacks, options) {
  97. OpenLayers.Util.extend(this, options);
  98. this.control = control;
  99. this.callbacks = callbacks;
  100. var map = this.map || control.map;
  101. if (map) {
  102. this.setMap(map);
  103. }
  104. this.id = OpenLayers.Util.createUniqueID(this.CLASS_NAME + "_");
  105. },
  106. /**
  107. * Method: setMap
  108. */
  109. setMap: function (map) {
  110. this.map = map;
  111. },
  112. /**
  113. * Method: checkModifiers
  114. * Check the keyMask on the handler. If no <keyMask> is set, this always
  115. * returns true. If a <keyMask> is set and it matches the combination
  116. * of keys down on an event, this returns true.
  117. *
  118. * Returns:
  119. * {Boolean} The keyMask matches the keys down on an event.
  120. */
  121. checkModifiers: function (evt) {
  122. if(this.keyMask == null) {
  123. return true;
  124. }
  125. /* calculate the keyboard modifier mask for this event */
  126. var keyModifiers =
  127. (evt.shiftKey ? OpenLayers.Handler.MOD_SHIFT : 0) |
  128. (evt.ctrlKey ? OpenLayers.Handler.MOD_CTRL : 0) |
  129. (evt.altKey ? OpenLayers.Handler.MOD_ALT : 0);
  130. /* if it differs from the handler object's key mask,
  131. bail out of the event handler */
  132. return (keyModifiers == this.keyMask);
  133. },
  134. /**
  135. * APIMethod: activate
  136. * Turn on the handler. Returns false if the handler was already active.
  137. *
  138. * Returns:
  139. * {Boolean} The handler was activated.
  140. */
  141. activate: function() {
  142. if(this.active) {
  143. return false;
  144. }
  145. // register for event handlers defined on this class.
  146. var events = OpenLayers.Events.prototype.BROWSER_EVENTS;
  147. for (var i=0, len=events.length; i<len; i++) {
  148. if (this[events[i]]) {
  149. this.register(events[i], this[events[i]]);
  150. }
  151. }
  152. this.active = true;
  153. return true;
  154. },
  155. /**
  156. * APIMethod: deactivate
  157. * Turn off the handler. Returns false if the handler was already inactive.
  158. *
  159. * Returns:
  160. * {Boolean} The handler was deactivated.
  161. */
  162. deactivate: function() {
  163. if(!this.active) {
  164. return false;
  165. }
  166. // unregister event handlers defined on this class.
  167. var events = OpenLayers.Events.prototype.BROWSER_EVENTS;
  168. for (var i=0, len=events.length; i<len; i++) {
  169. if (this[events[i]]) {
  170. this.unregister(events[i], this[events[i]]);
  171. }
  172. }
  173. this.active = false;
  174. return true;
  175. },
  176. /**
  177. * Method: callback
  178. * Trigger the control's named callback with the given arguments
  179. *
  180. * Parameters:
  181. * name - {String} The key for the callback that is one of the properties
  182. * of the handler's callbacks object.
  183. * args - {Array(*)} An array of arguments (any type) with which to call
  184. * the callback (defined by the control).
  185. */
  186. callback: function (name, args) {
  187. if (name && this.callbacks[name]) {
  188. this.callbacks[name].apply(this.control, args);
  189. }
  190. },
  191. /**
  192. * Method: register
  193. * register an event on the map
  194. */
  195. register: function (name, method) {
  196. // TODO: deal with registerPriority in 3.0
  197. this.map.events.registerPriority(name, this, method);
  198. this.map.events.registerPriority(name, this, this.setEvent);
  199. },
  200. /**
  201. * Method: unregister
  202. * unregister an event from the map
  203. */
  204. unregister: function (name, method) {
  205. this.map.events.unregister(name, this, method);
  206. this.map.events.unregister(name, this, this.setEvent);
  207. },
  208. /**
  209. * Method: setEvent
  210. * With each registered browser event, the handler sets its own evt
  211. * property. This property can be accessed by controls if needed
  212. * to get more information about the event that the handler is
  213. * processing.
  214. *
  215. * This allows modifier keys on the event to be checked (alt, shift,
  216. * and ctrl cannot be checked with the keyboard handler). For a
  217. * control to determine which modifier keys are associated with the
  218. * event that a handler is currently processing, it should access
  219. * (code)handler.evt.altKey || handler.evt.shiftKey ||
  220. * handler.evt.ctrlKey(end).
  221. *
  222. * Parameters:
  223. * evt - {Event} The browser event.
  224. */
  225. setEvent: function(evt) {
  226. this.evt = evt;
  227. return true;
  228. },
  229. /**
  230. * Method: destroy
  231. * Deconstruct the handler.
  232. */
  233. destroy: function () {
  234. // unregister event listeners
  235. this.deactivate();
  236. // eliminate circular references
  237. this.control = this.map = null;
  238. },
  239. CLASS_NAME: "OpenLayers.Handler"
  240. });
  241. /**
  242. * Constant: OpenLayers.Handler.MOD_NONE
  243. * If set as the <keyMask>, <checkModifiers> returns false if any key is down.
  244. */
  245. OpenLayers.Handler.MOD_NONE = 0;
  246. /**
  247. * Constant: OpenLayers.Handler.MOD_SHIFT
  248. * If set as the <keyMask>, <checkModifiers> returns false if Shift is down.
  249. */
  250. OpenLayers.Handler.MOD_SHIFT = 1;
  251. /**
  252. * Constant: OpenLayers.Handler.MOD_CTRL
  253. * If set as the <keyMask>, <checkModifiers> returns false if Ctrl is down.
  254. */
  255. OpenLayers.Handler.MOD_CTRL = 2;
  256. /**
  257. * Constant: OpenLayers.Handler.MOD_ALT
  258. * If set as the <keyMask>, <checkModifiers> returns false if Alt is down.
  259. */
  260. OpenLayers.Handler.MOD_ALT = 4;