More efficient category and list JS from mdawaffe. fixes #6677 for trunk

git-svn-id: http://svn.automattic.com/wordpress/trunk@7669 1a063a9b-81f0-0310-95a4-ce76da25c4cd
This commit is contained in:
ryan 2008-04-14 19:31:20 +00:00
parent 7d2a7755a3
commit 8eff77e82d
5 changed files with 28 additions and 19 deletions

View File

@ -1,4 +1,4 @@
<?php
<?php
$action = isset($action)? $action : '';
if ( isset($_GET['message']) )
$_GET['message'] = absint( $_GET['message'] );
@ -222,7 +222,7 @@ endif; ?>
<p id="category-add" class="wp-hidden-child">
<input type="text" name="newcat" id="newcat" class="form-required form-input-tip" value="<?php _e( 'New category name' ); ?>" tabindex="3" />
<?php wp_dropdown_categories( array( 'hide_empty' => 0, 'name' => 'newcat_parent', 'orderby' => 'name', 'hierarchical' => 1, 'show_option_none' => __('Parent category'), 'tab_index' => 3 ) ); ?>
<input type="button" id="category-add-sumbit" class="add:categorychecklist:categorydiv button" value="<?php _e( 'Add' ); ?>" tabindex="3" />
<input type="button" id="category-add-sumbit" class="add:categorychecklist:category-add button" value="<?php _e( 'Add' ); ?>" tabindex="3" />
<?php wp_nonce_field( 'add-category', '_ajax_nonce', false ); ?>
<span id="category-ajax-response"></span>
</p>
@ -233,15 +233,15 @@ endif; ?>
<li class="wp-no-js-hidden"><a href="#categories-pop" tabindex="3"><?php _e( 'Most Used' ); ?></a></li>
</ul>
<div id="categories-all" class="ui-tabs-panel">
<ul id="categorychecklist" class="list:category categorychecklist form-no-clear">
<?php dropdown_categories(); ?>
<div id="categories-pop" class="ui-tabs-panel" style="display: none;">
<ul id="categorychecklist-pop" class="categorychecklist form-no-clear" >
<?php $popular_ids = wp_popular_terms_checklist('category'); ?>
</ul>
</div>
<div id="categories-pop" class="ui-tabs-panel" style="display: none;">
<ul id="categorychecklist-pop" class="categorychecklist form-no-clear" >
<?php wp_popular_terms_checklist('category'); ?>
<div id="categories-all" class="ui-tabs-panel">
<ul id="categorychecklist" class="list:category categorychecklist form-no-clear">
<?php dropdown_categories( 0, 0, $popular_ids ); ?>
</ul>
</div>

View File

