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