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