]> scripts.mit.edu Git - autoinstalls/wordpress.git/blob - wp-admin/js/list-table.dev.js
Wordpress 3.1.4-scripts
[autoinstalls/wordpress.git] / wp-admin / js / list-table.dev.js
1 jQuery(document).ready(function($) {
2
3 window.listTable = {
4
5         init: function() {
6                 this.loading = false;
7
8                 this.reset( '.tablenav, .search-box, .wp-list-table' );
9
10                 if ( '' == $.query.GET('paged') )
11                         $.query.SET('paged', 1);
12                 this.set_total_pages();
13
14                 this.$tbody = $('#the-list, #the-comment-list');
15         },
16
17         /**
18          * Simulates form.reset() for all input, select, and textarea elements
19          * within a provided context.
20          */
21         reset: function( context ) {
22                 context = $(context);
23
24                 $('input', context).each( function(){
25                         this.value = this.defaultValue;
26                         this.checked = this.defaultChecked;
27                 });
28
29                 $('select', context).each( function(){
30                         var options = $('option', this),
31                                 anySelected = false;
32
33                         options.each( function(){
34                                 this.selected = this.defaultSelected;
35                                 anySelected = anySelected || this.defaultSelected;
36                         });
37
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;
42                 });
43
44                 $('textarea', context).each( function(){
45                         this.value = this.defaultValue;
46                 });
47         },
48
49         // paging
50         set_total_pages: function(num) {
51                 var last_page_url = $('.last-page').attr('href');
52
53                 if ( last_page_url )
54                         this.total_pages = num || $.query.load( last_page_url ).get('paged');
55         },
56
57         get_total_pages: function() {
58                 return this.total_pages;
59         },
60
61         htmlencode: function(value) {
62                 return $('<div/>').text(value).html();
63         },
64
65         update_rows: function(args, reset_paging, callback) {
66                 if ( this.loading )
67                         return false;
68
69                 var different = false, data = {};
70
71                 $.each(args, function(key, val) {
72                         if ( val != $.query.GET(key) ) {
73                                 $.query.SET(key, val);
74                                 different = true;
75                         }
76                 });
77
78                 if ( !different )
79                         return false;
80
81                 this.start_loading();
82
83                 if ( reset_paging )
84                         $.query.SET('paged', 1);
85
86                 $.each( $.query.get(), function(key, value) {
87                         if ( true === value )
88                                 data[key] = '';
89                         else
90                                 data[key] = value;
91                 });
92
93                 this._callback = callback;
94
95                 this.fetch_list(
96                         data,
97                         $.proxy(this, 'handle_success'),
98                         $.proxy(this, 'handle_error')
99                 );
100
101                 return true;
102         },
103
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()
109                 });
110
111                 $.ajax({
112                         url: ajaxurl,
113                         global: false,
114                         dataType: 'json',
115                         data: data,
116                         success: success_callback,
117                         error: error_callback
118                 });
119         },
120
121         handle_success: function(response) {
122                 if ( 'object' != typeof response ) {
123                         this.handle_error();
124                 } else {
125                         var tablenav = $('.tablenav-pages');
126
127                         this.stop_loading();
128
129                         $('div.updated, div.error').not('.persistent, .inline').remove();
130
131                         this.$tbody.html(response.rows);
132
133                         $('.displaying-num').html(response.total_items_i18n);
134                         $('.total-pages').html(response.total_pages_i18n);
135
136                         this.set_total_pages(response.total_pages);
137
138                         if ( response.total_pages > 1 )
139                                 tablenav.removeClass('one-page');
140
141                         $('.current-page').val($.query.GET('paged'));
142
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'));
146
147                         $('th.column-cb :input').attr('checked', false);
148
149                         if ( history.replaceState ) {
150                                 history.replaceState({}, '', location.pathname + $.query);
151                         }
152
153                         if ( this._callback )
154                                 this._callback();
155                 }
156         },
157
158         handle_error: function() {
159                 this.stop_loading();
160
161                 $('h2').after('<div class="error ajax below-h2"><p>' + listTableL10n.error + '</p></div>');
162         },
163
164         start_loading: function() {
165                 this.loading = true;
166
167                 $('.error.ajax').remove();
168
169                 $('.list-ajax-loading').css('visibility', 'visible');
170         },
171
172         stop_loading: function() {
173                 this.loading = false;
174
175                 $('.list-ajax-loading').css('visibility', 'hidden');
176         }
177 }
178
179 listTable.init();
180
181 // Ajaxify various UI elements
182
183         function change_page(paged, $el) {
184                 if ( paged < 1 )
185                         paged = 1;
186
187                 if ( paged > listTable.get_total_pages() )
188                         paged = listTable.get_total_pages();
189
190                 $(listTable).trigger('beforeChangePage');
191                 listTable.update_rows({'paged': paged}, false, function() {
192                         if ( $el.parents('.tablenav.bottom').length )
193                                 scrollTo(0, 0);
194
195                         $(listTable).trigger('changePage');
196                 });
197         }
198
199         // pagination
200         $('.tablenav-pages a').click(function() {
201                 var $el = $(this),
202                         paged = $.query.GET('paged');
203
204                 switch ( $el.attr('class') ) {
205                         case 'first-page':
206                                 paged = 1;
207                                 break;
208                         case 'prev-page':
209                                 paged -= 1;
210                                 break;
211                         case 'next-page':
212                                 paged += 1;
213                                 break;
214                         case 'last-page':
215                                 paged = listTable.get_total_pages();
216                                 break;
217                 }
218
219                 change_page(paged, $el);
220
221                 return false;
222         });
223
224         $('.current-page').keypress(function(e) {
225                 if ( 13 != e.keyCode )
226                         return;
227
228                 var $el = $(this);
229
230                 change_page(parseInt($el.val()) || 1, $el);
231
232                 return false;
233         });
234
235         // sortable columns
236         $('th.sortable a, th.sorted a').click(function() {
237
238                 function get_initial_order($el) {
239                         return $.query.load( $el.find('a').attr('href') ).get('order');
240                 }
241
242                 var $link = $(this),
243                         $th = $link.parent('th'),
244                         thIndex = $th.index(),
245                         orderby = $.query.load( $link.attr('href') ).get('orderby'),
246                         order;
247
248                 // th should include both headers in thead and tfoot
249                 $th = $th.closest('table').find('thead th:eq(' + thIndex + '), tfoot th:eq(' + thIndex + ')');
250
251                 if ( orderby == $.query.get('orderby') ) {
252                         // changing the direction
253                         order = ( 'asc' == $.query.get('order') ) ? 'desc' : 'asc';
254                 } else {
255                         // changing the parameter
256                         order = get_initial_order($th);
257
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'
263                                 );
264                         }
265
266                         $th.removeClass('sortable').addClass('sorted');
267                 }
268
269                 $th.removeClass('desc').removeClass('asc').addClass(order);
270
271                 listTable.update_rows({'orderby': orderby, 'order': order}, true);
272
273                 return false;
274         });
275
276         // searchbox
277         function change_search(ev) {
278                 if ( 'keypress' == ev.type && 13 != ev.keyCode )
279                         return;
280
281                 ev.preventDefault();
282                 ev.stopImmediatePropagation();
283
284                 var data = $(this).parent('.search-box').find(':input').serializeObject();
285
286                 listTable.update_rows(data, true, function() {
287                         if ( $('h2.nav-tab-wrapper').length )
288                                 return;
289
290                         if ( 'site-users-network' == pagenow || 'site-themes-network' == pagenow ) {
291                                 $('h4.search-text').remove();
292
293                                 if ( data.s )
294                                         $('ul.subsubsub').after($('<h4 class="clear search-text">').html(
295                                                 listTableL10n.search.replace('%s', this.htmlencode(data.s))
296                                         ));
297                         } else {
298                                 $('h2 .subtitle').remove();
299
300                                 if ( data.s )
301                                         $('h2').append($('<span class="subtitle">').html(
302                                                 listTableL10n.search.replace('%s', this.htmlencode(data.s))
303                                         ));
304                         }
305                 });
306         }
307         $('.search-box :submit').click(change_search);
308         $('.search-box :text').keypress(change_search);
309
310         // tablenav dropdowns
311         $('#post-query-submit').click(function() {
312                 var args = {};
313
314                 $(this).parents('.actions').find('select[name!="action"]').each(function() {
315                         var $el = $(this);
316
317                         args[$el.attr('name')] = $el.val();
318                 });
319
320                 listTable.update_rows(args, true);
321
322                 return false;
323         });
324
325         // view switch
326         $('.view-switch a').click(function() {
327                 var $this = $(this);
328
329                 listTable.update_rows({'mode': $.query.load($this.attr('href')).get('mode')}, false, function() {
330                         $('.view-switch .current').removeClass('current');
331                         $this.addClass('current');
332                 });
333
334                 return false;
335         });
336 });
337