2014-04-07 05:04:14 -04:00
|
|
|
(function( wp, $ ){
|
2014-03-22 17:04:15 -04:00
|
|
|
|
|
|
|
if ( ! wp || ! wp.customize ) { return; }
|
|
|
|
|
2015-12-06 13:39:25 -05:00
|
|
|
var api = wp.customize;
|
2014-03-22 17:04:15 -04:00
|
|
|
|
|
|
|
/**
|
|
|
|
* 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 15:41:14 -05:00
|
|
|
preview: null,
|
2014-03-22 17:04:15 -04:00
|
|
|
l10n: {},
|
2014-03-05 15:41:14 -05:00
|
|
|
|
|
|
|
init: function () {
|
2014-03-22 17:04:15 -04:00
|
|
|
var self = this;
|
2015-12-06 13:39:25 -05:00
|
|
|
|
|
|
|
this.preview = api.preview;
|
2014-03-05 15:41:14 -05:00
|
|
|
this.buildWidgetSelectors();
|
|
|
|
this.highlightControls();
|
|
|
|
|
2014-04-01 11:26:15 -04:00
|
|
|
this.preview.bind( 'highlight-widget', self.highlightWidget );
|
2014-03-05 15:41:14 -05:00
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Calculate the selector for the sidebar's widgets based on the registered sidebar's info
|
|
|
|
*/
|
|
|
|
buildWidgetSelectors: function () {
|
2014-03-22 17:04:15 -04:00
|
|
|
var self = this;
|
|
|
|
|
|
|
|
$.each( this.registeredSidebars, function ( i, sidebar ) {
|
|
|
|
var widgetTpl = [
|
2014-03-06 11:45:15 -05:00
|
|
|
sidebar.before_widget.replace('%1$s', '').replace('%2$s', ''),
|
|
|
|
sidebar.before_title,
|
|
|
|
sidebar.after_title,
|
|
|
|
sidebar.after_widget
|
|
|
|
].join(''),
|
2014-03-22 17:04:15 -04:00
|
|
|
emptyWidget,
|
|
|
|
widgetSelector,
|
|
|
|
widgetClasses;
|
2014-03-06 11:45:15 -05:00
|
|
|
|
2014-03-22 17:04:15 -04:00
|
|
|
emptyWidget = $(widgetTpl);
|
|
|
|
widgetSelector = emptyWidget.prop('tagName');
|
2014-04-13 12:41:15 -04:00
|
|
|
widgetClasses = emptyWidget.prop('className');
|
|
|
|
|
|
|
|
// Prevent a rare case when before_widget, before_title, after_title and after_widget is empty.
|
|
|
|
if ( ! widgetClasses ) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
widgetClasses = widgetClasses.replace(/^\s+|\s+$/g, '');
|
2014-03-06 11:45:15 -05:00
|
|
|
|
2014-03-22 17:04:15 -04:00
|
|
|
if ( widgetClasses ) {
|
|
|
|
widgetSelector += '.' + widgetClasses.split(/\s+/).join('.');
|
2014-03-05 15:41:14 -05:00
|
|
|
}
|
2014-03-22 17:04:15 -04:00
|
|
|
self.widgetSelectors.push(widgetSelector);
|
2014-03-05 15:41:14 -05:00
|
|
|
});
|
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
2014-04-01 11:26:15 -04:00
|
|
|
* Highlight the widget on widget updates or widget control mouse overs.
|
2014-03-05 15:41:14 -05:00
|
|
|
*
|
2014-04-01 11:26:15 -04:00
|
|
|
* @param {string} widgetId ID of the widget.
|
2014-03-05 15:41:14 -05:00
|
|
|
*/
|
2014-04-01 11:26:15 -04:00
|
|
|
highlightWidget: function( widgetId ) {
|
|
|
|
var $body = $( document.body ),
|
|
|
|
$widget = $( '#' + widgetId );
|
|
|
|
|
|
|
|
$body.find( '.widget-customizer-highlighted-widget' ).removeClass( 'widget-customizer-highlighted-widget' );
|
|
|
|
|
|
|
|
$widget.addClass( 'widget-customizer-highlighted-widget' );
|
|
|
|
setTimeout( function () {
|
|
|
|
$widget.removeClass( 'widget-customizer-highlighted-widget' );
|
|
|
|
}, 500 );
|
2014-03-05 15:41:14 -05:00
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
2014-04-01 11:26:15 -04:00
|
|
|
* Show a title and highlight widgets on hover. On shift+clicking
|
|
|
|
* focus the widget control.
|
2014-03-05 15:41:14 -05:00
|
|
|
*/
|
|
|
|
highlightControls: function() {
|
2014-04-01 11:26:15 -04:00
|
|
|
var self = this,
|
|
|
|
selector = this.widgetSelectors.join(',');
|
2014-03-05 15:41:14 -05:00
|
|
|
|
2014-03-22 17:04:15 -04:00
|
|
|
$(selector).attr( 'title', this.l10n.widgetTooltip );
|
2014-03-05 15:41:14 -05:00
|
|
|
|
|
|
|
$(document).on( 'mouseenter', selector, function () {
|
2014-04-01 11:26:15 -04:00
|
|
|
self.preview.send( 'highlight-widget-control', $( this ).prop( 'id' ) );
|
2014-03-05 15:41:14 -05:00
|
|
|
});
|
|
|
|
|
|
|
|
// Open expand the widget control when shift+clicking the widget element
|
|
|
|
$(document).on( 'click', selector, function ( e ) {
|
|
|
|
if ( ! e.shiftKey ) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
e.preventDefault();
|
2014-04-01 11:26:15 -04:00
|
|
|
|
|
|
|
self.preview.send( 'focus-widget-control', $( this ).prop( 'id' ) );
|
2014-03-05 15:41:14 -05:00
|
|
|
});
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
$(function () {
|
2014-03-22 17:04:15 -04:00
|
|
|
var settings = window._wpWidgetCustomizerPreviewSettings;
|
|
|
|
if ( ! settings ) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
$.extend( api.WidgetCustomizerPreview, settings );
|
|
|
|
|
|
|
|
api.WidgetCustomizerPreview.init();
|
2014-03-05 15:41:14 -05:00
|
|
|
});
|
|
|
|
|
2014-04-07 05:04:14 -04:00
|
|
|
})( window.wp, jQuery );
|