2014-03-22 22:04:15 +01:00
|
|
|
(function( $, wp ){
|
|
|
|
|
|
|
|
if ( ! wp || ! wp.customize ) { return; }
|
|
|
|
|
|
|
|
var api = wp.customize,
|
|
|
|
OldPreview;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* wp.customize.WidgetCustomizerPreview
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
api.WidgetCustomizerPreview = {
|
|
|
|
renderedSidebars: {}, // @todo Make rendered a property of the Backbone model
|
|
|
|
renderedWidgets: {}, // @todo Make rendered a property of the Backbone model
|
|
|
|
registeredSidebars: [], // @todo Make a Backbone collection
|
|
|
|
registeredWidgets: {}, // @todo Make array, Backbone collection
|
|
|
|
widgetSelectors: [],
|
2014-03-05 21:41:14 +01:00
|
|
|
preview: null,
|
2014-03-22 22:04:15 +01:00
|
|
|
l10n: {},
|
2014-03-05 21:41:14 +01:00
|
|
|
|
|
|
|
init: function () {
|
2014-03-22 22:04:15 +01:00
|
|
|
var self = this;
|
2014-03-05 21:41:14 +01:00
|
|
|
this.buildWidgetSelectors();
|
|
|
|
this.highlightControls();
|
|
|
|
|
2014-03-22 22:04:15 +01:00
|
|
|
this.preview.bind( 'active', function() {
|
|
|
|
self.preview.send( 'rendered-sidebars', self.renderedSidebars ); // @todo Only send array of IDs
|
|
|
|
self.preview.send( 'rendered-widgets', self.renderedWidgets ); // @todo Only send array of IDs
|
2014-03-05 21:41:14 +01:00
|
|
|
} );
|
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Calculate the selector for the sidebar's widgets based on the registered sidebar's info
|
|
|
|
*/
|
|
|
|
buildWidgetSelectors: function () {
|
2014-03-22 22:04:15 +01:00
|
|
|
var self = this;
|
|
|
|
|
|
|
|
$.each( this.registeredSidebars, function ( i, sidebar ) {
|
|
|
|
var widgetTpl = [
|
2014-03-06 17:45:15 +01:00
|
|
|
sidebar.before_widget.replace('%1$s', '').replace('%2$s', ''),
|
|
|
|
sidebar.before_title,
|
|
|
|
sidebar.after_title,
|
|
|
|
sidebar.after_widget
|
|
|
|
].join(''),
|
2014-03-22 22:04:15 +01:00
|
|
|
emptyWidget,
|
|
|
|
widgetSelector,
|
|
|
|
widgetClasses;
|
2014-03-06 17:45:15 +01:00
|
|
|
|
2014-03-22 22:04:15 +01:00
|
|
|
emptyWidget = $(widgetTpl);
|
|
|
|
widgetSelector = emptyWidget.prop('tagName');
|
|
|
|
widgetClasses = emptyWidget.prop('className').replace(/^\s+|\s+$/g, '');
|
2014-03-06 17:45:15 +01:00
|
|
|
|
2014-03-22 22:04:15 +01:00
|
|
|
if ( widgetClasses ) {
|
|
|
|
widgetSelector += '.' + widgetClasses.split(/\s+/).join('.');
|
2014-03-05 21:41:14 +01:00
|
|
|
}
|
2014-03-22 22:04:15 +01:00
|
|
|
self.widgetSelectors.push(widgetSelector);
|
2014-03-05 21:41:14 +01:00
|
|
|
});
|
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
2014-03-18 16:21:15 +01:00
|
|
|
* Obtain a rendered widget element. Assumes standard practice of using
|
|
|
|
* the widget ID as the ID for the DOM element. To eliminate this
|
|
|
|
* assumption, additional data-* attributes would need to be injected
|
|
|
|
* onto the rendered widget root element.
|
2014-03-05 21:41:14 +01:00
|
|
|
*
|
|
|
|
* @param {String} widget_id
|
|
|
|
* @return {jQuery}
|
|
|
|
*/
|
2014-03-18 16:21:15 +01:00
|
|
|
getWidgetElement: function ( widget_id ) {
|
|
|
|
return $( '#' + widget_id );
|
2014-03-05 21:41:14 +01:00
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
highlightControls: function() {
|
2014-03-22 22:04:15 +01:00
|
|
|
var selector = this.widgetSelectors.join(',');
|
2014-03-05 21:41:14 +01:00
|
|
|
|
2014-03-22 22:04:15 +01:00
|
|
|
$(selector).attr( 'title', this.l10n.widgetTooltip );
|
2014-03-05 21:41:14 +01:00
|
|
|
|
|
|
|
$(document).on( 'mouseenter', selector, function () {
|
|
|
|
var control = parent.WidgetCustomizer.getWidgetFormControlForWidget( $(this).prop('id') );
|
|
|
|
if ( control ) {
|
|
|
|
control.highlightSectionAndControl();
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
// Open expand the widget control when shift+clicking the widget element
|
|
|
|
$(document).on( 'click', selector, function ( e ) {
|
|
|
|
if ( ! e.shiftKey ) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
e.preventDefault();
|
|
|
|
var control = parent.WidgetCustomizer.getWidgetFormControlForWidget( $(this).prop('id') );
|
|
|
|
if ( control ) {
|
|
|
|
control.focus();
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Capture the instance of the Preview since it is private
|
|
|
|
*/
|
2014-03-22 22:04:15 +01:00
|
|
|
OldPreview = api.Preview;
|
|
|
|
api.Preview = OldPreview.extend( {
|
2014-03-05 21:41:14 +01:00
|
|
|
initialize: function( params, options ) {
|
2014-03-22 22:04:15 +01:00
|
|
|
api.WidgetCustomizerPreview.preview = this;
|
2014-03-05 21:41:14 +01:00
|
|
|
OldPreview.prototype.initialize.call( this, params, options );
|
|
|
|
}
|
|
|
|
} );
|
|
|
|
|
|
|
|
$(function () {
|
2014-03-22 22:04:15 +01:00
|
|
|
var settings = window._wpWidgetCustomizerPreviewSettings;
|
|
|
|
if ( ! settings ) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
$.extend( api.WidgetCustomizerPreview, settings );
|
|
|
|
|
|
|
|
api.WidgetCustomizerPreview.init();
|
2014-03-05 21:41:14 +01:00
|
|
|
});
|
|
|
|
|
2014-03-22 22:04:15 +01:00
|
|
|
})( jQuery, window.wp );
|