@@ -4,14 +4,85 @@ var WidgetCustomizer = (function ($) {
44
55var customize = wp . customize ;
66var 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 ($) {
2394control . 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 ( ) {
2798control . updateWidget ( ) ;
2899} ) ;
29100
@@ -48,8 +119,8 @@ var WidgetCustomizer = (function ($) {
48119control . container . find ( '.widget-content' ) . prop ( 'disabled' , true ) ;
49120
50121var 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 ;
53124if ( instance_override ) {
54125params . json_instance_override = JSON . stringify ( instance_override ) ;
55126}
@@ -169,7 +240,7 @@ var WidgetCustomizer = (function ($) {
169240 */
170241getPreviewWidgetElement : function ( ) {
171242var control = this ;
172- var iframe_contents = $ ( '#customize-preview iframe' ) . contents ( ) ;
243+ var iframe_contents = $ ( '#customize-preview' ) . find ( ' iframe') . contents ( ) ;
173244return iframe_contents . find ( '#' + control . params . widget_id ) ;
174245} ,
175246
@@ -259,8 +330,6 @@ var WidgetCustomizer = (function ($) {
259330return 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
265334return self ;
266335} ( jQuery ) ) ;
0 commit comments