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-08-18 23:02:24 +02:00
|
|
|
this.$overlay = $('<div id="loading-items>')
|
2010-08-25 03:59:13 +02:00
|
|
|
.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
|
|
|
change_page: function(paged) {
|
2010-11-17 18:03:27 +01:00
|
|
|
if ( paged < 1 || paged > this.total_pages )
|
2010-08-18 23:02:24 +02:00
|
|
|
return false;
|
2010-08-11 23:54:51 +02:00
|
|
|
|
2010-08-18 23:02:24 +02:00
|
|
|
this.update_rows({'paged': paged});
|
|
|
|
},
|
|
|
|
|
|
|
|
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-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({
|
|
|
|
width: this.$tbody.width() + 'px',
|
|
|
|
height: this.$tbody.height() - 20 + 'px'
|
|
|
|
})
|
|
|
|
.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-08-18 23:02:24 +02:00
|
|
|
// pagination
|
2010-08-11 23:54:51 +02:00
|
|
|
$('.tablenav-pages a').click(function() {
|
|
|
|
var paged = $.query.GET('paged');
|
|
|
|
|
|
|
|
switch ( $(this).attr('class') ) {
|
|
|
|
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-08-25 03:59:13 +02:00
|
|
|
listTable.change_page(paged);
|
2010-08-11 23:54:51 +02:00
|
|
|
|
|
|
|
return false;
|
|
|
|
});
|
|
|
|
|
|
|
|
$('.current-page').keypress(function(e) {
|
|
|
|
if ( 13 != e.keyCode )
|
|
|
|
return;
|
|
|
|
|
2010-08-25 03:59:13 +02:00
|
|
|
listTable.change_page(parseInt($(this).val()));
|
2010-08-11 23:54:51 +02:00
|
|
|
|
|
|
|
return false;
|
|
|
|
});
|
|
|
|
|
2010-08-18 23:02:24 +02:00
|
|
|
// sortable columns
|
2010-08-11 23:54:51 +02:00
|
|
|
$('th a').click(function() {
|
|
|
|
var orderby = $.query.GET('orderby'),
|
|
|
|
order = $.query.GET('order'),
|
|
|
|
$th = $(this).parent('th');
|
|
|
|
|
|
|
|
if ( $th.hasClass('sortable') ) {
|
|
|
|
orderby = $.query.load($(this).attr('href')).get('orderby');
|
|
|
|
order = 'asc';
|
|
|
|
|
|
|
|
$('th.sorted-desc, th.sorted-asc')
|
|
|
|
.removeClass('sorted-asc')
|
|
|
|
.removeClass('sorted-desc')
|
|
|
|
.addClass('sortable');
|
|
|
|
|
|
|
|
$th.removeClass('sortable').addClass('sorted-asc');
|
|
|
|
}
|
|
|
|
else if ( $th.hasClass('sorted-asc') ) {
|
|
|
|
order = 'desc';
|
|
|
|
$th.removeClass('sorted-asc').addClass('sorted-desc');
|
|
|
|
}
|
|
|
|
else if ( $th.hasClass('sorted-desc') ) {
|
|
|
|
order = 'asc';
|
|
|
|
$th.removeClass('sorted-desc').addClass('sorted-asc');
|
|
|
|
}
|
|
|
|
|
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() {
|
|
|
|
$('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;
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|