Skip to content

Commit 2509e1f

Browse files
committed
Merge pull request #1 from x-team/issue-1-reordering
Add drag-and-drop of customizer controls and have that update order in preview
2 parents 77278b8 + a5dc70f commit 2509e1f

File tree

7 files changed

+208
-28
lines changed

7 files changed

+208
-28
lines changed

.gitignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,5 @@
11
.DS_Store
22
/svn
3+
.project
4+
.settings/
5+
.buildpath
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
<?php
2+
3+
/**
4+
* Represent the widgets added to the sidebar and their order
5+
*/
6+
class Sidebar_Widgets_WP_Customize_Control extends WP_Customize_Control {
7+
public $type = 'sidebar_widgets';
8+
public $sidebar_id;
9+
10+
public function to_json() {
11+
parent::to_json();
12+
$exported_properties = array( 'sidebar_id' );
13+
foreach( $exported_properties as $key ) {
14+
$this->json[$key] = $this->$key;
15+
}
16+
}
17+
18+
public function render_content() {
19+
global $wp_widget_factory;
20+
21+
$id = 'customize-control-' . str_replace( '[', '-', str_replace( ']', '', $this->id ) );
22+
$class = 'customize-control customize-control-' . $this->type;
23+
24+
?>
25+
<li hidden id="<?php echo esc_attr( $id ); ?>" class="<?php echo esc_attr( $class ); ?>">
26+
<!-- @todo https://github.com/x-team/wp-widget-customizer/issues/3
27+
<label>
28+
<span class="customize-control-title"><?php echo esc_html_e( 'Add widget:', 'widget-customizer' ); ?></span>
29+
<select>
30+
<option></option>
31+
<?php foreach( $wp_widget_factory->widgets as $class_name => $widget ): ?>
32+
<option value="<?php echo esc_attr( $class_name ) ?>"><?php echo esc_html( $widget->name ) ?></option>
33+
<?php endforeach; ?>
34+
</select>
35+
</label>
36+
-->
37+
</li>
38+
39+
<?php
40+
}
41+
}

readme.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
![Banner](assets/banner-1544x500.png)
55
Edit widgets and preview changes in Theme Customizer, with a control for each widget form in sections added for each sidebar rendered in the preview.
66

