window_event.js 7.7 KB

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