2014-04-07 05:04:14 -04:00
|
|
|
/* global _wpCustomizeWidgetsSettings */
|
|
|
|
(function( wp, $ ){
|
2014-03-05 15:41:14 -05:00
|
|
|
|
2014-04-07 05:04:14 -04:00
|
|
|
if ( ! wp || ! wp.customize ) { return; }
|
|
|
|
|
|
|
|
// Set up our namespace...
|
|
|
|
var api = wp.customize,
|
2014-07-09 16:58:15 -04:00
|
|
|
l10n;
|
2014-04-07 05:04:14 -04:00
|
|
|
|
|
|
|
api.Widgets = api.Widgets || {};
|
|
|
|
|
|
|
|
// Link settings
|
|
|
|
api.Widgets.data = _wpCustomizeWidgetsSettings || {};
|
|
|
|
l10n = api.Widgets.data.l10n;
|
|
|
|
delete api.Widgets.data.l10n;
|
2014-03-05 15:41:14 -05:00
|
|
|
|
|
|
|
/**
|
2014-04-07 10:10:14 -04:00
|
|
|
* wp.customize.Widgets.WidgetModel
|
|
|
|
*
|
|
|
|
* A single widget model.
|
|
|
|
*
|
|
|
|
* @constructor
|
|
|
|
* @augments Backbone.Model
|
2014-03-05 15:41:14 -05:00
|
|
|
*/
|
2014-04-07 05:04:14 -04:00
|
|
|
api.Widgets.WidgetModel = Backbone.Model.extend({
|
2014-03-05 15:41:14 -05:00
|
|
|
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,
|
2014-04-08 04:12:15 -04:00
|
|
|
height: null,
|
|
|
|
search_matched: true
|
2014-04-07 05:04:14 -04:00
|
|
|
});
|
2014-03-06 11:45:15 -05:00
|
|
|
|
2014-04-07 10:10:14 -04:00
|
|
|
/**
|
|
|
|
* wp.customize.Widgets.WidgetCollection
|
|
|
|
*
|
|
|
|
* Collection for widget models.
|
|
|
|
*
|
|
|
|
* @constructor
|
|
|
|
* @augments Backbone.Model
|
|
|
|
*/
|
2014-04-07 05:04:14 -04:00
|
|
|
api.Widgets.WidgetCollection = Backbone.Collection.extend({
|
|
|
|
model: api.Widgets.WidgetModel,
|
2014-03-21 17:13:15 -04:00
|
|
|
|
|
|
|
// Controls searching on the current widget collection
|
|
|
|
// and triggers an update event
|
|
|
|
doSearch: function( value ) {
|
|
|
|
|
|
|
|
// Don't do anything if we've already done this search
|
|
|
|
// Useful because the search handler fires multiple times per keystroke
|
|
|
|
if ( this.terms === value ) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Updates terms with the value passed
|
|
|
|
this.terms = value;
|
|
|
|
|
|
|
|
// If we have terms, run a search...
|
|
|
|
if ( this.terms.length > 0 ) {
|
|
|
|
this.search( this.terms );
|
|
|
|
}
|
|
|
|
|
|
|
|
// If search is blank, show all themes
|
|
|
|
// Useful for resetting the views when you clean the input
|
|
|
|
if ( this.terms === '' ) {
|
2014-04-08 04:12:15 -04:00
|
|
|
this.each( function ( widget ) {
|
|
|
|
widget.set( 'search_matched', true );
|
|
|
|
} );
|
2014-03-21 17:13:15 -04:00
|
|
|
}
|
|
|
|
},
|
|
|
|
|
|
|
|
// Performs a search within the collection
|
|
|
|
// @uses RegExp
|
|
|
|
search: function( term ) {
|
2014-04-08 04:12:15 -04:00
|
|
|
var match, haystack;
|
2014-03-21 17:13:15 -04:00
|
|
|
|
|
|
|
// Escape the term string for RegExp meta characters
|
|
|
|
term = term.replace( /[-\/\\^$*+?.()|[\]{}]/g, '\\$&' );
|
|
|
|
|
|
|
|
// Consider spaces as word delimiters and match the whole string
|
|
|
|
// so matching terms can be combined
|
|
|
|
term = term.replace( / /g, ')(?=.*' );
|
|
|
|
match = new RegExp( '^(?=.*' + term + ').+', 'i' );
|
|
|
|
|
2014-04-08 04:12:15 -04:00
|
|
|
this.each( function ( data ) {
|
|
|
|
haystack = [ data.get( 'name' ), data.get( 'id' ), data.get( 'description' ) ].join( ' ' );
|
|
|
|
data.set( 'search_matched', match.test( haystack ) );
|
|
|
|
} );
|
2014-03-21 17:13:15 -04:00
|
|
|
}
|
2014-04-07 05:04:14 -04:00
|
|
|
});
|
|
|
|
api.Widgets.availableWidgets = new api.Widgets.WidgetCollection( api.Widgets.data.availableWidgets );
|
2014-03-05 15:41:14 -05:00
|
|
|
|
2014-04-07 10:10:14 -04:00
|
|
|
/**
|
|
|
|
* wp.customize.Widgets.SidebarModel
|
|
|
|
*
|
|
|
|
* A single sidebar model.
|
|
|
|
*
|
|
|
|
* @constructor
|
|
|
|
* @augments Backbone.Model
|
|
|
|
*/
|
2014-04-07 05:04:14 -04:00
|
|
|
api.Widgets.SidebarModel = Backbone.Model.extend({
|
2014-03-05 15:41:14 -05:00
|
|
|
after_title: null,
|
|
|
|
after_widget: null,
|
|
|
|
before_title: null,
|
|
|
|
before_widget: null,
|
|
|
|
'class': null,
|
|
|
|
description: null,
|
|
|
|
id: null,
|
|
|
|
name: null,
|
|
|
|
is_rendered: false
|
2014-04-07 05:04:14 -04:00
|
|
|
});
|
2014-03-06 11:45:15 -05:00
|
|
|
|
2014-04-07 10:10:14 -04:00
|
|
|
/**
|
|
|
|
* wp.customize.Widgets.SidebarCollection
|
|
|
|
*
|
|
|
|
* Collection for sidebar models.
|
|
|
|
*
|
|
|
|
* @constructor
|
|
|
|
* @augments Backbone.Collection
|
|
|
|
*/
|
2014-04-07 05:04:14 -04:00
|
|
|
api.Widgets.SidebarCollection = Backbone.Collection.extend({
|
|
|
|
model: api.Widgets.SidebarModel
|
|
|
|
});
|
|
|
|
api.Widgets.registeredSidebars = new api.Widgets.SidebarCollection( api.Widgets.data.registeredSidebars );
|
2014-03-05 15:41:14 -05:00
|
|
|
|
2014-04-07 10:10:14 -04:00
|
|
|
/**
|
|
|
|
* 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' );
|
|
|
|
|
2014-04-07 12:32:15 -04:00
|
|
|
this.listenTo( this.collection, 'change', this.updateList );
|
2014-04-07 10:10:14 -04:00
|
|
|
|
|
|
|
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
|
2014-07-09 16:58:15 -04:00
|
|
|
api.previewer.bind( 'url', this.close );
|
2014-04-07 10:10:14 -04:00
|
|
|
},
|
|
|
|
|
|
|
|
// 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 );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
2014-04-08 04:12:15 -04:00
|
|
|
// Changes visibility of available widgets
|
2014-04-07 10:10:14 -04:00
|
|
|
updateList: function() {
|
|
|
|
this.collection.each( function( widget ) {
|
|
|
|
var widgetTpl = $( '#widget-tpl-' + widget.id );
|
2014-04-08 04:12:15 -04:00
|
|
|
widgetTpl.toggle( widget.get( 'search_matched' ) && ! widget.get( 'is_disabled' ) );
|
2014-04-07 10:10:14 -04:00
|
|
|
if ( widget.get( 'is_disabled' ) && widgetTpl.is( this.selected ) ) {
|
|
|
|
this.selected = null;
|
|
|
|
}
|
|
|
|
} );
|
|
|
|
},
|
|
|
|
|
2014-04-08 04:12:15 -04:00
|
|
|
// Highlights a widget
|
2014-04-07 10:10:14 -04:00
|
|
|
select: function( widgetTpl ) {
|
|
|
|
this.selected = $( widgetTpl );
|
|
|
|
this.selected.siblings( '.widget-tpl' ).removeClass( 'selected' );
|
|
|
|
this.selected.addClass( 'selected' );
|
|
|
|
},
|
|
|
|
|
2014-04-08 04:12:15 -04:00
|
|
|
// Highlights a widget on focus
|
2014-04-07 10:10:14 -04:00
|
|
|
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 ),
|
2014-07-18 21:19:14 -04:00
|
|
|
isTab = ( event.which === 9 ),
|
|
|
|
isShift = ( event.shiftKey ),
|
2014-04-07 10:10:14 -04:00
|
|
|
selected = null,
|
|
|
|
firstVisible = this.$el.find( '> .widget-tpl:visible:first' ),
|
|
|
|
lastVisible = this.$el.find( '> .widget-tpl:visible:last' ),
|
2014-07-18 21:19:14 -04:00
|
|
|
isSearchFocused = $( event.target ).is( this.$search ),
|
|
|
|
isLastWidgetFocused = $( event.target ).is( '.widget-tpl:visible:last' );
|
2014-04-07 10:10:14 -04:00
|
|
|
|
|
|
|
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 } );
|
|
|
|
}
|
2014-07-18 21:19:14 -04:00
|
|
|
|
|
|
|
if ( isTab && ( isShift && isSearchFocused || ! isShift && isLastWidgetFocused ) ) {
|
|
|
|
this.currentSidebarControl.container.find( '.add-new-widget' ).focus();
|
|
|
|
event.preventDefault();
|
|
|
|
}
|
2014-04-07 10:10:14 -04:00
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2014-04-02 14:20:16 -04:00
|
|
|
/**
|
|
|
|
* Handlers for the widget-synced event, organized by widget ID base.
|
|
|
|
* Other widgets may provide their own update handlers by adding
|
|
|
|
* listeners for the widget-synced event.
|
|
|
|
*/
|
2014-04-07 05:04:14 -04:00
|
|
|
api.Widgets.formSyncHandlers = {
|
2014-04-02 14:20:16 -04:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @param {jQuery.Event} e
|
2014-04-07 05:04:14 -04:00
|
|
|
* @param {jQuery} widget
|
|
|
|
* @param {String} newForm
|
2014-04-02 14:20:16 -04:00
|
|
|
*/
|
2014-04-07 10:10:14 -04:00
|
|
|
rss: function( e, widget, newForm ) {
|
2014-04-07 05:04:14 -04:00
|
|
|
var oldWidgetError = widget.find( '.widget-error:first' ),
|
|
|
|
newWidgetError = $( '<div>' + newForm + '</div>' ).find( '.widget-error:first' );
|
|
|
|
|
|
|
|
if ( oldWidgetError.length && newWidgetError.length ) {
|
|
|
|
oldWidgetError.replaceWith( newWidgetError );
|
|
|
|
} else if ( oldWidgetError.length ) {
|
|
|
|
oldWidgetError.remove();
|
|
|
|
} else if ( newWidgetError.length ) {
|
|
|
|
widget.find( '.widget-content:first' ).prepend( newWidgetError );
|
2014-04-02 14:20:16 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2014-03-05 15:41:14 -05:00
|
|
|
/**
|
2014-04-07 10:10:14 -04:00
|
|
|
* wp.customize.Widgets.WidgetControl
|
|
|
|
*
|
|
|
|
* Customizer control for widgets.
|
2014-04-07 05:04:14 -04:00
|
|
|
* Note that 'widget_form' must match the WP_Widget_Form_Customize_Control::$type
|
2014-04-07 10:10:14 -04:00
|
|
|
*
|
|
|
|
* @constructor
|
|
|
|
* @augments wp.customize.Control
|
2014-03-05 15:41:14 -05:00
|
|
|
*/
|
2014-04-07 05:04:14 -04:00
|
|
|
api.Widgets.WidgetControl = api.Control.extend({
|
2014-03-05 15:41:14 -05:00
|
|
|
/**
|
|
|
|
* Set up the control
|
|
|
|
*/
|
|
|
|
ready: function() {
|
2014-04-07 12:32:15 -04:00
|
|
|
this._setupModel();
|
|
|
|
this._setupWideWidget();
|
|
|
|
this._setupControlToggle();
|
|
|
|
this._setupWidgetTitle();
|
|
|
|
this._setupReorderUI();
|
|
|
|
this._setupHighlightEffects();
|
|
|
|
this._setupUpdateUI();
|
|
|
|
this._setupRemoveUI();
|
2014-03-05 15:41:14 -05:00
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
2014-04-07 05:04:14 -04:00
|
|
|
* Handle changes to the setting
|
2014-03-05 15:41:14 -05:00
|
|
|
*/
|
|
|
|
_setupModel: function() {
|
2014-04-07 12:32:15 -04:00
|
|
|
var self = this, rememberSavedWidgetId;
|
2014-03-05 15:41:14 -05:00
|
|
|
|
2014-04-07 05:04:14 -04:00
|
|
|
api.Widgets.savedWidgetIds = api.Widgets.savedWidgetIds || [];
|
2014-03-05 15:41:14 -05:00
|
|
|
|
2014-04-07 05:04:14 -04:00
|
|
|
// Remember saved widgets so we know which to trash (move to inactive widgets sidebar)
|
2014-04-07 12:32:15 -04:00
|
|
|
rememberSavedWidgetId = function() {
|
|
|
|
api.Widgets.savedWidgetIds[self.params.widget_id] = true;
|
2014-04-07 05:04:14 -04:00
|
|
|
};
|
2014-04-07 12:32:15 -04:00
|
|
|
api.bind( 'ready', rememberSavedWidgetId );
|
|
|
|
api.bind( 'saved', rememberSavedWidgetId );
|
2014-03-06 11:45:15 -05:00
|
|
|
|
2014-04-07 12:32:15 -04:00
|
|
|
this._updateCount = 0;
|
|
|
|
this.isWidgetUpdating = false;
|
|
|
|
this.liveUpdateMode = true;
|
2014-03-05 15:41:14 -05:00
|
|
|
|
2014-04-07 05:04:14 -04:00
|
|
|
// Update widget whenever model changes
|
2014-04-07 12:32:15 -04:00
|
|
|
this.setting.bind( function( to, from ) {
|
|
|
|
if ( ! _( from ).isEqual( to ) && ! self.isWidgetUpdating ) {
|
|
|
|
self.updateWidget( { instance: to } );
|
2014-04-07 05:04:14 -04:00
|
|
|
}
|
|
|
|
} );
|
|
|
|
},
|
2014-03-05 15:41:14 -05:00
|
|
|
|
2014-04-07 05:04:14 -04:00
|
|
|
/**
|
|
|
|
* Add special behaviors for wide widget controls
|
|
|
|
*/
|
|
|
|
_setupWideWidget: function() {
|
2014-04-07 12:32:15 -04:00
|
|
|
var self = this, $widgetInside, $widgetForm, $customizeSidebar,
|
|
|
|
$themeControlsContainer, positionWidget;
|
2014-03-05 15:41:14 -05:00
|
|
|
|
2014-04-07 12:32:15 -04:00
|
|
|
if ( ! this.params.is_wide ) {
|
2014-04-07 05:04:14 -04:00
|
|
|
return;
|
|
|
|
}
|
2014-03-05 15:41:14 -05:00
|
|
|
|
2014-04-07 12:32:15 -04:00
|
|
|
$widgetInside = this.container.find( '.widget-inside' );
|
|
|
|
$widgetForm = $widgetInside.find( '> .form' );
|
|
|
|
$customizeSidebar = $( '.wp-full-overlay-sidebar-content:first' );
|
|
|
|
this.container.addClass( 'wide-widget-control' );
|
2014-03-05 15:41:14 -05:00
|
|
|
|
2014-04-07 12:32:15 -04:00
|
|
|
this.container.find( '.widget-content:first' ).css( {
|
|
|
|
'max-width': this.params.width,
|
|
|
|
'min-height': this.params.height
|
2014-04-07 05:04:14 -04:00
|
|
|
} );
|
2014-03-05 15:41:14 -05:00
|
|
|
|
2014-04-07 05:04:14 -04:00
|
|
|
/**
|
|
|
|
* Keep the widget-inside positioned so the top of fixed-positioned
|
|
|
|
* element is at the same top position as the widget-top. When the
|
|
|
|
* widget-top is scrolled out of view, keep the widget-top in view;
|
|
|
|
* likewise, don't allow the widget to drop off the bottom of the window.
|
|
|
|
* If a widget is too tall to fit in the window, don't let the height
|
|
|
|
* exceed the window height so that the contents of the widget control
|
|
|
|
* will become scrollable (overflow:auto).
|
|
|
|
*/
|
2014-04-07 12:32:15 -04:00
|
|
|
positionWidget = function() {
|
|
|
|
var offsetTop = self.container.offset().top,
|
|
|
|
windowHeight = $( window ).height(),
|
|
|
|
formHeight = $widgetForm.outerHeight(),
|
2014-04-07 05:04:14 -04:00
|
|
|
top;
|
2014-04-07 12:32:15 -04:00
|
|
|
$widgetInside.css( 'max-height', windowHeight );
|
2014-04-07 05:04:14 -04:00
|
|
|
top = Math.max(
|
|
|
|
0, // prevent top from going off screen
|
|
|
|
Math.min(
|
2014-04-07 12:32:15 -04:00
|
|
|
Math.max( offsetTop, 0 ), // distance widget in panel is from top of screen
|
|
|
|
windowHeight - formHeight // flush up against bottom of screen
|
2014-04-07 05:04:14 -04:00
|
|
|
)
|
|
|
|
);
|
2014-04-07 12:32:15 -04:00
|
|
|
$widgetInside.css( 'top', top );
|
2014-04-07 05:04:14 -04:00
|
|
|
};
|
2014-03-05 15:41:14 -05:00
|
|
|
|
2014-04-07 12:32:15 -04:00
|
|
|
$themeControlsContainer = $( '#customize-theme-controls' );
|
|
|
|
this.container.on( 'expand', function() {
|
|
|
|
positionWidget();
|
|
|
|
$customizeSidebar.on( 'scroll', positionWidget );
|
|
|
|
$( window ).on( 'resize', positionWidget );
|
|
|
|
$themeControlsContainer.on( 'expanded collapsed', positionWidget );
|
2014-03-05 15:41:14 -05:00
|
|
|
} );
|
2014-04-07 12:32:15 -04:00
|
|
|
this.container.on( 'collapsed', function() {
|
|
|
|
$customizeSidebar.off( 'scroll', positionWidget );
|
|
|
|
$( window ).off( 'resize', positionWidget );
|
|
|
|
$themeControlsContainer.off( 'expanded collapsed', positionWidget );
|
2014-03-05 15:41:14 -05:00
|
|
|
} );
|
|
|
|
|
2014-04-07 05:04:14 -04:00
|
|
|
// Reposition whenever a sidebar's widgets are changed
|
2014-04-07 10:10:14 -04:00
|
|
|
api.each( function( setting ) {
|
2014-04-07 05:04:14 -04:00
|
|
|
if ( 0 === setting.id.indexOf( 'sidebars_widgets[' ) ) {
|
|
|
|
setting.bind( function() {
|
2014-04-07 12:32:15 -04:00
|
|
|
if ( self.container.hasClass( 'expanded' ) ) {
|
|
|
|
positionWidget();
|
2014-04-07 05:04:14 -04:00
|
|
|
}
|
2014-03-05 15:41:14 -05:00
|
|
|
} );
|
|
|
|
}
|
|
|
|
} );
|
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
2014-04-07 05:04:14 -04:00
|
|
|
* Show/hide the control when clicking on the form title, when clicking
|
|
|
|
* the close button
|
2014-03-05 15:41:14 -05:00
|
|
|
*/
|
2014-04-07 05:04:14 -04:00
|
|
|
_setupControlToggle: function() {
|
2014-04-07 12:32:15 -04:00
|
|
|
var self = this, $closeBtn;
|
2014-03-05 15:41:14 -05:00
|
|
|
|
2014-04-07 12:32:15 -04:00
|
|
|
this.container.find( '.widget-top' ).on( 'click', function( e ) {
|
2014-04-07 05:04:14 -04:00
|
|
|
e.preventDefault();
|
2014-04-07 12:32:15 -04:00
|
|
|
var sidebarWidgetsControl = self.getSidebarWidgetsControl();
|
2014-04-07 16:00:16 -04:00
|
|
|
if ( sidebarWidgetsControl.isReordering ) {
|
2014-04-07 05:04:14 -04:00
|
|
|
return;
|
2014-03-05 15:41:14 -05:00
|
|
|
}
|
2014-04-07 12:32:15 -04:00
|
|
|
self.toggleForm();
|
2014-03-05 15:41:14 -05:00
|
|
|
} );
|
|
|
|
|
2014-04-07 12:32:15 -04:00
|
|
|
$closeBtn = this.container.find( '.widget-control-close' );
|
|
|
|
$closeBtn.on( 'click', function( e ) {
|
2014-04-07 05:04:14 -04:00
|
|
|
e.preventDefault();
|
2014-04-07 12:32:15 -04:00
|
|
|
self.collapseForm();
|
|
|
|
self.container.find( '.widget-top .widget-action:first' ).focus(); // keyboard accessibility
|
2014-03-05 15:41:14 -05:00
|
|
|
} );
|
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
2014-04-07 05:04:14 -04:00
|
|
|
* Update the title of the form if a title field is entered
|
2014-03-05 15:41:14 -05:00
|
|
|
*/
|
2014-04-07 05:04:14 -04:00
|
|
|
_setupWidgetTitle: function() {
|
2014-04-07 12:32:15 -04:00
|
|
|
var self = this, updateTitle;
|
2014-03-05 15:41:14 -05:00
|
|
|
|
2014-04-07 12:32:15 -04:00
|
|
|
updateTitle = function() {
|
|
|
|
var title = self.setting().title,
|
|
|
|
inWidgetTitle = self.container.find( '.in-widget-title' );
|
2014-04-07 05:04:14 -04:00
|
|
|
|
|
|
|
if ( title ) {
|
2014-04-07 12:32:15 -04:00
|
|
|
inWidgetTitle.text( ': ' + title );
|
2014-04-07 05:04:14 -04:00
|
|
|
} else {
|
2014-04-07 12:32:15 -04:00
|
|
|
inWidgetTitle.text( '' );
|
2014-03-05 15:41:14 -05:00
|
|
|
}
|
2014-04-07 05:04:14 -04:00
|
|
|
};
|
2014-04-07 12:32:15 -04:00
|
|
|
this.setting.bind( updateTitle );
|
|
|
|
updateTitle();
|
2014-04-07 05:04:14 -04:00
|
|
|
},
|
2014-03-05 15:41:14 -05:00
|
|
|
|
2014-04-07 05:04:14 -04:00
|
|
|
/**
|
|
|
|
* Set up the widget-reorder-nav
|
|
|
|
*/
|
|
|
|
_setupReorderUI: function() {
|
2014-04-07 12:32:15 -04:00
|
|
|
var self = this, selectSidebarItem, $moveWidgetArea,
|
|
|
|
$reorderNav, updateAvailableSidebars;
|
2014-04-07 05:04:14 -04:00
|
|
|
|
|
|
|
/**
|
|
|
|
* select the provided sidebar list item in the move widget area
|
|
|
|
*
|
|
|
|
* @param {jQuery} li
|
|
|
|
*/
|
2014-04-07 12:32:15 -04:00
|
|
|
selectSidebarItem = function( li ) {
|
2014-04-07 05:04:14 -04:00
|
|
|
li.siblings( '.selected' ).removeClass( 'selected' );
|
|
|
|
li.addClass( 'selected' );
|
2014-04-07 12:32:15 -04:00
|
|
|
var isSelfSidebar = ( li.data( 'id' ) === self.params.sidebar_id );
|
|
|
|
self.container.find( '.move-widget-btn' ).prop( 'disabled', isSelfSidebar );
|
2014-04-07 05:04:14 -04:00
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Add the widget reordering elements to the widget control
|
|
|
|
*/
|
2014-04-07 12:32:15 -04:00
|
|
|
this.container.find( '.widget-title-action' ).after( $( api.Widgets.data.tpl.widgetReorderNav ) );
|
|
|
|
$moveWidgetArea = $(
|
2014-04-07 05:04:14 -04:00
|
|
|
_.template( api.Widgets.data.tpl.moveWidgetArea, {
|
|
|
|
sidebars: _( api.Widgets.registeredSidebars.toArray() ).pluck( 'attributes' )
|
|
|
|
} )
|
|
|
|
);
|
2014-04-07 12:32:15 -04:00
|
|
|
this.container.find( '.widget-top' ).after( $moveWidgetArea );
|
2014-04-07 05:04:14 -04:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Update available sidebars when their rendered state changes
|
|
|
|
*/
|
2014-04-07 12:32:15 -04:00
|
|
|
updateAvailableSidebars = function() {
|
|
|
|
var $sidebarItems = $moveWidgetArea.find( 'li' ), selfSidebarItem;
|
|
|
|
|
|
|
|
selfSidebarItem = $sidebarItems.filter( function(){
|
|
|
|
return $( this ).data( 'id' ) === self.params.sidebar_id;
|
2014-04-07 05:04:14 -04:00
|
|
|
} );
|
2014-04-07 12:32:15 -04:00
|
|
|
|
|
|
|
$sidebarItems.each( function() {
|
2014-04-07 05:04:14 -04:00
|
|
|
var li = $( this ),
|
2014-04-07 12:32:15 -04:00
|
|
|
sidebarId,
|
2014-04-07 16:00:16 -04:00
|
|
|
sidebar;
|
2014-04-07 05:04:14 -04:00
|
|
|
|
2014-04-07 12:32:15 -04:00
|
|
|
sidebarId = li.data( 'id' );
|
2014-04-07 16:00:16 -04:00
|
|
|
sidebar = api.Widgets.registeredSidebars.get( sidebarId );
|
2014-04-07 12:32:15 -04:00
|
|
|
|
2014-04-07 16:00:16 -04:00
|
|
|
li.toggle( sidebar.get( 'is_rendered' ) );
|
2014-04-07 12:32:15 -04:00
|
|
|
|
2014-04-07 16:00:16 -04:00
|
|
|
if ( li.hasClass( 'selected' ) && ! sidebar.get( 'is_rendered' ) ) {
|
2014-04-07 12:32:15 -04:00
|
|
|
selectSidebarItem( selfSidebarItem );
|
2014-04-07 05:04:14 -04:00
|
|
|
}
|
|
|
|
} );
|
|
|
|
};
|
2014-04-07 12:32:15 -04:00
|
|
|
|
|
|
|
updateAvailableSidebars();
|
|
|
|
api.Widgets.registeredSidebars.on( 'change:is_rendered', updateAvailableSidebars );
|
2014-04-07 05:04:14 -04:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Handle clicks for up/down/move on the reorder nav
|
|
|
|
*/
|
2014-04-07 12:32:15 -04:00
|
|
|
$reorderNav = this.container.find( '.widget-reorder-nav' );
|
2014-07-18 21:58:15 -04:00
|
|
|
$reorderNav.find( '.move-widget, .move-widget-down, .move-widget-up' ).each( function() {
|
|
|
|
$( this ).prepend( self.container.find( '.widget-title' ).text() + ': ' );
|
|
|
|
} ).on( 'click keypress', function( event ) {
|
2014-04-07 05:04:14 -04:00
|
|
|
if ( event.type === 'keypress' && ( event.which !== 13 && event.which !== 32 ) ) {
|
2014-03-05 15:41:14 -05:00
|
|
|
return;
|
|
|
|
}
|
2014-04-07 05:04:14 -04:00
|
|
|
$( this ).focus();
|
2014-03-05 15:41:14 -05:00
|
|
|
|
2014-04-07 05:04:14 -04:00
|
|
|
if ( $( this ).is( '.move-widget' ) ) {
|
2014-04-07 12:32:15 -04:00
|
|
|
self.toggleWidgetMoveArea();
|
2014-03-05 15:41:14 -05:00
|
|
|
} else {
|
2014-04-07 12:32:15 -04:00
|
|
|
var isMoveDown = $( this ).is( '.move-widget-down' ),
|
|
|
|
isMoveUp = $( this ).is( '.move-widget-up' ),
|
|
|
|
i = self.getWidgetSidebarPosition();
|
2014-04-07 05:04:14 -04:00
|
|
|
|
2014-04-07 12:32:15 -04:00
|
|
|
if ( ( isMoveUp && i === 0 ) || ( isMoveDown && i === self.getSidebarWidgetsControl().setting().length - 1 ) ) {
|
2014-04-07 05:04:14 -04:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2014-04-07 12:32:15 -04:00
|
|
|
if ( isMoveUp ) {
|
|
|
|
self.moveUp();
|
2014-04-07 05:04:14 -04:00
|
|
|
} else {
|
2014-04-07 12:32:15 -04:00
|
|
|
self.moveDown();
|
2014-04-07 05:04:14 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
$( this ).focus(); // re-focus after the container was moved
|
2014-03-05 15:41:14 -05:00
|
|
|
}
|
|
|
|
} );
|
|
|
|
|
2014-04-07 05:04:14 -04:00
|
|
|
/**
|
|
|
|
* Handle selecting a sidebar to move to
|
|
|
|
*/
|
2014-04-07 12:32:15 -04:00
|
|
|
this.container.find( '.widget-area-select' ).on( 'click keypress', 'li', function( e ) {
|
2014-04-07 05:04:14 -04:00
|
|
|
if ( event.type === 'keypress' && ( event.which !== 13 && event.which !== 32 ) ) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
e.preventDefault();
|
2014-04-07 12:32:15 -04:00
|
|
|
selectSidebarItem( $( this ) );
|
2014-04-07 05:04:14 -04:00
|
|
|
} );
|
2014-03-05 15:41:14 -05:00
|
|
|
|
2014-04-07 05:04:14 -04:00
|
|
|
/**
|
|
|
|
* Move widget to another sidebar
|
|
|
|
*/
|
2014-04-07 12:32:15 -04:00
|
|
|
this.container.find( '.move-widget-btn' ).click( function() {
|
|
|
|
self.getSidebarWidgetsControl().toggleReordering( false );
|
|
|
|
|
|
|
|
var oldSidebarId = self.params.sidebar_id,
|
|
|
|
newSidebarId = self.container.find( '.widget-area-select li.selected' ).data( 'id' ),
|
|
|
|
oldSidebarWidgetsSetting, newSidebarWidgetsSetting,
|
|
|
|
oldSidebarWidgetIds, newSidebarWidgetIds, i;
|
|
|
|
|
|
|
|
oldSidebarWidgetsSetting = api( 'sidebars_widgets[' + oldSidebarId + ']' );
|
|
|
|
newSidebarWidgetsSetting = api( 'sidebars_widgets[' + newSidebarId + ']' );
|
|
|
|
oldSidebarWidgetIds = Array.prototype.slice.call( oldSidebarWidgetsSetting() );
|
|
|
|
newSidebarWidgetIds = Array.prototype.slice.call( newSidebarWidgetsSetting() );
|
|
|
|
|
|
|
|
i = self.getWidgetSidebarPosition();
|
|
|
|
oldSidebarWidgetIds.splice( i, 1 );
|
|
|
|
newSidebarWidgetIds.push( self.params.widget_id );
|
|
|
|
|
|
|
|
oldSidebarWidgetsSetting( oldSidebarWidgetIds );
|
|
|
|
newSidebarWidgetsSetting( newSidebarWidgetIds );
|
|
|
|
|
|
|
|
self.focus();
|
2014-04-07 05:04:14 -04:00
|
|
|
} );
|
2014-03-05 15:41:14 -05:00
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
2014-04-07 05:04:14 -04:00
|
|
|
* Highlight widgets in preview when interacted with in the customizer
|
2014-03-05 15:41:14 -05:00
|
|
|
*/
|
2014-04-07 05:04:14 -04:00
|
|
|
_setupHighlightEffects: function() {
|
2014-04-07 12:32:15 -04:00
|
|
|
var self = this;
|
2014-03-06 11:45:15 -05:00
|
|
|
|
2014-04-07 05:04:14 -04:00
|
|
|
// Highlight whenever hovering or clicking over the form
|
2014-04-07 12:32:15 -04:00
|
|
|
this.container.on( 'mouseenter click', function() {
|
|
|
|
self.setting.previewer.send( 'highlight-widget', self.params.widget_id );
|
2014-04-07 05:04:14 -04:00
|
|
|
} );
|
2014-03-06 11:45:15 -05:00
|
|
|
|
2014-04-07 05:04:14 -04:00
|
|
|
// Highlight when the setting is updated
|
2014-04-07 12:32:15 -04:00
|
|
|
this.setting.bind( function() {
|
|
|
|
self.setting.previewer.send( 'highlight-widget', self.params.widget_id );
|
2014-03-05 15:41:14 -05:00
|
|
|
} );
|
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
2014-04-07 05:04:14 -04:00
|
|
|
* Set up event handlers for widget updating
|
2014-03-05 15:41:14 -05:00
|
|
|
*/
|
2014-04-07 05:04:14 -04:00
|
|
|
_setupUpdateUI: function() {
|
2014-04-07 12:32:15 -04:00
|
|
|
var self = this, $widgetRoot, $widgetContent,
|
|
|
|
$saveBtn, updateWidgetDebounced, formSyncHandler;
|
2014-03-06 11:45:15 -05:00
|
|
|
|
2014-04-07 12:32:15 -04:00
|
|
|
$widgetRoot = this.container.find( '.widget:first' );
|
|
|
|
$widgetContent = $widgetRoot.find( '.widget-content:first' );
|
2014-03-05 15:41:14 -05:00
|
|
|
|
2014-04-07 05:04:14 -04:00
|
|
|
// Configure update button
|
2014-04-07 12:32:15 -04:00
|
|
|
$saveBtn = this.container.find( '.widget-control-save' );
|
|
|
|
$saveBtn.val( l10n.saveBtnLabel );
|
|
|
|
$saveBtn.attr( 'title', l10n.saveBtnTooltip );
|
|
|
|
$saveBtn.removeClass( 'button-primary' ).addClass( 'button-secondary' );
|
|
|
|
$saveBtn.on( 'click', function( e ) {
|
2014-04-07 05:04:14 -04:00
|
|
|
e.preventDefault();
|
2014-04-07 16:00:16 -04:00
|
|
|
self.updateWidget( { disable_form: true } ); // @todo disable_form is unused?
|
2014-04-07 05:04:14 -04:00
|
|
|
} );
|
2014-03-05 15:41:14 -05:00
|
|
|
|
2014-04-07 12:32:15 -04:00
|
|
|
updateWidgetDebounced = _.debounce( function() {
|
|
|
|
self.updateWidget();
|
2014-04-07 05:04:14 -04:00
|
|
|
}, 250 );
|
2014-03-05 15:41:14 -05:00
|
|
|
|
2014-04-07 05:04:14 -04:00
|
|
|
// Trigger widget form update when hitting Enter within an input
|
2014-04-15 20:11:35 -04:00
|
|
|
$widgetContent.on( 'keydown', 'input', function( e ) {
|
2014-04-07 05:04:14 -04:00
|
|
|
if ( 13 === e.which ) { // Enter
|
|
|
|
e.preventDefault();
|
2014-04-07 16:00:16 -04:00
|
|
|
self.updateWidget( { ignoreActiveElement: true } );
|
2014-04-07 05:04:14 -04:00
|
|
|
}
|
2014-03-05 15:41:14 -05:00
|
|
|
} );
|
|
|
|
|
2014-04-07 05:04:14 -04:00
|
|
|
// Handle widgets that support live previews
|
2014-04-07 12:32:15 -04:00
|
|
|
$widgetContent.on( 'change input propertychange', ':input', function( e ) {
|
|
|
|
if ( self.liveUpdateMode ) {
|
2014-04-07 05:04:14 -04:00
|
|
|
if ( e.type === 'change' ) {
|
2014-04-07 12:32:15 -04:00
|
|
|
self.updateWidget();
|
2014-04-07 05:04:14 -04:00
|
|
|
} else if ( this.checkValidity && this.checkValidity() ) {
|
2014-04-07 12:32:15 -04:00
|
|
|
updateWidgetDebounced();
|
2014-04-07 05:04:14 -04:00
|
|
|
}
|
2014-03-05 15:41:14 -05:00
|
|
|
}
|
2014-04-07 05:04:14 -04:00
|
|
|
} );
|
|
|
|
|
|
|
|
// Remove loading indicators when the setting is saved and the preview updates
|
2014-04-07 12:32:15 -04:00
|
|
|
this.setting.previewer.channel.bind( 'synced', function() {
|
|
|
|
self.container.removeClass( 'previewer-loading' );
|
2014-04-07 05:04:14 -04:00
|
|
|
} );
|
2014-04-07 12:32:15 -04:00
|
|
|
|
2014-07-09 16:58:15 -04:00
|
|
|
api.previewer.bind( 'widget-updated', function( updatedWidgetId ) {
|
2014-04-07 12:32:15 -04:00
|
|
|
if ( updatedWidgetId === self.params.widget_id ) {
|
|
|
|
self.container.removeClass( 'previewer-loading' );
|
2014-03-05 15:41:14 -05:00
|
|
|
}
|
|
|
|
} );
|
|
|
|
|
2014-04-07 12:32:15 -04:00
|
|
|
formSyncHandler = api.Widgets.formSyncHandlers[ this.params.widget_id_base ];
|
|
|
|
if ( formSyncHandler ) {
|
|
|
|
$( document ).on( 'widget-synced', function( e, widget ) {
|
|
|
|
if ( $widgetRoot.is( widget ) ) {
|
|
|
|
formSyncHandler.apply( document, arguments );
|
2014-04-07 05:04:14 -04:00
|
|
|
}
|
|
|
|
} );
|
2014-03-05 15:41:14 -05:00
|
|
|
}
|
2014-04-07 05:04:14 -04:00
|
|
|
},
|
2014-03-05 15:41:14 -05:00
|
|
|
|
2014-07-09 19:58:16 -04:00
|
|
|
/**
|
|
|
|
* Update widget control to indicate whether it is currently rendered.
|
|
|
|
*
|
|
|
|
* Overrides api.Control.toggle()
|
|
|
|
*
|
|
|
|
* @param {Boolean} active
|
|
|
|
*/
|
|
|
|
toggle: function ( active ) {
|
|
|
|
this.container.toggleClass( 'widget-rendered', active );
|
|
|
|
},
|
|
|
|
|
2014-04-07 05:04:14 -04:00
|
|
|
/**
|
|
|
|
* Set up event handlers for widget removal
|
|
|
|
*/
|
|
|
|
_setupRemoveUI: function() {
|
2014-04-07 12:32:15 -04:00
|
|
|
var self = this, $removeBtn, replaceDeleteWithRemove;
|
2014-04-07 05:04:14 -04:00
|
|
|
|
|
|
|
// Configure remove button
|
2014-04-07 12:32:15 -04:00
|
|
|
$removeBtn = this.container.find( 'a.widget-control-remove' );
|
|
|
|
$removeBtn.on( 'click', function( e ) {
|
2014-04-07 05:04:14 -04:00
|
|
|
e.preventDefault();
|
|
|
|
|
|
|
|
// Find an adjacent element to add focus to when this widget goes away
|
2014-04-07 12:32:15 -04:00
|
|
|
var $adjacentFocusTarget;
|
|
|
|
if ( self.container.next().is( '.customize-control-widget_form' ) ) {
|
|
|
|
$adjacentFocusTarget = self.container.next().find( '.widget-action:first' );
|
|
|
|
} else if ( self.container.prev().is( '.customize-control-widget_form' ) ) {
|
|
|
|
$adjacentFocusTarget = self.container.prev().find( '.widget-action:first' );
|
2014-03-05 15:41:14 -05:00
|
|
|
} else {
|
2014-04-07 12:32:15 -04:00
|
|
|
$adjacentFocusTarget = self.container.next( '.customize-control-sidebar_widgets' ).find( '.add-new-widget:first' );
|
2014-03-05 15:41:14 -05:00
|
|
|
}
|
2014-04-02 14:20:16 -04:00
|
|
|
|
2014-04-07 12:32:15 -04:00
|
|
|
self.container.slideUp( function() {
|
2014-04-07 16:00:16 -04:00
|
|
|
var sidebarsWidgetsControl = api.Widgets.getSidebarWidgetControlContainingWidget( self.params.widget_id ),
|
|
|
|
sidebarWidgetIds, i;
|
2014-03-05 15:41:14 -05:00
|
|
|
|
2014-04-07 16:00:16 -04:00
|
|
|
if ( ! sidebarsWidgetsControl ) {
|
2014-04-07 12:32:15 -04:00
|
|
|
return;
|
2014-04-07 05:04:14 -04:00
|
|
|
}
|
2014-04-07 16:00:16 -04:00
|
|
|
|
|
|
|
sidebarWidgetIds = sidebarsWidgetsControl.setting().slice();
|
|
|
|
i = _.indexOf( sidebarWidgetIds, self.params.widget_id );
|
2014-04-07 05:04:14 -04:00
|
|
|
if ( -1 === i ) {
|
2014-04-07 12:32:15 -04:00
|
|
|
return;
|
2014-04-07 05:04:14 -04:00
|
|
|
}
|
2014-04-07 16:00:16 -04:00
|
|
|
|
|
|
|
sidebarWidgetIds.splice( i, 1 );
|
|
|
|
sidebarsWidgetsControl.setting( sidebarWidgetIds );
|
|
|
|
|
2014-04-07 12:32:15 -04:00
|
|
|
$adjacentFocusTarget.focus(); // keyboard accessibility
|
2014-04-07 05:04:14 -04:00
|
|
|
} );
|
|
|
|
} );
|
2014-03-05 15:41:14 -05:00
|
|
|
|
2014-04-07 12:32:15 -04:00
|
|
|
replaceDeleteWithRemove = function() {
|
|
|
|
$removeBtn.text( l10n.removeBtnLabel ); // wp_widget_control() outputs the link as "Delete"
|
|
|
|
$removeBtn.attr( 'title', l10n.removeBtnTooltip );
|
2014-04-07 05:04:14 -04:00
|
|
|
};
|
2014-04-07 12:32:15 -04:00
|
|
|
|
|
|
|
if ( this.params.is_new ) {
|
|
|
|
api.bind( 'saved', replaceDeleteWithRemove );
|
2014-04-07 05:04:14 -04:00
|
|
|
} else {
|
2014-04-07 12:32:15 -04:00
|
|
|
replaceDeleteWithRemove();
|
2014-04-07 05:04:14 -04:00
|
|
|
}
|
|
|
|
},
|
2014-03-05 15:41:14 -05:00
|
|
|
|
|
|
|
/**
|
2014-04-07 05:04:14 -04:00
|
|
|
* Find all inputs in a widget container that should be considered when
|
|
|
|
* comparing the loaded form with the sanitized form, whose fields will
|
|
|
|
* be aligned to copy the sanitized over. The elements returned by this
|
|
|
|
* are passed into this._getInputsSignature(), and they are iterated
|
|
|
|
* over when copying sanitized values over to the the form loaded.
|
|
|
|
*
|
|
|
|
* @param {jQuery} container element in which to look for inputs
|
|
|
|
* @returns {jQuery} inputs
|
|
|
|
* @private
|
2014-03-05 15:41:14 -05:00
|
|
|
*/
|
2014-04-07 10:10:14 -04:00
|
|
|
_getInputs: function( container ) {
|
2014-04-07 05:04:14 -04:00
|
|
|
return $( container ).find( ':input[name]' );
|
2014-03-05 15:41:14 -05:00
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
2014-04-07 05:04:14 -04:00
|
|
|
* Iterate over supplied inputs and create a signature string for all of them together.
|
|
|
|
* This string can be used to compare whether or not the form has all of the same fields.
|
|
|
|
*
|
|
|
|
* @param {jQuery} inputs
|
|
|
|
* @returns {string}
|
|
|
|
* @private
|
2014-03-05 15:41:14 -05:00
|
|
|
*/
|
2014-04-07 10:10:14 -04:00
|
|
|
_getInputsSignature: function( inputs ) {
|
2014-04-07 12:32:15 -04:00
|
|
|
var inputsSignatures = _( inputs ).map( function( input ) {
|
|
|
|
var $input = $( input ), signatureParts;
|
|
|
|
|
|
|
|
if ( $input.is( ':checkbox, :radio' ) ) {
|
|
|
|
signatureParts = [ $input.attr( 'id' ), $input.attr( 'name' ), $input.prop( 'value' ) ];
|
2014-04-07 05:04:14 -04:00
|
|
|
} else {
|
2014-04-07 12:32:15 -04:00
|
|
|
signatureParts = [ $input.attr( 'id' ), $input.attr( 'name' ) ];
|
2014-03-05 15:41:14 -05:00
|
|
|
}
|
2014-04-07 12:32:15 -04:00
|
|
|
|
|
|
|
return signatureParts.join( ',' );
|
2014-03-05 15:41:14 -05:00
|
|
|
} );
|
2014-04-07 12:32:15 -04:00
|
|
|
|
|
|
|
return inputsSignatures.join( ';' );
|
2014-03-05 15:41:14 -05:00
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
2014-04-07 05:04:14 -04:00
|
|
|
* Get the property that represents the state of an input.
|
|
|
|
*
|
|
|
|
* @param {jQuery|DOMElement} input
|
|
|
|
* @returns {string}
|
|
|
|
* @private
|
2014-03-05 15:41:14 -05:00
|
|
|
*/
|
2014-04-07 10:10:14 -04:00
|
|
|
_getInputStatePropertyName: function( input ) {
|
2014-04-07 12:32:15 -04:00
|
|
|
var $input = $( input );
|
|
|
|
|
|
|
|
if ( $input.is( ':radio, :checkbox' ) ) {
|
2014-04-07 05:04:14 -04:00
|
|
|
return 'checked';
|
|
|
|
} else {
|
|
|
|
return 'value';
|
2014-03-05 15:41:14 -05:00
|
|
|
}
|
2014-04-07 05:04:14 -04:00
|
|
|
},
|
2014-03-06 11:45:15 -05:00
|
|
|
|
2014-04-07 05:04:14 -04:00
|
|
|
/***********************************************************************
|
|
|
|
* Begin public API methods
|
|
|
|
**********************************************************************/
|
2014-03-05 15:41:14 -05:00
|
|
|
|
2014-04-07 05:04:14 -04:00
|
|
|
/**
|
|
|
|
* @return {wp.customize.controlConstructor.sidebar_widgets[]}
|
|
|
|
*/
|
|
|
|
getSidebarWidgetsControl: function() {
|
2014-04-07 12:32:15 -04:00
|
|
|
var settingId, sidebarWidgetsControl;
|
|
|
|
|
|
|
|
settingId = 'sidebars_widgets[' + this.params.sidebar_id + ']';
|
|
|
|
sidebarWidgetsControl = api.control( settingId );
|
2014-03-05 15:41:14 -05:00
|
|
|
|
2014-04-07 12:32:15 -04:00
|
|
|
if ( ! sidebarWidgetsControl ) {
|
|
|
|
return;
|
2014-04-07 05:04:14 -04:00
|
|
|
}
|
2014-04-07 12:32:15 -04:00
|
|
|
|
|
|
|
return sidebarWidgetsControl;
|
2014-03-05 15:41:14 -05:00
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
2014-04-07 05:04:14 -04:00
|
|
|
* Submit the widget form via Ajax and get back the updated instance,
|
|
|
|
* along with the new widget control form to render.
|
|
|
|
*
|
|
|
|
* @param {object} [args]
|
|
|
|
* @param {Object|null} [args.instance=null] When the model changes, the instance is sent here; otherwise, the inputs from the form are used
|
|
|
|
* @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.
|
2014-04-07 16:00:16 -04:00
|
|
|
* @param {Boolean} [args.ignoreActiveElement=false] Whether or not updating a field will be deferred if focus is still on the element.
|
2014-03-05 15:41:14 -05:00
|
|
|
*/
|
2014-04-07 10:10:14 -04:00
|
|
|
updateWidget: function( args ) {
|
2014-04-07 12:32:15 -04:00
|
|
|
var self = this, instanceOverride, completeCallback, $widgetRoot, $widgetContent,
|
|
|
|
updateNumber, params, data, $inputs, processing, jqxhr, isChanged;
|
2014-03-06 11:45:15 -05:00
|
|
|
|
2014-04-07 05:04:14 -04:00
|
|
|
args = $.extend( {
|
|
|
|
instance: null,
|
|
|
|
complete: null,
|
2014-04-07 16:00:16 -04:00
|
|
|
ignoreActiveElement: false
|
2014-04-07 05:04:14 -04:00
|
|
|
}, args );
|
2014-03-05 15:41:14 -05:00
|
|
|
|
2014-04-07 12:32:15 -04:00
|
|
|
instanceOverride = args.instance;
|
|
|
|
completeCallback = args.complete;
|
2014-03-05 15:41:14 -05:00
|
|
|
|
2014-04-07 12:32:15 -04:00
|
|
|
this._updateCount += 1;
|
|
|
|
updateNumber = this._updateCount;
|
2014-03-06 11:45:15 -05:00
|
|
|
|
2014-04-07 12:32:15 -04:00
|
|
|
$widgetRoot = this.container.find( '.widget:first' );
|
|
|
|
$widgetContent = $widgetRoot.find( '.widget-content:first' );
|
2014-03-06 11:45:15 -05:00
|
|
|
|
2014-04-07 05:04:14 -04:00
|
|
|
// Remove a previous error message
|
2014-04-07 12:32:15 -04:00
|
|
|
$widgetContent.find( '.widget-error' ).remove();
|
2014-03-05 15:41:14 -05:00
|
|
|
|
2014-04-07 12:32:15 -04:00
|
|
|
this.container.addClass( 'widget-form-loading' );
|
|
|
|
this.container.addClass( 'previewer-loading' );
|
2014-04-07 05:04:14 -04:00
|
|
|
processing = api.state( 'processing' );
|
|
|
|
processing( processing() + 1 );
|
2014-03-05 15:41:14 -05:00
|
|
|
|
2014-04-07 12:32:15 -04:00
|
|
|
if ( ! this.liveUpdateMode ) {
|
|
|
|
this.container.addClass( 'widget-form-disabled' );
|
2014-04-07 05:04:14 -04:00
|
|
|
}
|
2014-03-05 15:41:14 -05:00
|
|
|
|
2014-04-07 05:04:14 -04:00
|
|
|
params = {};
|
|
|
|
params.action = 'update-widget';
|
|
|
|
params.wp_customize = 'on';
|
|
|
|
params.nonce = api.Widgets.data.nonce;
|
2014-04-14 18:46:16 -04:00
|
|
|
params.theme = api.settings.theme.stylesheet;
|
2014-03-05 15:41:14 -05:00
|
|
|
|
2014-04-07 05:04:14 -04:00
|
|
|
data = $.param( params );
|
2014-04-07 12:32:15 -04:00
|
|
|
$inputs = this._getInputs( $widgetContent );
|
2014-03-06 11:45:15 -05:00
|
|
|
|
2014-04-07 05:04:14 -04:00
|
|
|
// Store the value we're submitting in data so that when the response comes back,
|
|
|
|
// we know if it got sanitized; if there is no difference in the sanitized value,
|
|
|
|
// then we do not need to touch the UI and mess up the user's ongoing editing.
|
2014-04-07 12:32:15 -04:00
|
|
|
$inputs.each( function() {
|
2014-04-07 05:04:14 -04:00
|
|
|
var input = $( this ),
|
2014-04-07 12:32:15 -04:00
|
|
|
property = self._getInputStatePropertyName( this );
|
|
|
|
input.data( 'state' + updateNumber, input.prop( property ) );
|
2014-04-07 05:04:14 -04:00
|
|
|
} );
|
2014-03-05 15:41:14 -05:00
|
|
|
|
2014-04-07 12:32:15 -04:00
|
|
|
if ( instanceOverride ) {
|
|
|
|
data += '&' + $.param( { 'sanitized_widget_setting': JSON.stringify( instanceOverride ) } );
|
2014-04-07 05:04:14 -04:00
|
|
|
} else {
|
2014-04-07 12:32:15 -04:00
|
|
|
data += '&' + $inputs.serialize();
|
2014-04-07 05:04:14 -04:00
|
|
|
}
|
2014-04-07 12:32:15 -04:00
|
|
|
data += '&' + $widgetContent.find( '~ :input' ).serialize();
|
|
|
|
|
|
|
|
jqxhr = $.post( wp.ajax.settings.url, data );
|
2014-04-07 05:04:14 -04:00
|
|
|
|
2014-04-07 12:32:15 -04:00
|
|
|
jqxhr.done( function( r ) {
|
|
|
|
var message, sanitizedForm, $sanitizedInputs, hasSameInputsInResponse,
|
|
|
|
isLiveUpdateAborted = false;
|
2014-04-07 05:04:14 -04:00
|
|
|
|
|
|
|
// Check if the user is logged out.
|
|
|
|
if ( '0' === r ) {
|
2014-07-09 16:58:15 -04:00
|
|
|
api.previewer.preview.iframe.hide();
|
|
|
|
api.previewer.login().done( function() {
|
2014-04-07 12:32:15 -04:00
|
|
|
self.updateWidget( args );
|
2014-07-09 16:58:15 -04:00
|
|
|
api.previewer.preview.iframe.show();
|
2014-04-07 05:04:14 -04:00
|
|
|
} );
|
2014-03-05 15:41:14 -05:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2014-04-07 05:04:14 -04:00
|
|
|
// Check for cheaters.
|
|
|
|
if ( '-1' === r ) {
|
2014-07-09 16:58:15 -04:00
|
|
|
api.previewer.cheatin();
|
2014-04-07 05:04:14 -04:00
|
|
|
return;
|
|
|
|
}
|
2014-03-06 11:45:15 -05:00
|
|
|
|
2014-04-07 05:04:14 -04:00
|
|
|
if ( r.success ) {
|
2014-04-07 12:32:15 -04:00
|
|
|
sanitizedForm = $( '<div>' + r.data.form + '</div>' );
|
|
|
|
$sanitizedInputs = self._getInputs( sanitizedForm );
|
|
|
|
hasSameInputsInResponse = self._getInputsSignature( $inputs ) === self._getInputsSignature( $sanitizedInputs );
|
2014-04-07 05:04:14 -04:00
|
|
|
|
|
|
|
// Restore live update mode if sanitized fields are now aligned with the existing fields
|
2014-04-07 12:32:15 -04:00
|
|
|
if ( hasSameInputsInResponse && ! self.liveUpdateMode ) {
|
|
|
|
self.liveUpdateMode = true;
|
|
|
|
self.container.removeClass( 'widget-form-disabled' );
|
|
|
|
self.container.find( 'input[name="savewidget"]' ).hide();
|
2014-03-05 15:41:14 -05:00
|
|
|
}
|
|
|
|
|
2014-04-07 05:04:14 -04:00
|
|
|
// Sync sanitized field states to existing fields if they are aligned
|
2014-04-07 12:32:15 -04:00
|
|
|
if ( hasSameInputsInResponse && self.liveUpdateMode ) {
|
|
|
|
$inputs.each( function( i ) {
|
|
|
|
var $input = $( this ),
|
|
|
|
$sanitizedInput = $( $sanitizedInputs[i] ),
|
|
|
|
property = self._getInputStatePropertyName( this ),
|
|
|
|
submittedState, sanitizedState, canUpdateState;
|
|
|
|
|
|
|
|
submittedState = $input.data( 'state' + updateNumber );
|
|
|
|
sanitizedState = $sanitizedInput.prop( property );
|
|
|
|
$input.data( 'sanitized', sanitizedState );
|
|
|
|
|
2014-04-07 16:00:16 -04:00
|
|
|
canUpdateState = ( submittedState !== sanitizedState && ( args.ignoreActiveElement || ! $input.is( document.activeElement ) ) );
|
2014-04-07 12:32:15 -04:00
|
|
|
if ( canUpdateState ) {
|
|
|
|
$input.prop( property, sanitizedState );
|
2014-04-07 05:04:14 -04:00
|
|
|
}
|
|
|
|
} );
|
2014-04-07 12:32:15 -04:00
|
|
|
|
|
|
|
$( document ).trigger( 'widget-synced', [ $widgetRoot, r.data.form ] );
|
2014-04-07 05:04:14 -04:00
|
|
|
|
|
|
|
// Otherwise, if sanitized fields are not aligned with existing fields, disable live update mode if enabled
|
2014-04-07 12:32:15 -04:00
|
|
|
} else if ( self.liveUpdateMode ) {
|
|
|
|
self.liveUpdateMode = false;
|
|
|
|
self.container.find( 'input[name="savewidget"]' ).show();
|
|
|
|
isLiveUpdateAborted = true;
|
|
|
|
|
2014-04-07 05:04:14 -04:00
|
|
|
// Otherwise, replace existing form with the sanitized form
|
2014-03-05 15:41:14 -05:00
|
|
|
} else {
|
2014-04-07 12:32:15 -04:00
|
|
|
$widgetContent.html( r.data.form );
|
|
|
|
|
|
|
|
self.container.removeClass( 'widget-form-disabled' );
|
|
|
|
|
|
|
|
$( document ).trigger( 'widget-updated', [ $widgetRoot ] );
|
2014-03-05 15:41:14 -05:00
|
|
|
}
|
|
|
|
|
2014-04-07 05:04:14 -04:00
|
|
|
/**
|
|
|
|
* If the old instance is identical to the new one, there is nothing new
|
|
|
|
* needing to be rendered, and so we can preempt the event for the
|
|
|
|
* preview finishing loading.
|
|
|
|
*/
|
2014-04-07 12:32:15 -04:00
|
|
|
isChanged = ! isLiveUpdateAborted && ! _( self.setting() ).isEqual( r.data.instance );
|
|
|
|
if ( isChanged ) {
|
|
|
|
self.isWidgetUpdating = true; // suppress triggering another updateWidget
|
|
|
|
self.setting( r.data.instance );
|
|
|
|
self.isWidgetUpdating = false;
|
2014-04-07 05:04:14 -04:00
|
|
|
} else {
|
|
|
|
// no change was made, so stop the spinner now instead of when the preview would updates
|
2014-04-07 12:32:15 -04:00
|
|
|
self.container.removeClass( 'previewer-loading' );
|
2014-04-07 05:04:14 -04:00
|
|
|
}
|
|
|
|
|
2014-04-07 12:32:15 -04:00
|
|
|
if ( completeCallback ) {
|
2014-04-07 16:00:16 -04:00
|
|
|
completeCallback.call( self, null, { noChange: ! isChanged, ajaxFinished: true } );
|
2014-04-07 05:04:14 -04:00
|
|
|
}
|
|
|
|
} else {
|
2014-04-07 12:32:15 -04:00
|
|
|
// General error message
|
2014-04-07 05:04:14 -04:00
|
|
|
message = l10n.error;
|
2014-04-07 12:32:15 -04:00
|
|
|
|
2014-04-07 05:04:14 -04:00
|
|
|
if ( r.data && r.data.message ) {
|
|
|
|
message = r.data.message;
|
|
|
|
}
|
2014-04-07 12:32:15 -04:00
|
|
|
|
|
|
|
if ( completeCallback ) {
|
|
|
|
completeCallback.call( self, message );
|
2014-04-07 05:04:14 -04:00
|
|
|
} else {
|
2014-04-07 12:32:15 -04:00
|
|
|
$widgetContent.prepend( '<p class="widget-error"><strong>' + message + '</strong></p>' );
|
2014-04-07 05:04:14 -04:00
|
|
|
}
|
2014-03-05 15:41:14 -05:00
|
|
|
}
|
|
|
|
} );
|
2014-04-07 12:32:15 -04:00
|
|
|
|
2014-04-07 10:10:14 -04:00
|
|
|
jqxhr.fail( function( jqXHR, textStatus ) {
|
2014-04-07 12:32:15 -04:00
|
|
|
if ( completeCallback ) {
|
|
|
|
completeCallback.call( self, textStatus );
|
2014-03-05 15:41:14 -05:00
|
|
|
}
|
|
|
|
} );
|
2014-04-07 12:32:15 -04:00
|
|
|
|
2014-04-07 05:04:14 -04:00
|
|
|
jqxhr.always( function() {
|
2014-04-07 12:32:15 -04:00
|
|
|
self.container.removeClass( 'widget-form-loading' );
|
|
|
|
|
|
|
|
$inputs.each( function() {
|
|
|
|
$( this ).removeData( 'state' + updateNumber );
|
2014-04-07 05:04:14 -04:00
|
|
|
} );
|
2014-03-05 15:41:14 -05:00
|
|
|
|
2014-04-07 05:04:14 -04:00
|
|
|
processing( processing() - 1 );
|
2014-03-05 15:41:14 -05:00
|
|
|
} );
|
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
2014-04-07 05:04:14 -04:00
|
|
|
* Expand the accordion section containing a control
|
2014-03-05 15:41:14 -05:00
|
|
|
*/
|
2014-04-07 05:04:14 -04:00
|
|
|
expandControlSection: function() {
|
2014-04-07 12:32:15 -04:00
|
|
|
var $section = this.container.closest( '.accordion-section' );
|
|
|
|
|
|
|
|
if ( ! $section.hasClass( 'open' ) ) {
|
|
|
|
$section.find( '.accordion-section-title:first' ).trigger( 'click' );
|
2014-04-07 05:04:14 -04:00
|
|
|
}
|
|
|
|
},
|
2014-03-05 15:41:14 -05:00
|
|
|
|
2014-04-07 05:04:14 -04:00
|
|
|
/**
|
|
|
|
* Expand the widget form control
|
|
|
|
*/
|
|
|
|
expandForm: function() {
|
|
|
|
this.toggleForm( true );
|
|
|
|
},
|
2014-03-05 15:41:14 -05:00
|
|
|
|
2014-04-07 05:04:14 -04:00
|
|
|
/**
|
|
|
|
* Collapse the widget form control
|
|
|
|
*/
|
|
|
|
collapseForm: function() {
|
|
|
|
this.toggleForm( false );
|
2014-03-05 15:41:14 -05:00
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
2014-04-07 05:04:14 -04:00
|
|
|
* Expand or collapse the widget control
|
|
|
|
*
|
2014-04-07 12:32:15 -04:00
|
|
|
* @param {boolean|undefined} [showOrHide] If not supplied, will be inverse of current visibility
|
2014-03-05 15:41:14 -05:00
|
|
|
*/
|
2014-04-07 12:32:15 -04:00
|
|
|
toggleForm: function( showOrHide ) {
|
|
|
|
var self = this, $widget, $inside, complete;
|
2014-03-05 15:41:14 -05:00
|
|
|
|
2014-04-07 12:32:15 -04:00
|
|
|
$widget = this.container.find( 'div.widget:first' );
|
|
|
|
$inside = $widget.find( '.widget-inside:first' );
|
|
|
|
if ( typeof showOrHide === 'undefined' ) {
|
|
|
|
showOrHide = ! $inside.is( ':visible' );
|
2014-04-07 05:04:14 -04:00
|
|
|
}
|
2014-03-05 15:41:14 -05:00
|
|
|
|
2014-04-07 05:04:14 -04:00
|
|
|
// Already expanded or collapsed, so noop
|
2014-04-07 12:32:15 -04:00
|
|
|
if ( $inside.is( ':visible' ) === showOrHide ) {
|
2014-04-07 05:04:14 -04:00
|
|
|
return;
|
|
|
|
}
|
2014-03-05 15:41:14 -05:00
|
|
|
|
2014-04-07 12:32:15 -04:00
|
|
|
if ( showOrHide ) {
|
2014-04-07 05:04:14 -04:00
|
|
|
// Close all other widget controls before expanding this one
|
2014-04-07 12:32:15 -04:00
|
|
|
api.control.each( function( otherControl ) {
|
|
|
|
if ( self.params.type === otherControl.params.type && self !== otherControl ) {
|
|
|
|
otherControl.collapseForm();
|
2014-04-07 05:04:14 -04:00
|
|
|
}
|
|
|
|
} );
|
2014-03-05 15:41:14 -05:00
|
|
|
|
2014-04-07 05:04:14 -04:00
|
|
|
complete = function() {
|
2014-04-07 12:32:15 -04:00
|
|
|
self.container.removeClass( 'expanding' );
|
|
|
|
self.container.addClass( 'expanded' );
|
|
|
|
self.container.trigger( 'expanded' );
|
2014-04-07 05:04:14 -04:00
|
|
|
};
|
2014-04-07 12:32:15 -04:00
|
|
|
|
|
|
|
if ( self.params.is_wide ) {
|
|
|
|
$inside.fadeIn( 'fast', complete );
|
2014-04-07 05:04:14 -04:00
|
|
|
} else {
|
2014-04-07 12:32:15 -04:00
|
|
|
$inside.slideDown( 'fast', complete );
|
2014-03-05 15:41:14 -05:00
|
|
|
}
|
2014-04-07 12:32:15 -04:00
|
|
|
|
|
|
|
self.container.trigger( 'expand' );
|
|
|
|
self.container.addClass( 'expanding' );
|
2014-04-07 05:04:14 -04:00
|
|
|
} else {
|
|
|
|
complete = function() {
|
2014-04-07 12:32:15 -04:00
|
|
|
self.container.removeClass( 'collapsing' );
|
|
|
|
self.container.removeClass( 'expanded' );
|
|
|
|
self.container.trigger( 'collapsed' );
|
2014-04-07 05:04:14 -04:00
|
|
|
};
|
2014-04-07 12:32:15 -04:00
|
|
|
|
|
|
|
self.container.trigger( 'collapse' );
|
|
|
|
self.container.addClass( 'collapsing' );
|
|
|
|
|
|
|
|
if ( self.params.is_wide ) {
|
|
|
|
$inside.fadeOut( 'fast', complete );
|
2014-04-07 05:04:14 -04:00
|
|
|
} else {
|
2014-04-07 12:32:15 -04:00
|
|
|
$inside.slideUp( 'fast', function() {
|
|
|
|
$widget.css( { width:'', margin:'' } );
|
2014-04-07 05:04:14 -04:00
|
|
|
complete();
|
|
|
|
} );
|
2014-03-05 15:41:14 -05:00
|
|
|
}
|
2014-04-07 05:04:14 -04:00
|
|
|
}
|
|
|
|
},
|
2014-03-05 15:41:14 -05:00
|
|
|
|
2014-04-07 05:04:14 -04:00
|
|
|
/**
|
|
|
|
* Expand the containing sidebar section, expand the form, and focus on
|
|
|
|
* the first input in the control
|
|
|
|
*/
|
|
|
|
focus: function() {
|
2014-04-07 12:32:15 -04:00
|
|
|
this.expandControlSection();
|
|
|
|
this.expandForm();
|
|
|
|
this.container.find( '.widget-content :focusable:first' ).focus();
|
2014-04-07 05:04:14 -04:00
|
|
|
},
|
2014-03-05 15:41:14 -05:00
|
|
|
|
2014-04-07 05:04:14 -04:00
|
|
|
/**
|
|
|
|
* Get the position (index) of the widget in the containing sidebar
|
|
|
|
*
|
|
|
|
* @returns {Number}
|
|
|
|
*/
|
|
|
|
getWidgetSidebarPosition: function() {
|
2014-04-07 12:32:15 -04:00
|
|
|
var sidebarWidgetIds, position;
|
|
|
|
|
|
|
|
sidebarWidgetIds = this.getSidebarWidgetsControl().setting();
|
|
|
|
position = _.indexOf( sidebarWidgetIds, this.params.widget_id );
|
2014-04-02 14:20:16 -04:00
|
|
|
|
2014-04-07 05:04:14 -04:00
|
|
|
if ( position === -1 ) {
|
2014-04-07 12:32:15 -04:00
|
|
|
return;
|
2014-04-02 14:20:16 -04:00
|
|
|
}
|
2014-04-07 12:32:15 -04:00
|
|
|
|
2014-04-07 05:04:14 -04:00
|
|
|
return position;
|
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Move widget up one in the sidebar
|
|
|
|
*/
|
|
|
|
moveUp: function() {
|
|
|
|
this._moveWidgetByOne( -1 );
|
2014-03-05 15:41:14 -05:00
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
2014-04-07 05:04:14 -04:00
|
|
|
* Move widget up one in the sidebar
|
2014-03-05 15:41:14 -05:00
|
|
|
*/
|
2014-04-07 05:04:14 -04:00
|
|
|
moveDown: function() {
|
|
|
|
this._moveWidgetByOne( 1 );
|
|
|
|
},
|
2014-03-05 15:41:14 -05:00
|
|
|
|
2014-04-07 05:04:14 -04:00
|
|
|
/**
|
|
|
|
* @private
|
|
|
|
*
|
|
|
|
* @param {Number} offset 1|-1
|
|
|
|
*/
|
2014-04-07 10:10:14 -04:00
|
|
|
_moveWidgetByOne: function( offset ) {
|
2014-04-07 12:32:15 -04:00
|
|
|
var i, sidebarWidgetsSetting, sidebarWidgetIds, adjacentWidgetId;
|
2014-03-05 15:41:14 -05:00
|
|
|
|
2014-04-07 12:32:15 -04:00
|
|
|
i = this.getWidgetSidebarPosition();
|
2014-03-06 11:45:15 -05:00
|
|
|
|
2014-04-07 12:32:15 -04:00
|
|
|
sidebarWidgetsSetting = this.getSidebarWidgetsControl().setting;
|
|
|
|
sidebarWidgetIds = Array.prototype.slice.call( sidebarWidgetsSetting() ); // clone
|
|
|
|
adjacentWidgetId = sidebarWidgetIds[i + offset];
|
|
|
|
sidebarWidgetIds[i + offset] = this.params.widget_id;
|
|
|
|
sidebarWidgetIds[i] = adjacentWidgetId;
|
2014-03-05 15:41:14 -05:00
|
|
|
|
2014-04-07 12:32:15 -04:00
|
|
|
sidebarWidgetsSetting( sidebarWidgetIds );
|
2014-03-05 15:41:14 -05:00
|
|
|
},
|
|
|
|
|
2014-04-02 14:20:16 -04:00
|
|
|
/**
|
2014-04-07 05:04:14 -04:00
|
|
|
* Toggle visibility of the widget move area
|
2014-04-02 14:20:16 -04:00
|
|
|
*
|
2014-04-07 12:32:15 -04:00
|
|
|
* @param {Boolean} [showOrHide]
|
2014-04-02 14:20:16 -04:00
|
|
|
*/
|
2014-04-07 12:32:15 -04:00
|
|
|
toggleWidgetMoveArea: function( showOrHide ) {
|
|
|
|
var self = this, $moveWidgetArea;
|
|
|
|
|
|
|
|
$moveWidgetArea = this.container.find( '.move-widget-area' );
|
|
|
|
|
|
|
|
if ( typeof showOrHide === 'undefined' ) {
|
|
|
|
showOrHide = ! $moveWidgetArea.hasClass( 'active' );
|
2014-04-07 05:04:14 -04:00
|
|
|
}
|
2014-04-07 12:32:15 -04:00
|
|
|
|
|
|
|
if ( showOrHide ) {
|
2014-04-07 05:04:14 -04:00
|
|
|
// reset the selected sidebar
|
2014-04-07 12:32:15 -04:00
|
|
|
$moveWidgetArea.find( '.selected' ).removeClass( 'selected' );
|
|
|
|
|
|
|
|
$moveWidgetArea.find( 'li' ).filter( function() {
|
|
|
|
return $( this ).data( 'id' ) === self.params.sidebar_id;
|
2014-04-07 05:04:14 -04:00
|
|
|
} ).addClass( 'selected' );
|
2014-04-07 12:32:15 -04:00
|
|
|
|
|
|
|
this.container.find( '.move-widget-btn' ).prop( 'disabled', true );
|
2014-04-07 05:04:14 -04:00
|
|
|
}
|
2014-04-02 14:20:16 -04:00
|
|
|
|
2014-04-07 12:32:15 -04:00
|
|
|
$moveWidgetArea.toggleClass( 'active', showOrHide );
|
2014-03-05 15:41:14 -05:00
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
2014-04-07 05:04:14 -04:00
|
|
|
* Highlight the widget control and section
|
2014-03-05 15:41:14 -05:00
|
|
|
*/
|
2014-04-07 05:04:14 -04:00
|
|
|
highlightSectionAndControl: function() {
|
2014-04-07 12:32:15 -04:00
|
|
|
var $target;
|
2014-04-07 05:04:14 -04:00
|
|
|
|
2014-04-07 12:32:15 -04:00
|
|
|
if ( this.container.is( ':hidden' ) ) {
|
|
|
|
$target = this.container.closest( '.control-section' );
|
2014-03-05 15:41:14 -05:00
|
|
|
} else {
|
2014-04-07 12:32:15 -04:00
|
|
|
$target = this.container;
|
2014-03-05 15:41:14 -05:00
|
|
|
}
|
|
|
|
|
2014-04-07 12:32:15 -04:00
|
|
|
$( '.highlighted' ).removeClass( 'highlighted' );
|
|
|
|
$target.addClass( 'highlighted' );
|
|
|
|
|
2014-04-07 05:04:14 -04:00
|
|
|
setTimeout( function() {
|
2014-04-07 12:32:15 -04:00
|
|
|
$target.removeClass( 'highlighted' );
|
2014-04-07 05:04:14 -04:00
|
|
|
}, 500 );
|
|
|
|
}
|
|
|
|
} );
|
2014-03-05 15:41:14 -05:00
|
|
|
|
2014-04-07 05:04:14 -04:00
|
|
|
/**
|
2014-04-07 10:10:14 -04:00
|
|
|
* wp.customize.Widgets.SidebarControl
|
|
|
|
*
|
|
|
|
* Customizer control for widgets.
|
2014-04-07 05:04:14 -04:00
|
|
|
* Note that 'sidebar_widgets' must match the WP_Widget_Area_Customize_Control::$type
|
2014-04-07 10:10:14 -04:00
|
|
|
*
|
|
|
|
* @constructor
|
|
|
|
* @augments wp.customize.Control
|
2014-04-07 05:04:14 -04:00
|
|
|
*/
|
|
|
|
api.Widgets.SidebarControl = api.Control.extend({
|
2014-03-05 15:41:14 -05:00
|
|
|
/**
|
2014-04-07 05:04:14 -04:00
|
|
|
* Set up the control
|
2014-03-05 15:41:14 -05:00
|
|
|
*/
|
2014-04-07 05:04:14 -04:00
|
|
|
ready: function() {
|
2014-04-07 16:00:16 -04:00
|
|
|
this.$controlSection = this.container.closest( '.control-section' );
|
|
|
|
this.$sectionContent = this.container.closest( '.accordion-section-content' );
|
|
|
|
|
|
|
|
this._setupModel();
|
|
|
|
this._setupSortable();
|
|
|
|
this._setupAddition();
|
|
|
|
this._applyCardinalOrderClassNames();
|
2014-03-05 15:41:14 -05:00
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
2014-04-07 05:04:14 -04:00
|
|
|
* Update ordering of widget control forms when the setting is updated
|
2014-03-05 15:41:14 -05:00
|
|
|
*/
|
2014-04-07 05:04:14 -04:00
|
|
|
_setupModel: function() {
|
2014-04-07 16:00:16 -04:00
|
|
|
var self = this,
|
|
|
|
registeredSidebar = api.Widgets.registeredSidebars.get( this.params.sidebar_id );
|
|
|
|
|
|
|
|
this.setting.bind( function( newWidgetIds, oldWidgetIds ) {
|
|
|
|
var widgetFormControls, $sidebarWidgetsAddControl, finalControlContainers, removedWidgetIds;
|
|
|
|
|
|
|
|
removedWidgetIds = _( oldWidgetIds ).difference( newWidgetIds );
|
|
|
|
|
|
|
|
// Filter out any persistent widget IDs for widgets which have been deactivated
|
|
|
|
newWidgetIds = _( newWidgetIds ).filter( function( newWidgetId ) {
|
|
|
|
var parsedWidgetId = parseWidgetId( newWidgetId );
|
|
|
|
|
|
|
|
return !! api.Widgets.availableWidgets.findWhere( { id_base: parsedWidgetId.id_base } );
|
2014-04-07 05:04:14 -04:00
|
|
|
} );
|
2014-03-05 15:41:14 -05:00
|
|
|
|
2014-04-07 16:00:16 -04:00
|
|
|
widgetFormControls = _( newWidgetIds ).map( function( widgetId ) {
|
|
|
|
var widgetFormControl = api.Widgets.getWidgetFormControlForWidget( widgetId );
|
|
|
|
|
|
|
|
if ( ! widgetFormControl ) {
|
|
|
|
widgetFormControl = self.addWidget( widgetId );
|
2014-04-07 05:04:14 -04:00
|
|
|
}
|
2014-04-07 16:00:16 -04:00
|
|
|
|
|
|
|
return widgetFormControl;
|
2014-04-07 05:04:14 -04:00
|
|
|
} );
|
2014-03-05 15:41:14 -05:00
|
|
|
|
2014-04-07 05:04:14 -04:00
|
|
|
// Sort widget controls to their new positions
|
2014-04-07 16:00:16 -04:00
|
|
|
widgetFormControls.sort( function( a, b ) {
|
|
|
|
var aIndex = _.indexOf( newWidgetIds, a.params.widget_id ),
|
|
|
|
bIndex = _.indexOf( newWidgetIds, b.params.widget_id );
|
|
|
|
|
|
|
|
if ( aIndex === bIndex ) {
|
2014-04-07 05:04:14 -04:00
|
|
|
return 0;
|
|
|
|
}
|
2014-03-22 16:55:18 -04:00
|
|
|
|
2014-04-07 16:00:16 -04:00
|
|
|
return aIndex < bIndex ? -1 : 1;
|
|
|
|
} );
|
2014-03-05 15:41:14 -05:00
|
|
|
|
2014-04-07 05:04:14 -04:00
|
|
|
// Append the controls to put them in the right order
|
2014-04-07 16:00:16 -04:00
|
|
|
finalControlContainers = _( widgetFormControls ).map( function( widgetFormControls ) {
|
|
|
|
return widgetFormControls.container[0];
|
2014-04-07 05:04:14 -04:00
|
|
|
} );
|
2014-04-02 14:20:16 -04:00
|
|
|
|
2014-04-07 16:00:16 -04:00
|
|
|
$sidebarWidgetsAddControl = self.$sectionContent.find( '.customize-control-sidebar_widgets' );
|
|
|
|
$sidebarWidgetsAddControl.before( finalControlContainers );
|
|
|
|
|
2014-04-07 05:04:14 -04:00
|
|
|
// Re-sort widget form controls (including widgets form other sidebars newly moved here)
|
2014-04-07 16:00:16 -04:00
|
|
|
self._applyCardinalOrderClassNames();
|
2014-03-05 15:41:14 -05:00
|
|
|
|
2014-04-07 05:04:14 -04:00
|
|
|
// If the widget was dragged into the sidebar, make sure the sidebar_id param is updated
|
2014-04-07 16:00:16 -04:00
|
|
|
_( widgetFormControls ).each( function( widgetFormControl ) {
|
|
|
|
widgetFormControl.params.sidebar_id = self.params.sidebar_id;
|
2014-04-07 05:04:14 -04:00
|
|
|
} );
|
2014-03-05 15:41:14 -05:00
|
|
|
|
2014-04-07 05:04:14 -04:00
|
|
|
// Cleanup after widget removal
|
2014-04-07 16:00:16 -04:00
|
|
|
_( removedWidgetIds ).each( function( removedWidgetId ) {
|
2014-03-05 15:41:14 -05:00
|
|
|
|
2014-04-07 05:04:14 -04:00
|
|
|
// Using setTimeout so that when moving a widget to another sidebar, the other sidebars_widgets settings get a chance to update
|
|
|
|
setTimeout( function() {
|
2014-04-07 16:00:16 -04:00
|
|
|
var removedControl, wasDraggedToAnotherSidebar, inactiveWidgets, removedIdBase,
|
|
|
|
widget, isPresentInAnotherSidebar = false;
|
2014-03-05 15:41:14 -05:00
|
|
|
|
2014-04-07 05:04:14 -04:00
|
|
|
// Check if the widget is in another sidebar
|
2014-04-07 16:00:16 -04:00
|
|
|
api.each( function( otherSetting ) {
|
2014-04-08 00:03:14 -04:00
|
|
|
if ( otherSetting.id === self.setting.id || 0 !== otherSetting.id.indexOf( 'sidebars_widgets[' ) || otherSetting.id === 'sidebars_widgets[wp_inactive_widgets]' ) {
|
2014-04-07 05:04:14 -04:00
|
|
|
return;
|
|
|
|
}
|
2014-03-06 11:45:15 -05:00
|
|
|
|
2014-04-07 16:00:16 -04:00
|
|
|
var otherSidebarWidgets = otherSetting(), i;
|
|
|
|
|
|
|
|
i = _.indexOf( otherSidebarWidgets, removedWidgetId );
|
2014-04-07 05:04:14 -04:00
|
|
|
if ( -1 !== i ) {
|
2014-04-07 16:00:16 -04:00
|
|
|
isPresentInAnotherSidebar = true;
|
2014-04-07 05:04:14 -04:00
|
|
|
}
|
|
|
|
} );
|
2014-03-22 16:55:18 -04:00
|
|
|
|
2014-04-07 05:04:14 -04:00
|
|
|
// If the widget is present in another sidebar, abort!
|
2014-04-07 16:00:16 -04:00
|
|
|
if ( isPresentInAnotherSidebar ) {
|
2014-04-07 05:04:14 -04:00
|
|
|
return;
|
|
|
|
}
|
2014-03-22 16:55:18 -04:00
|
|
|
|
2014-04-07 16:00:16 -04:00
|
|
|
removedControl = api.Widgets.getWidgetFormControlForWidget( removedWidgetId );
|
2014-03-05 15:41:14 -05:00
|
|
|
|
2014-04-07 05:04:14 -04:00
|
|
|
// Detect if widget control was dragged to another sidebar
|
2014-04-07 16:00:16 -04:00
|
|
|
wasDraggedToAnotherSidebar = removedControl && $.contains( document, removedControl.container[0] ) && ! $.contains( self.$sectionContent[0], removedControl.container[0] );
|
2014-04-02 14:20:16 -04:00
|
|
|
|
2014-04-07 05:04:14 -04:00
|
|
|
// Delete any widget form controls for removed widgets
|
2014-04-07 16:00:16 -04:00
|
|
|
if ( removedControl && ! wasDraggedToAnotherSidebar ) {
|
|
|
|
api.control.remove( removedControl.id );
|
|
|
|
removedControl.container.remove();
|
2014-04-07 05:04:14 -04:00
|
|
|
}
|
2014-03-06 11:45:15 -05:00
|
|
|
|
2014-04-07 05:04:14 -04:00
|
|
|
// Move widget to inactive widgets sidebar (move it to trash) if has been previously saved
|
|
|
|
// This prevents the inactive widgets sidebar from overflowing with throwaway widgets
|
2014-04-07 16:00:16 -04:00
|
|
|
if ( api.Widgets.savedWidgetIds[removedWidgetId] ) {
|
2014-04-08 00:03:14 -04:00
|
|
|
inactiveWidgets = api.value( 'sidebars_widgets[wp_inactive_widgets]' )().slice();
|
2014-04-07 16:00:16 -04:00
|
|
|
inactiveWidgets.push( removedWidgetId );
|
2014-04-08 00:03:14 -04:00
|
|
|
api.value( 'sidebars_widgets[wp_inactive_widgets]' )( _( inactiveWidgets ).unique() );
|
2014-04-07 05:04:14 -04:00
|
|
|
}
|
2014-03-05 15:41:14 -05:00
|
|
|
|
2014-04-07 05:04:14 -04:00
|
|
|
// Make old single widget available for adding again
|
2014-04-07 16:00:16 -04:00
|
|
|
removedIdBase = parseWidgetId( removedWidgetId ).id_base;
|
|
|
|
widget = api.Widgets.availableWidgets.findWhere( { id_base: removedIdBase } );
|
2014-04-07 05:04:14 -04:00
|
|
|
if ( widget && ! widget.get( 'is_multi' ) ) {
|
|
|
|
widget.set( 'is_disabled', false );
|
|
|
|
}
|
|
|
|
} );
|
2014-04-02 14:20:16 -04:00
|
|
|
|
2014-04-07 05:04:14 -04:00
|
|
|
} );
|
|
|
|
} );
|
2014-03-05 15:41:14 -05:00
|
|
|
|
2014-04-07 05:04:14 -04:00
|
|
|
// Update the model with whether or not the sidebar is rendered
|
2014-07-09 19:58:16 -04:00
|
|
|
self.active.bind( function ( active ) {
|
|
|
|
registeredSidebar.set( 'is_rendered', active );
|
2014-04-07 05:04:14 -04:00
|
|
|
} );
|
2014-07-09 19:58:16 -04:00
|
|
|
},
|
2014-03-05 15:41:14 -05:00
|
|
|
|
2014-07-09 19:58:16 -04:00
|
|
|
/**
|
|
|
|
* Show the sidebar section when it becomes visible.
|
|
|
|
*
|
|
|
|
* Overrides api.Control.toggle()
|
|
|
|
*
|
|
|
|
* @param {Boolean} active
|
|
|
|
*/
|
|
|
|
toggle: function ( active ) {
|
|
|
|
var $section, sectionSelector;
|
2014-04-07 16:00:16 -04:00
|
|
|
|
2014-07-09 19:58:16 -04:00
|
|
|
sectionSelector = '#accordion-section-sidebar-widgets-' + this.params.sidebar_id;
|
|
|
|
$section = $( sectionSelector );
|
2014-04-07 16:00:16 -04:00
|
|
|
|
2014-07-09 19:58:16 -04:00
|
|
|
if ( active ) {
|
|
|
|
$section.stop().slideDown( function() {
|
|
|
|
$( this ).css( 'height', 'auto' ); // so that the .accordion-section-content won't overflow
|
|
|
|
} );
|
2014-04-07 16:00:16 -04:00
|
|
|
|
2014-07-09 19:58:16 -04:00
|
|
|
} else {
|
|
|
|
// Make sure that hidden sections get closed first
|
|
|
|
if ( $section.hasClass( 'open' ) ) {
|
|
|
|
// it would be nice if accordionSwitch() in accordion.js was public
|
|
|
|
$section.find( '.accordion-section-title' ).trigger( 'click' );
|
2014-03-05 15:41:14 -05:00
|
|
|
}
|
2014-07-09 19:58:16 -04:00
|
|
|
|
|
|
|
$section.stop().slideUp();
|
|
|
|
}
|
2014-03-05 15:41:14 -05:00
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
2014-04-07 05:04:14 -04:00
|
|
|
* Allow widgets in sidebar to be re-ordered, and for the order to be previewed
|
2014-03-05 15:41:14 -05:00
|
|
|
*/
|
2014-04-07 05:04:14 -04:00
|
|
|
_setupSortable: function() {
|
2014-04-07 16:00:16 -04:00
|
|
|
var self = this;
|
|
|
|
|
|
|
|
this.isReordering = false;
|
2014-03-05 15:41:14 -05:00
|
|
|
|
2014-04-07 05:04:14 -04:00
|
|
|
/**
|
|
|
|
* Update widget order setting when controls are re-ordered
|
|
|
|
*/
|
2014-04-07 16:00:16 -04:00
|
|
|
this.$sectionContent.sortable( {
|
2014-04-07 05:04:14 -04:00
|
|
|
items: '> .customize-control-widget_form',
|
|
|
|
handle: '.widget-top',
|
|
|
|
axis: 'y',
|
|
|
|
connectWith: '.accordion-section-content:has(.customize-control-sidebar_widgets)',
|
|
|
|
update: function() {
|
2014-04-07 16:00:16 -04:00
|
|
|
var widgetContainerIds = self.$sectionContent.sortable( 'toArray' ), widgetIds;
|
|
|
|
|
|
|
|
widgetIds = $.map( widgetContainerIds, function( widgetContainerId ) {
|
|
|
|
return $( '#' + widgetContainerId ).find( ':input[name=widget-id]' ).val();
|
2014-04-07 05:04:14 -04:00
|
|
|
} );
|
2014-04-07 16:00:16 -04:00
|
|
|
|
|
|
|
self.setting( widgetIds );
|
2014-04-07 05:04:14 -04:00
|
|
|
}
|
|
|
|
} );
|
2014-03-05 15:41:14 -05:00
|
|
|
|
2014-04-07 05:04:14 -04:00
|
|
|
/**
|
|
|
|
* Expand other customizer sidebar section when dragging a control widget over it,
|
|
|
|
* allowing the control to be dropped into another section
|
|
|
|
*/
|
2014-04-07 16:00:16 -04:00
|
|
|
this.$controlSection.find( '.accordion-section-title' ).droppable({
|
2014-04-07 05:04:14 -04:00
|
|
|
accept: '.customize-control-widget_form',
|
|
|
|
over: function() {
|
2014-04-07 16:00:16 -04:00
|
|
|
if ( ! self.$controlSection.hasClass( 'open' ) ) {
|
|
|
|
self.$controlSection.addClass( 'open' );
|
|
|
|
self.$sectionContent.toggle( false ).slideToggle( 150, function() {
|
|
|
|
self.$sectionContent.sortable( 'refreshPositions' );
|
2014-04-07 05:04:14 -04:00
|
|
|
} );
|
|
|
|
}
|
|
|
|
}
|
2014-04-07 16:00:16 -04:00
|
|
|
});
|
2014-04-07 05:04:14 -04:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Keyboard-accessible reordering
|
|
|
|
*/
|
2014-04-07 16:00:16 -04:00
|
|
|
this.container.find( '.reorder-toggle' ).on( 'click keydown', function( event ) {
|
2014-04-07 05:04:14 -04:00
|
|
|
if ( event.type === 'keydown' && ! ( event.which === 13 || event.which === 32 ) ) { // Enter or Spacebar
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2014-04-07 16:00:16 -04:00
|
|
|
self.toggleReordering( ! self.isReordering );
|
2014-04-07 05:04:14 -04:00
|
|
|
} );
|
2014-03-05 15:41:14 -05:00
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
2014-04-07 05:04:14 -04:00
|
|
|
* Set up UI for adding a new widget
|
2014-03-05 15:41:14 -05:00
|
|
|
*/
|
2014-04-07 05:04:14 -04:00
|
|
|
_setupAddition: function() {
|
2014-04-07 16:00:16 -04:00
|
|
|
var self = this;
|
2014-03-05 15:41:14 -05:00
|
|
|
|
2014-04-07 16:00:16 -04:00
|
|
|
this.container.find( '.add-new-widget' ).on( 'click keydown', function( event ) {
|
2014-04-07 05:04:14 -04:00
|
|
|
if ( event.type === 'keydown' && ! ( event.which === 13 || event.which === 32 ) ) { // Enter or Spacebar
|
|
|
|
return;
|
2014-03-05 15:41:14 -05:00
|
|
|
}
|
2014-04-07 05:04:14 -04:00
|
|
|
|
2014-04-07 16:00:16 -04:00
|
|
|
if ( self.$sectionContent.hasClass( 'reordering' ) ) {
|
2014-04-07 05:04:14 -04:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ( ! $( 'body' ).hasClass( 'adding-widget' ) ) {
|
2014-04-07 16:00:16 -04:00
|
|
|
api.Widgets.availableWidgetsPanel.open( self );
|
2014-03-05 15:41:14 -05:00
|
|
|
} else {
|
2014-04-07 05:04:14 -04:00
|
|
|
api.Widgets.availableWidgetsPanel.close();
|
2014-03-05 15:41:14 -05:00
|
|
|
}
|
2014-04-07 05:04:14 -04:00
|
|
|
} );
|
2014-03-05 15:41:14 -05:00
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
2014-04-07 05:04:14 -04:00
|
|
|
* Add classes to the widget_form controls to assist with styling
|
2014-03-05 15:41:14 -05:00
|
|
|
*/
|
2014-04-07 05:04:14 -04:00
|
|
|
_applyCardinalOrderClassNames: function() {
|
2014-04-07 16:00:16 -04:00
|
|
|
this.$sectionContent.find( '.customize-control-widget_form' )
|
2014-04-07 05:04:14 -04:00
|
|
|
.removeClass( 'first-widget' )
|
|
|
|
.removeClass( 'last-widget' )
|
|
|
|
.find( '.move-widget-down, .move-widget-up' ).prop( 'tabIndex', 0 );
|
|
|
|
|
2014-04-07 16:00:16 -04:00
|
|
|
this.$sectionContent.find( '.customize-control-widget_form:first' )
|
2014-04-07 05:04:14 -04:00
|
|
|
.addClass( 'first-widget' )
|
|
|
|
.find( '.move-widget-up' ).prop( 'tabIndex', -1 );
|
2014-04-07 16:00:16 -04:00
|
|
|
|
|
|
|
this.$sectionContent.find( '.customize-control-widget_form:last' )
|
2014-04-07 05:04:14 -04:00
|
|
|
.addClass( 'last-widget' )
|
|
|
|
.find( '.move-widget-down' ).prop( 'tabIndex', -1 );
|
2014-03-05 15:41:14 -05:00
|
|
|
},
|
|
|
|
|
2014-04-07 05:04:14 -04:00
|
|
|
|
|
|
|
/***********************************************************************
|
|
|
|
* Begin public API methods
|
|
|
|
**********************************************************************/
|
|
|
|
|
2014-03-05 15:41:14 -05:00
|
|
|
/**
|
2014-04-07 05:04:14 -04:00
|
|
|
* Enable/disable the reordering UI
|
2014-03-05 15:41:14 -05:00
|
|
|
*
|
2014-04-07 16:00:16 -04:00
|
|
|
* @param {Boolean} showOrHide to enable/disable reordering
|
2014-03-05 15:41:14 -05:00
|
|
|
*/
|
2014-04-07 16:00:16 -04:00
|
|
|
toggleReordering: function( showOrHide ) {
|
|
|
|
showOrHide = Boolean( showOrHide );
|
|
|
|
|
|
|
|
if ( showOrHide === this.$sectionContent.hasClass( 'reordering' ) ) {
|
2014-04-07 05:04:14 -04:00
|
|
|
return;
|
|
|
|
}
|
2014-03-06 11:45:15 -05:00
|
|
|
|
2014-04-07 16:00:16 -04:00
|
|
|
this.isReordering = showOrHide;
|
|
|
|
this.$sectionContent.toggleClass( 'reordering', showOrHide );
|
2014-04-07 05:04:14 -04:00
|
|
|
|
2014-04-07 16:00:16 -04:00
|
|
|
if ( showOrHide ) {
|
|
|
|
_( this.getWidgetFormControls() ).each( function( formControl ) {
|
|
|
|
formControl.collapseForm();
|
2014-04-07 05:04:14 -04:00
|
|
|
} );
|
2014-07-18 23:24:15 -04:00
|
|
|
|
|
|
|
this.$sectionContent.find( '.first-widget .move-widget' ).focus();
|
2014-07-18 23:32:17 -04:00
|
|
|
this.$sectionContent.find( '.add-new-widget' ).prop( 'tabIndex', -1 );
|
|
|
|
} else {
|
|
|
|
this.$sectionContent.find( '.add-new-widget' ).prop( 'tabIndex', 0 );
|
2014-03-05 15:41:14 -05:00
|
|
|
}
|
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
2014-04-07 05:04:14 -04:00
|
|
|
* @return {wp.customize.controlConstructor.widget_form[]}
|
2014-03-05 15:41:14 -05:00
|
|
|
*/
|
2014-04-07 05:04:14 -04:00
|
|
|
getWidgetFormControls: function() {
|
2014-04-07 16:00:16 -04:00
|
|
|
var formControls;
|
2014-03-05 15:41:14 -05:00
|
|
|
|
2014-04-07 16:00:16 -04:00
|
|
|
formControls = _( this.setting() ).map( function( widgetId ) {
|
|
|
|
var settingId = widgetIdToSettingId( widgetId ),
|
|
|
|
formControl = api.control( settingId );
|
2014-04-07 05:04:14 -04:00
|
|
|
|
2014-04-07 16:00:16 -04:00
|
|
|
if ( ! formControl ) {
|
|
|
|
return;
|
2014-04-07 05:04:14 -04:00
|
|
|
}
|
2014-04-07 16:00:16 -04:00
|
|
|
|
|
|
|
return formControl;
|
2014-04-07 05:04:14 -04:00
|
|
|
} );
|
2014-04-07 16:00:16 -04:00
|
|
|
|
|
|
|
return formControls;
|
2014-03-05 15:41:14 -05:00
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
2014-04-07 16:00:16 -04:00
|
|
|
* @param {string} widgetId or an id_base for adding a previously non-existing widget
|
2014-04-08 04:12:15 -04:00
|
|
|
* @returns {object|false} widget_form control instance, or false on error
|
2014-03-05 15:41:14 -05:00
|
|
|
*/
|
2014-04-07 16:00:16 -04:00
|
|
|
addWidget: function( widgetId ) {
|
|
|
|
var self = this, controlHtml, $widget, controlType = 'widget_form', $control, controlConstructor,
|
|
|
|
parsedWidgetId = parseWidgetId( widgetId ),
|
|
|
|
widgetNumber = parsedWidgetId.number,
|
|
|
|
widgetIdBase = parsedWidgetId.id_base,
|
|
|
|
widget = api.Widgets.availableWidgets.findWhere( {id_base: widgetIdBase} ),
|
|
|
|
settingId, isExistingWidget, widgetFormControl, sidebarWidgets, settingArgs;
|
2014-03-05 15:41:14 -05:00
|
|
|
|
2014-04-07 05:04:14 -04:00
|
|
|
if ( ! widget ) {
|
2014-04-08 04:12:15 -04:00
|
|
|
return false;
|
2014-04-07 05:04:14 -04:00
|
|
|
}
|
2014-04-07 16:00:16 -04:00
|
|
|
|
|
|
|
if ( widgetNumber && ! widget.get( 'is_multi' ) ) {
|
2014-04-08 04:12:15 -04:00
|
|
|
return false;
|
2014-04-07 05:04:14 -04:00
|
|
|
}
|
2014-03-06 11:45:15 -05:00
|
|
|
|
2014-04-07 05:04:14 -04:00
|
|
|
// Set up new multi widget
|
2014-04-07 16:00:16 -04:00
|
|
|
if ( widget.get( 'is_multi' ) && ! widgetNumber ) {
|
2014-04-07 05:04:14 -04:00
|
|
|
widget.set( 'multi_number', widget.get( 'multi_number' ) + 1 );
|
2014-04-07 16:00:16 -04:00
|
|
|
widgetNumber = widget.get( 'multi_number' );
|
2014-04-07 05:04:14 -04:00
|
|
|
}
|
2014-03-05 15:41:14 -05:00
|
|
|
|
2014-04-14 18:46:16 -04:00
|
|
|
controlHtml = $.trim( $( '#widget-tpl-' + widget.get( 'id' ) ).html() );
|
2014-04-07 05:04:14 -04:00
|
|
|
if ( widget.get( 'is_multi' ) ) {
|
2014-04-07 16:00:16 -04:00
|
|
|
controlHtml = controlHtml.replace( /<[^<>]+>/g, function( m ) {
|
|
|
|
return m.replace( /__i__|%i%/g, widgetNumber );
|
2014-04-07 05:04:14 -04:00
|
|
|
} );
|
|
|
|
} else {
|
|
|
|
widget.set( 'is_disabled', true ); // Prevent single widget from being added again now
|
|
|
|
}
|
2014-03-05 15:41:14 -05:00
|
|
|
|
2014-04-07 16:00:16 -04:00
|
|
|
$widget = $( controlHtml );
|
|
|
|
|
|
|
|
$control = $( '<li/>' )
|
|
|
|
.addClass( 'customize-control' )
|
|
|
|
.addClass( 'customize-control-' + controlType )
|
|
|
|
.append( $widget );
|
|
|
|
|
|
|
|
// Remove icon which is visible inside the panel
|
|
|
|
$control.find( '> .widget-icon' ).remove();
|
|
|
|
|
2014-04-07 05:04:14 -04:00
|
|
|
if ( widget.get( 'is_multi' ) ) {
|
2014-04-07 16:00:16 -04:00
|
|
|
$control.find( 'input[name="widget_number"]' ).val( widgetNumber );
|
|
|
|
$control.find( 'input[name="multi_number"]' ).val( widgetNumber );
|
2014-03-05 15:41:14 -05:00
|
|
|
}
|
2014-04-07 05:04:14 -04:00
|
|
|
|
2014-04-07 16:00:16 -04:00
|
|
|
widgetId = $control.find( '[name="widget-id"]' ).val();
|
|
|
|
|
|
|
|
$control.hide(); // to be slid-down below
|
|
|
|
|
|
|
|
settingId = 'widget_' + widget.get( 'id_base' );
|
2014-04-07 05:04:14 -04:00
|
|
|
if ( widget.get( 'is_multi' ) ) {
|
2014-04-07 16:00:16 -04:00
|
|
|
settingId += '[' + widgetNumber + ']';
|
2014-03-05 15:41:14 -05:00
|
|
|
}
|
2014-04-07 16:00:16 -04:00
|
|
|
$control.attr( 'id', 'customize-control-' + settingId.replace( /\]/g, '' ).replace( /\[/g, '-' ) );
|
2014-03-05 15:41:14 -05:00
|
|
|
|
2014-04-07 16:00:16 -04:00
|
|
|
this.container.after( $control );
|
2014-03-05 15:41:14 -05:00
|
|
|
|
2014-04-07 05:04:14 -04:00
|
|
|
// Only create setting if it doesn't already exist (if we're adding a pre-existing inactive widget)
|
2014-04-07 16:00:16 -04:00
|
|
|
isExistingWidget = api.has( settingId );
|
|
|
|
if ( ! isExistingWidget ) {
|
|
|
|
settingArgs = {
|
2014-04-07 05:04:14 -04:00
|
|
|
transport: 'refresh',
|
2014-04-07 16:00:16 -04:00
|
|
|
previewer: this.setting.previewer
|
2014-04-07 05:04:14 -04:00
|
|
|
};
|
2014-04-07 16:00:16 -04:00
|
|
|
api.create( settingId, settingId, {}, settingArgs );
|
2014-04-07 05:04:14 -04:00
|
|
|
}
|
2014-03-06 11:45:15 -05:00
|
|
|
|
2014-04-07 16:00:16 -04:00
|
|
|
controlConstructor = api.controlConstructor[controlType];
|
|
|
|
widgetFormControl = new controlConstructor( settingId, {
|
2014-04-07 05:04:14 -04:00
|
|
|
params: {
|
|
|
|
settings: {
|
2014-04-07 16:00:16 -04:00
|
|
|
'default': settingId
|
2014-04-07 05:04:14 -04:00
|
|
|
},
|
2014-04-07 16:00:16 -04:00
|
|
|
sidebar_id: self.params.sidebar_id,
|
|
|
|
widget_id: widgetId,
|
2014-04-07 05:04:14 -04:00
|
|
|
widget_id_base: widget.get( 'id_base' ),
|
2014-04-07 16:00:16 -04:00
|
|
|
type: controlType,
|
|
|
|
is_new: ! isExistingWidget,
|
2014-04-07 05:04:14 -04:00
|
|
|
width: widget.get( 'width' ),
|
|
|
|
height: widget.get( 'height' ),
|
|
|
|
is_wide: widget.get( 'is_wide' )
|
|
|
|
},
|
2014-04-07 16:00:16 -04:00
|
|
|
previewer: self.setting.previewer
|
2014-04-07 05:04:14 -04:00
|
|
|
} );
|
2014-04-07 16:00:16 -04:00
|
|
|
api.control.add( settingId, widgetFormControl );
|
2014-04-07 05:04:14 -04:00
|
|
|
|
|
|
|
// Make sure widget is removed from the other sidebars
|
2014-04-07 16:00:16 -04:00
|
|
|
api.each( function( otherSetting ) {
|
|
|
|
if ( otherSetting.id === self.setting.id ) {
|
2014-04-07 05:04:14 -04:00
|
|
|
return;
|
|
|
|
}
|
2014-04-07 16:00:16 -04:00
|
|
|
|
|
|
|
if ( 0 !== otherSetting.id.indexOf( 'sidebars_widgets[' ) ) {
|
2014-04-07 05:04:14 -04:00
|
|
|
return;
|
|
|
|
}
|
2014-04-07 16:00:16 -04:00
|
|
|
|
|
|
|
var otherSidebarWidgets = otherSetting().slice(),
|
|
|
|
i = _.indexOf( otherSidebarWidgets, widgetId );
|
|
|
|
|
2014-04-07 05:04:14 -04:00
|
|
|
if ( -1 !== i ) {
|
2014-04-07 16:00:16 -04:00
|
|
|
otherSidebarWidgets.splice( i );
|
|
|
|
otherSetting( otherSidebarWidgets );
|
2014-04-07 05:04:14 -04:00
|
|
|
}
|
|
|
|
} );
|
|
|
|
|
|
|
|
// Add widget to this sidebar
|
2014-04-07 16:00:16 -04:00
|
|
|
sidebarWidgets = this.setting().slice();
|
|
|
|
if ( -1 === _.indexOf( sidebarWidgets, widgetId ) ) {
|
|
|
|
sidebarWidgets.push( widgetId );
|
|
|
|
this.setting( sidebarWidgets );
|
2014-03-05 15:41:14 -05:00
|
|
|
}
|
|
|
|
|
2014-04-07 16:00:16 -04:00
|
|
|
$control.slideDown( function() {
|
|
|
|
if ( isExistingWidget ) {
|
|
|
|
widgetFormControl.expandForm();
|
|
|
|
widgetFormControl.updateWidget( {
|
|
|
|
instance: widgetFormControl.setting(),
|
2014-04-07 10:10:14 -04:00
|
|
|
complete: function( error ) {
|
2014-04-07 05:04:14 -04:00
|
|
|
if ( error ) {
|
|
|
|
throw error;
|
|
|
|
}
|
2014-04-07 16:00:16 -04:00
|
|
|
widgetFormControl.focus();
|
2014-04-07 05:04:14 -04:00
|
|
|
}
|
|
|
|
} );
|
|
|
|
} else {
|
2014-04-07 16:00:16 -04:00
|
|
|
widgetFormControl.focus();
|
2014-04-07 05:04:14 -04:00
|
|
|
}
|
|
|
|
} );
|
|
|
|
|
2014-04-07 16:00:16 -04:00
|
|
|
$( document ).trigger( 'widget-added', [ $widget ] );
|
2014-04-07 05:04:14 -04:00
|
|
|
|
2014-04-07 16:00:16 -04:00
|
|
|
return widgetFormControl;
|
2014-03-05 15:41:14 -05:00
|
|
|
}
|
|
|
|
} );
|
|
|
|
|
2014-04-07 16:00:16 -04:00
|
|
|
/**
|
|
|
|
* Extends wp.customizer.controlConstructor with control constructor for
|
|
|
|
* widget_form and sidebar_widgets.
|
|
|
|
*/
|
2014-04-07 05:04:14 -04:00
|
|
|
$.extend( api.controlConstructor, {
|
|
|
|
widget_form: api.Widgets.WidgetControl,
|
|
|
|
sidebar_widgets: api.Widgets.SidebarControl
|
|
|
|
});
|
|
|
|
|
2014-04-07 16:00:16 -04:00
|
|
|
/**
|
|
|
|
* Init Customizer for widgets.
|
|
|
|
*/
|
2014-04-07 05:04:14 -04:00
|
|
|
api.bind( 'ready', function() {
|
|
|
|
// Set up the widgets panel
|
2014-04-07 10:10:14 -04:00
|
|
|
api.Widgets.availableWidgetsPanel = new api.Widgets.AvailableWidgetsPanelView({
|
|
|
|
collection: api.Widgets.availableWidgets
|
|
|
|
});
|
2014-04-07 05:04:14 -04:00
|
|
|
|
|
|
|
// Highlight widget control
|
2014-07-09 16:58:15 -04:00
|
|
|
api.previewer.bind( 'highlight-widget-control', api.Widgets.highlightWidgetFormControl );
|
2014-04-07 05:04:14 -04:00
|
|
|
|
|
|
|
// Open and focus widget control
|
2014-07-09 16:58:15 -04:00
|
|
|
api.previewer.bind( 'focus-widget-control', api.Widgets.focusWidgetFormControl );
|
2014-04-07 05:04:14 -04:00
|
|
|
} );
|
|
|
|
|
2014-04-01 11:26:15 -04:00
|
|
|
/**
|
|
|
|
* Highlight a widget control.
|
|
|
|
*
|
|
|
|
* @param {string} widgetId
|
|
|
|
*/
|
2014-04-07 05:04:14 -04:00
|
|
|
api.Widgets.highlightWidgetFormControl = function( widgetId ) {
|
|
|
|
var control = api.Widgets.getWidgetFormControlForWidget( widgetId );
|
2014-04-01 11:26:15 -04:00
|
|
|
|
|
|
|
if ( control ) {
|
|
|
|
control.highlightSectionAndControl();
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Focus a widget control.
|
|
|
|
*
|
|
|
|
* @param {string} widgetId
|
|
|
|
*/
|
2014-04-07 05:04:14 -04:00
|
|
|
api.Widgets.focusWidgetFormControl = function( widgetId ) {
|
|
|
|
var control = api.Widgets.getWidgetFormControlForWidget( widgetId );
|
2014-04-01 11:26:15 -04:00
|
|
|
|
|
|
|
if ( control ) {
|
|
|
|
control.focus();
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
2014-03-05 15:41:14 -05:00
|
|
|
/**
|
|
|
|
* Given a widget control, find the sidebar widgets control that contains it.
|
2014-04-07 16:00:16 -04:00
|
|
|
* @param {string} widgetId
|
2014-03-05 15:41:14 -05:00
|
|
|
* @return {object|null}
|
|
|
|
*/
|
2014-04-07 16:00:16 -04:00
|
|
|
api.Widgets.getSidebarWidgetControlContainingWidget = function( widgetId ) {
|
|
|
|
var foundControl = null;
|
|
|
|
|
2014-04-07 12:32:15 -04:00
|
|
|
// @todo this can use widgetIdToSettingId(), then pass into wp.customize.control( x ).getSidebarWidgetsControl()
|
2014-04-07 10:10:14 -04:00
|
|
|
api.control.each( function( control ) {
|
2014-04-07 16:00:16 -04:00
|
|
|
if ( control.params.type === 'sidebar_widgets' && -1 !== _.indexOf( control.setting(), widgetId ) ) {
|
|
|
|
foundControl = control;
|
2014-03-05 15:41:14 -05:00
|
|
|
}
|
|
|
|
} );
|
2014-04-07 05:04:14 -04:00
|
|
|
|
2014-04-07 16:00:16 -04:00
|
|
|
return foundControl;
|
2014-03-05 15:41:14 -05:00
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
2014-04-07 16:00:16 -04:00
|
|
|
* Given a widget ID for a widget appearing in the preview, get the widget form control associated with it.
|
|
|
|
*
|
|
|
|
* @param {string} widgetId
|
2014-03-05 15:41:14 -05:00
|
|
|
* @return {object|null}
|
|
|
|
*/
|
2014-04-07 16:00:16 -04:00
|
|
|
api.Widgets.getWidgetFormControlForWidget = function( widgetId ) {
|
|
|
|
var foundControl = null;
|
|
|
|
|
2014-04-07 12:32:15 -04:00
|
|
|
// @todo We can just use widgetIdToSettingId() here
|
2014-04-07 10:10:14 -04:00
|
|
|
api.control.each( function( control ) {
|
2014-04-07 16:00:16 -04:00
|
|
|
if ( control.params.type === 'widget_form' && control.params.widget_id === widgetId ) {
|
|
|
|
foundControl = control;
|
2014-03-05 15:41:14 -05:00
|
|
|
}
|
|
|
|
} );
|
|
|
|
|
2014-04-07 16:00:16 -04:00
|
|
|
return foundControl;
|
2014-03-05 15:41:14 -05:00
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
2014-04-07 16:00:16 -04:00
|
|
|
* @param {String} widgetId
|
2014-03-05 15:41:14 -05:00
|
|
|
* @returns {Object}
|
|
|
|
*/
|
2014-04-07 16:00:16 -04:00
|
|
|
function parseWidgetId( widgetId ) {
|
2014-03-06 11:45:15 -05:00
|
|
|
var matches, parsed = {
|
2014-03-05 15:41:14 -05:00
|
|
|
number: null,
|
|
|
|
id_base: null
|
|
|
|
};
|
2014-04-07 16:00:16 -04:00
|
|
|
|
|
|
|
matches = widgetId.match( /^(.+)-(\d+)$/ );
|
2014-03-05 15:41:14 -05:00
|
|
|
if ( matches ) {
|
|
|
|
parsed.id_base = matches[1];
|
|
|
|
parsed.number = parseInt( matches[2], 10 );
|
|
|
|
} else {
|
|
|
|
// likely an old single widget
|
2014-04-07 16:00:16 -04:00
|
|
|
parsed.id_base = widgetId;
|
2014-03-05 15:41:14 -05:00
|
|
|
}
|
2014-04-07 16:00:16 -04:00
|
|
|
|
2014-03-05 15:41:14 -05:00
|
|
|
return parsed;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2014-04-07 16:00:16 -04:00
|
|
|
* @param {String} widgetId
|
2014-04-07 12:32:15 -04:00
|
|
|
* @returns {String} settingId
|
2014-03-05 15:41:14 -05:00
|
|
|
*/
|
2014-04-07 16:00:16 -04:00
|
|
|
function widgetIdToSettingId( widgetId ) {
|
|
|
|
var parsed = parseWidgetId( widgetId ), settingId;
|
2014-03-06 11:45:15 -05:00
|
|
|
|
2014-04-07 12:32:15 -04:00
|
|
|
settingId = 'widget_' + parsed.id_base;
|
2014-03-05 15:41:14 -05:00
|
|
|
if ( parsed.number ) {
|
2014-04-07 12:32:15 -04:00
|
|
|
settingId += '[' + parsed.number + ']';
|
2014-03-05 15:41:14 -05:00
|
|
|
}
|
2014-04-07 16:00:16 -04:00
|
|
|
|
2014-04-07 12:32:15 -04:00
|
|
|
return settingId;
|
2014-03-05 15:41:14 -05:00
|
|
|
}
|
|
|
|
|
2014-04-07 05:04:14 -04:00
|
|
|
})( window.wp, jQuery );
|