2010-08-11 17:54:51 -04:00
|
|
|
jQuery(document).ready(function($) {
|
|
|
|
|
2010-08-24 21:59:13 -04:00
|
|
|
window.listTable = {
|
2010-08-11 17:54:51 -04:00
|
|
|
|
2010-08-18 17:02:24 -04:00
|
|
|
init: function() {
|
|
|
|
this.loading = false;
|
|
|
|
|
2011-01-07 15:52:42 -05:00
|
|
|
this.reset( '.tablenav, .search-box, .wp-list-table' );
|
2010-08-11 17:54:51 -04:00
|
|
|
|
2010-08-18 17:02:24 -04:00
|
|
|
if ( '' == $.query.GET('paged') )
|
|
|
|
$.query.SET('paged', 1);
|
|
|
|
this.set_total_pages();
|
|
|
|
|
|
|
|
this.$tbody = $('#the-list, #the-comment-list');
|
|
|
|
},
|
2010-08-11 17:54:51 -04:00
|
|
|
|
2011-01-11 14:37:43 -05:00
|
|
|
/**
|
|
|
|
* Simulates form.reset() for all input, select, and textarea elements
|
|
|
|
* within a provided context.
|
|
|
|
*/
|
2011-01-07 15:52:42 -05:00
|
|
|
reset: function( context ) {
|
2011-01-11 14:37:43 -05:00
|
|
|
context = $(context);
|
|
|
|
|
2011-01-07 15:52:42 -05:00
|
|
|
$('input', context).each( function(){
|
|
|
|
this.value = this.defaultValue;
|
|
|
|
this.checked = this.defaultChecked;
|
|
|
|
});
|
2011-01-11 14:37:43 -05:00
|
|
|
|
|
|
|
$('select', context).each( function(){
|
|
|
|
var options = $('option', this),
|
|
|
|
anySelected = false;
|
2011-02-09 12:35:36 -05:00
|
|
|
|
2011-01-11 14:37:43 -05:00
|
|
|
options.each( function(){
|
|
|
|
this.selected = this.defaultSelected;
|
|
|
|
anySelected = anySelected || this.defaultSelected;
|
|
|
|
});
|
2011-02-09 12:35:36 -05:00
|
|
|
|
2011-01-11 14:37:43 -05:00
|
|
|
// If no options are selected within a single-select dropdown,
|
|
|
|
// select the first element by default.
|
|
|
|
if ( ! this.multiple && ! anySelected )
|
|
|
|
options[0].selected = true;
|
2011-01-07 15:52:42 -05:00
|
|
|
});
|
2011-01-11 14:37:43 -05:00
|
|
|
|
2011-01-07 15:52:42 -05:00
|
|
|
$('textarea', context).each( function(){
|
|
|
|
this.value = this.defaultValue;
|
|
|
|
});
|
|
|
|
},
|
|
|
|
|
2010-08-18 17:02:24 -04:00
|
|
|
// paging
|
2010-11-17 12:03:27 -05:00
|
|
|
set_total_pages: function(num) {
|
2010-11-17 12:26:39 -05:00
|
|
|
var last_page_url = $('.last-page').attr('href');
|
|
|
|
|
|
|
|
if ( last_page_url )
|
|
|
|
this.total_pages = num || $.query.load( last_page_url ).get('paged');
|
2010-08-18 17:02:24 -04:00
|
|
|
},
|
2010-08-11 17:54:51 -04:00
|
|
|
|
2010-08-18 17:02:24 -04:00
|
|
|
get_total_pages: function() {
|
|
|
|
return this.total_pages;
|
|
|
|
},
|
2010-08-11 17:54:51 -04:00
|
|
|
|
2010-08-18 17:02:24 -04:00
|
|
|
htmlencode: function(value) {
|
|
|
|
return $('<div/>').text(value).html();
|
|
|
|
},
|
2010-08-11 17:54:51 -04:00
|
|
|
|
2010-08-18 17:02:24 -04:00
|
|
|
update_rows: function(args, reset_paging, callback) {
|
|
|
|
if ( this.loading )
|
2010-08-11 17:54:51 -04:00
|
|
|
return false;
|
|
|
|
|
2010-11-13 20:46:49 -05:00
|
|
|
var different = false, data = {};
|
2010-08-11 17:54:51 -04:00
|
|
|
|
|
|
|
$.each(args, function(key, val) {
|
|
|
|
if ( val != $.query.GET(key) ) {
|
|
|
|
$.query.SET(key, val);
|
|
|
|
different = true;
|
|
|
|
}
|
|
|
|
});
|
2010-08-18 17:02:24 -04:00
|
|
|
|
2010-08-11 17:54:51 -04:00
|
|
|
if ( !different )
|
|
|
|
return false;
|
|
|
|
|
2010-12-20 13:45:05 -05:00
|
|
|
this.start_loading();
|
2010-08-11 17:54:51 -04:00
|
|
|
|
|
|
|
if ( reset_paging )
|
|
|
|
$.query.SET('paged', 1);
|
|
|
|
|
2010-11-13 19:48:30 -05:00
|
|
|
$.each( $.query.get(), function(key, value) {
|
|
|
|
if ( true === value )
|
|
|
|
data[key] = '';
|
|
|
|
else
|
2010-11-17 13:47:34 -05:00
|
|
|
data[key] = value;
|
2010-11-13 19:48:30 -05:00
|
|
|
});
|
2010-08-11 17:54:51 -04:00
|
|
|
|
2010-08-18 17:02:24 -04:00
|
|
|
this._callback = callback;
|
2010-11-13 11:57:25 -05:00
|
|
|
|
2010-08-18 20:06:51 -04:00
|
|
|
this.fetch_list(
|
2010-10-19 03:48:22 -04:00
|
|
|
data,
|
2010-08-18 20:06:51 -04:00
|
|
|
$.proxy(this, 'handle_success'),
|
|
|
|
$.proxy(this, 'handle_error')
|
|
|
|
);
|
|
|
|
|
|
|
|
return true;
|
|
|
|
},
|
|
|
|
|
|
|
|
fetch_list: function(data, success_callback, error_callback) {
|
|
|
|
data = $.extend(data, {
|
|
|
|
'action': 'fetch-list',
|
2011-01-11 15:03:50 -05:00
|
|
|
'list_args': list_args,
|
|
|
|
'_ajax_fetch_list_nonce': $('#_ajax_fetch_list_nonce').val()
|
2010-08-18 20:06:51 -04:00
|
|
|
});
|
|
|
|
|
2010-08-11 17:54:51 -04:00
|
|
|
$.ajax({
|
2010-08-11 20:45:40 -04:00
|
|
|
url: ajaxurl,
|
2010-08-11 17:54:51 -04:00
|
|
|
global: false,
|
|
|
|
dataType: 'json',
|
|
|
|
data: data,
|
2010-08-18 20:06:51 -04:00
|
|
|
success: success_callback,
|
2010-10-23 16:52:49 -04:00
|
|
|
error: error_callback
|
2010-08-18 17:02:24 -04:00
|
|
|
});
|
|
|
|
},
|
2010-08-11 17:54:51 -04:00
|
|
|
|
2010-08-18 17:02:24 -04:00
|
|
|
handle_success: function(response) {
|
|
|
|
if ( 'object' != typeof response ) {
|
|
|
|
this.handle_error();
|
|
|
|
} else {
|
2011-01-16 16:34:38 -05:00
|
|
|
var tablenav = $('.tablenav-pages');
|
2011-01-01 18:19:26 -05:00
|
|
|
|
2010-12-20 13:45:05 -05:00
|
|
|
this.stop_loading();
|
2010-08-11 17:54:51 -04:00
|
|
|
|
2010-12-24 12:45:59 -05:00
|
|
|
$('div.updated, div.error').not('.persistent, .inline').remove();
|
|
|
|
|
2010-08-18 17:02:24 -04:00
|
|
|
this.$tbody.html(response.rows);
|
2010-08-11 17:54:51 -04:00
|
|
|
|
2010-11-17 12:03:27 -05:00
|
|
|
$('.displaying-num').html(response.total_items_i18n);
|
|
|
|
$('.total-pages').html(response.total_pages_i18n);
|
2010-08-11 17:54:51 -04:00
|
|
|
|
2010-11-17 12:03:27 -05:00
|
|
|
this.set_total_pages(response.total_pages);
|
2010-08-11 17:54:51 -04:00
|
|
|
|
2010-12-26 17:06:15 -05:00
|
|
|
if ( response.total_pages > 1 )
|
2011-01-01 18:19:26 -05:00
|
|
|
tablenav.removeClass('one-page');
|
2010-12-26 17:06:15 -05:00
|
|
|
|
2010-08-18 17:02:24 -04:00
|
|
|
$('.current-page').val($.query.GET('paged'));
|
2010-08-11 17:54:51 -04:00
|
|
|
|
2011-01-01 18:19:26 -05:00
|
|
|
// Disable buttons that should noop.
|
|
|
|
tablenav.find('.first-page, .prev-page').toggleClass('disabled', 1 == $.query.GET('paged'));
|
2011-01-02 05:55:10 -05:00
|
|
|
tablenav.find('.next-page, .last-page').toggleClass('disabled', response.total_pages == $.query.GET('paged'));
|
2011-01-01 18:19:26 -05:00
|
|
|
|
2010-11-05 19:07:00 -04:00
|
|
|
$('th.column-cb :input').attr('checked', false);
|
|
|
|
|
2010-11-29 14:08:15 -05:00
|
|
|
if ( history.replaceState ) {
|
|
|
|
history.replaceState({}, '', location.pathname + $.query);
|
2010-11-29 13:57:33 -05:00
|
|
|
}
|
|
|
|
|
2010-08-18 17:02:24 -04:00
|
|
|
if ( this._callback )
|
|
|
|
this._callback();
|
|
|
|
}
|
|
|
|
},
|
2010-08-11 17:54:51 -04:00
|
|
|
|
2010-08-18 17:02:24 -04:00
|
|
|
handle_error: function() {
|
2010-12-20 13:45:05 -05:00
|
|
|
this.stop_loading();
|
2010-08-18 17:02:24 -04:00
|
|
|
|
2010-08-24 21:59:13 -04:00
|
|
|
$('h2').after('<div class="error ajax below-h2"><p>' + listTableL10n.error + '</p></div>');
|
2010-08-18 17:02:24 -04:00
|
|
|
},
|
|
|
|
|
2010-12-20 13:45:05 -05:00
|
|
|
start_loading: function() {
|
2010-08-18 17:02:24 -04:00
|
|
|
this.loading = true;
|
|
|
|
|
|
|
|
$('.error.ajax').remove();
|
|
|
|
|
2010-12-20 13:45:05 -05:00
|
|
|
$('.list-ajax-loading').css('visibility', 'visible');
|
2010-08-18 17:02:24 -04:00
|
|
|
},
|
|
|
|
|
2010-12-20 13:45:05 -05:00
|
|
|
stop_loading: function() {
|
2010-08-18 17:02:24 -04:00
|
|
|
this.loading = false;
|
2010-12-20 13:45:05 -05:00
|
|
|
|
|
|
|
$('.list-ajax-loading').css('visibility', 'hidden');
|
2010-08-11 17:54:51 -04:00
|
|
|
}
|
2010-08-18 17:02:24 -04:00
|
|
|
}
|
|
|
|
|
2010-08-24 21:59:13 -04:00
|
|
|
listTable.init();
|
2010-08-18 17:02:24 -04:00
|
|
|
|
|
|
|
// Ajaxify various UI elements
|
2010-08-11 17:54:51 -04:00
|
|
|
|
2010-11-28 14:05:12 -05:00
|
|
|
function change_page(paged, $el) {
|
2010-12-01 01:02:19 -05:00
|
|
|
if ( paged < 1 )
|
|
|
|
paged = 1;
|
|
|
|
|
|
|
|
if ( paged > listTable.get_total_pages() )
|
|
|
|
paged = listTable.get_total_pages();
|
2010-11-28 14:05:12 -05:00
|
|
|
|
2010-12-20 12:53:50 -05:00
|
|
|
$(listTable).trigger('beforeChangePage');
|
2010-12-04 06:01:23 -05:00
|
|
|
listTable.update_rows({'paged': paged}, false, function() {
|
|
|
|
if ( $el.parents('.tablenav.bottom').length )
|
|
|
|
scrollTo(0, 0);
|
2011-01-05 23:11:14 -05:00
|
|
|
|
2010-12-14 06:53:31 -05:00
|
|
|
$(listTable).trigger('changePage');
|
2010-12-04 06:01:23 -05:00
|
|
|
});
|
2010-11-28 14:05:12 -05:00
|
|
|
}
|
|
|
|
|
2010-08-18 17:02:24 -04:00
|
|
|
// pagination
|
2010-08-11 17:54:51 -04:00
|
|
|
$('.tablenav-pages a').click(function() {
|
2010-11-28 14:05:12 -05:00
|
|
|
var $el = $(this),
|
|
|
|
paged = $.query.GET('paged');
|
2010-08-11 17:54:51 -04:00
|
|
|
|
2010-11-28 14:05:12 -05:00
|
|
|
switch ( $el.attr('class') ) {
|
2010-08-11 17:54:51 -04:00
|
|
|
case 'first-page':
|
|
|
|
paged = 1;
|
|
|
|
break;
|
|
|
|
case 'prev-page':
|
|
|
|
paged -= 1;
|
|
|
|
break;
|
|
|
|
case 'next-page':
|
|
|
|
paged += 1;
|
|
|
|
break;
|
|
|
|
case 'last-page':
|
2010-08-24 21:59:13 -04:00
|
|
|
paged = listTable.get_total_pages();
|
2010-08-11 17:54:51 -04:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2010-11-28 14:05:12 -05:00
|
|
|
change_page(paged, $el);
|
2010-08-11 17:54:51 -04:00
|
|
|
|
|
|
|
return false;
|
|
|
|
});
|
|
|
|
|
|
|
|
$('.current-page').keypress(function(e) {
|
|
|
|
if ( 13 != e.keyCode )
|
|
|
|
return;
|
|
|
|
|
2010-11-28 14:05:12 -05:00
|
|
|
var $el = $(this);
|
|
|
|
|
2010-12-01 01:02:19 -05:00
|
|
|
change_page(parseInt($el.val()) || 1, $el);
|
2010-08-11 17:54:51 -04:00
|
|
|
|
|
|
|
return false;
|
|
|
|
});
|
|
|
|
|
2010-08-18 17:02:24 -04:00
|
|
|
// sortable columns
|
2010-11-25 21:03:02 -05:00
|
|
|
$('th.sortable a, th.sorted a').click(function() {
|
2010-08-11 17:54:51 -04:00
|
|
|
|
2010-11-25 21:03:02 -05:00
|
|
|
function get_initial_order($el) {
|
|
|
|
return $.query.load( $el.find('a').attr('href') ).get('order');
|
|
|
|
}
|
2010-08-11 17:54:51 -04:00
|
|
|
|
2010-11-25 21:03:02 -05:00
|
|
|
var $link = $(this),
|
|
|
|
$th = $link.parent('th'),
|
2010-12-12 15:21:58 -05:00
|
|
|
thIndex = $th.index(),
|
2010-11-25 21:03:02 -05:00
|
|
|
orderby = $.query.load( $link.attr('href') ).get('orderby'),
|
|
|
|
order;
|
2010-08-11 17:54:51 -04:00
|
|
|
|
2010-12-12 15:21:58 -05:00
|
|
|
// th should include both headers in thead and tfoot
|
|
|
|
$th = $th.closest('table').find('thead th:eq(' + thIndex + '), tfoot th:eq(' + thIndex + ')');
|
|
|
|
|
2010-11-25 21:03:02 -05:00
|
|
|
if ( orderby == $.query.get('orderby') ) {
|
|
|
|
// changing the direction
|
|
|
|
order = ( 'asc' == $.query.get('order') ) ? 'desc' : 'asc';
|
|
|
|
} else {
|
|
|
|
// changing the parameter
|
|
|
|
order = get_initial_order($th);
|
|
|
|
|
|
|
|
var $old_th = $('th.sorted');
|
|
|
|
if ( $old_th.length ) {
|
|
|
|
$old_th.removeClass('sorted').addClass('sortable');
|
|
|
|
$old_th.removeClass('desc').removeClass('asc').addClass(
|
|
|
|
'asc' == get_initial_order( $old_th ) ? 'desc' : 'asc'
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
$th.removeClass('sortable').addClass('sorted');
|
2010-08-11 17:54:51 -04:00
|
|
|
}
|
|
|
|
|
2010-11-25 21:03:02 -05:00
|
|
|
$th.removeClass('desc').removeClass('asc').addClass(order);
|
|
|
|
|
2010-08-24 21:59:13 -04:00
|
|
|
listTable.update_rows({'orderby': orderby, 'order': order}, true);
|
2010-08-11 17:54:51 -04:00
|
|
|
|
|
|
|
return false;
|
|
|
|
});
|
|
|
|
|
2010-08-18 17:02:24 -04:00
|
|
|
// searchbox
|
2010-10-27 16:17:00 -04:00
|
|
|
function change_search(ev) {
|
2010-10-27 16:19:52 -04:00
|
|
|
if ( 'keypress' == ev.type && 13 != ev.keyCode )
|
2010-10-27 16:17:00 -04:00
|
|
|
return;
|
2010-08-11 17:54:51 -04:00
|
|
|
|
2010-10-27 16:17:00 -04:00
|
|
|
ev.preventDefault();
|
|
|
|
ev.stopImmediatePropagation();
|
2010-08-11 17:54:51 -04:00
|
|
|
|
2010-10-27 16:17:00 -04:00
|
|
|
var data = $(this).parent('.search-box').find(':input').serializeObject();
|
2010-08-11 17:54:51 -04:00
|
|
|
|
2010-10-27 16:17:00 -04:00
|
|
|
listTable.update_rows(data, true, function() {
|
2010-11-21 11:20:08 -05:00
|
|
|
if ( $('h2.nav-tab-wrapper').length )
|
|
|
|
return;
|
|
|
|
|
2011-01-05 23:11:14 -05:00
|
|
|
if ( 'site-users-network' == pagenow || 'site-themes-network' == pagenow ) {
|
2010-12-15 13:32:25 -05:00
|
|
|
$('h4.search-text').remove();
|
|
|
|
|
|
|
|
if ( data.s )
|
2011-01-05 23:11:14 -05:00
|
|
|
$('ul.subsubsub').after($('<h4 class="clear search-text">').html(
|
|
|
|
listTableL10n.search.replace('%s', this.htmlencode(data.s))
|
2010-12-15 13:32:25 -05:00
|
|
|
));
|
2011-01-05 23:11:14 -05:00
|
|
|
} else {
|
2010-12-15 13:32:25 -05:00
|
|
|
$('h2 .subtitle').remove();
|
|
|
|
|
2011-01-05 23:11:14 -05:00
|
|
|
if ( data.s )
|
|
|
|
$('h2').append($('<span class="subtitle">').html(
|
|
|
|
listTableL10n.search.replace('%s', this.htmlencode(data.s))
|
|
|
|
));
|
2010-12-15 13:32:25 -05:00
|
|
|
}
|
2010-10-27 16:17:00 -04:00
|
|
|
});
|
|
|
|
}
|
|
|
|
$('.search-box :submit').click(change_search);
|
|
|
|
$('.search-box :text').keypress(change_search);
|
2010-08-11 17:54:51 -04:00
|
|
|
|
|
|
|
// tablenav dropdowns
|
|
|
|
$('#post-query-submit').click(function() {
|
2010-10-23 16:52:49 -04:00
|
|
|
var args = {};
|
2010-08-11 17:54:51 -04:00
|
|
|
|
2010-08-18 17:02:24 -04:00
|
|
|
$(this).parents('.actions').find('select[name!="action"]').each(function() {
|
|
|
|
var $el = $(this);
|
|
|
|
|
|
|
|
args[$el.attr('name')] = $el.val();
|
2010-08-11 17:54:51 -04:00
|
|
|
});
|
|
|
|
|
2010-08-24 21:59:13 -04:00
|
|
|
listTable.update_rows(args, true);
|
2010-08-11 17:54:51 -04:00
|
|
|
|
|
|
|
return false;
|
|
|
|
});
|
|
|
|
|
|
|
|
// view switch
|
|
|
|
$('.view-switch a').click(function() {
|
|
|
|
var $this = $(this);
|
|
|
|
|
2010-08-24 21:59:13 -04:00
|
|
|
listTable.update_rows({'mode': $.query.load($this.attr('href')).get('mode')}, false, function() {
|
2010-08-11 17:54:51 -04:00
|
|
|
$('.view-switch .current').removeClass('current');
|
|
|
|
$this.addClass('current');
|
|
|
|
});
|
|
|
|
|
|
|
|
return false;
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|