refactor admin-table.js code

git-svn-id: http://svn.automattic.com/wordpress/trunk@15507 1a063a9b-81f0-0310-95a4-ce76da25c4cd
This commit is contained in:
scribu 2010-08-18 21:02:24 +00:00
parent 02668b6614
commit fbca3e400c
3 changed files with 112 additions and 104 deletions

View File

@ -1,52 +1,58 @@
jQuery(document).ready(function($) {
$('form').each(function() {
this.reset();
});
if ( '' == $.query.GET('paged') )
$.query.SET('paged', 1);
var adminTable = {
var total_pages;
var set_total_pages = function() {
total_pages = parseInt($('.total-pages').eq(0).text());
}
init: function() {
this.loading = false;
set_total_pages();
$('form').each(function() {
this.reset();
});
var loading = false,
$tbody = $('#the-list, #the-comment-list'),
$overlay = $('<div id="loading-items>')
if ( '' == $.query.GET('paged') )
$.query.SET('paged', 1);
this.set_total_pages();
this.$tbody = $('#the-list, #the-comment-list');
this.$overlay = $('<div id="loading-items>')
.html(adminTableL10n.loading)
.hide()
.prependTo($('body'));
},
var show_overlay = function() {
loading = true;
// paging
set_total_pages: function() {
this.total_pages = parseInt($('.total-pages').eq(0).text());
},
$('.error.ajax').remove();
get_total_pages: function() {
return this.total_pages;
},
$overlay
.css({
width: $tbody.width() + 'px',
height: $tbody.height() - 20 + 'px'
})
.css($tbody.offset())
.show();
}
change_page: function(paged) {
if ( paged < 1 || paged >this.total_pages )
return false;
var hide_overlay = function() {
loading = false;
$overlay.hide();
}
this.update_rows({'paged': paged});
},
var handle_error = function() {
hide_overlay();
// searching
change_search: function(s) {
this.update_rows({'s': s}, true, function() {
$('h2 .subtitle').remove();
$('h2').after('<div class="error ajax below-h2"><p>' + adminTableL10n.error + '</p></div>');
}
if ( s )
$('h2').eq(0).append($('<span class="subtitle">').html(adminTableL10n.search.replace('%s', this.htmlencode(s))));
});
},
var update_rows = function(args, reset_paging, callback) {
if ( loading )
htmlencode: function(value) {
return $('<div/>').text(value).html();
},
update_rows: function(args, reset_paging, callback) {
if ( this.loading )
return false;
var different = false;
@ -57,11 +63,11 @@ jQuery(document).ready(function($) {
different = true;
}
});
if ( !different )
return false;
show_overlay();
this.show_overlay();
if ( reset_paging )
$.query.SET('paged', 1);
@ -71,44 +77,71 @@ jQuery(document).ready(function($) {
data['action'] = 'fetch-list';
data['list_args'] = list_args;
this._callback = callback;
$.ajax({
url: ajaxurl,
global: false,
dataType: 'json',
data: data,
success: function(response) {
if ( 'object' != typeof response ) {
handle_error();
} else {
hide_overlay();
$tbody.html(response.rows);
$('.displaying-num').html(response.total_items);
$('.total-pages').html(response.total_pages);
set_total_pages();
$('.current-page').val($.query.GET('paged'));
if ( callback )
callback();
}
},
error: handle_error
success: $.proxy(this, 'handle_success'),
error: $.proxy(this, 'handle_error')
});
return true;
},
handle_success: function(response) {
if ( 'object' != typeof response ) {
this.handle_error();
} else {
this.hide_overlay();
this.$tbody.html(response.rows);
$('.displaying-num').html(response.total_items);
$('.total-pages').html(response.total_pages);
this.set_total_pages();
$('.current-page').val($.query.GET('paged'));
if ( this._callback )
this._callback();
}
},
handle_error: function() {
this.hide_overlay();
$('h2').after('<div class="error ajax below-h2"><p>' + adminTableL10n.error + '</p></div>');
},
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();
}
}
// paging
var change_page = function(paged) {
if ( paged < 1 || paged > total_pages )
return false;
adminTable.init();
update_rows({'paged': paged});
}
// Ajaxify various UI elements
// pagination
$('.tablenav-pages a').click(function() {
var paged = $.query.GET('paged');
@ -123,11 +156,11 @@ jQuery(document).ready(function($) {
paged += 1;
break;
case 'last-page':
paged = total_pages;
paged = adminTable.get_total_pages();
break;
}
change_page(paged);
adminTable.change_page(paged);
return false;
});
@ -136,12 +169,12 @@ jQuery(document).ready(function($) {
if ( 13 != e.keyCode )
return;
change_page(parseInt($(this).val()));
adminTable.change_page(parseInt($(this).val()));
return false;
});
// sorting
// sortable columns
$('th a').click(function() {
var orderby = $.query.GET('orderby'),
order = $.query.GET('order'),
@ -167,27 +200,14 @@ jQuery(document).ready(function($) {
$th.removeClass('sorted-desc').addClass('sorted-asc');
}
update_rows({'orderby': orderby, 'order': order}, true);
adminTable.update_rows({'orderby': orderby, 'order': order}, true);
return false;
});
// searching
var htmlencode = function(value) {
return $('<div/>').text(value).html();
}
var change_search = function(s) {
update_rows({'s': s}, true, function() {
$('h2 .subtitle').remove();
if ( s )
$('h2').eq(0).append($('<span class="subtitle">').html(adminTableL10n.search.replace('%s', htmlencode(s))));
});
}
// searchbox
$('.search-box :submit').click(function() {
change_search($(this).parent('.search-box').find(':text').val());
adminTable.change_search($(this).parent('.search-box').find(':text').val());
return false;
});
@ -196,20 +216,22 @@ jQuery(document).ready(function($) {
if ( 13 != e.keyCode )
return;
change_search($(this).val());
adminTable.change_search($(this).val());
return false;
});
// tablenav dropdowns
$('#post-query-submit').click(function() {
var $this = $(this), key, val, args = {};
var key, val, args = {};
$this.parents('.actions').find('select[name!="action"]').each(function() {
args[$this.attr('name')] = $this.val();
$(this).parents('.actions').find('select[name!="action"]').each(function() {
var $el = $(this);
args[$el.attr('name')] = $el.val();
});
update_rows(args, true);
adminTable.update_rows(args, true);
return false;
});
@ -218,26 +240,12 @@ jQuery(document).ready(function($) {
$('.view-switch a').click(function() {
var $this = $(this);
update_rows({'mode': $.query.load($this.attr('href')).get('mode')}, false, function() {
adminTable.update_rows({'mode': $.query.load($this.attr('href')).get('mode')}, false, function() {
$('.view-switch .current').removeClass('current');
$this.addClass('current');
});
return false;
});
/*
// problem: when switching from one to the other, columns are not always the same
$('.subsubsub a').click(function() {
var $this = $(this);
update_rows($.query.load($this.attr('href')).get(), true, function() {
$('.subsubsub .current').removeClass('current');
$this.addClass('current');
});
return false;
});
/**/
});

View File

@ -1 +1 @@
jQuery(document).ready(function(g){g("form").each(function(){this.reset()});if(""==g.query.GET("paged")){g.query.SET("paged",1)}var l;var b=function(){l=parseInt(g(".total-pages").eq(0).text())};b();var c=false,f=g("#the-list, #the-comment-list"),m=g('<div id="loading-items>').html(adminTableL10n.loading).hide().prependTo(g("body"));var a=function(){c=true;g(".error.ajax").remove();m.css({width:f.width()+"px",height:f.height()-20+"px"}).css(f.offset()).show()};var i=function(){c=false;m.hide()};var e=function(){i();g("h2").after('<div class="error ajax below-h2"><p>'+adminTableL10n.error+"</p></div>")};var k=function(o,n,r){if(c){return false}var q=false;g.each(o,function(s,t){if(t!=g.query.GET(s)){g.query.SET(s,t);q=true}});if(!q){return false}a();if(n){g.query.SET("paged",1)}var p=g.query.get();p.action="fetch-list";p.list_args=list_args;g.ajax({url:ajaxurl,global:false,dataType:"json",data:p,success:function(s){if("object"!=typeof s){e()}else{i();f.html(s.rows);g(".displaying-num").html(s.total_items);g(".total-pages").html(s.total_pages);b();g(".current-page").val(g.query.GET("paged"));if(r){r()}}},error:e});return true};var d=function(n){if(n<1||n>l){return false}k({paged:n})};g(".tablenav-pages a").click(function(){var n=g.query.GET("paged");switch(g(this).attr("class")){case"first-page":n=1;break;case"prev-page":n-=1;break;case"next-page":n+=1;break;case"last-page":n=l;break}d(n);return false});g(".current-page").keypress(function(n){if(13!=n.keyCode){return}d(parseInt(g(this).val()));return false});g("th a").click(function(){var p=g.query.GET("orderby"),n=g.query.GET("order"),o=g(this).parent("th");if(o.hasClass("sortable")){p=g.query.load(g(this).attr("href")).get("orderby");n="asc";g("th.sorted-desc, th.sorted-asc").removeClass("sorted-asc").removeClass("sorted-desc").addClass("sortable");o.removeClass("sortable").addClass("sorted-asc")}else{if(o.hasClass("sorted-asc")){n="desc";o.removeClass("sorted-asc").addClass("sorted-desc")}else{if(o.hasClass("sorted-desc")){n="asc";o.removeClass("sorted-desc").addClass("sorted-asc")}}}k({orderby:p,order:n},true);return false});var h=function(n){return g("<div/>").text(n).html()};var j=function(n){k({s:n},true,function(){g("h2 .subtitle").remove();if(n){g("h2").eq(0).append(g('<span class="subtitle">').html(adminTableL10n.search.replace("%s",h(n))))}})};g(".search-box :submit").click(function(){j(g(this).parent(".search-box").find(":text").val());return false});g(".search-box :text").keypress(function(n){if(13!=n.keyCode){return}j(g(this).val());return false});g("#post-query-submit").click(function(){var p=g(this),o,q,n={};p.parents(".actions").find('select[name!="action"]').each(function(){n[p.attr("name")]=p.val()});k(n,true);return false});g(".view-switch a").click(function(){var n=g(this);k({mode:g.query.load(n.attr("href")).get("mode")},false,function(){g(".view-switch .current").removeClass("current");n.addClass("current")});return false})});
jQuery(document).ready(function(b){var a={init:function(){this.loading=false;b("form").each(function(){this.reset()});if(""==b.query.GET("paged")){b.query.SET("paged",1)}this.set_total_pages();this.$tbody=b("#the-list, #the-comment-list");this.$overlay=b('<div id="loading-items>').html(adminTableL10n.loading).hide().prependTo(b("body"))},set_total_pages:function(){this.total_pages=parseInt(b(".total-pages").eq(0).text())},get_total_pages:function(){return this.total_pages},change_page:function(c){if(c<1||c>this.total_pages){return false}this.update_rows({paged:c})},change_search:function(c){this.update_rows({s:c},true,function(){b("h2 .subtitle").remove();if(c){b("h2").eq(0).append(b('<span class="subtitle">').html(adminTableL10n.search.replace("%s",this.htmlencode(c))))}})},htmlencode:function(c){return b("<div/>").text(c).html()},update_rows:function(d,c,g){if(this.loading){return false}var f=false;b.each(d,function(h,i){if(i!=b.query.GET(h)){b.query.SET(h,i);f=true}});if(!f){return false}this.show_overlay();if(c){b.query.SET("paged",1)}var e=b.query.get();e.action="fetch-list";e.list_args=list_args;this._callback=g;b.ajax({url:ajaxurl,global:false,dataType:"json",data:e,success:b.proxy(this,"handle_success"),error:b.proxy(this,"handle_error")});return true},handle_success:function(c){if("object"!=typeof c){this.handle_error()}else{this.hide_overlay();this.$tbody.html(c.rows);b(".displaying-num").html(c.total_items);b(".total-pages").html(c.total_pages);this.set_total_pages();b(".current-page").val(b.query.GET("paged"));if(this._callback){this._callback()}}},handle_error:function(){this.hide_overlay();b("h2").after('<div class="error ajax below-h2"><p>'+adminTableL10n.error+"</p></div>")},show_overlay:function(){this.loading=true;b(".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()}};a.init();b(".tablenav-pages a").click(function(){var c=b.query.GET("paged");switch(b(this).attr("class")){case"first-page":c=1;break;case"prev-page":c-=1;break;case"next-page":c+=1;break;case"last-page":c=a.get_total_pages();break}a.change_page(c);return false});b(".current-page").keypress(function(c){if(13!=c.keyCode){return}a.change_page(parseInt(b(this).val()));return false});b("th a").click(function(){var e=b.query.GET("orderby"),c=b.query.GET("order"),d=b(this).parent("th");if(d.hasClass("sortable")){e=b.query.load(b(this).attr("href")).get("orderby");c="asc";b("th.sorted-desc, th.sorted-asc").removeClass("sorted-asc").removeClass("sorted-desc").addClass("sortable");d.removeClass("sortable").addClass("sorted-asc")}else{if(d.hasClass("sorted-asc")){c="desc";d.removeClass("sorted-asc").addClass("sorted-desc")}else{if(d.hasClass("sorted-desc")){c="asc";d.removeClass("sorted-desc").addClass("sorted-asc")}}}a.update_rows({orderby:e,order:c},true);return false});b(".search-box :submit").click(function(){a.change_search(b(this).parent(".search-box").find(":text").val());return false});b(".search-box :text").keypress(function(c){if(13!=c.keyCode){return}a.change_search(b(this).val());return false});b("#post-query-submit").click(function(){var d,e,c={};b(this).parents(".actions").find('select[name!="action"]').each(function(){var f=b(this);c[f.attr("name")]=f.val()});a.update_rows(c,true);return false});b(".view-switch a").click(function(){var c=b(this);a.update_rows({mode:b.query.load(c.attr("href")).get("mode")},false,function(){b(".view-switch .current").removeClass("current");c.addClass("current")});return false})});

View File

@ -338,7 +338,7 @@ function wp_default_scripts( &$scripts ) {
$scripts->add( 'theme-preview', "/wp-admin/js/theme-preview$suffix.js", array( 'thickbox', 'jquery' ), '20100407' );
$scripts->add_data( 'theme-preview', 'group', 1 );
$scripts->add( 'admin-table', "/wp-admin/js/admin-table$suffix.js", array( 'jquery', 'jquery-query' ), '20100812' );
$scripts->add( 'admin-table', "/wp-admin/js/admin-table$suffix.js", array( 'jquery', 'jquery-query' ), '20100818' );
$scripts->add_data( 'admin-table', 'group', 1 );
$scripts->localize( 'admin-table', 'adminTableL10n', array(
'loading' => __('Loading...'),