forked from xwp/wp-widget-customizer
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathwidget-customizer-preview.js
85 lines (73 loc) · 2.53 KB
/
widget-customizer-preview.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
/*global wp, jQuery, WidgetCustomizerPreview_exports */
var WidgetCustomizerPreview = (function ($) {
'use strict';
var self = {
rendered_sidebars: [],
registered_sidebars: {},
widget_selectors: [],
i18n: {},
init: function () {
this.buildWidgetSelectors();
this.toggleSections();
this.highlightControls();
},
/**
* Calculate the selector for the sidebar's widgets based on the registered sidebar's info
*/
buildWidgetSelectors: function () {
$.each( self.registered_sidebars, function ( id, sidebar ) {
var widget_tpl = [
sidebar.before_widget.replace('%1$s', '').replace('%2$s', ''),
sidebar.before_title,
sidebar.after_title,
sidebar.after_widget
].join('');
var empty_widget = $(widget_tpl);
var widget_selector = empty_widget.prop('tagName');
var widget_classes = empty_widget.prop('className').replace(/^\s+|\s+$/g, '');
if ( widget_classes ) {
widget_selector += '.' + widget_classes.split(/\s+/).join('.');
}
self.widget_selectors.push(widget_selector);
});
},
/**
* @todo This will fail if a sidebar does not have at least one widget. Can be fixed with http://core.trac.wordpress.org/ticket/25368
* @todo Use a method off of parent.WidgetCustomizer
* @todo Use postMessage instead of accessing parent window?
*/
toggleSections: function () {
parent.jQuery('.control-section[id^="accordion-section-sidebar-widgets-"]').hide();
$.each( self.rendered_sidebars, function ( i, sidebar_id ) {
parent.jQuery('#accordion-section-sidebar-widgets-' + sidebar_id).show();
});
},
/**
*
*/
highlightControls: function() {
var selector = this.widget_selectors.join(',');
$(selector).attr( 'title', self.i18n.widget_tooltip );
$(document).on( 'mouseenter', selector, function () {
var control = parent.WidgetCustomizer.getControlInstanceForWidget( $(this).prop('id') );
if ( control ) {
control.highlightSectionAndControl();
}
});
// @todo click can interfere with interacting with the widget in the preview window; better to make a EDIT link overlay appear when hovering over the widget?
$(document).on( 'click', selector, function () {
var control = parent.WidgetCustomizer.getControlInstanceForWidget( $(this).prop('id') );
if ( control ) {
control.expandControlSection();
control.expandForm();
control.container.find(':input:visible:first').focus();
}
});
}
};
$.extend(self, WidgetCustomizerPreview_exports);
$(function () {
self.init();
});
return self;
}( jQuery ));