Widget Customizer: Fix widget filtering.

props westonruter.
fixes #27451.
Built from https://develop.svn.wordpress.org/trunk@28044


git-svn-id: http://core.svn.wordpress.org/trunk@27874 1a063a9b-81f0-0310-95a4-ce76da25c4cd
This commit is contained in:
Dominik Schilling 2014-04-08 08:12:15 +00:00
parent 268092761d
commit 3f3a1380d2
3 changed files with 19 additions and 30 deletions

View File

@ -36,7 +36,8 @@
transport: 'refresh', transport: 'refresh',
params: [], params: [],
width: null, width: null,
height: null height: null,
search_matched: true
}); });
/** /**
@ -71,20 +72,16 @@
// If search is blank, show all themes // If search is blank, show all themes
// Useful for resetting the views when you clean the input // Useful for resetting the views when you clean the input
if ( this.terms === '' ) { if ( this.terms === '' ) {
this.reset( api.Widgets.data.availableWidgets ); this.each( function ( widget ) {
widget.set( 'search_matched', true );
} );
} }
// Trigger an 'update' event
this.trigger( 'update' );
}, },
// Performs a search within the collection // Performs a search within the collection
// @uses RegExp // @uses RegExp
search: function( term ) { search: function( term ) {
var match, results, haystack; var match, haystack;
// Start with a full collection
this.reset( api.Widgets.data.availableWidgets, { silent: true } );
// Escape the term string for RegExp meta characters // Escape the term string for RegExp meta characters
term = term.replace( /[-\/\\^$*+?.()|[\]{}]/g, '\\$&' ); term = term.replace( /[-\/\\^$*+?.()|[\]{}]/g, '\\$&' );
@ -94,13 +91,10 @@
term = term.replace( / /g, ')(?=.*' ); term = term.replace( / /g, ')(?=.*' );
match = new RegExp( '^(?=.*' + term + ').+', 'i' ); match = new RegExp( '^(?=.*' + term + ').+', 'i' );
results = this.filter( function( data ) { this.each( function ( data ) {
haystack = _.union( data.get( 'name' ), data.get( 'id' ), data.get( 'description' ) ); haystack = [ data.get( 'name' ), data.get( 'id' ), data.get( 'description' ) ].join( ' ' );
data.set( 'search_matched', match.test( haystack ) );
return match.test( haystack ); } );
});
this.reset( results );
} }
}); });
api.Widgets.availableWidgets = new api.Widgets.WidgetCollection( api.Widgets.data.availableWidgets ); api.Widgets.availableWidgets = new api.Widgets.WidgetCollection( api.Widgets.data.availableWidgets );
@ -176,7 +170,6 @@
_.bindAll( this, 'close' ); _.bindAll( this, 'close' );
this.listenTo( this.collection, 'update', this.updateList );
this.listenTo( this.collection, 'change', this.updateList ); this.listenTo( this.collection, 'change', this.updateList );
this.updateList(); this.updateList();
@ -222,29 +215,25 @@
} }
}, },
// Changes visibilty of available widgets // Changes visibility of available widgets
updateList: function() { updateList: function() {
// First hide all widgets...
this.$el.find( '.widget-tpl' ).hide();
// ..and then show only available widgets which could be filtered
this.collection.each( function( widget ) { this.collection.each( function( widget ) {
var widgetTpl = $( '#widget-tpl-' + widget.id ); var widgetTpl = $( '#widget-tpl-' + widget.id );
widgetTpl.toggle( ! widget.get( 'is_disabled' ) ); widgetTpl.toggle( widget.get( 'search_matched' ) && ! widget.get( 'is_disabled' ) );
if ( widget.get( 'is_disabled' ) && widgetTpl.is( this.selected ) ) { if ( widget.get( 'is_disabled' ) && widgetTpl.is( this.selected ) ) {
this.selected = null; this.selected = null;
} }
} ); } );
}, },
// Hightlights a widget // Highlights a widget
select: function( widgetTpl ) { select: function( widgetTpl ) {
this.selected = $( widgetTpl ); this.selected = $( widgetTpl );
this.selected.siblings( '.widget-tpl' ).removeClass( 'selected' ); this.selected.siblings( '.widget-tpl' ).removeClass( 'selected' );
this.selected.addClass( 'selected' ); this.selected.addClass( 'selected' );
}, },
// Hightlights a widget on focus // Highlights a widget on focus
focus: function( event ) { focus: function( event ) {
this.select( $( event.currentTarget ) ); this.select( $( event.currentTarget ) );
}, },
@ -1601,7 +1590,7 @@
/** /**
* @param {string} widgetId or an id_base for adding a previously non-existing widget * @param {string} widgetId or an id_base for adding a previously non-existing widget
* @returns {object} widget_form control instance * @returns {object|false} widget_form control instance, or false on error
*/ */
addWidget: function( widgetId ) { addWidget: function( widgetId ) {
var self = this, controlHtml, $widget, controlType = 'widget_form', $control, controlConstructor, var self = this, controlHtml, $widget, controlType = 'widget_form', $control, controlConstructor,
@ -1612,11 +1601,11 @@
settingId, isExistingWidget, widgetFormControl, sidebarWidgets, settingArgs; settingId, isExistingWidget, widgetFormControl, sidebarWidgets, settingArgs;
if ( ! widget ) { if ( ! widget ) {
return; return false;
} }
if ( widgetNumber && ! widget.get( 'is_multi' ) ) { if ( widgetNumber && ! widget.get( 'is_multi' ) ) {
return; return false;
} }
// Set up new multi widget // Set up new multi widget

File diff suppressed because one or more lines are too long

View File

@ -1150,7 +1150,7 @@ final class WP_Customize_Widgets {
$added_input_vars = array(); $added_input_vars = array();
if ( ! empty( $_POST['sanitized_widget_setting'] ) ) { if ( ! empty( $_POST['sanitized_widget_setting'] ) ) {
$sanitized_widget_setting = json_decode( $this->get_post_value( 'sanitized_widget_setting' ), true ); $sanitized_widget_setting = json_decode( $this->get_post_value( 'sanitized_widget_setting' ), true );
if ( empty( $sanitized_widget_setting ) ) { if ( false === $sanitized_widget_setting ) {
$this->stop_capturing_option_updates(); $this->stop_capturing_option_updates();
return new WP_Error( 'widget_setting_malformed' ); return new WP_Error( 'widget_setting_malformed' );
} }