Handler.js 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325
  1. /* Copyright (c) 2006-2013 by OpenLayers Contributors (see authors.txt for
  2. * full list of contributors). Published under the 2-clause BSD license.
  3. * See license.txt in the OpenLayers distribution or repository 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. * Property: touch
  82. * {Boolean} Indicates the support of touch events. When touch events are
  83. * started touch will be true and all mouse related listeners will do
  84. * nothing.
  85. */
  86. touch: false,
  87. /**
  88. * Constructor: OpenLayers.Handler
  89. * Construct a handler.
  90. *
  91. * Parameters:
  92. * control - {<OpenLayers.Control>} The control that initialized this
  93. * handler. The control is assumed to have a valid map property; that
  94. * map is used in the handler's own setMap method. If a map property
  95. * is present in the options argument it will be used instead.
  96. * callbacks - {Object} An object whose properties correspond to abstracted
  97. * events or sequences of browser events. The values for these
  98. * properties are functions defined by the control that get called by
  99. * the handler.
  100. * options - {Object} An optional object whose properties will be set on
  101. * the handler.
  102. */
  103. initialize: function(control, callbacks, options) {
  104. OpenLayers.Util.extend(this, options);
  105. this.control = control;
  106. this.callbacks = callbacks;
  107. var map = this.map || control.map;
  108. if (map) {
  109. this.setMap(map);
  110. }
  111. this.id = OpenLayers.Util.createUniqueID(this.CLASS_NAME + "_");
  112. },
  113. /**
  114. * Method: setMap
  115. */
  116. setMap: function (map) {
  117. this.map = map;
  118. },
  119. /**
  120. * Method: checkModifiers
  121. * Check the keyMask on the handler. If no <keyMask> is set, this always
  122. * returns true. If a <keyMask> is set and it matches the combination
  123. * of keys down on an event, this returns true.
  124. *
  125. * Returns:
  126. * {Boolean} The keyMask matches the keys down on an event.
  127. */
  128. checkModifiers: function (evt) {
  129. if(this.keyMask == null) {
  130. return true;
  131. }
  132. /* calculate the keyboard modifier mask for this event */
  133. var keyModifiers =
  134. (evt.shiftKey ? OpenLayers.Handler.MOD_SHIFT : 0) |
  135. (evt.ctrlKey ? OpenLayers.Handler.MOD_CTRL : 0) |
  136. (evt.altKey ? OpenLayers.Handler.MOD_ALT : 0) |
  137. (evt.metaKey ? OpenLayers.Handler.MOD_META : 0);
  138. /* if it differs from the handler object's key mask,
  139. bail out of the event handler */
  140. return (keyModifiers == this.keyMask);
  141. },
  142. /**
  143. * APIMethod: activate
  144. * Turn on the handler. Returns false if the handler was already active.
  145. *
  146. * Returns:
  147. * {Boolean} The handler was activated.
  148. */
  149. activate: function() {
  150. if(this.active) {
  151. return false;
  152. }
  153. // register for event handlers defined on this class.
  154. var events = OpenLayers.Events.prototype.BROWSER_EVENTS;
  155. for (var i=0, len=events.length; i<len; i++) {
  156. if (this[events[i]]) {
  157. this.register(events[i], this[events[i]]);
  158. }
  159. }
  160. this.active = true;
  161. return true;
  162. },
  163. /**
  164. * APIMethod: deactivate
  165. * Turn off the handler. Returns false if the handler was already inactive.
  166. *
  167. * Returns:
  168. * {Boolean} The handler was deactivated.
  169. */
  170. deactivate: function() {
  171. if(!this.active) {
  172. return false;
  173. }
  174. // unregister event handlers defined on this class.
  175. var events = OpenLayers.Events.prototype.BROWSER_EVENTS;
  176. for (var i=0, len=events.length; i<len; i++) {
  177. if (this[events[i]]) {
  178. this.unregister(events[i], this[events[i]]);
  179. }
  180. }
  181. this.touch = false;
  182. this.active = false;
  183. return true;
  184. },
  185. /**
  186. * Method: startTouch
  187. * Start touch events, this method must be called by subclasses in
  188. * "touchstart" method. When touch events are started <touch> will be
  189. * true and all mouse related listeners will do nothing.
  190. */
  191. startTouch: function() {
  192. if (!this.touch) {
  193. this.touch = true;
  194. var events = [
  195. "mousedown", "mouseup", "mousemove", "click", "dblclick",
  196. "mouseout"
  197. ];
  198. for (var i=0, len=events.length; i<len; i++) {
  199. if (this[events[i]]) {
  200. this.unregister(events[i], this[events[i]]);
  201. }
  202. }
  203. }
  204. },
  205. /**
  206. * Method: callback
  207. * Trigger the control's named callback with the given arguments
  208. *
  209. * Parameters:
  210. * name - {String} The key for the callback that is one of the properties
  211. * of the handler's callbacks object.
  212. * args - {Array(*)} An array of arguments (any type) with which to call
  213. * the callback (defined by the control).
  214. */
  215. callback: function (name, args) {
  216. if (name && this.callbacks[name]) {
  217. this.callbacks[name].apply(this.control, args);
  218. }
  219. },
  220. /**
  221. * Method: register
  222. * register an event on the map
  223. */
  224. register: function (name, method) {
  225. // TODO: deal with registerPriority in 3.0
  226. this.map.events.registerPriority(name, this, method);
  227. this.map.events.registerPriority(name, this, this.setEvent);
  228. },
  229. /**
  230. * Method: unregister
  231. * unregister an event from the map
  232. */
  233. unregister: function (name, method) {
  234. this.map.events.unregister(name, this, method);
  235. this.map.events.unregister(name, this, this.setEvent);
  236. },
  237. /**
  238. * Method: setEvent
  239. * With each registered browser event, the handler sets its own evt
  240. * property. This property can be accessed by controls if needed
  241. * to get more information about the event that the handler is
  242. * processing.
  243. *
  244. * This allows modifier keys on the event to be checked (alt, shift, ctrl,
  245. * and meta cannot be checked with the keyboard handler). For a
  246. * control to determine which modifier keys are associated with the
  247. * event that a handler is currently processing, it should access
  248. * (code)handler.evt.altKey || handler.evt.shiftKey ||
  249. * handler.evt.ctrlKey || handler.evt.metaKey(end).
  250. *
  251. * Parameters:
  252. * evt - {Event} The browser event.
  253. */
  254. setEvent: function(evt) {
  255. this.evt = evt;
  256. return true;
  257. },
  258. /**
  259. * Method: destroy
  260. * Deconstruct the handler.
  261. */
  262. destroy: function () {
  263. // unregister event listeners
  264. this.deactivate();
  265. // eliminate circular references
  266. this.control = this.map = null;
  267. },
  268. CLASS_NAME: "OpenLayers.Handler"
  269. });
  270. /**
  271. * Constant: OpenLayers.Handler.MOD_NONE
  272. * If set as the <keyMask>, <checkModifiers> returns false if any key is down.
  273. */
  274. OpenLayers.Handler.MOD_NONE = 0;
  275. /**
  276. * Constant: OpenLayers.Handler.MOD_SHIFT
  277. * If set as the <keyMask>, <checkModifiers> returns false if Shift is down.
  278. */
  279. OpenLayers.Handler.MOD_SHIFT = 1;
  280. /**
  281. * Constant: OpenLayers.Handler.MOD_CTRL
  282. * If set as the <keyMask>, <checkModifiers> returns false if Ctrl is down.
  283. */
  284. OpenLayers.Handler.MOD_CTRL = 2;
  285. /**
  286. * Constant: OpenLayers.Handler.MOD_ALT
  287. * If set as the <keyMask>, <checkModifiers> returns false if Alt is down.
  288. */
  289. OpenLayers.Handler.MOD_ALT = 4;
  290. /**
  291. * Constant: OpenLayers.Handler.MOD_META
  292. * If set as the <keyMask>, <checkModifiers> returns false if Cmd is down.
  293. */
  294. OpenLayers.Handler.MOD_META = 8;