You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

99 lines
3.7 KiB

1 year ago
  1. (function($){
  2. $.fn.filter_visible = function(depth) {
  3. depth = depth || 3;
  4. var is_visible = function() {
  5. var p = $(this), i;
  6. for(i=0; i<depth-1; ++i) {
  7. if (!p.is(':visible')) return false;
  8. p = p.parent();
  9. }
  10. return true;
  11. };
  12. return this.filter(is_visible);
  13. };
  14. $.table_hotkeys = function(table, keys, opts) {
  15. opts = $.extend($.table_hotkeys.defaults, opts);
  16. var selected_class, destructive_class, set_current_row, adjacent_row_callback, get_adjacent_row, adjacent_row, prev_row, next_row, check, get_first_row, get_last_row, make_key_callback, first_row;
  17. selected_class = opts.class_prefix + opts.selected_suffix;
  18. destructive_class = opts.class_prefix + opts.destructive_suffix;
  19. set_current_row = function (tr) {
  20. if ($.table_hotkeys.current_row) $.table_hotkeys.current_row.removeClass(selected_class);
  21. tr.addClass(selected_class);
  22. tr[0].scrollIntoView(false);
  23. $.table_hotkeys.current_row = tr;
  24. };
  25. adjacent_row_callback = function(which) {
  26. if (!adjacent_row(which) && typeof opts[which+'_page_link_cb'] === 'function' ) {
  27. opts[which+'_page_link_cb']();
  28. }
  29. };
  30. get_adjacent_row = function(which) {
  31. var first_row, method;
  32. if (!$.table_hotkeys.current_row) {
  33. first_row = get_first_row();
  34. $.table_hotkeys.current_row = first_row;
  35. return first_row[0];
  36. }
  37. method = 'prev' == which? $.fn.prevAll : $.fn.nextAll;
  38. return method.call($.table_hotkeys.current_row, opts.cycle_expr).filter_visible()[0];
  39. };
  40. adjacent_row = function(which) {
  41. var adj = get_adjacent_row(which);
  42. if (!adj) return false;
  43. set_current_row($(adj));
  44. return true;
  45. };
  46. prev_row = function() { return adjacent_row('prev'); };
  47. next_row = function() { return adjacent_row('next'); };
  48. check = function() {
  49. $(opts.checkbox_expr, $.table_hotkeys.current_row).each(function() {
  50. this.checked = !this.checked;
  51. });
  52. };
  53. get_first_row = function() {
  54. return $(opts.cycle_expr, table).filter_visible().eq(opts.start_row_index);
  55. };
  56. get_last_row = function() {
  57. var rows = $(opts.cycle_expr, table).filter_visible();
  58. return rows.eq(rows.length-1);
  59. };
  60. make_key_callback = function(expr) {
  61. return function() {
  62. if ( null == $.table_hotkeys.current_row ) return false;
  63. var clickable = $(expr, $.table_hotkeys.current_row);
  64. if (!clickable.length) return false;
  65. if (clickable.is('.'+destructive_class)) next_row() || prev_row();
  66. clickable.trigger( 'click' );
  67. };
  68. };
  69. first_row = get_first_row();
  70. if (!first_row.length) return;
  71. if (opts.highlight_first)
  72. set_current_row(first_row);
  73. else if (opts.highlight_last)
  74. set_current_row(get_last_row());
  75. $.hotkeys.add(opts.prev_key, opts.hotkeys_opts, function() {return adjacent_row_callback('prev');});
  76. $.hotkeys.add(opts.next_key, opts.hotkeys_opts, function() {return adjacent_row_callback('next');});
  77. $.hotkeys.add(opts.mark_key, opts.hotkeys_opts, check);
  78. $.each(keys, function() {
  79. var callback, key;
  80. if ( typeof this[1] === 'function' ) {
  81. callback = this[1];
  82. key = this[0];
  83. $.hotkeys.add(key, opts.hotkeys_opts, function(event) { return callback(event, $.table_hotkeys.current_row); });
  84. } else {
  85. key = this;
  86. $.hotkeys.add(key, opts.hotkeys_opts, make_key_callback('.'+opts.class_prefix+key));
  87. }
  88. });
  89. };
  90. $.table_hotkeys.current_row = null;
  91. $.table_hotkeys.defaults = {cycle_expr: 'tr', class_prefix: 'vim-', selected_suffix: 'current',
  92. destructive_suffix: 'destructive', hotkeys_opts: {disableInInput: true, type: 'keypress'},
  93. checkbox_expr: ':checkbox', next_key: 'j', prev_key: 'k', mark_key: 'x',
  94. start_row_index: 2, highlight_first: false, highlight_last: false, next_page_link_cb: false, prev_page_link_cb: false};
  95. })(jQuery);