window_event.js 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245
  1. /* This file is part of AToMPM - A Tool for Multi-Paradigm Modelling
  2. * Copyright 2011 by the AToMPM team and licensed under the LGPL
  3. * See COPYING.lesser and README.md in the root of this project for full details
  4. */
  5. /**
  6. * The WindowEventHelper function is a helper function that holds all of
  7. * the helpers methods for the window_event javascript file.
  8. *
  9. * @returns the WindowEventHelper object
  10. */
  11. WindowEventHelper = function(){
  12. /**
  13. * Allows for the user to tab through input elements when the
  14. * div_dialog popup is currently displayed
  15. */
  16. this.tabThroughPopupInputs = function() {
  17. // Get the currently focused element
  18. var activeElement = document.activeElement;
  19. // Only move forward if this isn't null
  20. if( activeElement != null) {
  21. // We're in a table, so we need to get the td, then the tr, then the table
  22. var table = activeElement.parentNode.parentNode.parentNode;
  23. // Are we actually in the table?
  24. if( table != null && table.rows != undefined){
  25. for(var i=0; i < table.length; i++){
  26. let row = table.rows[i];
  27. // Is the current row the parent row of the active element?
  28. if( row == activeElement.parentNode.parentNode ){
  29. if( currentKeys[ KEY_SHIFT ] == 1){
  30. if( i == 0 ){
  31. table.rows[ table.rows.length - 1 ].cells[1].firstChild.focus();
  32. } else {
  33. table.rows[i - 1].cells[1].firstChild.focus();
  34. }
  35. } else {
  36. if( table.rows[i + 1] == null ){
  37. table.rows[0].cells[1].firstChild.focus();
  38. } else {
  39. table.rows[i + 1].cells[1].firstChild.focus();
  40. }
  41. }
  42. // No reason to keep looping, so stop the process
  43. break;
  44. }
  45. }
  46. }
  47. }
  48. };
  49. /**
  50. * A generic method to process events
  51. */
  52. this.processEvent = function(index, value, eventType, event) {
  53. if( index != null )
  54. currentKeys[ index ] = value;
  55. if( eventType != null )
  56. BehaviorManager.handleUserEvent( eventType, event );
  57. };
  58. /**
  59. * The default onLoadFunction function. This sets up the initial
  60. * behavior of the canvas
  61. * When the page is loaded, either present the login screen
  62. * or initiate the client
  63. */
  64. this.onLoad = function(){
  65. console.debug = function(){}; //comment to enable debugging messages
  66. if( ! UserManagement.isUserLoggedIn() )
  67. WindowManagement.showLoginScreen();
  68. else{
  69. __user = window.localStorage.getItem('user');
  70. __initClient();
  71. }
  72. $('#mainInput').focus();
  73. };
  74. /**
  75. * The default oncontextmenu function.
  76. * Disables the right click context menu
  77. */
  78. this.onContextMenu = function() {
  79. return false;
  80. };
  81. /**
  82. * Process the mouse down event
  83. * Ignore the right click by default
  84. */
  85. this.onMouseDown = function(event){
  86. if (event.button == MOUSE_RIGHT) {
  87. return false;
  88. }
  89. // block clicks on the background when the dialog is displayed
  90. if( $(event.target).attr("id") == 'div_dim_bg' )
  91. ;
  92. else if( $(event.target).parent().attr("id") == 'div_canvas' )
  93. if( event.button == MOUSE_LEFT)
  94. processEvent( null, null, __EVENT_LEFT_PRESS_CANVAS, event);
  95. };
  96. /**
  97. * Process the mouse up event
  98. */
  99. this.onMouseUp = function(event){
  100. // block clicks on the background when the dialog is displayed
  101. if( $(event.target).attr("id") == 'div_dim_bg')
  102. ;
  103. else if( $(event.target).parent().attr("id") == 'div_canvas' )
  104. {
  105. if( event.button == MOUSE_LEFT )
  106. processEvent( null, null, __EVENT_LEFT_RELEASE_CANVAS, event);
  107. else if( event.button == MOUSE_RIGHT )
  108. processEvent( null, null, __EVENT_RIGHT_RELEASE_CANVAS, event);
  109. else if( event.button == MOUSE_MIDDLE )
  110. processEvent( null, null, __EVENT_MIDDLE_RELEASE_CANVAS, event);
  111. }
  112. };
  113. /**
  114. * Process the mouse move event. Only process the event
  115. * if the canvas is the target of the movement
  116. */
  117. this.onMouseMove = function(event){
  118. if( BehaviorManager.isStatechartLoaded() ){
  119. if( __isCanvasElement( $(event.target)) )
  120. processEvent( null, null, __EVENT_MOUSE_MOVE, event);
  121. } else {
  122. console.warn( "Cannot process the event until the state chart has been loaded.");
  123. }
  124. };
  125. /**
  126. * Process the key down event
  127. */
  128. this.onKeyDown = function(event){
  129. if( event.keyCode == KEY_SHIFT ){
  130. processEvent( KEY_SHIFT, 1, null, event);
  131. } else if( event.keyCode == KEY_CTRL ){
  132. processEvent( KEY_CTRL, 1, null, event);
  133. } else if( event.keyCode == KEY_ALT ){
  134. processEvent( KEY_ALT, 1, null, event);
  135. } else if( event.keyCode == KEY_DEL ){
  136. processEvent( KEY_DEL, 1, null, event);
  137. } else if( event.keyCode == KEY_INS ){
  138. processEvent( KEY_INS, 1, null, event);
  139. } else if( event.keyCode == KEY_ESC ){
  140. processEvent( KEY_ESC, 1, null, event);
  141. } else if( event.keyCode == KEY_TAB ){
  142. processEvent( KEY_TAB, 1, null, event);
  143. // Only process the tab event here if the popup dialog is displayed
  144. if( __dialog_stack.length ){
  145. tabThroughPopupInputs();
  146. }
  147. if( $('#div_login').css("display") == "block" ) { // HUSEYIN-LOGIN
  148. return true;
  149. }
  150. // This default behavior is to stop the tab here, in order
  151. // to keep other DOM elements from being selected
  152. event.stopPropagation();
  153. event.preventDefault();
  154. return false;
  155. } else if( event.keyCode == KEY_ENTER ){
  156. processEvent( KEY_ENTER, 1, null, event);
  157. if( $('#div_login').css("display") == "block" ) { // HUSEYIN-LOGIN
  158. UserManagement.validateCredentials($('#input_username').val(), $('#input_password').val());
  159. }
  160. }
  161. };
  162. /**
  163. * Process the key up event
  164. */
  165. this.onKeyUp = function(event){
  166. if( event.keyCode == KEY_SHIFT ){
  167. processEvent( KEY_SHIFT, 0, __EVENT_KEYUP_SHIFT, event);
  168. } else if( event.keyCode == KEY_CTRL ){
  169. processEvent( KEY_CTRL, 0, __EVENT_KEYUP_CTRL, event);
  170. } else if( event.keyCode == KEY_ALT ){
  171. processEvent( KEY_ALT, 0, __EVENT_KEYUP_ALT, event);
  172. } else if( event.keyCode == KEY_TAB ){
  173. processEvent( KEY_TAB, 0, __EVENT_KEYUP_TAB, event);
  174. } else if( event.keyCode == KEY_DEL ){
  175. processEvent( KEY_DEL, 0, __EVENT_KEYUP_DEL, event);
  176. } else if( event.keyCode == KEY_INS ){
  177. processEvent( KEY_INS, 0, __EVENT_KEYUP_INS, event);
  178. } else if( event.keyCode == KEY_CMD1 || event.keyCode == KEY_CMD2 || event.keyCode == KEY_CMD3 ){
  179. processEvent( event.keyCode, 0, __EVENT_KEYUP_DEL, event);
  180. } else if( event.keyCode == KEY_ESC ){
  181. processEvent( KEY_ESC, 0, __EVENT_KEYUP_ESC, event);
  182. } else if( event.keyCode == KEY_ENTER ) {
  183. if (currentKeys[KEY_SHIFT]==undefined || currentKeys[KEY_SHIFT]==0)
  184. processEvent( KEY_ENTER, 0, __EVENT_KEYUP_ENTER, event);
  185. }
  186. };
  187. /**
  188. * Determines whether the current key is pressed or not
  189. */
  190. this.isKeyPressed = function( keyCode ){
  191. return currentKeys[ keyCode ] == 1;
  192. };
  193. /**
  194. * Called when the window gains focus
  195. * Resets all keys
  196. */
  197. this.onfocus = function(){
  198. for (let keyCode in currentKeys){
  199. currentKeys[ keyCode ] = 0;
  200. }
  201. };
  202. /**
  203. * Initializes the default window behavior
  204. */
  205. this.initDefault = function(){
  206. window.onload = this.onLoad;
  207. window.oncontextmenu = this.onContextMenu;
  208. window.onmousedown = this.onMouseDown;
  209. window.onmouseup = this.onMouseUp;
  210. window.onmousemove = this.onMouseMove;
  211. window.onkeydown = this.onKeyDown;
  212. window.onkeyup = this.onKeyUp;
  213. window.onfocus = this.onfocus;
  214. };
  215. return this;
  216. }();
  217. /**
  218. * Initiate the default Window Event actions
  219. */
  220. WindowEventHelper.initDefault();