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