From 5f76bf3b0df2cb241b0ecb5dd9c67f50de034520 Mon Sep 17 00:00:00 2001 From: Dominik Schilling Date: Mon, 7 Apr 2014 14:10:14 +0000 Subject: [PATCH] Widget Customizer: Make the available widgets panel a Backbone view. see #27690. Built from https://develop.svn.wordpress.org/trunk@27986 git-svn-id: http://core.svn.wordpress.org/trunk@27816 1a063a9b-81f0-0310-95a4-ce76da25c4cd --- wp-admin/js/customize-widgets.js | 579 +++++++++++++++------------ wp-admin/js/customize-widgets.min.js | 2 +- 2 files changed, 323 insertions(+), 258 deletions(-) diff --git a/wp-admin/js/customize-widgets.js b/wp-admin/js/customize-widgets.js index 70747e2826..e628a3ad29 100644 --- a/wp-admin/js/customize-widgets.js +++ b/wp-admin/js/customize-widgets.js @@ -15,7 +15,12 @@ delete api.Widgets.data.l10n; /** - * Set up model + * wp.customize.Widgets.WidgetModel + * + * A single widget model. + * + * @constructor + * @augments Backbone.Model */ api.Widgets.WidgetModel = Backbone.Model.extend({ id: null, @@ -34,6 +39,14 @@ height: null }); + /** + * wp.customize.Widgets.WidgetCollection + * + * Collection for widget models. + * + * @constructor + * @augments Backbone.Model + */ api.Widgets.WidgetCollection = Backbone.Collection.extend({ model: api.Widgets.WidgetModel, @@ -92,6 +105,14 @@ }); api.Widgets.availableWidgets = new api.Widgets.WidgetCollection( api.Widgets.data.availableWidgets ); + /** + * wp.customize.Widgets.SidebarModel + * + * A single sidebar model. + * + * @constructor + * @augments Backbone.Model + */ api.Widgets.SidebarModel = Backbone.Model.extend({ after_title: null, after_widget: null, @@ -104,11 +125,251 @@ is_rendered: false }); + /** + * wp.customize.Widgets.SidebarCollection + * + * Collection for sidebar models. + * + * @constructor + * @augments Backbone.Collection + */ api.Widgets.SidebarCollection = Backbone.Collection.extend({ model: api.Widgets.SidebarModel }); api.Widgets.registeredSidebars = new api.Widgets.SidebarCollection( api.Widgets.data.registeredSidebars ); + /** + * wp.customize.Widgets.AvailableWidgetsPanelView + * + * View class for the available widgets panel. + * + * @constructor + * @augments wp.Backbone.View + * @augments Backbone.View + */ + api.Widgets.AvailableWidgetsPanelView = wp.Backbone.View.extend({ + + el: '#available-widgets', + + events: { + 'input #widgets-search': 'search', + 'keyup #widgets-search': 'search', + 'change #widgets-search': 'search', + 'search #widgets-search': 'search', + 'focus .widget-tpl' : 'focus', + 'click .widget-tpl' : '_submit', + 'keypress .widget-tpl' : '_submit', + 'keydown' : 'keyboardAccessible' + }, + + // Cache current selected widget + selected: null, + + // Cache sidebar control which has opened panel + currentSidebarControl: null, + $search: null, + + initialize: function() { + var self = this; + + this.$search = $( '#widgets-search' ); + + _.bindAll( this, 'close' ); + + this.listenTo( this.collection, 'update', this.updateList ); + + this.updateList(); + + // If the available widgets panel is open and the customize controls are + // interacted with (i.e. available widgets panel is blurred) then close the + // available widgets panel. + $( '#customize-controls' ).on( 'click keydown', function( e ) { + var isAddNewBtn = $( e.target ).is( '.add-new-widget, .add-new-widget *' ); + if ( $( 'body' ).hasClass( 'adding-widget' ) && ! isAddNewBtn ) { + self.close(); + } + } ); + + // Close the panel if the URL in the preview changes + api.Widgets.Previewer.bind( 'url', this.close ); + }, + + // Performs a search and handles selected widget + search: function( event ) { + var firstVisible; + + this.collection.doSearch( event.target.value ); + + // Remove a widget from being selected if it is no longer visible + if ( this.selected && ! this.selected.is( ':visible' ) ) { + this.selected.removeClass( 'selected' ); + this.selected = null; + } + + // If a widget was selected but the filter value has been cleared out, clear selection + if ( this.selected && ! event.target.value ) { + this.selected.removeClass( 'selected' ); + this.selected = null; + } + + // If a filter has been entered and a widget hasn't been selected, select the first one shown + if ( ! this.selected && event.target.value ) { + firstVisible = this.$el.find( '> .widget-tpl:visible:first' ); + if ( firstVisible.length ) { + this.select( firstVisible ); + } + } + }, + + // Changes visibilty of available widgets + 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 ) { + var widgetTpl = $( '#widget-tpl-' + widget.id ); + widgetTpl.toggle( ! widget.get( 'is_disabled' ) ); + if ( widget.get( 'is_disabled' ) && widgetTpl.is( this.selected ) ) { + this.selected = null; + } + } ); + }, + + // Hightlights a widget + select: function( widgetTpl ) { + this.selected = $( widgetTpl ); + this.selected.siblings( '.widget-tpl' ).removeClass( 'selected' ); + this.selected.addClass( 'selected' ); + }, + + // Hightlights a widget on focus + focus: function( event ) { + this.select( $( event.currentTarget ) ); + }, + + // Submit handler for keypress and click on widget + _submit: function( event ) { + // Only proceed with keypress if it is Enter or Spacebar + if ( event.type === 'keypress' && ( event.which !== 13 && event.which !== 32 ) ) { + return; + } + + this.submit( $( event.currentTarget ) ); + }, + + // Adds a selected widget to the sidebar + submit: function( widgetTpl ) { + var widgetId, widget; + + if ( ! widgetTpl ) { + widgetTpl = this.selected; + } + + if ( ! widgetTpl || ! this.currentSidebarControl ) { + return; + } + + this.select( widgetTpl ); + + widgetId = $( this.selected ).data( 'widget-id' ); + widget = this.collection.findWhere( { id: widgetId } ); + if ( ! widget ) { + return; + } + + this.currentSidebarControl.addWidget( widget.get( 'id_base' ) ); + + this.close(); + }, + + // Opens the panel + open: function( sidebarControl ) { + this.currentSidebarControl = sidebarControl; + + // Wide widget controls appear over the preview, and so they need to be collapsed when the panel opens + _( this.currentSidebarControl.getWidgetFormControls() ).each( function( control ) { + if ( control.params.is_wide ) { + control.collapseForm(); + } + } ); + + $( 'body' ).addClass( 'adding-widget' ); + + this.$el.find( '.selected' ).removeClass( 'selected' ); + + // Reset search + this.collection.doSearch( '' ); + + this.$search.focus(); + }, + + // Closes the panel + close: function( options ) { + options = options || {}; + + if ( options.returnFocus && this.currentSidebarControl ) { + this.currentSidebarControl.container.find( '.add-new-widget' ).focus(); + } + + this.currentSidebarControl = null; + this.selected = null; + + $( 'body' ).removeClass( 'adding-widget' ); + + this.$search.val( '' ); + }, + + // Add keyboard accessiblity to the panel + keyboardAccessible: function( event ) { + var isEnter = ( event.which === 13 ), + isEsc = ( event.which === 27 ), + isDown = ( event.which === 40 ), + isUp = ( event.which === 38 ), + selected = null, + firstVisible = this.$el.find( '> .widget-tpl:visible:first' ), + lastVisible = this.$el.find( '> .widget-tpl:visible:last' ), + isSearchFocused = $( event.target ).is( this.$search ); + + if ( isDown || isUp ) { + if ( isDown ) { + if ( isSearchFocused ) { + selected = firstVisible; + } else if ( this.selected && this.selected.nextAll( '.widget-tpl:visible' ).length !== 0 ) { + selected = this.selected.nextAll( '.widget-tpl:visible:first' ); + } + } else if ( isUp ) { + if ( isSearchFocused ) { + selected = lastVisible; + } else if ( this.selected && this.selected.prevAll( '.widget-tpl:visible' ).length !== 0 ) { + selected = this.selected.prevAll( '.widget-tpl:visible:first' ); + } + } + + this.select( selected ); + + if ( selected ) { + selected.focus(); + } else { + this.$search.focus(); + } + + return; + } + + // If enter pressed but nothing entered, don't do anything + if ( isEnter && ! this.$search.val() ) { + return; + } + + if ( isEnter ) { + this.submit(); + } else if ( isEsc ) { + this.close( { returnFocus: true } ); + } + } + }); + /** * Handlers for the widget-synced event, organized by widget ID base. * Other widgets may provide their own update handlers by adding @@ -121,7 +382,7 @@ * @param {jQuery} widget * @param {String} newForm */ - rss: function ( e, widget, newForm ) { + rss: function( e, widget, newForm ) { var oldWidgetError = widget.find( '.widget-error:first' ), newWidgetError = $( '
' + newForm + '
' ).find( '.widget-error:first' ); @@ -136,8 +397,13 @@ }; /** - * Widget Form control + * wp.customize.Widgets.WidgetControl + * + * Customizer control for widgets. * Note that 'widget_form' must match the WP_Widget_Form_Customize_Control::$type + * + * @constructor + * @augments wp.customize.Control */ api.Widgets.WidgetControl = api.Control.extend({ /** @@ -246,7 +512,7 @@ } ); // Reposition whenever a sidebar's widgets are changed - api.each( function ( setting ) { + api.each( function( setting ) { if ( 0 === setting.id.indexOf( 'sidebars_widgets[' ) ) { setting.bind( function() { if ( control.container.hasClass( 'expanded' ) ) { @@ -264,7 +530,7 @@ _setupControlToggle: function() { var control = this, close_btn; - control.container.find( '.widget-top' ).on( 'click', function ( e ) { + control.container.find( '.widget-top' ).on( 'click', function( e ) { e.preventDefault(); var sidebar_widgets_control = control.getSidebarWidgetsControl(); if ( sidebar_widgets_control.is_reordering ) { @@ -275,7 +541,7 @@ close_btn = control.container.find( '.widget-control-close' ); // @todo Hitting Enter on this link does nothing; will be resolved in core with - close_btn.on( 'click', function ( e ) { + close_btn.on( 'click', function( e ) { e.preventDefault(); control.collapseForm(); control.container.find( '.widget-top .widget-action:first' ).focus(); // keyboard accessibility @@ -317,7 +583,7 @@ * * @param {jQuery} li */ - select_sidebar_item = function ( li ) { + select_sidebar_item = function( li ) { li.siblings( '.selected' ).removeClass( 'selected' ); li.addClass( 'selected' ); var is_self_sidebar = ( li.data( 'id' ) === control.params.sidebar_id ); @@ -363,7 +629,7 @@ * Handle clicks for up/down/move on the reorder nav */ reorder_nav = control.container.find( '.widget-reorder-nav' ); - reorder_nav.find( '.move-widget, .move-widget-down, .move-widget-up' ).on( 'click keypress', function ( event ) { + reorder_nav.find( '.move-widget, .move-widget-down, .move-widget-up' ).on( 'click keypress', function( event ) { if ( event.type === 'keypress' && ( event.which !== 13 && event.which !== 32 ) ) { return; } @@ -393,7 +659,7 @@ /** * Handle selecting a sidebar to move to */ - control.container.find( '.widget-area-select' ).on( 'click keypress', 'li', function ( e ) { + control.container.find( '.widget-area-select' ).on( 'click keypress', 'li', function( e ) { if ( event.type === 'keypress' && ( event.which !== 13 && event.which !== 32 ) ) { return; } @@ -472,7 +738,7 @@ save_btn.val( l10n.saveBtnLabel ); save_btn.attr( 'title', l10n.saveBtnTooltip ); save_btn.removeClass( 'button-primary' ).addClass( 'button-secondary' ); - save_btn.on( 'click', function ( e ) { + save_btn.on( 'click', function( e ) { e.preventDefault(); control.updateWidget( { disable_form: true } ); } ); @@ -491,7 +757,7 @@ } ); // Handle widgets that support live previews - widget_content.on( 'change input propertychange', ':input', function ( e ) { + widget_content.on( 'change input propertychange', ':input', function( e ) { if ( control.live_update_mode ) { if ( e.type === 'change' ) { control.updateWidget(); @@ -505,21 +771,21 @@ control.setting.previewer.channel.bind( 'synced', function() { control.container.removeClass( 'previewer-loading' ); } ); - api.Widgets.Previewer.bind( 'widget-updated', function ( updated_widget_id ) { + api.Widgets.Previewer.bind( 'widget-updated', function( updated_widget_id ) { if ( updated_widget_id === control.params.widget_id ) { control.container.removeClass( 'previewer-loading' ); } } ); // Update widget control to indicate whether it is currently rendered (cf. Widget Visibility) - api.Widgets.Previewer.bind( 'rendered-widgets', function ( rendered_widgets ) { + api.Widgets.Previewer.bind( 'rendered-widgets', function( rendered_widgets ) { var is_rendered = !! rendered_widgets[control.params.widget_id]; control.container.toggleClass( 'widget-rendered', is_rendered ); } ); form_update_event_handler = api.Widgets.formSyncHandlers[ control.params.widget_id_base ]; if ( form_update_event_handler ) { - $( document ).on( 'widget-synced', function ( e, widget_el ) { + $( document ).on( 'widget-synced', function( e, widget_el ) { if ( widget_root.is( widget_el ) ) { form_update_event_handler.apply( document, arguments ); } @@ -538,7 +804,7 @@ // Configure remove button remove_btn = control.container.find( 'a.widget-control-remove' ); // @todo Hitting Enter on this link does nothing; will be resolved in core with - remove_btn.on( 'click', function ( e ) { + remove_btn.on( 'click', function( e ) { e.preventDefault(); // Find an adjacent element to add focus to when this widget goes away @@ -592,7 +858,7 @@ * @returns {jQuery} inputs * @private */ - _getInputs: function ( container ) { + _getInputs: function( container ) { return $( container ).find( ':input[name]' ); }, @@ -604,8 +870,8 @@ * @returns {string} * @private */ - _getInputsSignature: function ( inputs ) { - var inputs_signatures = _( inputs ).map( function ( input ) { + _getInputsSignature: function( inputs ) { + var inputs_signatures = _( inputs ).map( function( input ) { input = $( input ); var signature_parts; if ( input.is( ':checkbox, :radio' ) ) { @@ -625,7 +891,7 @@ * @returns {string} * @private */ - _getInputStatePropertyName: function ( input ) { + _getInputStatePropertyName: function( input ) { input = $( input ); if ( input.is( ':radio, :checkbox' ) ) { return 'checked'; @@ -661,7 +927,7 @@ * @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. * @param {Boolean} [args.ignore_active_element=false] Whether or not updating a field will be deferred if focus is still on the element. */ - updateWidget: function ( args ) { + updateWidget: function( args ) { var control = this, instance_override, complete_callback, @@ -726,7 +992,7 @@ } data += '&' + widget_content.find( '~ :input' ).serialize(); - jqxhr = $.post( wp.ajax.settings.url, data, function ( r ) { + jqxhr = $.post( wp.ajax.settings.url, data, function( r ) { var message, sanitized_form, sanitized_inputs, @@ -763,7 +1029,7 @@ // Sync sanitized field states to existing fields if they are aligned if ( has_same_inputs_in_response && control.live_update_mode ) { - inputs.each( function ( i ) { + inputs.each( function( i ) { var input = $( this ), sanitized_input = $( sanitized_inputs[i] ), property = control._getInputStatePropertyName( this ), @@ -827,7 +1093,7 @@ } } } ); - jqxhr.fail( function ( jqXHR, textStatus ) { + jqxhr.fail( function( jqXHR, textStatus ) { if ( complete_callback ) { complete_callback.call( control, textStatus ); } @@ -872,7 +1138,7 @@ * * @param {boolean|undefined} [do_expand] If not supplied, will be inverse of current visibility */ - toggleForm: function ( do_expand ) { + toggleForm: function( do_expand ) { var control = this, widget, inside, complete; widget = control.container.find( 'div.widget:first' ); @@ -888,7 +1154,7 @@ if ( do_expand ) { // Close all other widget controls before expanding this one - api.control.each( function ( other_control ) { + api.control.each( function( other_control ) { if ( control.params.type === other_control.params.type && control !== other_control ) { other_control.collapseForm(); } @@ -974,7 +1240,7 @@ * * @param {Number} offset 1|-1 */ - _moveWidgetByOne: function ( offset ) { + _moveWidgetByOne: function( offset ) { var control = this, i, sidebar_widgets_setting, @@ -997,7 +1263,7 @@ * * @param {Boolean} [toggle] */ - toggleWidgetMoveArea: function ( toggle ) { + toggleWidgetMoveArea: function( toggle ) { var control = this, move_widget_area; move_widget_area = control.container.find( '.move-widget-area' ); if ( typeof toggle === 'undefined' ) { @@ -1043,8 +1309,13 @@ } ); /** - * Sidebar Widgets control + * wp.customize.Widgets.SidebarControl + * + * Customizer control for widgets. * Note that 'sidebar_widgets' must match the WP_Widget_Area_Customize_Control::$type + * + * @constructor + * @augments wp.customize.Control */ api.Widgets.SidebarControl = api.Control.extend({ /** @@ -1074,12 +1345,12 @@ removed_widget_ids = _( old_widget_ids ).difference( new_widget_ids ); // Filter out any persistent widget_ids for widgets which have been deactivated - new_widget_ids = _( new_widget_ids ).filter( function ( new_widget_id ) { + new_widget_ids = _( new_widget_ids ).filter( function( new_widget_id ) { var parsed_widget_id = parse_widget_id( new_widget_id ); return !! api.Widgets.availableWidgets.findWhere( { id_base: parsed_widget_id.id_base } ); } ); - widget_form_controls = _( new_widget_ids ).map( function ( widget_id ) { + widget_form_controls = _( new_widget_ids ).map( function( widget_id ) { var widget_form_control = api.Widgets.getWidgetFormControlForWidget( widget_id ); if ( ! widget_form_control ) { widget_form_control = control.addWidget( widget_id ); @@ -1088,7 +1359,7 @@ } ); // Sort widget controls to their new positions - widget_form_controls.sort( function ( a, b ) { + widget_form_controls.sort( function( a, b ) { var a_index = _.indexOf( new_widget_ids, a.params.widget_id ), b_index = _.indexOf( new_widget_ids, b.params.widget_id ); if ( a_index === b_index ) { @@ -1109,12 +1380,12 @@ 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 ) { + _( widget_form_controls ).each( function( widget_form_control ) { widget_form_control.params.sidebar_id = control.params.sidebar_id; } ); // Cleanup after widget removal - _( removed_widget_ids ).each( function ( removed_widget_id ) { + _( removed_widget_ids ).each( function( removed_widget_id ) { // Using setTimeout so that when moving a widget to another sidebar, the other sidebars_widgets settings get a chance to update setTimeout( function() { @@ -1126,7 +1397,7 @@ widget; // Check if the widget is in another sidebar - api.each( function ( other_setting ) { + api.each( function( other_setting ) { if ( other_setting.id === control.setting.id || 0 !== other_setting.id.indexOf( 'sidebars_widgets[' ) || other_setting.id === 'sidebars_widgets[wp_inactive_widgets]' ) { return; } @@ -1178,13 +1449,13 @@ } ); // Update the model with whether or not the sidebar is rendered - api.Widgets.Previewer.bind( 'rendered-sidebars', function ( rendered_sidebars ) { + api.Widgets.Previewer.bind( 'rendered-sidebars', function( rendered_sidebars ) { var is_rendered = !! rendered_sidebars[control.params.sidebar_id]; registered_sidebar.set( 'is_rendered', is_rendered ); } ); // Show the sidebar section when it becomes visible - registered_sidebar.on( 'change:is_rendered', function ( ) { + registered_sidebar.on( 'change:is_rendered', function( ) { var section_selector = '#accordion-section-sidebar-widgets-' + this.get( 'id' ), section; section = $( section_selector ); if ( this.get( 'is_rendered' ) ) { @@ -1219,7 +1490,7 @@ connectWith: '.accordion-section-content:has(.customize-control-sidebar_widgets)', update: function() { var widget_container_ids = control.section_content.sortable( 'toArray' ), widget_ids; - widget_ids = $.map( widget_container_ids, function ( widget_container_id ) { + widget_ids = $.map( widget_container_ids, function( widget_container_id ) { return $( '#' + widget_container_id ).find( ':input[name=widget-id]' ).val(); } ); control.setting( widget_ids ); @@ -1306,7 +1577,7 @@ * * @param {Boolean} toggle to enable/disable reordering */ - toggleReordering: function ( toggle ) { + toggleReordering: function( toggle ) { var control = this; toggle = Boolean( toggle ); if ( toggle === control.section_content.hasClass( 'reordering' ) ) { @@ -1317,7 +1588,7 @@ control.section_content.toggleClass( 'reordering', toggle ); if ( toggle ) { - _( control.getWidgetFormControls() ).each( function ( form_control ) { + _( control.getWidgetFormControls() ).each( function( form_control ) { form_control.collapseForm(); } ); } @@ -1329,7 +1600,7 @@ getWidgetFormControls: function() { var control = this, form_controls; - form_controls = _( control.setting() ).map( function ( widget_id ) { + form_controls = _( control.setting() ).map( function( widget_id ) { var setting_id = widget_id_to_setting_id( widget_id ), form_control = api.control( setting_id ); @@ -1345,7 +1616,7 @@ * @param {string} widget_id or an id_base for adding a previously non-existing widget * @returns {object} widget_form control instance */ - addWidget: function ( widget_id ) { + addWidget: function( widget_id ) { var control = this, control_html, widget_el, @@ -1377,7 +1648,7 @@ control_html = $( '#widget-tpl-' + widget.get( 'id' ) ).html(); if ( widget.get( 'is_multi' ) ) { - control_html = control_html.replace( /<[^<>]+>/g, function ( m ) { + control_html = control_html.replace( /<[^<>]+>/g, function( m ) { return m.replace( /__i__|%i%/g, widget_number ); } ); } else { @@ -1435,7 +1706,7 @@ api.control.add( setting_id, widget_form_control ); // Make sure widget is removed from the other sidebars - api.each( function ( other_setting ) { + api.each( function( other_setting ) { if ( other_setting.id === control.setting.id ) { return; } @@ -1462,7 +1733,7 @@ widget_form_control.expandForm(); widget_form_control.updateWidget( { instance: widget_form_control.setting(), - complete: function ( error ) { + complete: function( error ) { if ( error ) { throw error; } @@ -1488,7 +1759,9 @@ api.bind( 'ready', function() { // Set up the widgets panel - api.Widgets.availableWidgetsPanel.setup(); + api.Widgets.availableWidgetsPanel = new api.Widgets.AvailableWidgetsPanelView({ + collection: api.Widgets.availableWidgets + }); // Highlight widget control api.Widgets.Previewer.bind( 'highlight-widget-control', api.Widgets.highlightWidgetFormControl ); @@ -1540,10 +1813,10 @@ * @param {string} widget_id * @return {object|null} */ - api.Widgets.getSidebarWidgetControlContainingWidget = function ( widget_id ) { + api.Widgets.getSidebarWidgetControlContainingWidget = function( widget_id ) { var found_control = null; // @todo this can use widget_id_to_setting_id(), then pass into wp.customize.control( x ).getSidebarWidgetsControl() - api.control.each( function ( control ) { + api.control.each( function( control ) { if ( control.params.type === 'sidebar_widgets' && -1 !== _.indexOf( control.setting(), widget_id ) ) { found_control = control; } @@ -1557,10 +1830,10 @@ * @param {string} widget_id * @return {object|null} */ - api.Widgets.getWidgetFormControlForWidget = function ( widget_id ) { + api.Widgets.getWidgetFormControlForWidget = function( widget_id ) { var found_control = null; // @todo We can just use widget_id_to_setting_id() here - api.control.each( function ( control ) { + api.control.each( function( control ) { if ( control.params.type === 'widget_form' && control.params.widget_id === widget_id ) { found_control = control; } @@ -1569,214 +1842,6 @@ return found_control; }; - /** - * Available Widgets Panel - */ - api.Widgets.availableWidgetsPanel = { - active_sidebar_widgets_control: null, - selected_widget_tpl: null, - container: null, - filter_input: null, - - /** - * Set up event listeners - */ - setup: function() { - var panel = this; - - panel.container = $( '#available-widgets' ); - panel.filter_input = $( '#available-widgets-filter' ).find( 'input' ); - - api.Widgets.availableWidgets.on( 'change update', panel.update_available_widgets_list ); - panel.update_available_widgets_list(); - - // If the available widgets panel is open and the customize controls are - // interacted with (i.e. available widgets panel is blurred) then close the - // available widgets panel. - $( '#customize-controls' ).on( 'click keydown', function ( e ) { - var is_add_new_widget_btn = $( e.target ).is( '.add-new-widget, .add-new-widget *' ); - if ( $( 'body' ).hasClass( 'adding-widget' ) && ! is_add_new_widget_btn ) { - panel.close(); - } - } ); - - // Close the panel if the URL in the preview changes - api.Widgets.Previewer.bind( 'url', function() { - panel.close(); - } ); - - // Submit a selection when clicked or keypressed - panel.container.find( '.widget-tpl' ).on( 'click keypress', function( event ) { - - // Only proceed with keypress if it is Enter or Spacebar - if ( event.type === 'keypress' && ( event.which !== 13 && event.which !== 32 ) ) { - return; - } - - panel.submit( this ); - } ); - - panel.filter_input.on( 'input keyup change', function( event ) { - var first_visible_widget; - - api.Widgets.availableWidgets.doSearch( event.target.value ); - - // Remove a widget from being selected if it is no longer visible - if ( panel.selected_widget_tpl && ! panel.selected_widget_tpl.is( ':visible' ) ) { - panel.selected_widget_tpl.removeClass( 'selected' ); - panel.selected_widget_tpl = null; - } - - // If a widget was selected but the filter value has been cleared out, clear selection - if ( panel.selected_widget_tpl && ! event.target.value ) { - panel.selected_widget_tpl.removeClass( 'selected' ); - panel.selected_widget_tpl = null; - } - - // If a filter has been entered and a widget hasn't been selected, select the first one shown - if ( ! panel.selected_widget_tpl && event.target.value ) { - first_visible_widget = panel.container.find( '> .widget-tpl:visible:first' ); - if ( first_visible_widget.length ) { - panel.select( first_visible_widget ); - } - } - } ); - - // Select a widget when it is focused on - panel.container.find( ' > .widget-tpl' ).on( 'focus', function() { - panel.select( this ); - } ); - - panel.container.on( 'keydown', function ( event ) { - var is_enter = ( event.which === 13 ), - is_esc = ( event.which === 27 ), - is_down = ( event.which === 40 ), - is_up = ( event.which === 38 ), - selected_widget_tpl = null, - first_visible_widget = panel.container.find( '> .widget-tpl:visible:first' ), - last_visible_widget = panel.container.find( '> .widget-tpl:visible:last' ), - is_input_focused = $( event.target ).is( panel.filter_input ); - - if ( is_down || is_up ) { - if ( is_down ) { - if ( is_input_focused ) { - selected_widget_tpl = first_visible_widget; - } else if ( panel.selected_widget_tpl && panel.selected_widget_tpl.nextAll( '.widget-tpl:visible' ).length !== 0 ) { - selected_widget_tpl = panel.selected_widget_tpl.nextAll( '.widget-tpl:visible:first' ); - } - } else if ( is_up ) { - if ( is_input_focused ) { - selected_widget_tpl = last_visible_widget; - } else if ( panel.selected_widget_tpl && panel.selected_widget_tpl.prevAll( '.widget-tpl:visible' ).length !== 0 ) { - selected_widget_tpl = panel.selected_widget_tpl.prevAll( '.widget-tpl:visible:first' ); - } - } - panel.select( selected_widget_tpl ); - if ( selected_widget_tpl ) { - selected_widget_tpl.focus(); - } else { - panel.filter_input.focus(); - } - return; - } - - // If enter pressed but nothing entered, don't do anything - if ( is_enter && ! panel.filter_input.val() ) { - return; - } - - if ( is_enter ) { - panel.submit(); - } else if ( is_esc ) { - panel.close( { return_focus: true } ); - } - } ); - }, - - /** - * Updates widgets list. - */ - update_available_widgets_list: function() { - var panel = api.Widgets.availableWidgetsPanel; - - // First hide all widgets... - panel.container.find( '.widget-tpl' ).hide(); - - // ..and then show only available widgets which could be filtered - api.Widgets.availableWidgets.each( function ( widget ) { - var widget_tpl = $( '#widget-tpl-' + widget.id ); - widget_tpl.toggle( ! widget.get( 'is_disabled' ) ); - if ( widget.get( 'is_disabled' ) && widget_tpl.is( panel.selected_widget_tpl ) ) { - panel.selected_widget_tpl = null; - } - } ); - }, - - /** - * @param widget_tpl - */ - select: function ( widget_tpl ) { - var panel = this; - panel.selected_widget_tpl = $( widget_tpl ); - panel.selected_widget_tpl.siblings( '.widget-tpl' ).removeClass( 'selected' ); - panel.selected_widget_tpl.addClass( 'selected' ); - }, - - submit: function ( widget_tpl ) { - var panel = this, widget_id, widget; - if ( ! widget_tpl ) { - widget_tpl = panel.selected_widget_tpl; - } - if ( ! widget_tpl || ! panel.active_sidebar_widgets_control ) { - return; - } - panel.select( widget_tpl ); - - widget_id = $( panel.selected_widget_tpl ).data( 'widget-id' ); - widget = api.Widgets.availableWidgets.findWhere( {id: widget_id} ); - if ( ! widget ) { - throw new Error( 'Widget unexpectedly not found.' ); - } - panel.active_sidebar_widgets_control.addWidget( widget.get( 'id_base' ) ); - panel.close(); - }, - - /** - * @param sidebars_widgets_control - */ - open: function ( sidebars_widgets_control ) { - var panel = this; - panel.active_sidebar_widgets_control = sidebars_widgets_control; - - // Wide widget controls appear over the preview, and so they need to be collapsed when the panel opens - _( sidebars_widgets_control.getWidgetFormControls() ).each( function ( control ) { - if ( control.params.is_wide ) { - control.collapseForm(); - } - } ); - - $( 'body' ).addClass( 'adding-widget' ); - panel.container.find( '.widget-tpl' ).removeClass( 'selected' ); - api.Widgets.availableWidgets.doSearch( '' ); - panel.filter_input.focus(); - }, - - /** - * Hide the panel - */ - close: function ( options ) { - var panel = this; - options = options || {}; - if ( options.return_focus && panel.active_sidebar_widgets_control ) { - panel.active_sidebar_widgets_control.container.find( '.add-new-widget' ).focus(); - } - panel.active_sidebar_widgets_control = null; - panel.selected_widget_tpl = null; - $( 'body' ).removeClass( 'adding-widget' ); - panel.filter_input.val( '' ); - } - }; - /** * @param {String} widget_id * @returns {Object} diff --git a/wp-admin/js/customize-widgets.min.js b/wp-admin/js/customize-widgets.min.js index 741ece38bb..1566cd6a62 100644 --- a/wp-admin/js/customize-widgets.min.js +++ b/wp-admin/js/customize-widgets.min.js @@ -1 +1 @@ -!function(a,b){function c(a){var b,c={number:null,id_base:null};return b=a.match(/^(.+)-(\d+)$/),b?(c.id_base=b[1],c.number=parseInt(b[2],10)):c.id_base=a,c}function d(a){var b,d=c(a);return b="widget_"+d.id_base,d.number&&(b+="["+d.number+"]"),b}if(a&&a.customize){var e,f,g=a.customize;g.Widgets=g.Widgets||{},g.Widgets.data=_wpCustomizeWidgetsSettings||{},e=g.Widgets.data.l10n,delete g.Widgets.data.l10n,g.Widgets.WidgetModel=Backbone.Model.extend({id:null,temp_id:null,classname:null,control_tpl:null,description:null,is_disabled:null,is_multi:null,multi_number:null,name:null,id_base:null,transport:"refresh",params:[],width:null,height:null}),g.Widgets.WidgetCollection=Backbone.Collection.extend({model:g.Widgets.WidgetModel,doSearch:function(a){this.terms!==a&&(this.terms=a,this.terms.length>0&&this.search(this.terms),""===this.terms&&this.reset(g.Widgets.data.availableWidgets),this.trigger("update"))},search:function(a){var b,c,d;this.reset(g.Widgets.data.availableWidgets,{silent:!0}),a=a.replace(/[-\/\\^$*+?.()|[\]{}]/g,"\\$&"),a=a.replace(/ /g,")(?=.*"),b=new RegExp("^(?=.*"+a+").+","i"),c=this.filter(function(a){return d=_.union(a.get("name"),a.get("id"),a.get("description")),b.test(d)}),this.reset(c)}}),g.Widgets.availableWidgets=new g.Widgets.WidgetCollection(g.Widgets.data.availableWidgets),g.Widgets.SidebarModel=Backbone.Model.extend({after_title:null,after_widget:null,before_title:null,before_widget:null,"class":null,description:null,id:null,name:null,is_rendered:!1}),g.Widgets.SidebarCollection=Backbone.Collection.extend({model:g.Widgets.SidebarModel}),g.Widgets.registeredSidebars=new g.Widgets.SidebarCollection(g.Widgets.data.registeredSidebars),g.Widgets.formSyncHandlers={rss:function(a,c,d){var e=c.find(".widget-error:first"),f=b("
"+d+"
").find(".widget-error:first");e.length&&f.length?e.replaceWith(f):e.length?e.remove():f.length&&c.find(".widget-content:first").prepend(f)}},g.Widgets.WidgetControl=g.Control.extend({ready:function(){var a=this;a._setupModel(),a._setupWideWidget(),a._setupControlToggle(),a._setupWidgetTitle(),a._setupReorderUI(),a._setupHighlightEffects(),a._setupUpdateUI(),a._setupRemoveUI()},_setupModel:function(){var a,b=this;g.Widgets.savedWidgetIds=g.Widgets.savedWidgetIds||[],a=function(){g.Widgets.savedWidgetIds[b.params.widget_id]=!0},g.bind("ready",a),g.bind("saved",a),b._update_count=0,b.is_widget_updating=!1,b.live_update_mode=!0,b.setting.bind(function(a,c){_(c).isEqual(a)||b.is_widget_updating||b.updateWidget({instance:a})})},_setupWideWidget:function(){var a,c,d,e,f,h=this;h.params.is_wide&&(a=h.container.find(".widget-inside"),c=a.find("> .form"),d=b(".wp-full-overlay-sidebar-content:first"),h.container.addClass("wide-widget-control"),h.container.find(".widget-content:first").css({"max-width":h.params.width,"min-height":h.params.height}),e=function(){var d,e=h.container.offset().top,f=b(window).height(),g=c.outerHeight();a.css("max-height",f),d=Math.max(0,Math.min(Math.max(e,0),f-g)),a.css("top",d)},f=b("#customize-theme-controls"),h.container.on("expand",function(){e(),d.on("scroll",e),b(window).on("resize",e),f.on("expanded collapsed",e)}),h.container.on("collapsed",function(){d.off("scroll",e),b(window).off("resize",e),f.off("expanded collapsed",e)}),g.each(function(a){0===a.id.indexOf("sidebars_widgets[")&&a.bind(function(){h.container.hasClass("expanded")&&e()})}))},_setupControlToggle:function(){var a,b=this;b.container.find(".widget-top").on("click",function(a){a.preventDefault();var c=b.getSidebarWidgetsControl();c.is_reordering||b.toggleForm()}),a=b.container.find(".widget-control-close"),a.on("click",function(a){a.preventDefault(),b.collapseForm(),b.container.find(".widget-top .widget-action:first").focus()})},_setupWidgetTitle:function(){var a,b=this;a=function(){var a=b.setting().title,c=b.container.find(".in-widget-title");c.text(a?": "+a:"")},b.setting.bind(a),a()},_setupReorderUI:function(){var a,c,d,e,f=this;a=function(a){a.siblings(".selected").removeClass("selected"),a.addClass("selected");var b=a.data("id")===f.params.sidebar_id;f.container.find(".move-widget-btn").prop("disabled",b)},f.container.find(".widget-title-action").after(b(g.Widgets.data.tpl.widgetReorderNav)),c=b(_.template(g.Widgets.data.tpl.moveWidgetArea,{sidebars:_(g.Widgets.registeredSidebars.toArray()).pluck("attributes")})),f.container.find(".widget-top").after(c),e=function(){var d,e=c.find("li");d=e.filter(function(){return b(this).data("id")===f.params.sidebar_id}),e.each(function(){var c,e,f=b(this);c=f.data("id"),e=g.Widgets.registeredSidebars.get(c),f.toggle(e.get("is_rendered")),f.hasClass("selected")&&!e.get("is_rendered")&&a(d)})},e(),g.Widgets.registeredSidebars.on("change:is_rendered",e),d=f.container.find(".widget-reorder-nav"),d.find(".move-widget, .move-widget-down, .move-widget-up").on("click keypress",function(a){if("keypress"!==a.type||13===a.which||32===a.which)if(b(this).focus(),b(this).is(".move-widget"))f.toggleWidgetMoveArea();else{var c=b(this).is(".move-widget-down"),d=b(this).is(".move-widget-up"),e=f.getWidgetSidebarPosition();if(d&&0===e||c&&e===f.getSidebarWidgetsControl().setting().length-1)return;d?f.moveUp():f.moveDown(),b(this).focus()}}),f.container.find(".widget-area-select").on("click keypress","li",function(c){("keypress"!==event.type||13===event.which||32===event.which)&&(c.preventDefault(),a(b(this)))}),f.container.find(".move-widget-btn").click(function(){f.getSidebarWidgetsControl().toggleReordering(!1);var a,b,c,d,e,h=f.params.sidebar_id,i=f.container.find(".widget-area-select li.selected").data("id");a=g("sidebars_widgets["+h+"]"),b=g("sidebars_widgets["+i+"]"),c=Array.prototype.slice.call(a()),d=Array.prototype.slice.call(b()),e=f.getWidgetSidebarPosition(),c.splice(e,1),d.push(f.params.widget_id),a(c),b(d),f.focus()})},_setupHighlightEffects:function(){var a=this;a.container.on("mouseenter click",function(){a.setting.previewer.send("highlight-widget",a.params.widget_id)}),a.setting.bind(function(){a.setting.previewer.send("highlight-widget",a.params.widget_id)}),a.container.on("expand",function(){a.scrollPreviewWidgetIntoView()})},_setupUpdateUI:function(){var a,c,d,f,h,i=this;a=i.container.find(".widget:first"),c=a.find(".widget-content:first"),d=i.container.find(".widget-control-save"),d.val(e.saveBtnLabel),d.attr("title",e.saveBtnTooltip),d.removeClass("button-primary").addClass("button-secondary"),d.on("click",function(a){a.preventDefault(),i.updateWidget({disable_form:!0})}),f=_.debounce(function(){i.updateWidget()},250),i.container.find(".widget-content").on("keydown","input",function(a){13===a.which&&(a.preventDefault(),i.updateWidget({ignore_active_element:!0}))}),c.on("change input propertychange",":input",function(a){i.live_update_mode&&("change"===a.type?i.updateWidget():this.checkValidity&&this.checkValidity()&&f())}),i.setting.previewer.channel.bind("synced",function(){i.container.removeClass("previewer-loading")}),g.Widgets.Previewer.bind("widget-updated",function(a){a===i.params.widget_id&&i.container.removeClass("previewer-loading")}),g.Widgets.Previewer.bind("rendered-widgets",function(a){var b=!!a[i.params.widget_id];i.container.toggleClass("widget-rendered",b)}),h=g.Widgets.formSyncHandlers[i.params.widget_id_base],h&&b(document).on("widget-synced",function(b,c){a.is(c)&&h.apply(document,arguments)})},_setupRemoveUI:function(){var a,b,c=this;a=c.container.find("a.widget-control-remove"),a.on("click",function(a){a.preventDefault();var b;b=c.container.next().is(".customize-control-widget_form")?c.container.next().find(".widget-action:first"):c.container.prev().is(".customize-control-widget_form")?c.container.prev().find(".widget-action:first"):c.container.next(".customize-control-sidebar_widgets").find(".add-new-widget:first"),c.container.slideUp(function(){var a,d,e=g.Widgets.getSidebarWidgetControlContainingWidget(c.params.widget_id);if(!e)throw new Error("Unable to find sidebars_widgets_control");if(a=e.setting().slice(),d=_.indexOf(a,c.params.widget_id),-1===d)throw new Error("Widget is not in sidebar");a.splice(d,1),e.setting(a),b.focus()})}),b=function(){a.text(e.removeBtnLabel),a.attr("title",e.removeBtnTooltip)},c.params.is_new?g.bind("saved",b):b()},_getInputs:function(a){return b(a).find(":input[name]")},_getInputsSignature:function(a){var c=_(a).map(function(a){a=b(a);var c;return c=a.is(":checkbox, :radio")?[a.attr("id"),a.attr("name"),a.prop("value")]:[a.attr("id"),a.attr("name")],c.join(",")});return c.join(";")},_getInputStatePropertyName:function(a){return a=b(a),a.is(":radio, :checkbox")?"checked":"value"},getSidebarWidgetsControl:function(){var a,b,c=this;if(a="sidebars_widgets["+c.params.sidebar_id+"]",b=g.control(a),!b)throw new Error("Unable to locate sidebar_widgets control for "+c.params.sidebar_id);return b},updateWidget:function(c){var d,f,h,i,j,k,l,m,n,o,p,q=this;c=b.extend({instance:null,complete:null,ignore_active_element:!1},c),d=c.instance,f=c.complete,q._update_count+=1,i=q._update_count,h=q.container.find(".widget:first"),j=h.find(".widget-content:first"),j.find(".widget-error").remove(),q.container.addClass("widget-form-loading"),q.container.addClass("previewer-loading"),n=g.state("processing"),n(n()+1),q.live_update_mode||q.container.addClass("widget-form-disabled"),k={},k.action="update-widget",k.wp_customize="on",k.nonce=g.Widgets.data.nonce,l=b.param(k),m=q._getInputs(j),m.each(function(){var a=b(this),c=q._getInputStatePropertyName(this);a.data("state"+i,a.prop(c))}),l+=d?"&"+b.param({sanitized_widget_setting:JSON.stringify(d)}):"&"+m.serialize(),l+="&"+j.find("~ :input").serialize(),o=b.post(a.ajax.settings.url,l,function(a){var d,k,l,n,o=!1;return"0"===a?(g.Widgets.Previewer.preview.iframe.hide(),void g.Widgets.Previewer.login().done(function(){q.updateWidget(c),g.Widgets.Previewer.preview.iframe.show()})):"-1"===a?void g.Widgets.Previewer.cheatin():void(a.success?(k=b("
"+a.data.form+"
"),l=q._getInputs(k),n=q._getInputsSignature(m)===q._getInputsSignature(l),n&&!q.live_update_mode&&(q.live_update_mode=!0,q.container.removeClass("widget-form-disabled"),q.container.find('input[name="savewidget"]').hide()),n&&q.live_update_mode?(m.each(function(a){var d,e,f,g=b(this),h=b(l[a]),j=q._getInputStatePropertyName(this);d=g.data("state"+i),e=h.prop(j),g.data("sanitized",e),f=d!==e&&(c.ignore_active_element||!g.is(document.activeElement)),f&&g.prop(j,e)}),b(document).trigger("widget-synced",[h,a.data.form])):q.live_update_mode?(q.live_update_mode=!1,q.container.find('input[name="savewidget"]').show(),o=!0):(j.html(a.data.form),q.container.removeClass("widget-form-disabled"),b(document).trigger("widget-updated",[h])),p=!o&&!_(q.setting()).isEqual(a.data.instance),p?(q.is_widget_updating=!0,q.setting(a.data.instance),q.is_widget_updating=!1):q.container.removeClass("previewer-loading"),f&&f.call(q,null,{no_change:!p,ajax_finished:!0})):(d=e.error,a.data&&a.data.message&&(d=a.data.message),f?f.call(q,d):j.prepend('

'+d+"

")))}),o.fail(function(a,b){f&&f.call(q,b)}),o.always(function(){q.container.removeClass("widget-form-loading"),m.each(function(){b(this).removeData("state"+i)}),n(n()-1)})},expandControlSection:function(){var a=this.container.closest(".accordion-section");a.hasClass("open")||a.find(".accordion-section-title:first").trigger("click")},expandForm:function(){this.toggleForm(!0)},collapseForm:function(){this.toggleForm(!1)},toggleForm:function(a){var b,c,d,e=this;b=e.container.find("div.widget:first"),c=b.find(".widget-inside:first"),"undefined"==typeof a&&(a=!c.is(":visible")),c.is(":visible")!==a&&(a?(g.control.each(function(a){e.params.type===a.params.type&&e!==a&&a.collapseForm()}),d=function(){e.container.removeClass("expanding"),e.container.addClass("expanded"),e.container.trigger("expanded")},e.params.is_wide?c.fadeIn("fast",d):c.slideDown("fast",d),e.container.trigger("expand"),e.container.addClass("expanding")):(e.container.trigger("collapse"),e.container.addClass("collapsing"),d=function(){e.container.removeClass("collapsing"),e.container.removeClass("expanded"),e.container.trigger("collapsed")},e.params.is_wide?c.fadeOut("fast",d):c.slideUp("fast",function(){b.css({width:"",margin:""}),d()})))},focus:function(){var a=this;a.expandControlSection(),a.expandForm(),a.container.find(".widget-content :focusable:first").focus()},getWidgetSidebarPosition:function(){var a,b,c=this;if(a=c.getSidebarWidgetsControl().setting(),b=_.indexOf(a,c.params.widget_id),-1===b)throw new Error("Widget was unexpectedly not present in the sidebar.");return b},moveUp:function(){this._moveWidgetByOne(-1)},moveDown:function(){this._moveWidgetByOne(1)},_moveWidgetByOne:function(a){var b,c,d,e,f=this;b=f.getWidgetSidebarPosition(),c=f.getSidebarWidgetsControl().setting,d=Array.prototype.slice.call(c()),e=d[b+a],d[b+a]=f.params.widget_id,d[b]=e,c(d)},toggleWidgetMoveArea:function(a){var c,d=this;c=d.container.find(".move-widget-area"),"undefined"==typeof a&&(a=!c.hasClass("active")),a&&(c.find(".selected").removeClass("selected"),c.find("li").filter(function(){return b(this).data("id")===d.params.sidebar_id}).addClass("selected"),d.container.find(".move-widget-btn").prop("disabled",!0)),c.toggleClass("active",a)},scrollPreviewWidgetIntoView:function(){},highlightSectionAndControl:function(){var a,c=this;a=c.container.is(":hidden")?c.container.closest(".control-section"):c.container,b(".widget-customizer-highlighted").removeClass("widget-customizer-highlighted"),a.addClass("widget-customizer-highlighted"),setTimeout(function(){a.removeClass("widget-customizer-highlighted")},500)}}),g.Widgets.SidebarControl=g.Control.extend({ready:function(){var a=this;a.control_section=a.container.closest(".control-section"),a.section_content=a.container.closest(".accordion-section-content"),a._setupModel(),a._setupSortable(),a._setupAddition(),a._applyCardinalOrderClassNames()},_setupModel:function(){var a=this,d=g.Widgets.registeredSidebars.get(a.params.sidebar_id);a.setting.bind(function(d,e){var f,h,i,j=_(e).difference(d);d=_(d).filter(function(a){var b=c(a);return!!g.Widgets.availableWidgets.findWhere({id_base:b.id_base})}),f=_(d).map(function(b){var c=g.Widgets.getWidgetFormControlForWidget(b);return c||(c=a.addWidget(b)),c}),f.sort(function(a,b){var c=_.indexOf(d,a.params.widget_id),e=_.indexOf(d,b.params.widget_id);return c===e?0:e>c?-1:1}),h=a.section_content.find(".customize-control-sidebar_widgets"),i=_(f).map(function(a){return a.container[0]}),h.before(i),a._applyCardinalOrderClassNames(),_(f).each(function(b){b.params.sidebar_id=a.params.sidebar_id}),_(j).each(function(d){setTimeout(function(){var e,f,h,i,j,k=!1;g.each(function(b){if(b.id!==a.setting.id&&0===b.id.indexOf("sidebars_widgets[")&&"sidebars_widgets[wp_inactive_widgets]"!==b.id){var c,e=b();c=_.indexOf(e,d),-1!==c&&(k=!0)}}),k||(e=g.Widgets.getWidgetFormControlForWidget(d),f=e&&b.contains(document,e.container[0])&&!b.contains(a.section_content[0],e.container[0]),e&&!f&&(g.control.remove(e.id),e.container.remove()),g.Widgets.savedWidgetIds[d]&&(h=g.value("sidebars_widgets[wp_inactive_widgets]")().slice(),h.push(d),g.value("sidebars_widgets[wp_inactive_widgets]")(_(h).unique())),i=c(d).id_base,j=g.Widgets.availableWidgets.findWhere({id_base:i}),j&&!j.get("is_multi")&&j.set("is_disabled",!1))})})}),g.Widgets.Previewer.bind("rendered-sidebars",function(b){var c=!!b[a.params.sidebar_id];d.set("is_rendered",c)}),d.on("change:is_rendered",function(){var a,c="#accordion-section-sidebar-widgets-"+this.get("id");a=b(c),this.get("is_rendered")?a.stop().slideDown(function(){b(this).css("height","auto")}):(a.hasClass("open")&&a.find(".accordion-section-title").trigger("click"),a.stop().slideUp())})},_setupSortable:function(){var a=this;a.is_reordering=!1,a.section_content.sortable({items:"> .customize-control-widget_form",handle:".widget-top",axis:"y",connectWith:".accordion-section-content:has(.customize-control-sidebar_widgets)",update:function(){var c,d=a.section_content.sortable("toArray");c=b.map(d,function(a){return b("#"+a).find(":input[name=widget-id]").val()}),a.setting(c)}}),a.control_section.find(".accordion-section-title").droppable({accept:".customize-control-widget_form",over:function(){a.control_section.hasClass("open")||(a.control_section.addClass("open"),a.section_content.toggle(!1).slideToggle(150,function(){a.section_content.sortable("refreshPositions")}))}}),a.container.find(".reorder-toggle").on("click keydown",function(b){("keydown"!==b.type||13===b.which||32===b.which)&&a.toggleReordering(!a.is_reordering)})},_setupAddition:function(){var a=this;a.container.find(".add-new-widget").on("click keydown",function(c){("keydown"!==c.type||13===c.which||32===c.which)&&(a.section_content.hasClass("reordering")||(b("body").hasClass("adding-widget")?g.Widgets.availableWidgetsPanel.close():g.Widgets.availableWidgetsPanel.open(a)))})},_applyCardinalOrderClassNames:function(){var a=this;a.section_content.find(".customize-control-widget_form").removeClass("first-widget").removeClass("last-widget").find(".move-widget-down, .move-widget-up").prop("tabIndex",0),a.section_content.find(".customize-control-widget_form:first").addClass("first-widget").find(".move-widget-up").prop("tabIndex",-1),a.section_content.find(".customize-control-widget_form:last").addClass("last-widget").find(".move-widget-down").prop("tabIndex",-1)},toggleReordering:function(a){var b=this;a=Boolean(a),a!==b.section_content.hasClass("reordering")&&(b.is_reordering=a,b.section_content.toggleClass("reordering",a),a&&_(b.getWidgetFormControls()).each(function(a){a.collapseForm()}))},getWidgetFormControls:function(){var a,b=this;return a=_(b.setting()).map(function(a){var b=d(a),c=g.control(b);if(!c)throw new Error("Unable to find widget_form control for "+a);return c})},addWidget:function(a){var d,e,f,h,i,j,k,l,m,n=this,o="widget_form",p=c(a),q=p.number,r=p.id_base,s=g.Widgets.availableWidgets.findWhere({id_base:r});if(!s)throw new Error("Widget unexpectedly not found.");if(q&&!s.get("is_multi"))throw new Error("Did not expect a widget number to be supplied for a non-multi widget");return s.get("is_multi")&&!q&&(s.set("multi_number",s.get("multi_number")+1),q=s.get("multi_number")),d=b("#widget-tpl-"+s.get("id")).html(),s.get("is_multi")?d=d.replace(/<[^<>]+>/g,function(a){return a.replace(/__i__|%i%/g,q)}):s.set("is_disabled",!0),e=b(d),f=b("
  • "),f.addClass("customize-control"),f.addClass("customize-control-"+o),f.append(e),f.find("> .widget-icon").remove(),s.get("is_multi")&&(f.find('input[name="widget_number"]').val(q),f.find('input[name="multi_number"]').val(q)),a=f.find('[name="widget-id"]').val(),f.hide(),h="widget_"+s.get("id_base"),s.get("is_multi")&&(h+="["+q+"]"),f.attr("id","customize-control-"+h.replace(/\]/g,"").replace(/\[/g,"-")),n.container.after(f),i=g.has(h),i||(m={transport:"refresh",previewer:n.setting.previewer},g.create(h,h,{},m)),j=g.controlConstructor[o],k=new j(h,{params:{settings:{"default":h},sidebar_id:n.params.sidebar_id,widget_id:a,widget_id_base:s.get("id_base"),type:o,is_new:!i,width:s.get("width"),height:s.get("height"),is_wide:s.get("is_wide")},previewer:n.setting.previewer}),g.control.add(h,k),g.each(function(b){if(b.id!==n.setting.id&&0===b.id.indexOf("sidebars_widgets[")){var c,d=b().slice();c=_.indexOf(d,a),-1!==c&&(d.splice(c),b(d))}}),l=n.setting().slice(),-1===_.indexOf(l,a)&&(l.push(a),n.setting(l)),f.slideDown(function(){i?(k.expandForm(),k.updateWidget({instance:k.setting(),complete:function(a){if(a)throw a;k.focus()}})):k.focus()}),b(document).trigger("widget-added",[e]),k}}),b.extend(g.controlConstructor,{widget_form:g.Widgets.WidgetControl,sidebar_widgets:g.Widgets.SidebarControl}),g.bind("ready",function(){g.Widgets.availableWidgetsPanel.setup(),g.Widgets.Previewer.bind("highlight-widget-control",g.Widgets.highlightWidgetFormControl),g.Widgets.Previewer.bind("focus-widget-control",g.Widgets.focusWidgetFormControl)}),f=g.Previewer,g.Previewer=f.extend({initialize:function(a,b){g.Widgets.Previewer=this,f.prototype.initialize.call(this,a,b),this.bind("refresh",this.refresh)}}),g.Widgets.highlightWidgetFormControl=function(a){var b=g.Widgets.getWidgetFormControlForWidget(a);b&&b.highlightSectionAndControl()},g.Widgets.focusWidgetFormControl=function(a){var b=g.Widgets.getWidgetFormControlForWidget(a);b&&b.focus()},g.Widgets.getSidebarWidgetControlContainingWidget=function(a){var b=null;return g.control.each(function(c){"sidebar_widgets"===c.params.type&&-1!==_.indexOf(c.setting(),a)&&(b=c)}),b},g.Widgets.getWidgetFormControlForWidget=function(a){var b=null;return g.control.each(function(c){"widget_form"===c.params.type&&c.params.widget_id===a&&(b=c)}),b},g.Widgets.availableWidgetsPanel={active_sidebar_widgets_control:null,selected_widget_tpl:null,container:null,filter_input:null,setup:function(){var a=this;a.container=b("#available-widgets"),a.filter_input=b("#available-widgets-filter").find("input"),g.Widgets.availableWidgets.on("change update",a.update_available_widgets_list),a.update_available_widgets_list(),b("#customize-controls").on("click keydown",function(c){var d=b(c.target).is(".add-new-widget, .add-new-widget *");b("body").hasClass("adding-widget")&&!d&&a.close()}),g.Widgets.Previewer.bind("url",function(){a.close()}),a.container.find(".widget-tpl").on("click keypress",function(b){("keypress"!==b.type||13===b.which||32===b.which)&&a.submit(this)}),a.filter_input.on("input keyup change",function(b){var c;g.Widgets.availableWidgets.doSearch(b.target.value),a.selected_widget_tpl&&!a.selected_widget_tpl.is(":visible")&&(a.selected_widget_tpl.removeClass("selected"),a.selected_widget_tpl=null),a.selected_widget_tpl&&!b.target.value&&(a.selected_widget_tpl.removeClass("selected"),a.selected_widget_tpl=null),!a.selected_widget_tpl&&b.target.value&&(c=a.container.find("> .widget-tpl:visible:first"),c.length&&a.select(c))}),a.container.find(" > .widget-tpl").on("focus",function(){a.select(this)}),a.container.on("keydown",function(c){var d=13===c.which,e=27===c.which,f=40===c.which,g=38===c.which,h=null,i=a.container.find("> .widget-tpl:visible:first"),j=a.container.find("> .widget-tpl:visible:last"),k=b(c.target).is(a.filter_input);return f||g?(f?k?h=i:a.selected_widget_tpl&&0!==a.selected_widget_tpl.nextAll(".widget-tpl:visible").length&&(h=a.selected_widget_tpl.nextAll(".widget-tpl:visible:first")):g&&(k?h=j:a.selected_widget_tpl&&0!==a.selected_widget_tpl.prevAll(".widget-tpl:visible").length&&(h=a.selected_widget_tpl.prevAll(".widget-tpl:visible:first"))),a.select(h),void(h?h.focus():a.filter_input.focus())):void((!d||a.filter_input.val())&&(d?a.submit():e&&a.close({return_focus:!0})))})},update_available_widgets_list:function(){var a=g.Widgets.availableWidgetsPanel;a.container.find(".widget-tpl").hide(),g.Widgets.availableWidgets.each(function(c){var d=b("#widget-tpl-"+c.id);d.toggle(!c.get("is_disabled")),c.get("is_disabled")&&d.is(a.selected_widget_tpl)&&(a.selected_widget_tpl=null)})},select:function(a){var c=this;c.selected_widget_tpl=b(a),c.selected_widget_tpl.siblings(".widget-tpl").removeClass("selected"),c.selected_widget_tpl.addClass("selected")},submit:function(a){var c,d,e=this;if(a||(a=e.selected_widget_tpl),a&&e.active_sidebar_widgets_control){if(e.select(a),c=b(e.selected_widget_tpl).data("widget-id"),d=g.Widgets.availableWidgets.findWhere({id:c}),!d)throw new Error("Widget unexpectedly not found.");e.active_sidebar_widgets_control.addWidget(d.get("id_base")),e.close()}},open:function(a){var c=this;c.active_sidebar_widgets_control=a,_(a.getWidgetFormControls()).each(function(a){a.params.is_wide&&a.collapseForm()}),b("body").addClass("adding-widget"),c.container.find(".widget-tpl").removeClass("selected"),g.Widgets.availableWidgets.doSearch(""),c.filter_input.focus()},close:function(a){var c=this;a=a||{},a.return_focus&&c.active_sidebar_widgets_control&&c.active_sidebar_widgets_control.container.find(".add-new-widget").focus(),c.active_sidebar_widgets_control=null,c.selected_widget_tpl=null,b("body").removeClass("adding-widget"),c.filter_input.val("")}}}}(window.wp,jQuery); \ No newline at end of file +!function(a,b){function c(a){var b,c={number:null,id_base:null};return b=a.match(/^(.+)-(\d+)$/),b?(c.id_base=b[1],c.number=parseInt(b[2],10)):c.id_base=a,c}function d(a){var b,d=c(a);return b="widget_"+d.id_base,d.number&&(b+="["+d.number+"]"),b}if(a&&a.customize){var e,f,g=a.customize;g.Widgets=g.Widgets||{},g.Widgets.data=_wpCustomizeWidgetsSettings||{},e=g.Widgets.data.l10n,delete g.Widgets.data.l10n,g.Widgets.WidgetModel=Backbone.Model.extend({id:null,temp_id:null,classname:null,control_tpl:null,description:null,is_disabled:null,is_multi:null,multi_number:null,name:null,id_base:null,transport:"refresh",params:[],width:null,height:null}),g.Widgets.WidgetCollection=Backbone.Collection.extend({model:g.Widgets.WidgetModel,doSearch:function(a){this.terms!==a&&(this.terms=a,this.terms.length>0&&this.search(this.terms),""===this.terms&&this.reset(g.Widgets.data.availableWidgets),this.trigger("update"))},search:function(a){var b,c,d;this.reset(g.Widgets.data.availableWidgets,{silent:!0}),a=a.replace(/[-\/\\^$*+?.()|[\]{}]/g,"\\$&"),a=a.replace(/ /g,")(?=.*"),b=new RegExp("^(?=.*"+a+").+","i"),c=this.filter(function(a){return d=_.union(a.get("name"),a.get("id"),a.get("description")),b.test(d)}),this.reset(c)}}),g.Widgets.availableWidgets=new g.Widgets.WidgetCollection(g.Widgets.data.availableWidgets),g.Widgets.SidebarModel=Backbone.Model.extend({after_title:null,after_widget:null,before_title:null,before_widget:null,"class":null,description:null,id:null,name:null,is_rendered:!1}),g.Widgets.SidebarCollection=Backbone.Collection.extend({model:g.Widgets.SidebarModel}),g.Widgets.registeredSidebars=new g.Widgets.SidebarCollection(g.Widgets.data.registeredSidebars),g.Widgets.AvailableWidgetsPanelView=a.Backbone.View.extend({el:"#available-widgets",events:{"input #widgets-search":"search","keyup #widgets-search":"search","change #widgets-search":"search","search #widgets-search":"search","focus .widget-tpl":"focus","click .widget-tpl":"_submit","keypress .widget-tpl":"_submit",keydown:"keyboardAccessible"},selected:null,currentSidebarControl:null,$search:null,initialize:function(){var a=this;this.$search=b("#widgets-search"),_.bindAll(this,"close"),this.listenTo(this.collection,"update",this.updateList),this.updateList(),b("#customize-controls").on("click keydown",function(c){var d=b(c.target).is(".add-new-widget, .add-new-widget *");b("body").hasClass("adding-widget")&&!d&&a.close()}),g.Widgets.Previewer.bind("url",this.close)},search:function(a){var b;this.collection.doSearch(a.target.value),this.selected&&!this.selected.is(":visible")&&(this.selected.removeClass("selected"),this.selected=null),this.selected&&!a.target.value&&(this.selected.removeClass("selected"),this.selected=null),!this.selected&&a.target.value&&(b=this.$el.find("> .widget-tpl:visible:first"),b.length&&this.select(b))},updateList:function(){this.$el.find(".widget-tpl").hide(),this.collection.each(function(a){var c=b("#widget-tpl-"+a.id);c.toggle(!a.get("is_disabled")),a.get("is_disabled")&&c.is(this.selected)&&(this.selected=null)})},select:function(a){this.selected=b(a),this.selected.siblings(".widget-tpl").removeClass("selected"),this.selected.addClass("selected")},focus:function(a){this.select(b(a.currentTarget))},_submit:function(a){("keypress"!==a.type||13===a.which||32===a.which)&&this.submit(b(a.currentTarget))},submit:function(a){var c,d;a||(a=this.selected),a&&this.currentSidebarControl&&(this.select(a),c=b(this.selected).data("widget-id"),d=this.collection.findWhere({id:c}),d&&(this.currentSidebarControl.addWidget(d.get("id_base")),this.close()))},open:function(a){this.currentSidebarControl=a,_(this.currentSidebarControl.getWidgetFormControls()).each(function(a){a.params.is_wide&&a.collapseForm()}),b("body").addClass("adding-widget"),this.$el.find(".selected").removeClass("selected"),this.collection.doSearch(""),this.$search.focus()},close:function(a){a=a||{},a.returnFocus&&this.currentSidebarControl&&this.currentSidebarControl.container.find(".add-new-widget").focus(),this.currentSidebarControl=null,this.selected=null,b("body").removeClass("adding-widget"),this.$search.val("")},keyboardAccessible:function(a){var c=13===a.which,d=27===a.which,e=40===a.which,f=38===a.which,g=null,h=this.$el.find("> .widget-tpl:visible:first"),i=this.$el.find("> .widget-tpl:visible:last"),j=b(a.target).is(this.$search);return e||f?(e?j?g=h:this.selected&&0!==this.selected.nextAll(".widget-tpl:visible").length&&(g=this.selected.nextAll(".widget-tpl:visible:first")):f&&(j?g=i:this.selected&&0!==this.selected.prevAll(".widget-tpl:visible").length&&(g=this.selected.prevAll(".widget-tpl:visible:first"))),this.select(g),void(g?g.focus():this.$search.focus())):void((!c||this.$search.val())&&(c?this.submit():d&&this.close({returnFocus:!0})))}}),g.Widgets.formSyncHandlers={rss:function(a,c,d){var e=c.find(".widget-error:first"),f=b("
    "+d+"
    ").find(".widget-error:first");e.length&&f.length?e.replaceWith(f):e.length?e.remove():f.length&&c.find(".widget-content:first").prepend(f)}},g.Widgets.WidgetControl=g.Control.extend({ready:function(){var a=this;a._setupModel(),a._setupWideWidget(),a._setupControlToggle(),a._setupWidgetTitle(),a._setupReorderUI(),a._setupHighlightEffects(),a._setupUpdateUI(),a._setupRemoveUI()},_setupModel:function(){var a,b=this;g.Widgets.savedWidgetIds=g.Widgets.savedWidgetIds||[],a=function(){g.Widgets.savedWidgetIds[b.params.widget_id]=!0},g.bind("ready",a),g.bind("saved",a),b._update_count=0,b.is_widget_updating=!1,b.live_update_mode=!0,b.setting.bind(function(a,c){_(c).isEqual(a)||b.is_widget_updating||b.updateWidget({instance:a})})},_setupWideWidget:function(){var a,c,d,e,f,h=this;h.params.is_wide&&(a=h.container.find(".widget-inside"),c=a.find("> .form"),d=b(".wp-full-overlay-sidebar-content:first"),h.container.addClass("wide-widget-control"),h.container.find(".widget-content:first").css({"max-width":h.params.width,"min-height":h.params.height}),e=function(){var d,e=h.container.offset().top,f=b(window).height(),g=c.outerHeight();a.css("max-height",f),d=Math.max(0,Math.min(Math.max(e,0),f-g)),a.css("top",d)},f=b("#customize-theme-controls"),h.container.on("expand",function(){e(),d.on("scroll",e),b(window).on("resize",e),f.on("expanded collapsed",e)}),h.container.on("collapsed",function(){d.off("scroll",e),b(window).off("resize",e),f.off("expanded collapsed",e)}),g.each(function(a){0===a.id.indexOf("sidebars_widgets[")&&a.bind(function(){h.container.hasClass("expanded")&&e()})}))},_setupControlToggle:function(){var a,b=this;b.container.find(".widget-top").on("click",function(a){a.preventDefault();var c=b.getSidebarWidgetsControl();c.is_reordering||b.toggleForm()}),a=b.container.find(".widget-control-close"),a.on("click",function(a){a.preventDefault(),b.collapseForm(),b.container.find(".widget-top .widget-action:first").focus()})},_setupWidgetTitle:function(){var a,b=this;a=function(){var a=b.setting().title,c=b.container.find(".in-widget-title");c.text(a?": "+a:"")},b.setting.bind(a),a()},_setupReorderUI:function(){var a,c,d,e,f=this;a=function(a){a.siblings(".selected").removeClass("selected"),a.addClass("selected");var b=a.data("id")===f.params.sidebar_id;f.container.find(".move-widget-btn").prop("disabled",b)},f.container.find(".widget-title-action").after(b(g.Widgets.data.tpl.widgetReorderNav)),c=b(_.template(g.Widgets.data.tpl.moveWidgetArea,{sidebars:_(g.Widgets.registeredSidebars.toArray()).pluck("attributes")})),f.container.find(".widget-top").after(c),e=function(){var d,e=c.find("li");d=e.filter(function(){return b(this).data("id")===f.params.sidebar_id}),e.each(function(){var c,e,f=b(this);c=f.data("id"),e=g.Widgets.registeredSidebars.get(c),f.toggle(e.get("is_rendered")),f.hasClass("selected")&&!e.get("is_rendered")&&a(d)})},e(),g.Widgets.registeredSidebars.on("change:is_rendered",e),d=f.container.find(".widget-reorder-nav"),d.find(".move-widget, .move-widget-down, .move-widget-up").on("click keypress",function(a){if("keypress"!==a.type||13===a.which||32===a.which)if(b(this).focus(),b(this).is(".move-widget"))f.toggleWidgetMoveArea();else{var c=b(this).is(".move-widget-down"),d=b(this).is(".move-widget-up"),e=f.getWidgetSidebarPosition();if(d&&0===e||c&&e===f.getSidebarWidgetsControl().setting().length-1)return;d?f.moveUp():f.moveDown(),b(this).focus()}}),f.container.find(".widget-area-select").on("click keypress","li",function(c){("keypress"!==event.type||13===event.which||32===event.which)&&(c.preventDefault(),a(b(this)))}),f.container.find(".move-widget-btn").click(function(){f.getSidebarWidgetsControl().toggleReordering(!1);var a,b,c,d,e,h=f.params.sidebar_id,i=f.container.find(".widget-area-select li.selected").data("id");a=g("sidebars_widgets["+h+"]"),b=g("sidebars_widgets["+i+"]"),c=Array.prototype.slice.call(a()),d=Array.prototype.slice.call(b()),e=f.getWidgetSidebarPosition(),c.splice(e,1),d.push(f.params.widget_id),a(c),b(d),f.focus()})},_setupHighlightEffects:function(){var a=this;a.container.on("mouseenter click",function(){a.setting.previewer.send("highlight-widget",a.params.widget_id)}),a.setting.bind(function(){a.setting.previewer.send("highlight-widget",a.params.widget_id)}),a.container.on("expand",function(){a.scrollPreviewWidgetIntoView()})},_setupUpdateUI:function(){var a,c,d,f,h,i=this;a=i.container.find(".widget:first"),c=a.find(".widget-content:first"),d=i.container.find(".widget-control-save"),d.val(e.saveBtnLabel),d.attr("title",e.saveBtnTooltip),d.removeClass("button-primary").addClass("button-secondary"),d.on("click",function(a){a.preventDefault(),i.updateWidget({disable_form:!0})}),f=_.debounce(function(){i.updateWidget()},250),i.container.find(".widget-content").on("keydown","input",function(a){13===a.which&&(a.preventDefault(),i.updateWidget({ignore_active_element:!0}))}),c.on("change input propertychange",":input",function(a){i.live_update_mode&&("change"===a.type?i.updateWidget():this.checkValidity&&this.checkValidity()&&f())}),i.setting.previewer.channel.bind("synced",function(){i.container.removeClass("previewer-loading")}),g.Widgets.Previewer.bind("widget-updated",function(a){a===i.params.widget_id&&i.container.removeClass("previewer-loading")}),g.Widgets.Previewer.bind("rendered-widgets",function(a){var b=!!a[i.params.widget_id];i.container.toggleClass("widget-rendered",b)}),h=g.Widgets.formSyncHandlers[i.params.widget_id_base],h&&b(document).on("widget-synced",function(b,c){a.is(c)&&h.apply(document,arguments)})},_setupRemoveUI:function(){var a,b,c=this;a=c.container.find("a.widget-control-remove"),a.on("click",function(a){a.preventDefault();var b;b=c.container.next().is(".customize-control-widget_form")?c.container.next().find(".widget-action:first"):c.container.prev().is(".customize-control-widget_form")?c.container.prev().find(".widget-action:first"):c.container.next(".customize-control-sidebar_widgets").find(".add-new-widget:first"),c.container.slideUp(function(){var a,d,e=g.Widgets.getSidebarWidgetControlContainingWidget(c.params.widget_id);if(!e)throw new Error("Unable to find sidebars_widgets_control");if(a=e.setting().slice(),d=_.indexOf(a,c.params.widget_id),-1===d)throw new Error("Widget is not in sidebar");a.splice(d,1),e.setting(a),b.focus()})}),b=function(){a.text(e.removeBtnLabel),a.attr("title",e.removeBtnTooltip)},c.params.is_new?g.bind("saved",b):b()},_getInputs:function(a){return b(a).find(":input[name]")},_getInputsSignature:function(a){var c=_(a).map(function(a){a=b(a);var c;return c=a.is(":checkbox, :radio")?[a.attr("id"),a.attr("name"),a.prop("value")]:[a.attr("id"),a.attr("name")],c.join(",")});return c.join(";")},_getInputStatePropertyName:function(a){return a=b(a),a.is(":radio, :checkbox")?"checked":"value"},getSidebarWidgetsControl:function(){var a,b,c=this;if(a="sidebars_widgets["+c.params.sidebar_id+"]",b=g.control(a),!b)throw new Error("Unable to locate sidebar_widgets control for "+c.params.sidebar_id);return b},updateWidget:function(c){var d,f,h,i,j,k,l,m,n,o,p,q=this;c=b.extend({instance:null,complete:null,ignore_active_element:!1},c),d=c.instance,f=c.complete,q._update_count+=1,i=q._update_count,h=q.container.find(".widget:first"),j=h.find(".widget-content:first"),j.find(".widget-error").remove(),q.container.addClass("widget-form-loading"),q.container.addClass("previewer-loading"),n=g.state("processing"),n(n()+1),q.live_update_mode||q.container.addClass("widget-form-disabled"),k={},k.action="update-widget",k.wp_customize="on",k.nonce=g.Widgets.data.nonce,l=b.param(k),m=q._getInputs(j),m.each(function(){var a=b(this),c=q._getInputStatePropertyName(this);a.data("state"+i,a.prop(c))}),l+=d?"&"+b.param({sanitized_widget_setting:JSON.stringify(d)}):"&"+m.serialize(),l+="&"+j.find("~ :input").serialize(),o=b.post(a.ajax.settings.url,l,function(a){var d,k,l,n,o=!1;return"0"===a?(g.Widgets.Previewer.preview.iframe.hide(),void g.Widgets.Previewer.login().done(function(){q.updateWidget(c),g.Widgets.Previewer.preview.iframe.show()})):"-1"===a?void g.Widgets.Previewer.cheatin():void(a.success?(k=b("
    "+a.data.form+"
    "),l=q._getInputs(k),n=q._getInputsSignature(m)===q._getInputsSignature(l),n&&!q.live_update_mode&&(q.live_update_mode=!0,q.container.removeClass("widget-form-disabled"),q.container.find('input[name="savewidget"]').hide()),n&&q.live_update_mode?(m.each(function(a){var d,e,f,g=b(this),h=b(l[a]),j=q._getInputStatePropertyName(this);d=g.data("state"+i),e=h.prop(j),g.data("sanitized",e),f=d!==e&&(c.ignore_active_element||!g.is(document.activeElement)),f&&g.prop(j,e)}),b(document).trigger("widget-synced",[h,a.data.form])):q.live_update_mode?(q.live_update_mode=!1,q.container.find('input[name="savewidget"]').show(),o=!0):(j.html(a.data.form),q.container.removeClass("widget-form-disabled"),b(document).trigger("widget-updated",[h])),p=!o&&!_(q.setting()).isEqual(a.data.instance),p?(q.is_widget_updating=!0,q.setting(a.data.instance),q.is_widget_updating=!1):q.container.removeClass("previewer-loading"),f&&f.call(q,null,{no_change:!p,ajax_finished:!0})):(d=e.error,a.data&&a.data.message&&(d=a.data.message),f?f.call(q,d):j.prepend('

    '+d+"

    ")))}),o.fail(function(a,b){f&&f.call(q,b)}),o.always(function(){q.container.removeClass("widget-form-loading"),m.each(function(){b(this).removeData("state"+i)}),n(n()-1)})},expandControlSection:function(){var a=this.container.closest(".accordion-section");a.hasClass("open")||a.find(".accordion-section-title:first").trigger("click")},expandForm:function(){this.toggleForm(!0)},collapseForm:function(){this.toggleForm(!1)},toggleForm:function(a){var b,c,d,e=this;b=e.container.find("div.widget:first"),c=b.find(".widget-inside:first"),"undefined"==typeof a&&(a=!c.is(":visible")),c.is(":visible")!==a&&(a?(g.control.each(function(a){e.params.type===a.params.type&&e!==a&&a.collapseForm()}),d=function(){e.container.removeClass("expanding"),e.container.addClass("expanded"),e.container.trigger("expanded")},e.params.is_wide?c.fadeIn("fast",d):c.slideDown("fast",d),e.container.trigger("expand"),e.container.addClass("expanding")):(e.container.trigger("collapse"),e.container.addClass("collapsing"),d=function(){e.container.removeClass("collapsing"),e.container.removeClass("expanded"),e.container.trigger("collapsed")},e.params.is_wide?c.fadeOut("fast",d):c.slideUp("fast",function(){b.css({width:"",margin:""}),d()})))},focus:function(){var a=this;a.expandControlSection(),a.expandForm(),a.container.find(".widget-content :focusable:first").focus()},getWidgetSidebarPosition:function(){var a,b,c=this;if(a=c.getSidebarWidgetsControl().setting(),b=_.indexOf(a,c.params.widget_id),-1===b)throw new Error("Widget was unexpectedly not present in the sidebar.");return b},moveUp:function(){this._moveWidgetByOne(-1)},moveDown:function(){this._moveWidgetByOne(1)},_moveWidgetByOne:function(a){var b,c,d,e,f=this;b=f.getWidgetSidebarPosition(),c=f.getSidebarWidgetsControl().setting,d=Array.prototype.slice.call(c()),e=d[b+a],d[b+a]=f.params.widget_id,d[b]=e,c(d)},toggleWidgetMoveArea:function(a){var c,d=this;c=d.container.find(".move-widget-area"),"undefined"==typeof a&&(a=!c.hasClass("active")),a&&(c.find(".selected").removeClass("selected"),c.find("li").filter(function(){return b(this).data("id")===d.params.sidebar_id}).addClass("selected"),d.container.find(".move-widget-btn").prop("disabled",!0)),c.toggleClass("active",a)},scrollPreviewWidgetIntoView:function(){},highlightSectionAndControl:function(){var a,c=this;a=c.container.is(":hidden")?c.container.closest(".control-section"):c.container,b(".widget-customizer-highlighted").removeClass("widget-customizer-highlighted"),a.addClass("widget-customizer-highlighted"),setTimeout(function(){a.removeClass("widget-customizer-highlighted")},500)}}),g.Widgets.SidebarControl=g.Control.extend({ready:function(){var a=this;a.control_section=a.container.closest(".control-section"),a.section_content=a.container.closest(".accordion-section-content"),a._setupModel(),a._setupSortable(),a._setupAddition(),a._applyCardinalOrderClassNames()},_setupModel:function(){var a=this,d=g.Widgets.registeredSidebars.get(a.params.sidebar_id);a.setting.bind(function(d,e){var f,h,i,j=_(e).difference(d);d=_(d).filter(function(a){var b=c(a);return!!g.Widgets.availableWidgets.findWhere({id_base:b.id_base})}),f=_(d).map(function(b){var c=g.Widgets.getWidgetFormControlForWidget(b);return c||(c=a.addWidget(b)),c}),f.sort(function(a,b){var c=_.indexOf(d,a.params.widget_id),e=_.indexOf(d,b.params.widget_id);return c===e?0:e>c?-1:1}),h=a.section_content.find(".customize-control-sidebar_widgets"),i=_(f).map(function(a){return a.container[0]}),h.before(i),a._applyCardinalOrderClassNames(),_(f).each(function(b){b.params.sidebar_id=a.params.sidebar_id}),_(j).each(function(d){setTimeout(function(){var e,f,h,i,j,k=!1;g.each(function(b){if(b.id!==a.setting.id&&0===b.id.indexOf("sidebars_widgets[")&&"sidebars_widgets[wp_inactive_widgets]"!==b.id){var c,e=b();c=_.indexOf(e,d),-1!==c&&(k=!0)}}),k||(e=g.Widgets.getWidgetFormControlForWidget(d),f=e&&b.contains(document,e.container[0])&&!b.contains(a.section_content[0],e.container[0]),e&&!f&&(g.control.remove(e.id),e.container.remove()),g.Widgets.savedWidgetIds[d]&&(h=g.value("sidebars_widgets[wp_inactive_widgets]")().slice(),h.push(d),g.value("sidebars_widgets[wp_inactive_widgets]")(_(h).unique())),i=c(d).id_base,j=g.Widgets.availableWidgets.findWhere({id_base:i}),j&&!j.get("is_multi")&&j.set("is_disabled",!1))})})}),g.Widgets.Previewer.bind("rendered-sidebars",function(b){var c=!!b[a.params.sidebar_id];d.set("is_rendered",c)}),d.on("change:is_rendered",function(){var a,c="#accordion-section-sidebar-widgets-"+this.get("id");a=b(c),this.get("is_rendered")?a.stop().slideDown(function(){b(this).css("height","auto")}):(a.hasClass("open")&&a.find(".accordion-section-title").trigger("click"),a.stop().slideUp())})},_setupSortable:function(){var a=this;a.is_reordering=!1,a.section_content.sortable({items:"> .customize-control-widget_form",handle:".widget-top",axis:"y",connectWith:".accordion-section-content:has(.customize-control-sidebar_widgets)",update:function(){var c,d=a.section_content.sortable("toArray");c=b.map(d,function(a){return b("#"+a).find(":input[name=widget-id]").val()}),a.setting(c)}}),a.control_section.find(".accordion-section-title").droppable({accept:".customize-control-widget_form",over:function(){a.control_section.hasClass("open")||(a.control_section.addClass("open"),a.section_content.toggle(!1).slideToggle(150,function(){a.section_content.sortable("refreshPositions")}))}}),a.container.find(".reorder-toggle").on("click keydown",function(b){("keydown"!==b.type||13===b.which||32===b.which)&&a.toggleReordering(!a.is_reordering)})},_setupAddition:function(){var a=this;a.container.find(".add-new-widget").on("click keydown",function(c){("keydown"!==c.type||13===c.which||32===c.which)&&(a.section_content.hasClass("reordering")||(b("body").hasClass("adding-widget")?g.Widgets.availableWidgetsPanel.close():g.Widgets.availableWidgetsPanel.open(a)))})},_applyCardinalOrderClassNames:function(){var a=this;a.section_content.find(".customize-control-widget_form").removeClass("first-widget").removeClass("last-widget").find(".move-widget-down, .move-widget-up").prop("tabIndex",0),a.section_content.find(".customize-control-widget_form:first").addClass("first-widget").find(".move-widget-up").prop("tabIndex",-1),a.section_content.find(".customize-control-widget_form:last").addClass("last-widget").find(".move-widget-down").prop("tabIndex",-1)},toggleReordering:function(a){var b=this;a=Boolean(a),a!==b.section_content.hasClass("reordering")&&(b.is_reordering=a,b.section_content.toggleClass("reordering",a),a&&_(b.getWidgetFormControls()).each(function(a){a.collapseForm()}))},getWidgetFormControls:function(){var a,b=this;return a=_(b.setting()).map(function(a){var b=d(a),c=g.control(b);if(!c)throw new Error("Unable to find widget_form control for "+a);return c})},addWidget:function(a){var d,e,f,h,i,j,k,l,m,n=this,o="widget_form",p=c(a),q=p.number,r=p.id_base,s=g.Widgets.availableWidgets.findWhere({id_base:r});if(!s)throw new Error("Widget unexpectedly not found.");if(q&&!s.get("is_multi"))throw new Error("Did not expect a widget number to be supplied for a non-multi widget");return s.get("is_multi")&&!q&&(s.set("multi_number",s.get("multi_number")+1),q=s.get("multi_number")),d=b("#widget-tpl-"+s.get("id")).html(),s.get("is_multi")?d=d.replace(/<[^<>]+>/g,function(a){return a.replace(/__i__|%i%/g,q)}):s.set("is_disabled",!0),e=b(d),f=b("
  • "),f.addClass("customize-control"),f.addClass("customize-control-"+o),f.append(e),f.find("> .widget-icon").remove(),s.get("is_multi")&&(f.find('input[name="widget_number"]').val(q),f.find('input[name="multi_number"]').val(q)),a=f.find('[name="widget-id"]').val(),f.hide(),h="widget_"+s.get("id_base"),s.get("is_multi")&&(h+="["+q+"]"),f.attr("id","customize-control-"+h.replace(/\]/g,"").replace(/\[/g,"-")),n.container.after(f),i=g.has(h),i||(m={transport:"refresh",previewer:n.setting.previewer},g.create(h,h,{},m)),j=g.controlConstructor[o],k=new j(h,{params:{settings:{"default":h},sidebar_id:n.params.sidebar_id,widget_id:a,widget_id_base:s.get("id_base"),type:o,is_new:!i,width:s.get("width"),height:s.get("height"),is_wide:s.get("is_wide")},previewer:n.setting.previewer}),g.control.add(h,k),g.each(function(b){if(b.id!==n.setting.id&&0===b.id.indexOf("sidebars_widgets[")){var c,d=b().slice();c=_.indexOf(d,a),-1!==c&&(d.splice(c),b(d))}}),l=n.setting().slice(),-1===_.indexOf(l,a)&&(l.push(a),n.setting(l)),f.slideDown(function(){i?(k.expandForm(),k.updateWidget({instance:k.setting(),complete:function(a){if(a)throw a;k.focus()}})):k.focus()}),b(document).trigger("widget-added",[e]),k}}),b.extend(g.controlConstructor,{widget_form:g.Widgets.WidgetControl,sidebar_widgets:g.Widgets.SidebarControl}),g.bind("ready",function(){g.Widgets.availableWidgetsPanel=new g.Widgets.AvailableWidgetsPanelView({collection:g.Widgets.availableWidgets}),g.Widgets.Previewer.bind("highlight-widget-control",g.Widgets.highlightWidgetFormControl),g.Widgets.Previewer.bind("focus-widget-control",g.Widgets.focusWidgetFormControl)}),f=g.Previewer,g.Previewer=f.extend({initialize:function(a,b){g.Widgets.Previewer=this,f.prototype.initialize.call(this,a,b),this.bind("refresh",this.refresh)}}),g.Widgets.highlightWidgetFormControl=function(a){var b=g.Widgets.getWidgetFormControlForWidget(a);b&&b.highlightSectionAndControl()},g.Widgets.focusWidgetFormControl=function(a){var b=g.Widgets.getWidgetFormControlForWidget(a);b&&b.focus()},g.Widgets.getSidebarWidgetControlContainingWidget=function(a){var b=null;return g.control.each(function(c){"sidebar_widgets"===c.params.type&&-1!==_.indexOf(c.setting(),a)&&(b=c)}),b},g.Widgets.getWidgetFormControlForWidget=function(a){var b=null;return g.control.each(function(c){"widget_form"===c.params.type&&c.params.widget_id===a&&(b=c)}),b}}}(window.wp,jQuery); \ No newline at end of file