1 jQuery(document).ready(function($) {
8 this.reset( '.tablenav, .search-box, .wp-list-table' );
10 if ( '' == $.query.GET('paged') )
11 $.query.SET('paged', 1);
12 this.set_total_pages();
14 this.$tbody = $('#the-list, #the-comment-list');
18 * Simulates form.reset() for all input, select, and textarea elements
19 * within a provided context.
21 reset: function( context ) {
24 $('input', context).each( function(){
25 this.value = this.defaultValue;
26 this.checked = this.defaultChecked;
29 $('select', context).each( function(){
30 var options = $('option', this),
33 options.each( function(){
34 this.selected = this.defaultSelected;
35 anySelected = anySelected || this.defaultSelected;
38 // If no options are selected within a single-select dropdown,
39 // select the first element by default.
40 if ( ! this.multiple && ! anySelected )
41 options[0].selected = true;
44 $('textarea', context).each( function(){
45 this.value = this.defaultValue;
50 set_total_pages: function(num) {
51 var last_page_url = $('.last-page').attr('href');
54 this.total_pages = num || $.query.load( last_page_url ).get('paged');
57 get_total_pages: function() {
58 return this.total_pages;
61 htmlencode: function(value) {
62 return $('<div/>').text(value).html();
65 update_rows: function(args, reset_paging, callback) {
69 var different = false, data = {};
71 $.each(args, function(key, val) {
72 if ( val != $.query.GET(key) ) {
73 $.query.SET(key, val);
84 $.query.SET('paged', 1);
86 $.each( $.query.get(), function(key, value) {
93 this._callback = callback;
97 $.proxy(this, 'handle_success'),
98 $.proxy(this, 'handle_error')
104 fetch_list: function(data, success_callback, error_callback) {
105 data = $.extend(data, {
106 'action': 'fetch-list',
107 'list_args': list_args,
108 '_ajax_fetch_list_nonce': $('#_ajax_fetch_list_nonce').val()
116 success: success_callback,
117 error: error_callback
121 handle_success: function(response) {
122 if ( 'object' != typeof response ) {
125 var tablenav = $('.tablenav-pages');
129 $('div.updated, div.error').not('.persistent, .inline').remove();
131 this.$tbody.html(response.rows);
133 $('.displaying-num').html(response.total_items_i18n);
134 $('.total-pages').html(response.total_pages_i18n);
136 this.set_total_pages(response.total_pages);
138 if ( response.total_pages > 1 )
139 tablenav.removeClass('one-page');
141 $('.current-page').val($.query.GET('paged'));
143 // Disable buttons that should noop.
144 tablenav.find('.first-page, .prev-page').toggleClass('disabled', 1 == $.query.GET('paged'));
145 tablenav.find('.next-page, .last-page').toggleClass('disabled', response.total_pages == $.query.GET('paged'));
147 $('th.column-cb :input').attr('checked', false);
149 if ( history.replaceState ) {
150 history.replaceState({}, '', location.pathname + $.query);
153 if ( this._callback )
158 handle_error: function() {
161 $('h2').after('<div class="error ajax below-h2"><p>' + listTableL10n.error + '</p></div>');
164 start_loading: function() {
167 $('.error.ajax').remove();
169 $('.list-ajax-loading').css('visibility', 'visible');
172 stop_loading: function() {
173 this.loading = false;
175 $('.list-ajax-loading').css('visibility', 'hidden');
181 // Ajaxify various UI elements
183 function change_page(paged, $el) {
187 if ( paged > listTable.get_total_pages() )
188 paged = listTable.get_total_pages();
190 $(listTable).trigger('beforeChangePage');
191 listTable.update_rows({'paged': paged}, false, function() {
192 if ( $el.parents('.tablenav.bottom').length )
195 $(listTable).trigger('changePage');
200 $('.tablenav-pages a').click(function() {
202 paged = $.query.GET('paged');
204 switch ( $el.attr('class') ) {
215 paged = listTable.get_total_pages();
219 change_page(paged, $el);
224 $('.current-page').keypress(function(e) {
225 if ( 13 != e.keyCode )
230 change_page(parseInt($el.val()) || 1, $el);
236 $('th.sortable a, th.sorted a').click(function() {
238 function get_initial_order($el) {
239 return $.query.load( $el.find('a').attr('href') ).get('order');
243 $th = $link.parent('th'),
244 thIndex = $th.index(),
245 orderby = $.query.load( $link.attr('href') ).get('orderby'),
248 // th should include both headers in thead and tfoot
249 $th = $th.closest('table').find('thead th:eq(' + thIndex + '), tfoot th:eq(' + thIndex + ')');
251 if ( orderby == $.query.get('orderby') ) {
252 // changing the direction
253 order = ( 'asc' == $.query.get('order') ) ? 'desc' : 'asc';
255 // changing the parameter
256 order = get_initial_order($th);
258 var $old_th = $('th.sorted');
259 if ( $old_th.length ) {
260 $old_th.removeClass('sorted').addClass('sortable');
261 $old_th.removeClass('desc').removeClass('asc').addClass(
262 'asc' == get_initial_order( $old_th ) ? 'desc' : 'asc'
266 $th.removeClass('sortable').addClass('sorted');
269 $th.removeClass('desc').removeClass('asc').addClass(order);
271 listTable.update_rows({'orderby': orderby, 'order': order}, true);
277 function change_search(ev) {
278 if ( 'keypress' == ev.type && 13 != ev.keyCode )
282 ev.stopImmediatePropagation();
284 var data = $(this).parent('.search-box').find(':input').serializeObject();
286 listTable.update_rows(data, true, function() {
287 if ( $('h2.nav-tab-wrapper').length )
290 if ( 'site-users-network' == pagenow || 'site-themes-network' == pagenow ) {
291 $('h4.search-text').remove();
294 $('ul.subsubsub').after($('<h4 class="clear search-text">').html(
295 listTableL10n.search.replace('%s', this.htmlencode(data.s))
298 $('h2 .subtitle').remove();
301 $('h2').append($('<span class="subtitle">').html(
302 listTableL10n.search.replace('%s', this.htmlencode(data.s))
307 $('.search-box :submit').click(change_search);
308 $('.search-box :text').keypress(change_search);
310 // tablenav dropdowns
311 $('#post-query-submit').click(function() {
314 $(this).parents('.actions').find('select[name!="action"]').each(function() {
317 args[$el.attr('name')] = $el.val();
320 listTable.update_rows(args, true);
326 $('.view-switch a').click(function() {
329 listTable.update_rows({'mode': $.query.load($this.attr('href')).get('mode')}, false, function() {
330 $('.view-switch .current').removeClass('current');
331 $this.addClass('current');