@ -175,9 +175,10 @@ function get_nested_categories( $default = 0, $parent = 0 ) {
return $result;
}
function write_nested_categories( $categories ) {
function write_nested_categories( $categories, $popular_ids = array() ) {
foreach ( $categories as $category ) {
echo "\n", '<li id="category-', $category['cat_ID'], '"><label for="in-category-', $category['cat_ID'], '" class="selectit"><input value="', $category['cat_ID'], '" type="checkbox" name="post_category[]" id="in-category-', $category['cat_ID'], '"', ($category['checked'] ? ' checked="checked"' : "" ), '/> ', wp_specialchars( apply_filters('the_category', $category['cat_name'] )), '</label>';
$class = in_array( $category['cat_ID'], $popular_ids ) ? ' class="popular-category"' : '';
echo "\n", "<li id='category-$category[cat_ID]'$class>", '<label for="in-category-', $category['cat_ID'], '" class="selectit"><input value="', $category['cat_ID'], '" type="checkbox" name="post_category[]" id="in-category-', $category['cat_ID'], '"', ($category['checked'] ? ' checked="checked"' : "" ), '/> ', wp_specialchars( apply_filters('the_category', $category['cat_name'] )), '</label>';
if ( $category['children'] ) {
echo "\n<ul>";
@ -188,18 +189,20 @@ function write_nested_categories( $categories ) {
}
}
function dropdown_categories( $default = 0, $parent = 0 ) {
write_nested_categories( get_nested_categories( $default, $parent ) );
function dropdown_categories( $default = 0, $parent = 0, $popular_ids = array() ) {
write_nested_categories( get_nested_categories( $default, $parent ), $popular_ids );
}
function wp_popular_terms_checklist( $taxonomy, $default = 0, $number = 10 ) {
$categories = get_terms( $taxonomy, array( 'orderby' => 'count', 'order' => 'DESC', 'number' => $number ) );
$popular_ids = array();
foreach ( (array) $categories as $category ) {
$popular_ids[] = $category->term_id;
$id = "popular-category-$category->term_id";
?>
<li id="<?php echo $id; ?>" >
<li id="<?php echo $id; ?>" class="popular-category">
<label class="selectit" for="in-<?php echo $id; ?>">
<input id="in-<?php echo $id; ?>" type="checkbox" value="<?php echo (int) $category->term_id; ?>" />
<?php echo wp_specialchars( apply_filters( 'the_category', $category->name ) ); ?>
@ -208,6 +211,7 @@ function wp_popular_terms_checklist( $taxonomy, $default = 0, $number = 10 ) {
<?php
}
return $popular_ids;
}
function dropdown_link_categories( $default = 0 ) {

View File

@ -118,6 +118,10 @@ jQuery(document).ready( function() {
jQuery('#in-category-' + id + ', #in-popular-category-' + id).attr( 'checked', c );
noSyncChecks = false;
};
var catAddBefore = function( s ) {
s.data += '&' + jQuery( '#categorychecklist :checked' ).serialize();
return s;
};
var catAddAfter = function( r, s ) {
if ( !newCatParent ) newCatParent = jQuery('#newcat_parent');
if ( !newCatParentOption ) newCatParentOption = newCatParent.find( 'option[value=-1]' );
@ -140,6 +144,7 @@ jQuery(document).ready( function() {
jQuery('#categorychecklist').wpList( {
alt: '',
response: 'category-ajax-response',
addBefore: catAddBefore,
addAfter: catAddAfter
} );
jQuery('#category-add-toggle').click( function() {
@ -148,7 +153,7 @@ jQuery(document).ready( function() {
jQuery('#newcat').focus();
return false;
} );
jQuery('.categorychecklist :checkbox').change( syncChecks ).filter( ':checked' ).change();
jQuery('.categorychecklist .popular-category :checkbox').change( syncChecks ).filter( ':checked' ).change();
jQuery('.edit-timestamp').click(function () {
if (jQuery('#timestampdiv').is(":hidden")) {

View File

@ -313,11 +313,11 @@ var wpList = {
var addEl = this;
var c = wpList.parseClass(this,'add')[2] || addEl.id;
if ( !c ) { return; }
var forms = []; var ins = [];
var forms = []; var ins = []; // this is all really inefficient
$('#' + c + ' :input').focus( function() { currentFormEl = this; } ).blur( function() { currentFormEl = false; } ).each( function() {
ins.push(this);
$.merge(forms,$(this).parents('form'));
forms = $.unique(forms);
var f = $(this).parents('form:first').get(0);
if ( $.inArray(f,forms) < 0 ) { forms.push(f); }
} );
$(forms).submit( function() {
if ( 0 <= $.inArray(currentFormEl,ins) ) {

View File

@ -57,7 +57,7 @@ class WP_Scripts {
'whoaText' => __("Slow down, I'm still sending your data!")
) );
$this->add( 'wp-lists', '/wp-includes/js/wp-lists.js', array('wp-ajax-response'), '20080322' );
$this->add( 'wp-lists', '/wp-includes/js/wp-lists.js', array('wp-ajax-response'), '20080411' );
$this->localize( 'wp-lists', 'wpListL10n', array(
'url' => get_option( 'siteurl' ) . '/wp-admin/admin-ajax.php'
) );
@ -144,7 +144,7 @@ class WP_Scripts {
'save' => __('Save'),
'cancel' => __('Cancel'),
) );
$this->add( 'post', '/wp-admin/js/post.js', array('suggest', 'jquery-ui-tabs', 'wp-lists', 'postbox', 'slug'), '20080322' );
$this->add( 'post', '/wp-admin/js/post.js', array('suggest', 'jquery-ui-tabs', 'wp-lists', 'postbox', 'slug'), '20080411' );
$this->localize( 'post', 'postL10n', array(
'tagsUsed' => __('Tags used on this post:'),
'add' => attribute_escape(__('Add')),