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;
|
|
|
|
|
|
|
|
$('form').each(function() {
|
|
|
|
this.reset();
|
|
|
|
});
|
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
|
|
|
|
2010-12-04 12:01:23 +01:00
|
|
|
this.$overlay = $('<div id="loading-items">')
|
|
|
|
.html(listTableL10n.loading)
|
2010-08-11 23:54:51 +02:00
|
|
|
.hide()
|
|
|
|
.prependTo($('body'));
|
2010-08-18 23:02:24 +02:00
|
|
|
},
|
2010-08-11 23:54:51 +02:00
|
|
|
|
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-08-18 23:02:24 +02:00
|
|
|
this.show_overlay();
|
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',
|
2010-10-23 22:52:49 +02:00
|
|
|
'list_args': list_args
|
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 {
|
|
|
|
this.hide_overlay();
|
2010-08-11 23:54:51 +02:00
|
|
|
|
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-08-18 23:02:24 +02:00
|
|
|
$('.current-page').val($.query.GET('paged'));
|
2010-08-11 23:54:51 +02: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() {
|
|
|
|
this.hide_overlay();
|
|
|
|
|
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
|
|
|
},
|
|
|
|
|
|
|
|
show_overlay: function() {
|
|
|
|
this.loading = true;
|
|
|
|
|
|
|
|
$('.error.ajax').remove();
|
|
|
|
|
|
|
|
this.$overlay
|
|
|
|
.css({
|
2010-12-04 12:01:23 +01:00
|
|
|
width: this.$tbody.width() + 'px',
|
|
|
|
height: this.$tbody.height() - 20 + 'px'
|
2010-08-18 23:02:24 +02:00
|
|
|
})
|
|
|
|
.css(this.$tbody.offset())
|
|
|
|
.show();
|
|
|
|
},
|
|
|
|
|
|
|
|
hide_overlay: function() {
|
|
|
|
this.loading = false;
|
|
|
|
this.$overlay.hide();
|
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-04 12:01:23 +01:00
|
|
|
listTable.update_rows({'paged': paged}, false, function() {
|
|
|
|
if ( $el.parents('.tablenav.bottom').length )
|
|
|
|
scrollTo(0, 0);
|
|
|
|
});
|
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'),
|
|
|
|
orderby = $.query.load( $link.attr('href') ).get('orderby'),
|
|
|
|
order;
|
2010-08-11 23:54:51 +02:00
|
|
|
|
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;
|
|
|
|
|
2010-10-27 22:17:00 +02:00
|
|
|
$('h2 .subtitle').remove();
|
2010-08-11 23:54:51 +02:00
|
|
|
|
2010-10-27 22:17:00 +02:00
|
|
|
if ( data.s )
|
|
|
|
$('h2').append($('<span class="subtitle">').html(
|
|
|
|
listTableL10n.search.replace('%s', this.htmlencode(data.s))
|
|
|
|
));
|
|
|
|
});
|
|
|
|
}
|
|
|
|
$('.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;
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|