controller.js 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  1. /**
  2. * TraCINg-Server - Gathering and visualizing cyber incidents on the world
  3. *
  4. * Copyright 2013 Matthias Gazzari, Annemarie Mattmann, André Wolski
  5. *
  6. * Licensed under the Apache License, Version 2.0 (the "License");
  7. * you may not use this file except in compliance with the License.
  8. * You may obtain a copy of the License at
  9. *
  10. * http://www.apache.org/licenses/LICENSE-2.0
  11. *
  12. * Unless required by applicable law or agreed to in writing, software
  13. * distributed under the License is distributed on an "AS IS" BASIS,
  14. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  15. * See the License for the specific language governing permissions and
  16. * limitations under the License.
  17. */
  18. var Controller = function() {
  19. // attributes and constants
  20. var modifier;
  21. var args;
  22. var zoom;
  23. var move;
  24. var toggle;
  25. var enabled;
  26. /**
  27. * Init variables and set event listener
  28. */
  29. function init(obj) {
  30. // init modifier map
  31. modifier = {};
  32. // init enabled
  33. enabled = true;
  34. // set possible arguments used by callbacks
  35. args = {
  36. IN: 0,
  37. OUT: 1,
  38. UP: 2,
  39. DOWN: 3,
  40. LEFT: 4,
  41. RIGHT: 5
  42. };
  43. // initalize keyboard events
  44. document.addEventListener('keydown', onDocumentKeyDown, false);
  45. document.addEventListener('keyup', onDocumentKeyUp, false);
  46. obj.args = args;
  47. }
  48. /**
  49. * This function gets called if a key is released
  50. */
  51. function onDocumentKeyUp(event) {
  52. var keyCode = event.keyCode;
  53. // check for modifier keys
  54. if(keyCode == 17)
  55. modifier["STRG"] = false;
  56. if(keyCode == 18)
  57. modifier["ALT"] = false;
  58. if(keyCode == 91)
  59. modifier["SUPER"] = false;
  60. if(keyCode == 225)
  61. modifier["ALT_GR"] = false;
  62. }
  63. /**
  64. * This function gets called if a key is pressed.
  65. */
  66. function onDocumentKeyDown(event) {
  67. var keyCode = event.keyCode;
  68. // check modifier keys
  69. if(keyCode == 17)
  70. modifier["STRG"] = true;
  71. if(keyCode == 18)
  72. modifier["ALT"] = true;
  73. if(keyCode == 91)
  74. modifier["SUPER"] = true;
  75. if(keyCode == 225)
  76. modifier["ALT_GR"] = true;
  77. // check if one ore more modifiers are enabled
  78. var modifierEnabled = false;
  79. for(var key in modifier) {
  80. modifierEnabled |= modifier[key];
  81. }
  82. // try to execute an action if no modifiers are enabled and
  83. // the control is enabled
  84. if(!modifierEnabled && enabled) {
  85. switch(keyCode) {
  86. case 38:
  87. // arrow up: zoom in
  88. if (zoom != undefined) {
  89. event.preventDefault();
  90. zoom(args.IN);
  91. }
  92. break;
  93. case 40:
  94. // arrow down: zoom out
  95. if (zoom != undefined) {
  96. event.preventDefault();
  97. zoom(args.OUT);
  98. }
  99. break;
  100. case 87:
  101. // w: rotate the globe up, move maps up
  102. if (move != undefined) {
  103. event.preventDefault();
  104. move(args.UP);
  105. }
  106. break;
  107. case 65:
  108. // a: rotate the globe to the left, move maps left
  109. if (move != undefined) {
  110. event.preventDefault();
  111. move(args.LEFT);
  112. }
  113. break;
  114. case 83:
  115. // s: rotate the globe down, move maps down
  116. if (move != undefined) {
  117. event.preventDefault();
  118. move(args.DOWN);
  119. }
  120. break;
  121. case 68:
  122. // d: rotate the globe to the right, move maps right
  123. if (move != undefined) {
  124. event.preventDefault();
  125. move(args.RIGHT);
  126. }
  127. break;
  128. case 84:
  129. // t: toggle heatmap/map view (globe)
  130. if (toggle != undefined) {
  131. event.preventDefault();
  132. toggle();
  133. }
  134. break;
  135. }
  136. }
  137. }
  138. /**
  139. * Set the callbacks executed by their specific keyDown events
  140. */
  141. this.registerCallbacks = function(callbacks) {
  142. zoom = callbacks.zoom;
  143. move = callbacks.move;
  144. toggle = callbacks.toggle;
  145. }
  146. /**
  147. * Reset the callbacks executed by their specific keyDown events
  148. */
  149. this.unregisterCallbacks = function() {
  150. zoom = undefined;
  151. move = undefined;
  152. toggle = undefined;
  153. }
  154. /**
  155. * Toggle whether the key control is enabled or not
  156. */
  157. this.toggleEnabled = function() {
  158. enabled = !enabled;
  159. return enabled;
  160. }
  161. // init control
  162. init(this);
  163. }