PressThis: simplify and restyle the categories drop-down, remove the checkboxes and use JS to get the selected categories on submitting the form.

Part prop: Michael-Arestad. Fixes #31443.

git-svn-id: https://develop.svn.wordpress.org/trunk@31765 602fd350-edb4-49c9-b593-d223f7449a82
This commit is contained in:
Andrew Ozz 2015-03-13 16:56:45 +00:00
parent 13e66393f1
commit 2650be6c74
4 changed files with 93 additions and 74 deletions

View File

@ -963,34 +963,28 @@ input[type="search"].categories-search,
list-style: none; list-style: none;
} }
.categories-select input { .category {
clear: none; position: relative;
position: absolute;
top: 0;
left: 0;
display: block; display: block;
line-height: 0; padding: 13px 16px 14px 16px;
width: 100%;
height: 100%;
outline: 0;
padding: 0;
border: 0;
-webkit-border-radius: 0;
border-radius: 0;
text-align: center;
vertical-align: middle;
-webkit-appearance: none;
appearance: none;
cursor: pointer; cursor: pointer;
background: #fff;
} }
.categories-select input:checked { .category:focus,
.category.selected:focus {
outline: 0;
background: #2ea2cc;
color: #fff;
}
.category.selected {
-webkit-box-shadow: inset 6px 0 0 #2ea2cc; -webkit-box-shadow: inset 6px 0 0 #2ea2cc;
box-shadow: inset 6px 0 0 #2ea2cc; box-shadow: inset 6px 0 0 #2ea2cc;
background: rgba(46, 162, 204, 0.1); background: #E9F5F9;
} }
.categories-select input:checked:after { .category.selected:after {
display: inline-block; display: inline-block;
content: "\f147"; content: "\f147";
position: absolute; position: absolute;
@ -1014,41 +1008,31 @@ input[type="search"].categories-search,
-moz-osx-font-smoothing: grayscale; -moz-osx-font-smoothing: grayscale;
} }
.categories-select input:focus { .category.selected:focus:after {
-webkit-box-shadow: inset 6px 0 0 #2ea2cc; color: #fff;
box-shadow: inset 6px 0 0 #2ea2cc;
background: rgba(46, 162, 204, 0.05);
} }
.categories-select label { .categories-select ul .category {
position: relative;
display: block;
padding: 13px 16px 14px 16px;
cursor: pointer;
background: #fff;
}
.categories-select ul label {
padding-left: 24px; padding-left: 24px;
} }
.categories-select ul ul label { .categories-select ul ul .category {
padding-left: 32px; padding-left: 32px;
} }
.categories-select ul ul ul label { .categories-select ul ul ul .category {
padding-left: 40px; padding-left: 40px;
} }
.categories-select ul ul ul ul label { .categories-select ul ul ul ul .category {
padding-left: 48px; padding-left: 48px;
} }
.categories-select ul ul ul ul ul label { .categories-select ul ul ul ul ul .category {
padding-left: 56px; padding-left: 56px;
} }
.categories-select ul ul ul ul ul ul label { .categories-select ul ul ul ul ul ul .category {
padding-left: 64px; padding-left: 64px;
} }
@ -1060,27 +1044,6 @@ input[type="search"].categories-search,
display: block; display: block;
} }
.lt-ie9 .categories-select input {
top: 50%;
right: 10px;
left: auto;
margin-top: -8px;
width: 16px;
height: 16px;
}
/* TODO Reformats checkbox on Firefox until we remove checkbox in merge */
@-moz-document url-prefix() {
.categories-select input {
top: 50%;
right: 10px;
left: auto;
margin-top: -8px;
width: 16px;
height: 16px;
}
}
/* Category search */ /* Category search */
.categories-search-wrapper { .categories-search-wrapper {
position: relative; position: relative;
@ -1831,6 +1794,7 @@ html {
/* Keeps background the full height of the screen, but only visually. Clicks go through. */ /* Keeps background the full height of the screen, but only visually. Clicks go through. */
-webkit-box-shadow: 5001px 5000px 0 5000px #fff, 5000px 5000px 0 5000px #e5e5e5; -webkit-box-shadow: 5001px 5000px 0 5000px #fff, 5000px 5000px 0 5000px #e5e5e5;
box-shadow: 5001px 5000px 0 5000px #fff, 5000px 5000px 0 5000px #e5e5e5; box-shadow: 5001px 5000px 0 5000px #fff, 5000px 5000px 0 5000px #e5e5e5;
outline: 0;
} }
.options-panel-back { .options-panel-back {

View File

@ -794,7 +794,7 @@ class WP_Press_This {
</label> </label>
</div> </div>
<ul class="categories-select" aria-label="<?php esc_attr_e( 'Categories' ); ?>"> <ul class="categories-select" aria-label="<?php esc_attr_e( 'Categories' ); ?>">
<?php wp_terms_checklist( $post->ID, array( 'taxonomy' => 'category' ) ); ?> <?php wp_terms_checklist( $post->ID, array( 'taxonomy' => 'category', 'list_only' => true ) ); ?>
</ul> </ul>
<?php <?php
} }

View File

@ -82,18 +82,34 @@ class Walker_Category_Checklist extends Walker {
} else { } else {
$name = 'tax_input[' . $taxonomy . ']'; $name = 'tax_input[' . $taxonomy . ']';
} }
$args['popular_cats'] = empty( $args['popular_cats'] ) ? array() : $args['popular_cats']; $args['popular_cats'] = empty( $args['popular_cats'] ) ? array() : $args['popular_cats'];
$class = in_array( $category->term_id, $args['popular_cats'] ) ? ' class="popular-category"' : ''; $class = in_array( $category->term_id, $args['popular_cats'] ) ? ' class="popular-category"' : '';
$args['selected_cats'] = empty( $args['selected_cats'] ) ? array() : $args['selected_cats']; $args['selected_cats'] = empty( $args['selected_cats'] ) ? array() : $args['selected_cats'];
/** This filter is documented in wp-includes/category-template.php */ /** This filter is documented in wp-includes/category-template.php */
if ( ! empty( $args['list_only'] ) ) {
$aria_cheched = 'false';
$inner_class = 'category';
if ( in_array( $category->term_id, $args['selected_cats'] ) ) {
$inner_class .= ' selected';
$aria_cheched = 'true';
}
$output .= "\n" . '<li' . $class . '>' .
'<div class="' . $inner_class . '" data-term-id=' . $category->term_id .
' tabindex="0" role="checkbox" aria-checked="' . $aria_cheched . '">' .
esc_html( apply_filters( 'the_category', $category->name ) ) . '</div>';
} else {
$output .= "\n<li id='{$taxonomy}-{$category->term_id}'$class>" . $output .= "\n<li id='{$taxonomy}-{$category->term_id}'$class>" .
'<label class="selectit"><input value="' . $category->term_id . '" type="checkbox" name="'.$name.'[]" id="in-'.$taxonomy.'-' . $category->term_id . '"' . '<label class="selectit"><input value="' . $category->term_id . '" type="checkbox" name="'.$name.'[]" id="in-'.$taxonomy.'-' . $category->term_id . '"' .
checked( in_array( $category->term_id, $args['selected_cats'] ), true, false ) . checked( in_array( $category->term_id, $args['selected_cats'] ), true, false ) .
disabled( empty( $args['disabled'] ), false, false ) . ' /> ' . disabled( empty( $args['disabled'] ), false, false ) . ' /> ' .
esc_html( apply_filters( 'the_category', $category->name ) ) . '</label>'; esc_html( apply_filters( 'the_category', $category->name ) ) . '</label>';
} }
}
/** /**
* Ends the element output, if needed. * Ends the element output, if needed.
@ -203,6 +219,8 @@ function wp_terms_checklist( $post_id = 0, $args = array() ) {
$tax = get_taxonomy( $taxonomy ); $tax = get_taxonomy( $taxonomy );
$args['disabled'] = ! current_user_can( $tax->cap->assign_terms ); $args['disabled'] = ! current_user_can( $tax->cap->assign_terms );
$args['list_only'] = ! empty( $r['list_only'] );
if ( is_array( $r['selected_cats'] ) ) { if ( is_array( $r['selected_cats'] ) ) {
$args['selected_cats'] = $r['selected_cats']; $args['selected_cats'] = $r['selected_cats'];
} elseif ( $post_id ) { } elseif ( $post_id ) {

View File

@ -121,6 +121,9 @@
* Prepare the form data for saving. * Prepare the form data for saving.
*/ */
function prepareFormData() { function prepareFormData() {
var $form = $( '#pressthis-form' ),
$input = $( '<input type="hidden" name="post_category[]" value="">' );
editor && editor.save(); editor && editor.save();
$( '#post_title' ).val( sanitizeText( $( '#title-container' ).text() ) ); $( '#post_title' ).val( sanitizeText( $( '#title-container' ).text() ) );
@ -131,6 +134,16 @@
window.tagBox.flushTags( this, false, 1 ); window.tagBox.flushTags( this, false, 1 );
} ); } );
} }
// Get selected categories
$( '.categories-select .category' ).each( function( i, element ) {
var $cat = $( element );
if ( $cat.hasClass( 'selected' ) ) {
// Have to append a node as we submit the actual form on preview
$form.append( $input.clone().val( $cat.attr( 'data-term-id' ) || '' ) );
}
});
} }
/** /**
@ -232,18 +245,17 @@
if ( ! response.success ) { if ( ! response.success ) {
renderError( response.data.errorMessage ); renderError( response.data.errorMessage );
} else { } else {
// TODO: change if/when the html changes.
var $parent, $ul, var $parent, $ul,
$wrap = $( 'ul.categories-select' ); $wrap = $( 'ul.categories-select' );
$.each( response.data, function( i, newCat ) { $.each( response.data, function( i, newCat ) {
var $node = $( '<li>' ).attr( 'id', 'category-' + newCat.term_id ) var $node = $( '<li>' ).append( $( '<div class="category selected" tabindex="0" role="checkbox" aria-checked="true">' )
.append( $( '<label class="selectit">' ).text( newCat.name ) .attr( 'data-term-id', newCat.term_id )
.append( $( '<input type="checkbox" name="post_category[]" checked>' ).attr( 'value', newCat.term_id ) ) ); .text( newCat.name ) );
if ( newCat.parent ) { if ( newCat.parent ) {
if ( ! $ul || ! $ul.length ) { if ( ! $ul || ! $ul.length ) {
$parent = $wrap.find( '#category-' + newCat.parent ); $parent = $wrap.find( 'div[data-term-id="' + newCat.parent + '"]' ).parent();
$ul = $parent.find( 'ul.children:first' ); $ul = $parent.find( 'ul.children:first' );
if ( ! $ul.length ) { if ( ! $ul.length ) {
@ -251,11 +263,12 @@
} }
} }
$ul.append( $node ); $ul.prepend( $node );
// TODO: set focus on
} else { } else {
$wrap.prepend( $node ); $wrap.prepend( $node );
} }
$node.focus();
} ); } );
refreshCatsCache(); refreshCatsCache();
@ -509,6 +522,29 @@
} }
} }
function toggleCatItem( $element ) {
if ( $element.hasClass( 'selected' ) ) {
$element.removeClass( 'selected' ).attr( 'aria-checked', 'false' );
} else {
$element.addClass( 'selected' ).attr( 'aria-checked', 'true' );
}
}
function monitorCatList() {
$( '.categories-select' ).on( 'click.press-this keydown.press-this', function( event ) {
var $element = $( event.target );
if ( $element.is( 'div.category' ) ) {
if ( event.type === 'keydown' && event.keyCode !== 32 ) {
return;
}
toggleCatItem( $element );
event.preventDefault();
}
});
}
/* *************************************************************** /* ***************************************************************
* PROCESSING FUNCTIONS * PROCESSING FUNCTIONS
*************************************************************** */ *************************************************************** */
@ -564,6 +600,7 @@
monitorOptionsModal(); monitorOptionsModal();
monitorPlaceholder(); monitorPlaceholder();
monitorCatList();
$( '.options-open' ).on( 'click.press-this', openSidebar ); $( '.options-open' ).on( 'click.press-this', openSidebar );
$( '.options-close' ).on( 'click.press-this', closeSidebar ); $( '.options-close' ).on( 'click.press-this', closeSidebar );
@ -641,7 +678,7 @@
catsCache.push( { catsCache.push( {
node: $this, node: $this,
parents: $this.parents( 'li' ), parents: $this.parents( 'li' ),
text: $this.children( 'label' ).text().toLowerCase() text: $this.children( '.category' ).text().toLowerCase()
} ); } );
} ); } );
} }