7-
**Contributors:** [x-team](http://profiles.wordpress.org/x-team), [westonruter](http://profiles.wordpress.org/westonruter), [ricardocorreia](http://profiles.wordpress.org/ricardocorreia), [johnregan3](http://profiles.wordpress.org/johnregan3)
7+
**Contributors:** [x-team](http://profiles.wordpress.org/x-team), [westonruter](http://profiles.wordpress.org/westonruter), [bobbravo2](http://profiles.wordpress.org/bobbravo2), [ricardocorreia](http://profiles.wordpress.org/ricardocorreia), [johnregan3](http://profiles.wordpress.org/johnregan3)
88
**Tags:** [customizer](http://wordpress.org/plugins/tags/customizer), [widgets](http://wordpress.org/plugins/tags/widgets), [sidebars](http://wordpress.org/plugins/tags/sidebars), [preview](http://wordpress.org/plugins/tags/preview)
99
**Requires at least:** 3.6
1010
**Tested up to:** 3.7

readme.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
=== Widget Customizer ===
2-
Contributors: X-team, westonruter, ricardocorreia, johnregan3
2+
Contributors: X-team, westonruter, bobbravo2, ricardocorreia, johnregan3
33
Tags: customizer, widgets, sidebars, preview
44
Requires at least: 3.6
55
Tested up to: 3.7

widget-customizer.css

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,11 @@
1+
customize-control-sidebar_widgets {
2+
display: none;
3+
}
4+
15
.customize-control-widget_form.previewer-loading .spinner {
26
display: inline;
37
}
8+
49
.customize-control-widget_form.widget-form-loading .widget-content {
510
opacity: 0.7;
611
pointer-events: none;
@@ -21,6 +26,11 @@
2126
.customize-control-widget_form .widget .widget-top a {
2227
cursor: pointer;
2328
}
29+
30+
.customize-control-widget_form .widget .customize-control-title {
31+
cursor: move;
32+
}
33+
2434
.control-section.accordion-section.widget-customizer-highlighted > .accordion-section-title,
2535
.customize-control-widget_form.widget-customizer-highlighted {
2636
border-radius: 2 px;

widget-customizer.js

Lines changed: 80 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -4,14 +4,85 @@ var WidgetCustomizer = (function ($) {
44

55
var customize = wp.customize;
66
var self = {
7-
control: null,
8-
ajax_action: null,
9-
nonce_value: null,
10-
nonce_post_key: null
7+
update_widget_ajax_action: null,
8+
update_widget_nonce_value: null,
9+
update_widget_nonce_post_key: null
1110
};
1211
$.extend(self, WidgetCustomizer_exports);
1312

14-
self.constuctor = customize.Control.extend({
13+
/**
14+
* Sidebar Widgets control
15+
* Note that 'sidebar_widgets' must match the Sidebar_Widgets_WP_Customize_Control::$type
16+
*/
17+
customize.controlConstructor.sidebar_widgets = customize.Control.extend({
18+
19+
/**
20+
* Set up the control
21+
*/
22+
ready: function() {
23+
var control = this;
24+
control.setupOrdering();
25+
// @todo Set up control for adding new widgets (via a dropdown, and with jQuery Chosen)
26+
// @todo Set up control for deleting widgets (add a delete link to each widget form control)
27+
// @link https://github.com/x-team/wp-widget-customizer/issues/3
28+
},
29+
30+
/**
31+
* Allow widgets in sidebar to be re-ordered, and for the order to be previewed
32+
*/
33+
setupOrdering: function () {
34+
var control = this;
35+
36+
// Update widget order setting when controls are re-ordered
37+
this.container.closest( '.accordion-section-content' ).sortable({
38+
items: '> .customize-control-widget_form',
39+
axis: 'y',
40+
update: function () {
41+
var widget_container_ids = $(this).sortable('toArray');
42+
var widget_ids = $.map( widget_container_ids, function ( widget_container_id ) {
43+
return $('#' + widget_container_id).find(':input[name=widget-id]').val();
44+
});
45+
control.setting( widget_ids );
46+
}
47+
});
48+
49+
// Update ordering of widget control forms when the setting is updated
50+
control.setting.bind( function( to ) {
51+
var section_container = control.container.closest('.accordion-section-content');
52+
var controls = section_container.find('> .customize-control-widget_form');
53+
54+
// Build up index
55+
var widget_positions = {};
56+
$.each( to, function ( i, widget_id ) {
57+
widget_positions[widget_id] = i;
58+
});
59+
controls.each( function () {
60+
var widget_id = $(this).find('input[name="widget-id"]').val();
61+
$(this).data('widget-id', widget_id);
62+
});
63+
64+
// Sort widget controls to their new positions
65+
controls.sort(function ( a, b ) {
66+
var a_widget_id = $(a).data('widget-id');
67+
var b_widget_id = $(b).data('widget-id');
68+
if ( widget_positions[a_widget_id] === widget_positions[b_widget_id] ) {
69+
return 0;
70+
}
71+
return widget_positions[a_widget_id] < widget_positions[b_widget_id] ? -1 : 1;
72+
});
73+
74+
// Append the controls to put them in the right order
75+
section_container.append( controls );
76+
});
77+
}
78+
79+
});
80+
81+
/**
82+
* Widget Form control
83+
* Note that 'widget_form' must match the Widget_Form_WP_Customize_Control::$type
84+
*/
85+
customize.controlConstructor.widget_form = customize.Control.extend({
1586

1687
/**
1788
* Set up the control
@@ -23,7 +94,7 @@ var WidgetCustomizer = (function ($) {
2394
control.updateWidget( to );
2495
});
2596

26-
control.container.find( '.widget-control-update' ).on( 'click', function (e) {
97+
control.container.find( '.widget-control-update' ).on( 'click', function () {
2798
control.updateWidget();
2899
});
29100

@@ -48,8 +119,8 @@ var WidgetCustomizer = (function ($) {
48119
control.container.find( '.widget-content' ).prop( 'disabled', true );
49120

50121
var params = {};
51-
params.action = self.ajax_action;
52-
params[self.nonce_post_key] = self.nonce_value;
122+
params.action = self.update_widget_ajax_action;
123+
params[self.update_widget_nonce_post_key] = self.update_widget_nonce_value;
53124
if ( instance_override ) {
54125
params.json_instance_override = JSON.stringify( instance_override );
55126
}
@@ -169,7 +240,7 @@ var WidgetCustomizer = (function ($) {
169240
*/
170241
getPreviewWidgetElement: function () {
171242
var control = this;
172-
var iframe_contents = $('#customize-preview iframe').contents();
243+
var iframe_contents = $('#customize-preview').find('iframe').contents();
173244
return iframe_contents.find('#' + control.params.widget_id);
174245
},
175246

@@ -259,8 +330,6 @@ var WidgetCustomizer = (function ($) {
259330
return widget_control;
260331
};
261332

262-
// Note that 'widget_form' must match the Widget_Form_WP_Customize_Control::$type
263-
customize.controlConstructor.widget_form = self.constuctor;
264333

265334
return self;
266335
}( jQuery ));

0 commit comments

Comments
 (0)