~ubuntu-branches/debian/jessie/wordpress/jessie

« back to all changes in this revision

Viewing changes to wp-includes/js/customize-preview-widgets.js

  • Committer: Package Import Robot
  • Author(s): Craig Small
  • Date: 2014-04-17 20:56:19 UTC
  • mfrom: (1.2.35)
  • Revision ID: package-import@ubuntu.com-20140417205619-nurbet6eho4yvwfv
Tags: 3.9+dfsg-1
* New upstream release
* 3.9 seems to handle different locations for plugins so the
  plugin directory handling patches have been cut back.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
(function( wp, $ ){
 
2
 
 
3
        if ( ! wp || ! wp.customize ) { return; }
 
4
 
 
5
        var api = wp.customize,
 
6
                OldPreview;
 
7
 
 
8
        /**
 
9
         * wp.customize.WidgetCustomizerPreview
 
10
         *
 
11
         */
 
12
        api.WidgetCustomizerPreview = {
 
13
                renderedSidebars: {}, // @todo Make rendered a property of the Backbone model
 
14
                renderedWidgets: {}, // @todo Make rendered a property of the Backbone model
 
15
                registeredSidebars: [], // @todo Make a Backbone collection
 
16
                registeredWidgets: {}, // @todo Make array, Backbone collection
 
17
                widgetSelectors: [],
 
18
                preview: null,
 
19
                l10n: {},
 
20
 
 
21
                init: function () {
 
22
                        var self = this;
 
23
                        this.buildWidgetSelectors();
 
24
                        this.highlightControls();
 
25
 
 
26
                        this.preview.bind( 'active', function() {
 
27
                                self.preview.send( 'rendered-sidebars', self.renderedSidebars ); // @todo Only send array of IDs
 
28
                                self.preview.send( 'rendered-widgets', self.renderedWidgets ); // @todo Only send array of IDs
 
29
                        } );
 
30
 
 
31
                        this.preview.bind( 'highlight-widget', self.highlightWidget );
 
32
                },
 
33
 
 
34
                /**
 
35
                 * Calculate the selector for the sidebar's widgets based on the registered sidebar's info
 
36
                 */
 
37
                buildWidgetSelectors: function () {
 
38
                        var self = this;
 
39
 
 
40
                        $.each( this.registeredSidebars, function ( i, sidebar ) {
 
41
                                var widgetTpl = [
 
42
                                                sidebar.before_widget.replace('%1$s', '').replace('%2$s', ''),
 
43
                                                sidebar.before_title,
 
44
                                                sidebar.after_title,
 
45
                                                sidebar.after_widget
 
46
                                        ].join(''),
 
47
                                        emptyWidget,
 
48
                                        widgetSelector,
 
49
                                        widgetClasses;
 
50
 
 
51
                                emptyWidget = $(widgetTpl);
 
52
                                widgetSelector = emptyWidget.prop('tagName');
 
53
                                widgetClasses = emptyWidget.prop('className');
 
54
 
 
55
                                // Prevent a rare case when before_widget, before_title, after_title and after_widget is empty.
 
56
                                if ( ! widgetClasses ) {
 
57
                                        return;
 
58
                                }
 
59
 
 
60
                                widgetClasses = widgetClasses.replace(/^\s+|\s+$/g, '');
 
61
 
 
62
                                if ( widgetClasses ) {
 
63
                                        widgetSelector += '.' + widgetClasses.split(/\s+/).join('.');
 
64
                                }
 
65
                                self.widgetSelectors.push(widgetSelector);
 
66
                        });
 
67
                },
 
68
 
 
69
                /**
 
70
                 * Highlight the widget on widget updates or widget control mouse overs.
 
71
                 *
 
72
                 * @param  {string} widgetId ID of the widget.
 
73
                 */
 
74
                highlightWidget: function( widgetId ) {
 
75
                        var $body = $( document.body ),
 
76
                                $widget = $( '#' + widgetId );
 
77
 
 
78
                        $body.find( '.widget-customizer-highlighted-widget' ).removeClass( 'widget-customizer-highlighted-widget' );
 
79
 
 
80
                        $widget.addClass( 'widget-customizer-highlighted-widget' );
 
81
                        setTimeout( function () {
 
82
                                $widget.removeClass( 'widget-customizer-highlighted-widget' );
 
83
                        }, 500 );
 
84
                },
 
85
 
 
86
                /**
 
87
                 * Show a title and highlight widgets on hover. On shift+clicking
 
88
                 * focus the widget control.
 
89
                 */
 
90
                highlightControls: function() {
 
91
                        var self = this,
 
92
                                selector = this.widgetSelectors.join(',');
 
93
 
 
94
                        $(selector).attr( 'title', this.l10n.widgetTooltip );
 
95
 
 
96
                        $(document).on( 'mouseenter', selector, function () {
 
97
                                self.preview.send( 'highlight-widget-control', $( this ).prop( 'id' ) );
 
98
                        });
 
99
 
 
100
                        // Open expand the widget control when shift+clicking the widget element
 
101
                        $(document).on( 'click', selector, function ( e ) {
 
102
                                if ( ! e.shiftKey ) {
 
103
                                        return;
 
104
                                }
 
105
                                e.preventDefault();
 
106
 
 
107
                                self.preview.send( 'focus-widget-control', $( this ).prop( 'id' ) );
 
108
                        });
 
109
                }
 
110
        };
 
111
 
 
112
        /**
 
113
         * Capture the instance of the Preview since it is private
 
114
         */
 
115
        OldPreview = api.Preview;
 
116
        api.Preview = OldPreview.extend( {
 
117
                initialize: function( params, options ) {
 
118
                        api.WidgetCustomizerPreview.preview = this;
 
119
                        OldPreview.prototype.initialize.call( this, params, options );
 
120
                }
 
121
        } );
 
122
 
 
123
        $(function () {
 
124
                var settings = window._wpWidgetCustomizerPreviewSettings;
 
125
                if ( ! settings ) {
 
126
                        return;
 
127
                }
 
128
 
 
129
                $.extend( api.WidgetCustomizerPreview, settings );
 
130
 
 
131
                api.WidgetCustomizerPreview.init();
 
132
        });
 
133
 
 
134
})( window.wp, jQuery );