1
0

keyboardshortcuts.js 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  1. /**
  2. * Copyright (c) 2012 Erik Sargent <esthepiking at gmail dot com>
  3. * This file is licensed under the Affero General Public License version 3 or
  4. * later.
  5. */
  6. /*****************************
  7. * Keyboard shortcuts for Files app
  8. * ctrl/cmd+n: new folder
  9. * ctrl/cmd+shift+n: new file
  10. * esc (while new file context menu is open): close menu
  11. * up/down: select file/folder
  12. * enter: open file/folder
  13. * delete/backspace: delete file/folder
  14. *****************************/
  15. (function(Files) {
  16. var keys = [];
  17. var keyCodes = {
  18. shift: 16,
  19. n: 78,
  20. cmdFirefox: 224,
  21. cmdOpera: 17,
  22. leftCmdWebKit: 91,
  23. rightCmdWebKit: 93,
  24. ctrl: 17,
  25. esc: 27,
  26. downArrow: 40,
  27. upArrow: 38,
  28. enter: 13,
  29. del: 46
  30. };
  31. function removeA(arr) {
  32. var what, a = arguments,
  33. L = a.length,
  34. ax;
  35. while (L > 1 && arr.length) {
  36. what = a[--L];
  37. while ((ax = arr.indexOf(what)) !== -1) {
  38. arr.splice(ax, 1);
  39. }
  40. }
  41. return arr;
  42. }
  43. function newFile() {
  44. $("#new").addClass("active");
  45. $(".popup.popupTop").toggle(true);
  46. $('#new li[data-type="file"]').trigger('click');
  47. removeA(keys, keyCodes.n);
  48. }
  49. function newFolder() {
  50. $("#new").addClass("active");
  51. $(".popup.popupTop").toggle(true);
  52. $('#new li[data-type="folder"]').trigger('click');
  53. removeA(keys, keyCodes.n);
  54. }
  55. function esc() {
  56. $("#controls").trigger('click');
  57. }
  58. function down() {
  59. var select = -1;
  60. $("#fileList tr").each(function(index) {
  61. if ($(this).hasClass("mouseOver")) {
  62. select = index + 1;
  63. $(this).removeClass("mouseOver");
  64. }
  65. });
  66. if (select === -1) {
  67. $("#fileList tr:first").addClass("mouseOver");
  68. } else {
  69. $("#fileList tr").each(function(index) {
  70. if (index === select) {
  71. $(this).addClass("mouseOver");
  72. }
  73. });
  74. }
  75. }
  76. function up() {
  77. var select = -1;
  78. $("#fileList tr").each(function(index) {
  79. if ($(this).hasClass("mouseOver")) {
  80. select = index - 1;
  81. $(this).removeClass("mouseOver");
  82. }
  83. });
  84. if (select === -1) {
  85. $("#fileList tr:last").addClass("mouseOver");
  86. } else {
  87. $("#fileList tr").each(function(index) {
  88. if (index === select) {
  89. $(this).addClass("mouseOver");
  90. }
  91. });
  92. }
  93. }
  94. function enter() {
  95. $("#fileList tr").each(function(index) {
  96. if ($(this).hasClass("mouseOver")) {
  97. $(this).removeClass("mouseOver");
  98. $(this).find("span.nametext").trigger('click');
  99. }
  100. });
  101. }
  102. function del() {
  103. $("#fileList tr").each(function(index) {
  104. if ($(this).hasClass("mouseOver")) {
  105. $(this).removeClass("mouseOver");
  106. $(this).find("a.action.delete").trigger('click');
  107. }
  108. });
  109. }
  110. function rename() {
  111. $("#fileList tr").each(function(index) {
  112. if ($(this).hasClass("mouseOver")) {
  113. $(this).removeClass("mouseOver");
  114. $(this).find("a[data-action='Rename']").trigger('click');
  115. }
  116. });
  117. }
  118. Files.bindKeyboardShortcuts = function(document, $) {
  119. $(document).keydown(function(event) { //check for modifier keys
  120. if(!$(event.target).is('body')) {
  121. return;
  122. }
  123. var preventDefault = false;
  124. if ($.inArray(event.keyCode, keys) === -1) {
  125. keys.push(event.keyCode);
  126. }
  127. if (
  128. $.inArray(keyCodes.n, keys) !== -1 && ($.inArray(keyCodes.cmdFirefox, keys) !== -1 || $.inArray(keyCodes.cmdOpera, keys) !== -1 || $.inArray(keyCodes.leftCmdWebKit, keys) !== -1 || $.inArray(keyCodes.rightCmdWebKit, keys) !== -1 || $.inArray(keyCodes.ctrl, keys) !== -1 || event.ctrlKey)) {
  129. preventDefault = true; //new file/folder prevent browser from responding
  130. }
  131. if (preventDefault) {
  132. event.preventDefault(); //Prevent web browser from responding
  133. event.stopPropagation();
  134. return false;
  135. }
  136. });
  137. $(document).keyup(function(event) {
  138. // do your event.keyCode checks in here
  139. if (
  140. $.inArray(keyCodes.n, keys) !== -1 && ($.inArray(keyCodes.cmdFirefox, keys) !== -1 || $.inArray(keyCodes.cmdOpera, keys) !== -1 || $.inArray(keyCodes.leftCmdWebKit, keys) !== -1 || $.inArray(keyCodes.rightCmdWebKit, keys) !== -1 || $.inArray(keyCodes.ctrl, keys) !== -1 || event.ctrlKey)) {
  141. if ($.inArray(keyCodes.shift, keys) !== -1) { //16=shift, New File
  142. newFile();
  143. } else { //New Folder
  144. newFolder();
  145. }
  146. } else if ($("#new").hasClass("active") && $.inArray(keyCodes.esc, keys) !== -1) { //close new window
  147. esc();
  148. } else if ($.inArray(keyCodes.downArrow, keys) !== -1) { //select file
  149. down();
  150. } else if ($.inArray(keyCodes.upArrow, keys) !== -1) { //select file
  151. up();
  152. } else if (!$("#new").hasClass("active") && $.inArray(keyCodes.enter, keys) !== -1) { //open file
  153. enter();
  154. } else if (!$("#new").hasClass("active") && $.inArray(keyCodes.del, keys) !== -1) { //delete file
  155. del();
  156. }
  157. removeA(keys, event.keyCode);
  158. });
  159. };
  160. })((OCA.Files && OCA.Files.Files) || {});