1 /* global _wpCustomizeWidgetsSettings */
4 if ( ! wp || ! wp.customize ) { return; }
6 // Set up our namespace...
7 var api = wp.customize,
10 api.Widgets = api.Widgets || {};
13 api.Widgets.data = _wpCustomizeWidgetsSettings || {};
14 l10n = api.Widgets.data.l10n;
15 delete api.Widgets.data.l10n;
18 * wp.customize.Widgets.WidgetModel
20 * A single widget model.
23 * @augments Backbone.Model
25 api.Widgets.WidgetModel = Backbone.Model.extend({
44 * wp.customize.Widgets.WidgetCollection
46 * Collection for widget models.
49 * @augments Backbone.Model
51 api.Widgets.WidgetCollection = Backbone.Collection.extend({
52 model: api.Widgets.WidgetModel,
54 // Controls searching on the current widget collection
55 // and triggers an update event
56 doSearch: function( value ) {
58 // Don't do anything if we've already done this search
59 // Useful because the search handler fires multiple times per keystroke
60 if ( this.terms === value ) {
64 // Updates terms with the value passed
67 // If we have terms, run a search...
68 if ( this.terms.length > 0 ) {
69 this.search( this.terms );
72 // If search is blank, show all themes
73 // Useful for resetting the views when you clean the input
74 if ( this.terms === '' ) {
75 this.each( function ( widget ) {
76 widget.set( 'search_matched', true );
81 // Performs a search within the collection
83 search: function( term ) {
86 // Escape the term string for RegExp meta characters
87 term = term.replace( /[-\/\\^$*+?.()|[\]{}]/g, '\\$&' );
89 // Consider spaces as word delimiters and match the whole string
90 // so matching terms can be combined
91 term = term.replace( / /g, ')(?=.*' );
92 match = new RegExp( '^(?=.*' + term + ').+', 'i' );
94 this.each( function ( data ) {
95 haystack = [ data.get( 'name' ), data.get( 'id' ), data.get( 'description' ) ].join( ' ' );
96 data.set( 'search_matched', match.test( haystack ) );
100 api.Widgets.availableWidgets = new api.Widgets.WidgetCollection( api.Widgets.data.availableWidgets );
103 * wp.customize.Widgets.SidebarModel
105 * A single sidebar model.
108 * @augments Backbone.Model
110 api.Widgets.SidebarModel = Backbone.Model.extend({
123 * wp.customize.Widgets.SidebarCollection
125 * Collection for sidebar models.
128 * @augments Backbone.Collection
130 api.Widgets.SidebarCollection = Backbone.Collection.extend({
131 model: api.Widgets.SidebarModel
133 api.Widgets.registeredSidebars = new api.Widgets.SidebarCollection( api.Widgets.data.registeredSidebars );
136 * wp.customize.Widgets.AvailableWidgetsPanelView
138 * View class for the available widgets panel.
141 * @augments wp.Backbone.View
142 * @augments Backbone.View
144 api.Widgets.AvailableWidgetsPanelView = wp.Backbone.View.extend({
146 el: '#available-widgets',
149 'input #widgets-search': 'search',
150 'keyup #widgets-search': 'search',
151 'change #widgets-search': 'search',
152 'search #widgets-search': 'search',
153 'focus .widget-tpl' : 'focus',
154 'click .widget-tpl' : '_submit',
155 'keypress .widget-tpl' : '_submit',
156 'keydown' : 'keyboardAccessible'
159 // Cache current selected widget
162 // Cache sidebar control which has opened panel
163 currentSidebarControl: null,
166 initialize: function() {
169 this.$search = $( '#widgets-search' );
171 _.bindAll( this, 'close' );
173 this.listenTo( this.collection, 'change', this.updateList );
177 // If the available widgets panel is open and the customize controls are
178 // interacted with (i.e. available widgets panel is blurred) then close the
179 // available widgets panel.
180 $( '#customize-controls' ).on( 'click keydown', function( e ) {
181 var isAddNewBtn = $( e.target ).is( '.add-new-widget, .add-new-widget *' );
182 if ( $( 'body' ).hasClass( 'adding-widget' ) && ! isAddNewBtn ) {
187 // Close the panel if the URL in the preview changes
188 api.Widgets.Previewer.bind( 'url', this.close );
191 // Performs a search and handles selected widget
192 search: function( event ) {
195 this.collection.doSearch( event.target.value );
197 // Remove a widget from being selected if it is no longer visible
198 if ( this.selected && ! this.selected.is( ':visible' ) ) {
199 this.selected.removeClass( 'selected' );
200 this.selected = null;
203 // If a widget was selected but the filter value has been cleared out, clear selection
204 if ( this.selected && ! event.target.value ) {
205 this.selected.removeClass( 'selected' );
206 this.selected = null;
209 // If a filter has been entered and a widget hasn't been selected, select the first one shown
210 if ( ! this.selected && event.target.value ) {
211 firstVisible = this.$el.find( '> .widget-tpl:visible:first' );
212 if ( firstVisible.length ) {
213 this.select( firstVisible );
218 // Changes visibility of available widgets
219 updateList: function() {
220 this.collection.each( function( widget ) {
221 var widgetTpl = $( '#widget-tpl-' + widget.id );
222 widgetTpl.toggle( widget.get( 'search_matched' ) && ! widget.get( 'is_disabled' ) );
223 if ( widget.get( 'is_disabled' ) && widgetTpl.is( this.selected ) ) {
224 this.selected = null;
229 // Highlights a widget
230 select: function( widgetTpl ) {
231 this.selected = $( widgetTpl );
232 this.selected.siblings( '.widget-tpl' ).removeClass( 'selected' );
233 this.selected.addClass( 'selected' );
236 // Highlights a widget on focus
237 focus: function( event ) {
238 this.select( $( event.currentTarget ) );
241 // Submit handler for keypress and click on widget
242 _submit: function( event ) {
243 // Only proceed with keypress if it is Enter or Spacebar
244 if ( event.type === 'keypress' && ( event.which !== 13 && event.which !== 32 ) ) {
248 this.submit( $( event.currentTarget ) );
251 // Adds a selected widget to the sidebar
252 submit: function( widgetTpl ) {
253 var widgetId, widget;
256 widgetTpl = this.selected;
259 if ( ! widgetTpl || ! this.currentSidebarControl ) {
263 this.select( widgetTpl );
265 widgetId = $( this.selected ).data( 'widget-id' );
266 widget = this.collection.findWhere( { id: widgetId } );
271 this.currentSidebarControl.addWidget( widget.get( 'id_base' ) );
277 open: function( sidebarControl ) {
278 this.currentSidebarControl = sidebarControl;
280 // Wide widget controls appear over the preview, and so they need to be collapsed when the panel opens
281 _( this.currentSidebarControl.getWidgetFormControls() ).each( function( control ) {
282 if ( control.params.is_wide ) {
283 control.collapseForm();
287 $( 'body' ).addClass( 'adding-widget' );
289 this.$el.find( '.selected' ).removeClass( 'selected' );
292 this.collection.doSearch( '' );
294 this.$search.focus();
298 close: function( options ) {
299 options = options || {};
301 if ( options.returnFocus && this.currentSidebarControl ) {
302 this.currentSidebarControl.container.find( '.add-new-widget' ).focus();
305 this.currentSidebarControl = null;
306 this.selected = null;
308 $( 'body' ).removeClass( 'adding-widget' );
310 this.$search.val( '' );
313 // Add keyboard accessiblity to the panel
314 keyboardAccessible: function( event ) {
315 var isEnter = ( event.which === 13 ),
316 isEsc = ( event.which === 27 ),
317 isDown = ( event.which === 40 ),
318 isUp = ( event.which === 38 ),
320 firstVisible = this.$el.find( '> .widget-tpl:visible:first' ),
321 lastVisible = this.$el.find( '> .widget-tpl:visible:last' ),
322 isSearchFocused = $( event.target ).is( this.$search );
324 if ( isDown || isUp ) {
326 if ( isSearchFocused ) {
327 selected = firstVisible;
328 } else if ( this.selected && this.selected.nextAll( '.widget-tpl:visible' ).length !== 0 ) {
329 selected = this.selected.nextAll( '.widget-tpl:visible:first' );
332 if ( isSearchFocused ) {
333 selected = lastVisible;
334 } else if ( this.selected && this.selected.prevAll( '.widget-tpl:visible' ).length !== 0 ) {
335 selected = this.selected.prevAll( '.widget-tpl:visible:first' );
339 this.select( selected );
344 this.$search.focus();
350 // If enter pressed but nothing entered, don't do anything
351 if ( isEnter && ! this.$search.val() ) {
357 } else if ( isEsc ) {
358 this.close( { returnFocus: true } );
364 * Handlers for the widget-synced event, organized by widget ID base.
365 * Other widgets may provide their own update handlers by adding
366 * listeners for the widget-synced event.
368 api.Widgets.formSyncHandlers = {
371 * @param {jQuery.Event} e
372 * @param {jQuery} widget
373 * @param {String} newForm
375 rss: function( e, widget, newForm ) {
376 var oldWidgetError = widget.find( '.widget-error:first' ),
377 newWidgetError = $( '<div>' + newForm + '</div>' ).find( '.widget-error:first' );
379 if ( oldWidgetError.length && newWidgetError.length ) {
380 oldWidgetError.replaceWith( newWidgetError );
381 } else if ( oldWidgetError.length ) {
382 oldWidgetError.remove();
383 } else if ( newWidgetError.length ) {
384 widget.find( '.widget-content:first' ).prepend( newWidgetError );
390 * wp.customize.Widgets.WidgetControl
392 * Customizer control for widgets.
393 * Note that 'widget_form' must match the WP_Widget_Form_Customize_Control::$type
396 * @augments wp.customize.Control
398 api.Widgets.WidgetControl = api.Control.extend({
404 this._setupWideWidget();
405 this._setupControlToggle();
406 this._setupWidgetTitle();
407 this._setupReorderUI();
408 this._setupHighlightEffects();
409 this._setupUpdateUI();
410 this._setupRemoveUI();
414 * Handle changes to the setting
416 _setupModel: function() {
417 var self = this, rememberSavedWidgetId;
419 api.Widgets.savedWidgetIds = api.Widgets.savedWidgetIds || [];
421 // Remember saved widgets so we know which to trash (move to inactive widgets sidebar)
422 rememberSavedWidgetId = function() {
423 api.Widgets.savedWidgetIds[self.params.widget_id] = true;
425 api.bind( 'ready', rememberSavedWidgetId );
426 api.bind( 'saved', rememberSavedWidgetId );
428 this._updateCount = 0;
429 this.isWidgetUpdating = false;
430 this.liveUpdateMode = true;
432 // Update widget whenever model changes
433 this.setting.bind( function( to, from ) {
434 if ( ! _( from ).isEqual( to ) && ! self.isWidgetUpdating ) {
435 self.updateWidget( { instance: to } );
441 * Add special behaviors for wide widget controls
443 _setupWideWidget: function() {
444 var self = this, $widgetInside, $widgetForm, $customizeSidebar,
445 $themeControlsContainer, positionWidget;
447 if ( ! this.params.is_wide ) {
451 $widgetInside = this.container.find( '.widget-inside' );
452 $widgetForm = $widgetInside.find( '> .form' );
453 $customizeSidebar = $( '.wp-full-overlay-sidebar-content:first' );
454 this.container.addClass( 'wide-widget-control' );
456 this.container.find( '.widget-content:first' ).css( {
457 'max-width': this.params.width,
458 'min-height': this.params.height
462 * Keep the widget-inside positioned so the top of fixed-positioned
463 * element is at the same top position as the widget-top. When the
464 * widget-top is scrolled out of view, keep the widget-top in view;
465 * likewise, don't allow the widget to drop off the bottom of the window.
466 * If a widget is too tall to fit in the window, don't let the height
467 * exceed the window height so that the contents of the widget control
468 * will become scrollable (overflow:auto).
470 positionWidget = function() {
471 var offsetTop = self.container.offset().top,
472 windowHeight = $( window ).height(),
473 formHeight = $widgetForm.outerHeight(),
475 $widgetInside.css( 'max-height', windowHeight );
477 0, // prevent top from going off screen
479 Math.max( offsetTop, 0 ), // distance widget in panel is from top of screen
480 windowHeight - formHeight // flush up against bottom of screen
483 $widgetInside.css( 'top', top );
486 $themeControlsContainer = $( '#customize-theme-controls' );
487 this.container.on( 'expand', function() {
489 $customizeSidebar.on( 'scroll', positionWidget );
490 $( window ).on( 'resize', positionWidget );
491 $themeControlsContainer.on( 'expanded collapsed', positionWidget );
493 this.container.on( 'collapsed', function() {
494 $customizeSidebar.off( 'scroll', positionWidget );
495 $( window ).off( 'resize', positionWidget );
496 $themeControlsContainer.off( 'expanded collapsed', positionWidget );
499 // Reposition whenever a sidebar's widgets are changed
500 api.each( function( setting ) {
501 if ( 0 === setting.id.indexOf( 'sidebars_widgets[' ) ) {
502 setting.bind( function() {
503 if ( self.container.hasClass( 'expanded' ) ) {
512 * Show/hide the control when clicking on the form title, when clicking
515 _setupControlToggle: function() {
516 var self = this, $closeBtn;
518 this.container.find( '.widget-top' ).on( 'click', function( e ) {
520 var sidebarWidgetsControl = self.getSidebarWidgetsControl();
521 if ( sidebarWidgetsControl.isReordering ) {
527 $closeBtn = this.container.find( '.widget-control-close' );
528 $closeBtn.on( 'click', function( e ) {
531 self.container.find( '.widget-top .widget-action:first' ).focus(); // keyboard accessibility
536 * Update the title of the form if a title field is entered
538 _setupWidgetTitle: function() {
539 var self = this, updateTitle;
541 updateTitle = function() {
542 var title = self.setting().title,
543 inWidgetTitle = self.container.find( '.in-widget-title' );
546 inWidgetTitle.text( ': ' + title );
548 inWidgetTitle.text( '' );
551 this.setting.bind( updateTitle );
556 * Set up the widget-reorder-nav
558 _setupReorderUI: function() {
559 var self = this, selectSidebarItem, $moveWidgetArea,
560 $reorderNav, updateAvailableSidebars;
563 * select the provided sidebar list item in the move widget area
567 selectSidebarItem = function( li ) {
568 li.siblings( '.selected' ).removeClass( 'selected' );
569 li.addClass( 'selected' );
570 var isSelfSidebar = ( li.data( 'id' ) === self.params.sidebar_id );
571 self.container.find( '.move-widget-btn' ).prop( 'disabled', isSelfSidebar );
575 * Add the widget reordering elements to the widget control
577 this.container.find( '.widget-title-action' ).after( $( api.Widgets.data.tpl.widgetReorderNav ) );
579 _.template( api.Widgets.data.tpl.moveWidgetArea, {
580 sidebars: _( api.Widgets.registeredSidebars.toArray() ).pluck( 'attributes' )
583 this.container.find( '.widget-top' ).after( $moveWidgetArea );
586 * Update available sidebars when their rendered state changes
588 updateAvailableSidebars = function() {
589 var $sidebarItems = $moveWidgetArea.find( 'li' ), selfSidebarItem;
591 selfSidebarItem = $sidebarItems.filter( function(){
592 return $( this ).data( 'id' ) === self.params.sidebar_id;
595 $sidebarItems.each( function() {
600 sidebarId = li.data( 'id' );
601 sidebar = api.Widgets.registeredSidebars.get( sidebarId );
603 li.toggle( sidebar.get( 'is_rendered' ) );
605 if ( li.hasClass( 'selected' ) && ! sidebar.get( 'is_rendered' ) ) {
606 selectSidebarItem( selfSidebarItem );
611 updateAvailableSidebars();
612 api.Widgets.registeredSidebars.on( 'change:is_rendered', updateAvailableSidebars );
615 * Handle clicks for up/down/move on the reorder nav
617 $reorderNav = this.container.find( '.widget-reorder-nav' );
618 $reorderNav.find( '.move-widget, .move-widget-down, .move-widget-up' ).on( 'click keypress', function( event ) {
619 if ( event.type === 'keypress' && ( event.which !== 13 && event.which !== 32 ) ) {
624 if ( $( this ).is( '.move-widget' ) ) {
625 self.toggleWidgetMoveArea();
627 var isMoveDown = $( this ).is( '.move-widget-down' ),
628 isMoveUp = $( this ).is( '.move-widget-up' ),
629 i = self.getWidgetSidebarPosition();
631 if ( ( isMoveUp && i === 0 ) || ( isMoveDown && i === self.getSidebarWidgetsControl().setting().length - 1 ) ) {
641 $( this ).focus(); // re-focus after the container was moved
646 * Handle selecting a sidebar to move to
648 this.container.find( '.widget-area-select' ).on( 'click keypress', 'li', function( e ) {
649 if ( event.type === 'keypress' && ( event.which !== 13 && event.which !== 32 ) ) {
653 selectSidebarItem( $( this ) );
657 * Move widget to another sidebar
659 this.container.find( '.move-widget-btn' ).click( function() {
660 self.getSidebarWidgetsControl().toggleReordering( false );
662 var oldSidebarId = self.params.sidebar_id,
663 newSidebarId = self.container.find( '.widget-area-select li.selected' ).data( 'id' ),
664 oldSidebarWidgetsSetting, newSidebarWidgetsSetting,
665 oldSidebarWidgetIds, newSidebarWidgetIds, i;
667 oldSidebarWidgetsSetting = api( 'sidebars_widgets[' + oldSidebarId + ']' );
668 newSidebarWidgetsSetting = api( 'sidebars_widgets[' + newSidebarId + ']' );
669 oldSidebarWidgetIds = Array.prototype.slice.call( oldSidebarWidgetsSetting() );
670 newSidebarWidgetIds = Array.prototype.slice.call( newSidebarWidgetsSetting() );
672 i = self.getWidgetSidebarPosition();
673 oldSidebarWidgetIds.splice( i, 1 );
674 newSidebarWidgetIds.push( self.params.widget_id );
676 oldSidebarWidgetsSetting( oldSidebarWidgetIds );
677 newSidebarWidgetsSetting( newSidebarWidgetIds );
684 * Highlight widgets in preview when interacted with in the customizer
686 _setupHighlightEffects: function() {
689 // Highlight whenever hovering or clicking over the form
690 this.container.on( 'mouseenter click', function() {
691 self.setting.previewer.send( 'highlight-widget', self.params.widget_id );
694 // Highlight when the setting is updated
695 this.setting.bind( function() {
696 self.setting.previewer.send( 'highlight-widget', self.params.widget_id );
701 * Set up event handlers for widget updating
703 _setupUpdateUI: function() {
704 var self = this, $widgetRoot, $widgetContent,
705 $saveBtn, updateWidgetDebounced, formSyncHandler;
707 $widgetRoot = this.container.find( '.widget:first' );
708 $widgetContent = $widgetRoot.find( '.widget-content:first' );
710 // Configure update button
711 $saveBtn = this.container.find( '.widget-control-save' );
712 $saveBtn.val( l10n.saveBtnLabel );
713 $saveBtn.attr( 'title', l10n.saveBtnTooltip );
714 $saveBtn.removeClass( 'button-primary' ).addClass( 'button-secondary' );
715 $saveBtn.on( 'click', function( e ) {
717 self.updateWidget( { disable_form: true } ); // @todo disable_form is unused?
720 updateWidgetDebounced = _.debounce( function() {
724 // Trigger widget form update when hitting Enter within an input
725 $widgetContent.on( 'keydown', 'input', function( e ) {
726 if ( 13 === e.which ) { // Enter
728 self.updateWidget( { ignoreActiveElement: true } );
732 // Handle widgets that support live previews
733 $widgetContent.on( 'change input propertychange', ':input', function( e ) {
734 if ( self.liveUpdateMode ) {
735 if ( e.type === 'change' ) {
737 } else if ( this.checkValidity && this.checkValidity() ) {
738 updateWidgetDebounced();
743 // Remove loading indicators when the setting is saved and the preview updates
744 this.setting.previewer.channel.bind( 'synced', function() {
745 self.container.removeClass( 'previewer-loading' );
748 api.Widgets.Previewer.bind( 'widget-updated', function( updatedWidgetId ) {
749 if ( updatedWidgetId === self.params.widget_id ) {
750 self.container.removeClass( 'previewer-loading' );
754 // Update widget control to indicate whether it is currently rendered
755 api.Widgets.Previewer.bind( 'rendered-widgets', function( renderedWidgets ) {
756 var isRendered = !! renderedWidgets[self.params.widget_id];
758 self.container.toggleClass( 'widget-rendered', isRendered );
761 formSyncHandler = api.Widgets.formSyncHandlers[ this.params.widget_id_base ];
762 if ( formSyncHandler ) {
763 $( document ).on( 'widget-synced', function( e, widget ) {
764 if ( $widgetRoot.is( widget ) ) {
765 formSyncHandler.apply( document, arguments );
772 * Set up event handlers for widget removal
774 _setupRemoveUI: function() {
775 var self = this, $removeBtn, replaceDeleteWithRemove;
777 // Configure remove button
778 $removeBtn = this.container.find( 'a.widget-control-remove' );
779 $removeBtn.on( 'click', function( e ) {
782 // Find an adjacent element to add focus to when this widget goes away
783 var $adjacentFocusTarget;
784 if ( self.container.next().is( '.customize-control-widget_form' ) ) {
785 $adjacentFocusTarget = self.container.next().find( '.widget-action:first' );
786 } else if ( self.container.prev().is( '.customize-control-widget_form' ) ) {
787 $adjacentFocusTarget = self.container.prev().find( '.widget-action:first' );
789 $adjacentFocusTarget = self.container.next( '.customize-control-sidebar_widgets' ).find( '.add-new-widget:first' );
792 self.container.slideUp( function() {
793 var sidebarsWidgetsControl = api.Widgets.getSidebarWidgetControlContainingWidget( self.params.widget_id ),
796 if ( ! sidebarsWidgetsControl ) {
800 sidebarWidgetIds = sidebarsWidgetsControl.setting().slice();
801 i = _.indexOf( sidebarWidgetIds, self.params.widget_id );
806 sidebarWidgetIds.splice( i, 1 );
807 sidebarsWidgetsControl.setting( sidebarWidgetIds );
809 $adjacentFocusTarget.focus(); // keyboard accessibility
813 replaceDeleteWithRemove = function() {
814 $removeBtn.text( l10n.removeBtnLabel ); // wp_widget_control() outputs the link as "Delete"
815 $removeBtn.attr( 'title', l10n.removeBtnTooltip );
818 if ( this.params.is_new ) {
819 api.bind( 'saved', replaceDeleteWithRemove );
821 replaceDeleteWithRemove();
826 * Find all inputs in a widget container that should be considered when
827 * comparing the loaded form with the sanitized form, whose fields will
828 * be aligned to copy the sanitized over. The elements returned by this
829 * are passed into this._getInputsSignature(), and they are iterated
830 * over when copying sanitized values over to the the form loaded.
832 * @param {jQuery} container element in which to look for inputs
833 * @returns {jQuery} inputs
836 _getInputs: function( container ) {
837 return $( container ).find( ':input[name]' );
841 * Iterate over supplied inputs and create a signature string for all of them together.
842 * This string can be used to compare whether or not the form has all of the same fields.
844 * @param {jQuery} inputs
848 _getInputsSignature: function( inputs ) {
849 var inputsSignatures = _( inputs ).map( function( input ) {
850 var $input = $( input ), signatureParts;
852 if ( $input.is( ':checkbox, :radio' ) ) {
853 signatureParts = [ $input.attr( 'id' ), $input.attr( 'name' ), $input.prop( 'value' ) ];
855 signatureParts = [ $input.attr( 'id' ), $input.attr( 'name' ) ];
858 return signatureParts.join( ',' );
861 return inputsSignatures.join( ';' );
865 * Get the property that represents the state of an input.
867 * @param {jQuery|DOMElement} input
871 _getInputStatePropertyName: function( input ) {
872 var $input = $( input );
874 if ( $input.is( ':radio, :checkbox' ) ) {
881 /***********************************************************************
882 * Begin public API methods
883 **********************************************************************/
886 * @return {wp.customize.controlConstructor.sidebar_widgets[]}
888 getSidebarWidgetsControl: function() {
889 var settingId, sidebarWidgetsControl;
891 settingId = 'sidebars_widgets[' + this.params.sidebar_id + ']';
892 sidebarWidgetsControl = api.control( settingId );
894 if ( ! sidebarWidgetsControl ) {
898 return sidebarWidgetsControl;
902 * Submit the widget form via Ajax and get back the updated instance,
903 * along with the new widget control form to render.
905 * @param {object} [args]
906 * @param {Object|null} [args.instance=null] When the model changes, the instance is sent here; otherwise, the inputs from the form are used
907 * @param {Function|null} [args.complete=null] Function which is called when the request finishes. Context is bound to the control. First argument is any error. Following arguments are for success.
908 * @param {Boolean} [args.ignoreActiveElement=false] Whether or not updating a field will be deferred if focus is still on the element.
910 updateWidget: function( args ) {
911 var self = this, instanceOverride, completeCallback, $widgetRoot, $widgetContent,
912 updateNumber, params, data, $inputs, processing, jqxhr, isChanged;
917 ignoreActiveElement: false
920 instanceOverride = args.instance;
921 completeCallback = args.complete;
923 this._updateCount += 1;
924 updateNumber = this._updateCount;
926 $widgetRoot = this.container.find( '.widget:first' );
927 $widgetContent = $widgetRoot.find( '.widget-content:first' );
929 // Remove a previous error message
930 $widgetContent.find( '.widget-error' ).remove();
932 this.container.addClass( 'widget-form-loading' );
933 this.container.addClass( 'previewer-loading' );
934 processing = api.state( 'processing' );
935 processing( processing() + 1 );
937 if ( ! this.liveUpdateMode ) {
938 this.container.addClass( 'widget-form-disabled' );
942 params.action = 'update-widget';
943 params.wp_customize = 'on';
944 params.nonce = api.Widgets.data.nonce;
945 params.theme = api.settings.theme.stylesheet;
947 data = $.param( params );
948 $inputs = this._getInputs( $widgetContent );
950 // Store the value we're submitting in data so that when the response comes back,
951 // we know if it got sanitized; if there is no difference in the sanitized value,
952 // then we do not need to touch the UI and mess up the user's ongoing editing.
953 $inputs.each( function() {
954 var input = $( this ),
955 property = self._getInputStatePropertyName( this );
956 input.data( 'state' + updateNumber, input.prop( property ) );
959 if ( instanceOverride ) {
960 data += '&' + $.param( { 'sanitized_widget_setting': JSON.stringify( instanceOverride ) } );
962 data += '&' + $inputs.serialize();
964 data += '&' + $widgetContent.find( '~ :input' ).serialize();
966 jqxhr = $.post( wp.ajax.settings.url, data );
968 jqxhr.done( function( r ) {
969 var message, sanitizedForm, $sanitizedInputs, hasSameInputsInResponse,
970 isLiveUpdateAborted = false;
972 // Check if the user is logged out.
974 api.Widgets.Previewer.preview.iframe.hide();
975 api.Widgets.Previewer.login().done( function() {
976 self.updateWidget( args );
977 api.Widgets.Previewer.preview.iframe.show();
982 // Check for cheaters.
984 api.Widgets.Previewer.cheatin();
989 sanitizedForm = $( '<div>' + r.data.form + '</div>' );
990 $sanitizedInputs = self._getInputs( sanitizedForm );
991 hasSameInputsInResponse = self._getInputsSignature( $inputs ) === self._getInputsSignature( $sanitizedInputs );
993 // Restore live update mode if sanitized fields are now aligned with the existing fields
994 if ( hasSameInputsInResponse && ! self.liveUpdateMode ) {
995 self.liveUpdateMode = true;
996 self.container.removeClass( 'widget-form-disabled' );
997 self.container.find( 'input[name="savewidget"]' ).hide();
1000 // Sync sanitized field states to existing fields if they are aligned
1001 if ( hasSameInputsInResponse && self.liveUpdateMode ) {
1002 $inputs.each( function( i ) {
1003 var $input = $( this ),
1004 $sanitizedInput = $( $sanitizedInputs[i] ),
1005 property = self._getInputStatePropertyName( this ),
1006 submittedState, sanitizedState, canUpdateState;
1008 submittedState = $input.data( 'state' + updateNumber );
1009 sanitizedState = $sanitizedInput.prop( property );
1010 $input.data( 'sanitized', sanitizedState );
1012 canUpdateState = ( submittedState !== sanitizedState && ( args.ignoreActiveElement || ! $input.is( document.activeElement ) ) );
1013 if ( canUpdateState ) {
1014 $input.prop( property, sanitizedState );
1018 $( document ).trigger( 'widget-synced', [ $widgetRoot, r.data.form ] );
1020 // Otherwise, if sanitized fields are not aligned with existing fields, disable live update mode if enabled
1021 } else if ( self.liveUpdateMode ) {
1022 self.liveUpdateMode = false;
1023 self.container.find( 'input[name="savewidget"]' ).show();
1024 isLiveUpdateAborted = true;
1026 // Otherwise, replace existing form with the sanitized form
1028 $widgetContent.html( r.data.form );
1030 self.container.removeClass( 'widget-form-disabled' );
1032 $( document ).trigger( 'widget-updated', [ $widgetRoot ] );
1036 * If the old instance is identical to the new one, there is nothing new
1037 * needing to be rendered, and so we can preempt the event for the
1038 * preview finishing loading.
1040 isChanged = ! isLiveUpdateAborted && ! _( self.setting() ).isEqual( r.data.instance );
1042 self.isWidgetUpdating = true; // suppress triggering another updateWidget
1043 self.setting( r.data.instance );
1044 self.isWidgetUpdating = false;
1046 // no change was made, so stop the spinner now instead of when the preview would updates
1047 self.container.removeClass( 'previewer-loading' );
1050 if ( completeCallback ) {
1051 completeCallback.call( self, null, { noChange: ! isChanged, ajaxFinished: true } );
1054 // General error message
1055 message = l10n.error;
1057 if ( r.data && r.data.message ) {
1058 message = r.data.message;
1061 if ( completeCallback ) {
1062 completeCallback.call( self, message );
1064 $widgetContent.prepend( '<p class="widget-error"><strong>' + message + '</strong></p>' );
1069 jqxhr.fail( function( jqXHR, textStatus ) {
1070 if ( completeCallback ) {
1071 completeCallback.call( self, textStatus );
1075 jqxhr.always( function() {
1076 self.container.removeClass( 'widget-form-loading' );
1078 $inputs.each( function() {
1079 $( this ).removeData( 'state' + updateNumber );
1082 processing( processing() - 1 );
1087 * Expand the accordion section containing a control
1089 expandControlSection: function() {
1090 var $section = this.container.closest( '.accordion-section' );
1092 if ( ! $section.hasClass( 'open' ) ) {
1093 $section.find( '.accordion-section-title:first' ).trigger( 'click' );
1098 * Expand the widget form control
1100 expandForm: function() {
1101 this.toggleForm( true );
1105 * Collapse the widget form control
1107 collapseForm: function() {
1108 this.toggleForm( false );
1112 * Expand or collapse the widget control
1114 * @param {boolean|undefined} [showOrHide] If not supplied, will be inverse of current visibility
1116 toggleForm: function( showOrHide ) {
1117 var self = this, $widget, $inside, complete;
1119 $widget = this.container.find( 'div.widget:first' );
1120 $inside = $widget.find( '.widget-inside:first' );
1121 if ( typeof showOrHide === 'undefined' ) {
1122 showOrHide = ! $inside.is( ':visible' );
1125 // Already expanded or collapsed, so noop
1126 if ( $inside.is( ':visible' ) === showOrHide ) {
1131 // Close all other widget controls before expanding this one
1132 api.control.each( function( otherControl ) {
1133 if ( self.params.type === otherControl.params.type && self !== otherControl ) {
1134 otherControl.collapseForm();
1138 complete = function() {
1139 self.container.removeClass( 'expanding' );
1140 self.container.addClass( 'expanded' );
1141 self.container.trigger( 'expanded' );
1144 if ( self.params.is_wide ) {
1145 $inside.fadeIn( 'fast', complete );
1147 $inside.slideDown( 'fast', complete );
1150 self.container.trigger( 'expand' );
1151 self.container.addClass( 'expanding' );
1153 complete = function() {
1154 self.container.removeClass( 'collapsing' );
1155 self.container.removeClass( 'expanded' );
1156 self.container.trigger( 'collapsed' );
1159 self.container.trigger( 'collapse' );
1160 self.container.addClass( 'collapsing' );
1162 if ( self.params.is_wide ) {
1163 $inside.fadeOut( 'fast', complete );
1165 $inside.slideUp( 'fast', function() {
1166 $widget.css( { width:'', margin:'' } );
1174 * Expand the containing sidebar section, expand the form, and focus on
1175 * the first input in the control
1178 this.expandControlSection();
1180 this.container.find( '.widget-content :focusable:first' ).focus();
1184 * Get the position (index) of the widget in the containing sidebar
1188 getWidgetSidebarPosition: function() {
1189 var sidebarWidgetIds, position;
1191 sidebarWidgetIds = this.getSidebarWidgetsControl().setting();
1192 position = _.indexOf( sidebarWidgetIds, this.params.widget_id );
1194 if ( position === -1 ) {
1202 * Move widget up one in the sidebar
1204 moveUp: function() {
1205 this._moveWidgetByOne( -1 );
1209 * Move widget up one in the sidebar
1211 moveDown: function() {
1212 this._moveWidgetByOne( 1 );
1218 * @param {Number} offset 1|-1
1220 _moveWidgetByOne: function( offset ) {
1221 var i, sidebarWidgetsSetting, sidebarWidgetIds, adjacentWidgetId;
1223 i = this.getWidgetSidebarPosition();
1225 sidebarWidgetsSetting = this.getSidebarWidgetsControl().setting;
1226 sidebarWidgetIds = Array.prototype.slice.call( sidebarWidgetsSetting() ); // clone
1227 adjacentWidgetId = sidebarWidgetIds[i + offset];
1228 sidebarWidgetIds[i + offset] = this.params.widget_id;
1229 sidebarWidgetIds[i] = adjacentWidgetId;
1231 sidebarWidgetsSetting( sidebarWidgetIds );
1235 * Toggle visibility of the widget move area
1237 * @param {Boolean} [showOrHide]
1239 toggleWidgetMoveArea: function( showOrHide ) {
1240 var self = this, $moveWidgetArea;
1242 $moveWidgetArea = this.container.find( '.move-widget-area' );
1244 if ( typeof showOrHide === 'undefined' ) {
1245 showOrHide = ! $moveWidgetArea.hasClass( 'active' );
1249 // reset the selected sidebar
1250 $moveWidgetArea.find( '.selected' ).removeClass( 'selected' );
1252 $moveWidgetArea.find( 'li' ).filter( function() {
1253 return $( this ).data( 'id' ) === self.params.sidebar_id;
1254 } ).addClass( 'selected' );
1256 this.container.find( '.move-widget-btn' ).prop( 'disabled', true );
1259 $moveWidgetArea.toggleClass( 'active', showOrHide );
1263 * Highlight the widget control and section
1265 highlightSectionAndControl: function() {
1268 if ( this.container.is( ':hidden' ) ) {
1269 $target = this.container.closest( '.control-section' );
1271 $target = this.container;
1274 $( '.highlighted' ).removeClass( 'highlighted' );
1275 $target.addClass( 'highlighted' );
1277 setTimeout( function() {
1278 $target.removeClass( 'highlighted' );
1284 * wp.customize.Widgets.SidebarControl
1286 * Customizer control for widgets.
1287 * Note that 'sidebar_widgets' must match the WP_Widget_Area_Customize_Control::$type
1290 * @augments wp.customize.Control
1292 api.Widgets.SidebarControl = api.Control.extend({
1294 * Set up the control
1297 this.$controlSection = this.container.closest( '.control-section' );
1298 this.$sectionContent = this.container.closest( '.accordion-section-content' );
1301 this._setupSortable();
1302 this._setupAddition();
1303 this._applyCardinalOrderClassNames();
1307 * Update ordering of widget control forms when the setting is updated
1309 _setupModel: function() {
1311 registeredSidebar = api.Widgets.registeredSidebars.get( this.params.sidebar_id );
1313 this.setting.bind( function( newWidgetIds, oldWidgetIds ) {
1314 var widgetFormControls, $sidebarWidgetsAddControl, finalControlContainers, removedWidgetIds;
1316 removedWidgetIds = _( oldWidgetIds ).difference( newWidgetIds );
1318 // Filter out any persistent widget IDs for widgets which have been deactivated
1319 newWidgetIds = _( newWidgetIds ).filter( function( newWidgetId ) {
1320 var parsedWidgetId = parseWidgetId( newWidgetId );
1322 return !! api.Widgets.availableWidgets.findWhere( { id_base: parsedWidgetId.id_base } );
1325 widgetFormControls = _( newWidgetIds ).map( function( widgetId ) {
1326 var widgetFormControl = api.Widgets.getWidgetFormControlForWidget( widgetId );
1328 if ( ! widgetFormControl ) {
1329 widgetFormControl = self.addWidget( widgetId );
1332 return widgetFormControl;
1335 // Sort widget controls to their new positions
1336 widgetFormControls.sort( function( a, b ) {
1337 var aIndex = _.indexOf( newWidgetIds, a.params.widget_id ),
1338 bIndex = _.indexOf( newWidgetIds, b.params.widget_id );
1340 if ( aIndex === bIndex ) {
1344 return aIndex < bIndex ? -1 : 1;
1347 // Append the controls to put them in the right order
1348 finalControlContainers = _( widgetFormControls ).map( function( widgetFormControls ) {
1349 return widgetFormControls.container[0];
1352 $sidebarWidgetsAddControl = self.$sectionContent.find( '.customize-control-sidebar_widgets' );
1353 $sidebarWidgetsAddControl.before( finalControlContainers );
1355 // Re-sort widget form controls (including widgets form other sidebars newly moved here)
1356 self._applyCardinalOrderClassNames();
1358 // If the widget was dragged into the sidebar, make sure the sidebar_id param is updated
1359 _( widgetFormControls ).each( function( widgetFormControl ) {
1360 widgetFormControl.params.sidebar_id = self.params.sidebar_id;
1363 // Cleanup after widget removal
1364 _( removedWidgetIds ).each( function( removedWidgetId ) {
1366 // Using setTimeout so that when moving a widget to another sidebar, the other sidebars_widgets settings get a chance to update
1367 setTimeout( function() {
1368 var removedControl, wasDraggedToAnotherSidebar, inactiveWidgets, removedIdBase,
1369 widget, isPresentInAnotherSidebar = false;
1371 // Check if the widget is in another sidebar
1372 api.each( function( otherSetting ) {
1373 if ( otherSetting.id === self.setting.id || 0 !== otherSetting.id.indexOf( 'sidebars_widgets[' ) || otherSetting.id === 'sidebars_widgets[wp_inactive_widgets]' ) {
1377 var otherSidebarWidgets = otherSetting(), i;
1379 i = _.indexOf( otherSidebarWidgets, removedWidgetId );
1381 isPresentInAnotherSidebar = true;
1385 // If the widget is present in another sidebar, abort!
1386 if ( isPresentInAnotherSidebar ) {
1390 removedControl = api.Widgets.getWidgetFormControlForWidget( removedWidgetId );
1392 // Detect if widget control was dragged to another sidebar
1393 wasDraggedToAnotherSidebar = removedControl && $.contains( document, removedControl.container[0] ) && ! $.contains( self.$sectionContent[0], removedControl.container[0] );
1395 // Delete any widget form controls for removed widgets
1396 if ( removedControl && ! wasDraggedToAnotherSidebar ) {
1397 api.control.remove( removedControl.id );
1398 removedControl.container.remove();
1401 // Move widget to inactive widgets sidebar (move it to trash) if has been previously saved
1402 // This prevents the inactive widgets sidebar from overflowing with throwaway widgets
1403 if ( api.Widgets.savedWidgetIds[removedWidgetId] ) {
1404 inactiveWidgets = api.value( 'sidebars_widgets[wp_inactive_widgets]' )().slice();
1405 inactiveWidgets.push( removedWidgetId );
1406 api.value( 'sidebars_widgets[wp_inactive_widgets]' )( _( inactiveWidgets ).unique() );
1409 // Make old single widget available for adding again
1410 removedIdBase = parseWidgetId( removedWidgetId ).id_base;
1411 widget = api.Widgets.availableWidgets.findWhere( { id_base: removedIdBase } );
1412 if ( widget && ! widget.get( 'is_multi' ) ) {
1413 widget.set( 'is_disabled', false );
1420 // Update the model with whether or not the sidebar is rendered
1421 api.Widgets.Previewer.bind( 'rendered-sidebars', function( renderedSidebars ) {
1422 var isRendered = !! renderedSidebars[self.params.sidebar_id];
1424 registeredSidebar.set( 'is_rendered', isRendered );
1427 // Show the sidebar section when it becomes visible
1428 registeredSidebar.on( 'change:is_rendered', function( ) {
1429 var sectionSelector = '#accordion-section-sidebar-widgets-' + this.get( 'id' ), $section;
1431 $section = $( sectionSelector );
1432 if ( this.get( 'is_rendered' ) ) {
1433 $section.stop().slideDown( function() {
1434 $( this ).css( 'height', 'auto' ); // so that the .accordion-section-content won't overflow
1438 // Make sure that hidden sections get closed first
1439 if ( $section.hasClass( 'open' ) ) {
1440 // it would be nice if accordionSwitch() in accordion.js was public
1441 $section.find( '.accordion-section-title' ).trigger( 'click' );
1444 $section.stop().slideUp();
1450 * Allow widgets in sidebar to be re-ordered, and for the order to be previewed
1452 _setupSortable: function() {
1455 this.isReordering = false;
1458 * Update widget order setting when controls are re-ordered
1460 this.$sectionContent.sortable( {
1461 items: '> .customize-control-widget_form',
1462 handle: '.widget-top',
1464 connectWith: '.accordion-section-content:has(.customize-control-sidebar_widgets)',
1465 update: function() {
1466 var widgetContainerIds = self.$sectionContent.sortable( 'toArray' ), widgetIds;
1468 widgetIds = $.map( widgetContainerIds, function( widgetContainerId ) {
1469 return $( '#' + widgetContainerId ).find( ':input[name=widget-id]' ).val();
1472 self.setting( widgetIds );
1477 * Expand other customizer sidebar section when dragging a control widget over it,
1478 * allowing the control to be dropped into another section
1480 this.$controlSection.find( '.accordion-section-title' ).droppable({
1481 accept: '.customize-control-widget_form',
1483 if ( ! self.$controlSection.hasClass( 'open' ) ) {
1484 self.$controlSection.addClass( 'open' );
1485 self.$sectionContent.toggle( false ).slideToggle( 150, function() {
1486 self.$sectionContent.sortable( 'refreshPositions' );
1493 * Keyboard-accessible reordering
1495 this.container.find( '.reorder-toggle' ).on( 'click keydown', function( event ) {
1496 if ( event.type === 'keydown' && ! ( event.which === 13 || event.which === 32 ) ) { // Enter or Spacebar
1500 self.toggleReordering( ! self.isReordering );
1505 * Set up UI for adding a new widget
1507 _setupAddition: function() {
1510 this.container.find( '.add-new-widget' ).on( 'click keydown', function( event ) {
1511 if ( event.type === 'keydown' && ! ( event.which === 13 || event.which === 32 ) ) { // Enter or Spacebar
1515 if ( self.$sectionContent.hasClass( 'reordering' ) ) {
1519 if ( ! $( 'body' ).hasClass( 'adding-widget' ) ) {
1520 api.Widgets.availableWidgetsPanel.open( self );
1522 api.Widgets.availableWidgetsPanel.close();
1528 * Add classes to the widget_form controls to assist with styling
1530 _applyCardinalOrderClassNames: function() {
1531 this.$sectionContent.find( '.customize-control-widget_form' )
1532 .removeClass( 'first-widget' )
1533 .removeClass( 'last-widget' )
1534 .find( '.move-widget-down, .move-widget-up' ).prop( 'tabIndex', 0 );
1536 this.$sectionContent.find( '.customize-control-widget_form:first' )
1537 .addClass( 'first-widget' )
1538 .find( '.move-widget-up' ).prop( 'tabIndex', -1 );
1540 this.$sectionContent.find( '.customize-control-widget_form:last' )
1541 .addClass( 'last-widget' )
1542 .find( '.move-widget-down' ).prop( 'tabIndex', -1 );
1546 /***********************************************************************
1547 * Begin public API methods
1548 **********************************************************************/
1551 * Enable/disable the reordering UI
1553 * @param {Boolean} showOrHide to enable/disable reordering
1555 toggleReordering: function( showOrHide ) {
1556 showOrHide = Boolean( showOrHide );
1558 if ( showOrHide === this.$sectionContent.hasClass( 'reordering' ) ) {
1562 this.isReordering = showOrHide;
1563 this.$sectionContent.toggleClass( 'reordering', showOrHide );
1566 _( this.getWidgetFormControls() ).each( function( formControl ) {
1567 formControl.collapseForm();
1573 * @return {wp.customize.controlConstructor.widget_form[]}
1575 getWidgetFormControls: function() {
1578 formControls = _( this.setting() ).map( function( widgetId ) {
1579 var settingId = widgetIdToSettingId( widgetId ),
1580 formControl = api.control( settingId );
1582 if ( ! formControl ) {
1589 return formControls;
1593 * @param {string} widgetId or an id_base for adding a previously non-existing widget
1594 * @returns {object|false} widget_form control instance, or false on error
1596 addWidget: function( widgetId ) {
1597 var self = this, controlHtml, $widget, controlType = 'widget_form', $control, controlConstructor,
1598 parsedWidgetId = parseWidgetId( widgetId ),
1599 widgetNumber = parsedWidgetId.number,
1600 widgetIdBase = parsedWidgetId.id_base,
1601 widget = api.Widgets.availableWidgets.findWhere( {id_base: widgetIdBase} ),
1602 settingId, isExistingWidget, widgetFormControl, sidebarWidgets, settingArgs;
1608 if ( widgetNumber && ! widget.get( 'is_multi' ) ) {
1612 // Set up new multi widget
1613 if ( widget.get( 'is_multi' ) && ! widgetNumber ) {
1614 widget.set( 'multi_number', widget.get( 'multi_number' ) + 1 );
1615 widgetNumber = widget.get( 'multi_number' );
1618 controlHtml = $.trim( $( '#widget-tpl-' + widget.get( 'id' ) ).html() );
1619 if ( widget.get( 'is_multi' ) ) {
1620 controlHtml = controlHtml.replace( /<[^<>]+>/g, function( m ) {
1621 return m.replace( /__i__|%i%/g, widgetNumber );
1624 widget.set( 'is_disabled', true ); // Prevent single widget from being added again now
1627 $widget = $( controlHtml );
1629 $control = $( '<li/>' )
1630 .addClass( 'customize-control' )
1631 .addClass( 'customize-control-' + controlType )
1634 // Remove icon which is visible inside the panel
1635 $control.find( '> .widget-icon' ).remove();
1637 if ( widget.get( 'is_multi' ) ) {
1638 $control.find( 'input[name="widget_number"]' ).val( widgetNumber );
1639 $control.find( 'input[name="multi_number"]' ).val( widgetNumber );
1642 widgetId = $control.find( '[name="widget-id"]' ).val();
1644 $control.hide(); // to be slid-down below
1646 settingId = 'widget_' + widget.get( 'id_base' );
1647 if ( widget.get( 'is_multi' ) ) {
1648 settingId += '[' + widgetNumber + ']';
1650 $control.attr( 'id', 'customize-control-' + settingId.replace( /\]/g, '' ).replace( /\[/g, '-' ) );
1652 this.container.after( $control );
1654 // Only create setting if it doesn't already exist (if we're adding a pre-existing inactive widget)
1655 isExistingWidget = api.has( settingId );
1656 if ( ! isExistingWidget ) {
1658 transport: 'refresh',
1659 previewer: this.setting.previewer
1661 api.create( settingId, settingId, {}, settingArgs );
1664 controlConstructor = api.controlConstructor[controlType];
1665 widgetFormControl = new controlConstructor( settingId, {
1668 'default': settingId
1670 sidebar_id: self.params.sidebar_id,
1671 widget_id: widgetId,
1672 widget_id_base: widget.get( 'id_base' ),
1674 is_new: ! isExistingWidget,
1675 width: widget.get( 'width' ),
1676 height: widget.get( 'height' ),
1677 is_wide: widget.get( 'is_wide' )
1679 previewer: self.setting.previewer
1681 api.control.add( settingId, widgetFormControl );
1683 // Make sure widget is removed from the other sidebars
1684 api.each( function( otherSetting ) {
1685 if ( otherSetting.id === self.setting.id ) {
1689 if ( 0 !== otherSetting.id.indexOf( 'sidebars_widgets[' ) ) {
1693 var otherSidebarWidgets = otherSetting().slice(),
1694 i = _.indexOf( otherSidebarWidgets, widgetId );
1697 otherSidebarWidgets.splice( i );
1698 otherSetting( otherSidebarWidgets );
1702 // Add widget to this sidebar
1703 sidebarWidgets = this.setting().slice();
1704 if ( -1 === _.indexOf( sidebarWidgets, widgetId ) ) {
1705 sidebarWidgets.push( widgetId );
1706 this.setting( sidebarWidgets );
1709 $control.slideDown( function() {
1710 if ( isExistingWidget ) {
1711 widgetFormControl.expandForm();
1712 widgetFormControl.updateWidget( {
1713 instance: widgetFormControl.setting(),
1714 complete: function( error ) {
1718 widgetFormControl.focus();
1722 widgetFormControl.focus();
1726 $( document ).trigger( 'widget-added', [ $widget ] );
1728 return widgetFormControl;
1733 * Extends wp.customizer.controlConstructor with control constructor for
1734 * widget_form and sidebar_widgets.
1736 $.extend( api.controlConstructor, {
1737 widget_form: api.Widgets.WidgetControl,
1738 sidebar_widgets: api.Widgets.SidebarControl
1742 * Capture the instance of the Previewer since it is private
1744 OldPreviewer = api.Previewer;
1745 api.Previewer = OldPreviewer.extend({
1746 initialize: function( params, options ) {
1747 api.Widgets.Previewer = this;
1748 OldPreviewer.prototype.initialize.call( this, params, options );
1749 this.bind( 'refresh', this.refresh );
1754 * Init Customizer for widgets.
1756 api.bind( 'ready', function() {
1757 // Set up the widgets panel
1758 api.Widgets.availableWidgetsPanel = new api.Widgets.AvailableWidgetsPanelView({
1759 collection: api.Widgets.availableWidgets
1762 // Highlight widget control
1763 api.Widgets.Previewer.bind( 'highlight-widget-control', api.Widgets.highlightWidgetFormControl );
1765 // Open and focus widget control
1766 api.Widgets.Previewer.bind( 'focus-widget-control', api.Widgets.focusWidgetFormControl );
1770 * Highlight a widget control.
1772 * @param {string} widgetId
1774 api.Widgets.highlightWidgetFormControl = function( widgetId ) {
1775 var control = api.Widgets.getWidgetFormControlForWidget( widgetId );
1778 control.highlightSectionAndControl();
1783 * Focus a widget control.
1785 * @param {string} widgetId
1787 api.Widgets.focusWidgetFormControl = function( widgetId ) {
1788 var control = api.Widgets.getWidgetFormControlForWidget( widgetId );
1796 * Given a widget control, find the sidebar widgets control that contains it.
1797 * @param {string} widgetId
1798 * @return {object|null}
1800 api.Widgets.getSidebarWidgetControlContainingWidget = function( widgetId ) {
1801 var foundControl = null;
1803 // @todo this can use widgetIdToSettingId(), then pass into wp.customize.control( x ).getSidebarWidgetsControl()
1804 api.control.each( function( control ) {
1805 if ( control.params.type === 'sidebar_widgets' && -1 !== _.indexOf( control.setting(), widgetId ) ) {
1806 foundControl = control;
1810 return foundControl;
1814 * Given a widget ID for a widget appearing in the preview, get the widget form control associated with it.
1816 * @param {string} widgetId
1817 * @return {object|null}
1819 api.Widgets.getWidgetFormControlForWidget = function( widgetId ) {
1820 var foundControl = null;
1822 // @todo We can just use widgetIdToSettingId() here
1823 api.control.each( function( control ) {
1824 if ( control.params.type === 'widget_form' && control.params.widget_id === widgetId ) {
1825 foundControl = control;
1829 return foundControl;
1833 * @param {String} widgetId
1836 function parseWidgetId( widgetId ) {
1837 var matches, parsed = {
1842 matches = widgetId.match( /^(.+)-(\d+)$/ );
1844 parsed.id_base = matches[1];
1845 parsed.number = parseInt( matches[2], 10 );
1847 // likely an old single widget
1848 parsed.id_base = widgetId;
1855 * @param {String} widgetId
1856 * @returns {String} settingId
1858 function widgetIdToSettingId( widgetId ) {
1859 var parsed = parseWidgetId( widgetId ), settingId;
1861 settingId = 'widget_' + parsed.id_base;
1862 if ( parsed.number ) {
1863 settingId += '[' + parsed.number + ']';
1869 })( window.wp, jQuery );