]> scripts.mit.edu Git - autoinstalls/wordpress.git/blob - wp-includes/js/jquery/jquery.table-hotkeys.js
Wordpress 2.7.1-scripts
[autoinstalls/wordpress.git] / wp-includes / js / jquery / jquery.table-hotkeys.js
1 (function($){
2         $.fn.filter_visible = function(depth) {
3                 depth = depth || 3;
4                 var is_visible = function() {
5                         var p = $(this);
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 = 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;
23                 };
24                 var adjacent_row_callback = function(which) {
25                         if (!adjacent_row(which) && $.isFunction(opts[which+'_page_link_cb'])) {
26                                 opts[which+'_page_link_cb']();
27                         }
28                 };
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;
33                                 return first_row[0];
34                         }
35                         var method = 'prev' == which? $.fn.prevAll : $.fn.nextAll;
36                         return method.call($.table_hotkeys.current_row, opts.cycle_expr).filter_visible()[0];
37                 };
38                 var adjacent_row = function(which) {
39                         var adj = get_adjacent_row(which);
40                         if (!adj) return false;
41                         set_current_row($(adj));
42                         return true;
43                 };
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;
49                         });
50                 };
51                 var get_first_row = function() {
52                         return $(opts.cycle_expr, table).filter_visible().eq(opts.start_row_index);
53                 };
54                 var get_last_row = function() {
55                         var rows = $(opts.cycle_expr, table).filter_visible();
56                         return rows.eq(rows.length-1);
57                 };
58                 var make_key_callback = function(expr) {
59                         return function() {
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();
64                                 clickable.click();
65                         }
66                 };
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];
79                                 var key = this[0];
80                                 jQuery.hotkeys.add(key, opts.hotkeys_opts, function(event) { return callback(event, $.table_hotkeys.current_row); });
81                         } else {
82                                 var key = this;
83                                 jQuery.hotkeys.add(key, opts.hotkeys_opts, make_key_callback('.'+opts.class_prefix+key));
84                         }
85                 });
86
87         };
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};
93 })(jQuery);