Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add keyboard-accessible means of reordering widgets #21

Merged
merged 11 commits into from Jan 30, 2014

Fix live previews for moving widget to another sidebar

* Send all sidebars_widgets settings, not just the setting for the widget being edited
* Let incoming customized POST data completely override sidebars_widgets settings; stop merging
* Stop deleting widget instance settings from preview customizer because unnecessary and
  because of possible race condition when moving widgets across sidebars
  • Loading branch information...
westonruter committed Jan 30, 2014
commit fba1988b0181d058669f800fa28b94bf1271e5a5
@@ -179,14 +179,18 @@ var WidgetCustomizerPreview = (function ($) {
return;
}

var customized = {};
var sidebar_id = null;
var sidebar_widgets = [];
wp.customize.each( function ( setting, setting_id ) {
var matches = setting_id.match( /^sidebars_widgets\[(.+)\]/ );
if ( matches && setting().indexOf( widget_id ) !== -1 ) {
sidebar_id = matches[1];
sidebar_widgets = setting();
if ( ! matches ) {
return;
}
var other_sidebar_id = matches[1];
if ( setting().indexOf( widget_id ) !== -1 ) {
sidebar_id = other_sidebar_id;
}
customized[sidebar_id_to_setting_id( other_sidebar_id )] = setting();
} );
if ( ! sidebar_id ) {
throw new Error( 'Widget does not exist in a sidebar.' );
@@ -199,8 +203,7 @@ var WidgetCustomizerPreview = (function ($) {
setting_id: setting_id,
setting: JSON.stringify( to )
};
var customized = {};
customized[ sidebar_id_to_setting_id( sidebar_id ) ] = sidebar_widgets;

customized[setting_id] = to;
data.customized = JSON.stringify(customized);
data[self.render_widget_nonce_post_key] = self.render_widget_nonce_value;
@@ -286,16 +289,9 @@ var WidgetCustomizerPreview = (function ($) {
return;
}

// Remove widgets (their DOM element and their setting) when removed from sidebar
// Delete the widget from the DOM if it no longer exists in the sidebar
$.each( from, function ( i, old_widget_id ) {
if ( -1 === to.indexOf( old_widget_id ) ) {
var setting_id = widget_id_to_setting_id( old_widget_id );
if ( wp.customize.has( setting_id ) ) {
wp.customize.remove( setting_id );
delete already_bound_widgets[setting_id];
}

// Delete the widget from the DOM if it wasn't added to its new location in the other sidebar
self.getSidebarWidgetElement( sidebar_id, old_widget_id ).remove();
}
} );
@@ -168,13 +168,12 @@ var WidgetCustomizer = (function ($) {
return widget_form_controls.container[0];
} );

// Re-sort widget form controls
// Re-sort widget form controls (including widgets form other sidebars newly moved here)
sidebar_widgets_add_control.before( final_control_containers );
control.applyCardinalOrderClassNames();

// If the widget was dragged into the sidebar, make sure the sidebar_id param is updated
_( widget_form_controls ).each( function ( widget_form_control ) {
// @todo We need to delete the widget from the old sidebar, and re-fetch via Ajax
widget_form_control.params.sidebar_id = control.params.sidebar_id;
} );

@@ -269,10 +269,7 @@ static function prepreview_added_sidebars_widgets( $sidebars_widgets ) {
foreach ( self::$_customized as $setting_id => $value ) {
if ( preg_match( '/^sidebars_widgets\[(.+?)\]$/', $setting_id, $matches ) ) {
$sidebar_id = $matches[1];
if ( ! isset( $sidebars_widgets[$sidebar_id] ) ) {
$sidebars_widgets[$sidebar_id] = array();
}
$sidebars_widgets[$sidebar_id] = array_unique( array_merge( $value, $sidebars_widgets[$sidebar_id] ) );
$sidebars_widgets[$sidebar_id] = $value;
}
}
return $sidebars_widgets;
ProTip! Use n and p to navigate between commits in a pull request.
You can’t perform that action at this time.