keyboardshortcuts.js 4.8 KB

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