keyhandler.js 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. /* vim: set expandtab sw=4 ts=4 sts=4: */
  2. // global var that holds: 0- if ctrl key is not pressed 1- if ctrl key is pressed
  3. var ctrlKeyHistory = 0;
  4. /**
  5. * Allows moving around inputs/select by Ctrl+arrows
  6. *
  7. * @param object event data
  8. */
  9. function onKeyDownArrowsHandler (e) {
  10. e = e || window.event;
  11. var o = (e.srcElement || e.target);
  12. if (!o) {
  13. return;
  14. }
  15. if (o.tagName !== 'TEXTAREA' && o.tagName !== 'INPUT' && o.tagName !== 'SELECT') {
  16. return;
  17. }
  18. if ((e.which !== 17) && (e.which !== 37) && (e.which !== 38) && (e.which !== 39) && (e.which !== 40)) {
  19. return;
  20. }
  21. if (!o.id) {
  22. return;
  23. }
  24. if (e.type === 'keyup') {
  25. if (e.which === 17) {
  26. ctrlKeyHistory = 0;
  27. }
  28. return;
  29. } else if (e.type === 'keydown') {
  30. if (e.which === 17) {
  31. ctrlKeyHistory = 1;
  32. }
  33. }
  34. if (ctrlKeyHistory !== 1) {
  35. return;
  36. }
  37. e.preventDefault();
  38. var pos = o.id.split('_');
  39. if (pos[0] !== 'field' || typeof pos[2] === 'undefined') {
  40. return;
  41. }
  42. var x = pos[2];
  43. var y = pos[1];
  44. switch (e.keyCode) {
  45. case 38:
  46. // up
  47. y--;
  48. break;
  49. case 40:
  50. // down
  51. y++;
  52. break;
  53. case 37:
  54. // left
  55. x--;
  56. break;
  57. case 39:
  58. // right
  59. x++;
  60. break;
  61. default:
  62. return;
  63. }
  64. var is_firefox = navigator.userAgent.toLowerCase().indexOf('firefox/') > -1;
  65. var id = 'field_' + y + '_' + x;
  66. var nO = document.getElementById(id);
  67. if (! nO) {
  68. id = 'field_' + y + '_' + x + '_0';
  69. nO = document.getElementById(id);
  70. }
  71. // skip non existent fields
  72. if (! nO) {
  73. return;
  74. }
  75. // for firefox select tag
  76. var lvalue = o.selectedIndex;
  77. var nOvalue = nO.selectedIndex;
  78. nO.focus();
  79. if (is_firefox) {
  80. var ffcheck = 0;
  81. var ffversion;
  82. for (ffversion = 3 ; ffversion < 25 ; ffversion++) {
  83. var is_firefox_v_24 = navigator.userAgent.toLowerCase().indexOf('firefox/' + ffversion) > -1;
  84. if (is_firefox_v_24) {
  85. ffcheck = 1;
  86. break;
  87. }
  88. }
  89. if (ffcheck === 1) {
  90. if (e.which === 38 || e.which === 37) {
  91. nOvalue++;
  92. } else if (e.which === 40 || e.which === 39) {
  93. nOvalue--;
  94. }
  95. nO.selectedIndex = nOvalue;
  96. } else {
  97. if (e.which === 38 || e.which === 37) {
  98. lvalue++;
  99. } else if (e.which === 40 || e.which === 39) {
  100. lvalue--;
  101. }
  102. o.selectedIndex = lvalue;
  103. }
  104. }
  105. if (nO.tagName !== 'SELECT') {
  106. nO.select();
  107. }
  108. e.returnValue = false;
  109. }
  110. AJAX.registerTeardown('keyhandler.js', function () {
  111. $(document).off('keydown keyup', '#table_columns');
  112. $(document).off('keydown keyup', 'table.insertRowTable');
  113. });
  114. AJAX.registerOnload('keyhandler.js', function () {
  115. $(document).on('keydown keyup', '#table_columns', function (event) {
  116. onKeyDownArrowsHandler(event.originalEvent);
  117. });
  118. $(document).on('keydown keyup', 'table.insertRowTable', function (event) {
  119. onKeyDownArrowsHandler(event.originalEvent);
  120. });
  121. });