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;
|
|
|
|
|
|
|
|
$('form').each(function() {
|
|
|
|
this.reset();
|
|
|
|
});
|
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
|
|
|
|
2010-08-18 17:02:24 -04:00
|
|
|
this.$overlay = $('<div id="loading-items>')
|
2010-08-24 21:59:13 -04:00
|
|
|
.html(listTableL10n.loading)
|
2010-08-11 17:54:51 -04:00
|
|
|
.hide()
|
|
|
|
.prependTo($('body'));
|
2010-08-18 17:02:24 -04:00
|
|
|
},
|
2010-08-11 17:54:51 -04:00
|
|
|
|
2010-08-18 17:02:24 -04:00
|
|
|
// paging
|
|
|
|
set_total_pages: function() {
|
|
|
|
this.total_pages = parseInt($('.total-pages').eq(0).text());
|
|
|
|
},
|
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
|
|
|
change_page: function(paged) {
|
|
|
|
if ( paged < 1 || paged >this.total_pages )
|
|
|
|
return false;
|
2010-08-11 17:54:51 -04:00
|
|
|
|
2010-08-18 17:02:24 -04:00
|
|
|
this.update_rows({'paged': paged});
|
|
|
|
},
|
|
|
|
|
|
|
|
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-08-18 17:02:24 -04:00
|
|
|
this.show_overlay();
|
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
|
|
|
|
data[key] = value;
|
|
|
|
});
|
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',
|
2010-10-23 16:52:49 -04:00
|
|
|
'list_args': list_args
|
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 {
|
|
|
|
this.hide_overlay();
|
2010-08-11 17:54:51 -04:00
|
|
|
|
2010-08-18 17:02:24 -04:00
|
|
|
this.$tbody.html(response.rows);
|
2010-08-11 17:54:51 -04:00
|
|
|
|
2010-08-18 17:02:24 -04:00
|
|
|
$('.displaying-num').html(response.total_items);
|
2010-08-11 17:54:51 -04:00
|
|
|
|
2010-08-18 17:02:24 -04:00
|
|
|
$('.total-pages').html(response.total_pages);
|
|
|
|
this.set_total_pages();
|
2010-08-11 17:54:51 -04:00
|
|
|
|
2010-08-18 17:02:24 -04:00
|
|
|
$('.current-page').val($.query.GET('paged'));
|
2010-08-11 17:54:51 -04:00
|
|
|
|
2010-11-05 19:07:00 -04:00
|
|
|
$('th.column-cb :input').attr('checked', false);
|
|
|
|
|
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() {
|
|
|
|
this.hide_overlay();
|
|
|
|
|
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
|
|
|
},
|
|
|
|
|
|
|
|
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 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-08-18 17:02:24 -04:00
|
|
|
// pagination
|
2010-08-11 17:54:51 -04: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-24 21:59:13 -04:00
|
|
|
paged = listTable.get_total_pages();
|
2010-08-11 17:54:51 -04:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2010-08-24 21:59:13 -04:00
|
|
|
listTable.change_page(paged);
|
2010-08-11 17:54:51 -04:00
|
|
|
|
|
|
|
return false;
|
|
|
|
});
|
|
|
|
|
|
|
|
$('.current-page').keypress(function(e) {
|
|
|
|
if ( 13 != e.keyCode )
|
|
|
|
return;
|
|
|
|
|
2010-08-24 21:59:13 -04:00
|
|
|
listTable.change_page(parseInt($(this).val()));
|
2010-08-11 17:54:51 -04:00
|
|
|
|
|
|
|
return false;
|
|
|
|
});
|
|
|
|
|
2010-08-18 17:02:24 -04:00
|
|
|
// sortable columns
|
2010-08-11 17:54:51 -04: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-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() {
|
|
|
|
$('h2 .subtitle').remove();
|
2010-08-11 17:54:51 -04:00
|
|
|
|
2010-10-27 16:17:00 -04: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 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;
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|