window_event.js 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232
  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, row; row = table.rows[i]; i++){
  26. // Is the current row the parent row of the active element?
  27. if( row == activeElement.parentNode.parentNode ){
  28. if( currentKeys[ KEY_SHIFT ] == 1){
  29. if( i == 0 ){
  30. table.rows[ table.rows.length - 1 ].cells[1].firstChild.focus();
  31. } else {
  32. table.rows[i - 1].cells[1].firstChild.focus();
  33. }
  34. } else {
  35. if( table.rows[i + 1] == null ){
  36. table.rows[0].cells[1].firstChild.focus();
  37. } else {
  38. table.rows[i + 1].cells[1].firstChild.focus();
  39. }
  40. }
  41. // No reason to keep looping, so stop the process
  42. break;
  43. }
  44. }
  45. }
  46. }
  47. };
  48. /**
  49. * A generic method to process events
  50. */
  51. this.processEvent = function(index, value, eventType, event) {
  52. if( index != null )
  53. currentKeys[ index ] = value;
  54. if( eventType != null )
  55. BehaviorManager.handleUserEvent( eventType, event );
  56. };
  57. /**
  58. * The default onLoadFunction function. This sets up the initial
  59. * behavior of the canvas
  60. * When the page is loaded, either present the login screen
  61. * or initiate the client
  62. */
  63. this.onLoad = function(){
  64. console.debug = function(){}; //comment to enable debugging messages
  65. if( ! UserManagement.isUserLoggedIn() )
  66. WindowManagement.showLoginScreen();
  67. else{
  68. __user = window.localStorage.getItem('user');
  69. __initClient();
  70. }
  71. $('#mainInput').focus();
  72. };
  73. /**
  74. * The default oncontextmenu function.
  75. * Disables the right click context menu
  76. */
  77. this.onContextMenu = function() {
  78. return false;
  79. };
  80. /**
  81. * Process the mouse down event
  82. * Ignore the right click by default
  83. */
  84. this.onMouseDown = function(event){
  85. if (event.button == MOUSE_RIGHT) {
  86. return false;
  87. }
  88. // block clicks on the background when the dialog is displayed
  89. if( $(event.target).attr("id") == 'div_dim_bg' )
  90. ;
  91. else if( $(event.target).parent().attr("id") == 'div_canvas' )
  92. if( event.button == MOUSE_LEFT)
  93. processEvent( null, null, __EVENT_LEFT_PRESS_CANVAS, event);
  94. };
  95. /**
  96. * Process the mouse up event
  97. */
  98. this.onMouseUp = function(event){
  99. // block clicks on the background when the dialog is displayed
  100. if( $(event.target).attr("id") == 'div_dim_bg')
  101. ;
  102. else if( $(event.target).parent().attr("id") == 'div_canvas' )
  103. {
  104. if( event.button == MOUSE_LEFT )
  105. processEvent( null, null, __EVENT_LEFT_RELEASE_CANVAS, event);
  106. else if( event.button == MOUSE_RIGHT )
  107. processEvent( null, null, __EVENT_RIGHT_RELEASE_CANVAS, event);
  108. else if( event.button == MOUSE_MIDDLE )
  109. processEvent( null, null, __EVENT_MIDDLE_RELEASE_CANVAS, event);
  110. }
  111. };
  112. /**
  113. * Process the mouse move event. Only process the event
  114. * if the canvas is the target of the movement
  115. */
  116. this.onMouseMove = function(event){
  117. if( BehaviorManager.isStatechartLoaded() ){
  118. if( __isCanvasElement( $(event.target)) )
  119. processEvent( null, null, __EVENT_MOUSE_MOVE, event);
  120. } else {
  121. console.warn( "Cannot process the event until the state chart has been loaded.");
  122. }
  123. };
  124. /**
  125. * Process the key down event
  126. */
  127. this.onKeyDown = function(event){
  128. if( event.keyCode == KEY_SHIFT ){
  129. processEvent( KEY_SHIFT, 1, null, event);
  130. } else if( event.keyCode == KEY_CTRL ){
  131. processEvent( KEY_CTRL, 1, null, event);
  132. } else if( event.keyCode == KEY_ALT ){
  133. processEvent( KEY_ALT, 1, null, event);
  134. } else if( event.keyCode == KEY_DEL ){
  135. processEvent( KEY_DEL, 1, null, event);
  136. } else if( event.keyCode == KEY_INS ){
  137. processEvent( KEY_INS, 1, null, event);
  138. } else if( event.keyCode == KEY_ESC ){
  139. processEvent( KEY_ESC, 1, null, event);
  140. } else if( event.keyCode == KEY_TAB ){
  141. processEvent( KEY_TAB, 1, null, event);
  142. // Only process the tab event here if the popup dialog is displayed
  143. if( __dialog_stack.length ){
  144. tabThroughPopupInputs();
  145. }
  146. if( $('#div_login').css("display") == "block" ) { // HUSEYIN-LOGIN
  147. return true;
  148. }
  149. // This default behavior is to stop the tab here, in order
  150. // to keep other DOM elements from being selected
  151. event.stopPropagation();
  152. event.preventDefault();
  153. return false;
  154. } else if( event.keyCode == KEY_ENTER ){
  155. processEvent( KEY_ENTER, 1, null, event);
  156. if( $('#div_login').css("display") == "block" ) { // HUSEYIN-LOGIN
  157. UserManagement.validateCredentials($('#input_username').val(), $('#input_password').val());
  158. }
  159. }
  160. };
  161. /**
  162. * Process the key up event
  163. */
  164. this.onKeyUp = function(event){
  165. if( event.keyCode == KEY_SHIFT ){
  166. processEvent( KEY_SHIFT, 0, __EVENT_KEYUP_SHIFT, event);
  167. } else if( event.keyCode == KEY_CTRL ){
  168. processEvent( KEY_CTRL, 0, __EVENT_KEYUP_CTRL, event);
  169. } else if( event.keyCode == KEY_ALT ){
  170. processEvent( KEY_ALT, 0, __EVENT_KEYUP_ALT, event);
  171. } else if( event.keyCode == KEY_TAB ){
  172. processEvent( KEY_TAB, 0, __EVENT_KEYUP_TAB, event);
  173. } else if( event.keyCode == KEY_DEL ){
  174. processEvent( KEY_DEL, 0, __EVENT_KEYUP_DEL, event);
  175. } else if( event.keyCode == KEY_INS ){
  176. processEvent( KEY_INS, 0, __EVENT_KEYUP_INS, event);
  177. } else if( event.keyCode == KEY_CMD1 || event.keyCode == KEY_CMD2 || event.keyCode == KEY_CMD3 ){
  178. processEvent( event.keyCode, 0, __EVENT_KEYUP_DEL, event);
  179. } else if( event.keyCode == KEY_ESC ){
  180. processEvent( KEY_ESC, 0, __EVENT_KEYUP_ESC, event);
  181. } else if( event.keyCode == KEY_ENTER ) {
  182. if (currentKeys[KEY_SHIFT]==undefined || currentKeys[KEY_SHIFT]==0)
  183. processEvent( KEY_ENTER, 0, __EVENT_KEYUP_ENTER, event);
  184. }
  185. };
  186. /**
  187. * Determines whether the current key is pressed or not
  188. */
  189. this.isKeyPressed = function( keyCode ){
  190. return currentKeys[ keyCode ] == 1;
  191. };
  192. /**
  193. * Initializes the default window behavior
  194. */
  195. this.initDefault = function(){
  196. window.onload = this.onLoad;
  197. window.oncontextmenu = this.onContextMenu;
  198. window.onmousedown = this.onMouseDown;
  199. window.onmouseup = this.onMouseUp;
  200. window.onmousemove = this.onMouseMove;
  201. window.onkeydown = this.onKeyDown;
  202. window.onkeyup = this.onKeyUp;
  203. };
  204. return this;
  205. }();
  206. /**
  207. * Initiate the default Window Event actions
  208. */
  209. WindowEventHelper.initDefault();