]> scripts.mit.edu Git - autoinstalls/wordpress.git/blob - wp-includes/js/customize-preview.js
WordPress 4.4-scripts
[autoinstalls/wordpress.git] / wp-includes / js / customize-preview.js
1 /*
2  * Script run inside a Customizer preview frame.
3  */
4 (function( exports, $ ){
5         var api = wp.customize,
6                 debounce;
7
8         /**
9          * Returns a debounced version of the function.
10          *
11          * @todo Require Underscore.js for this file and retire this.
12          */
13         debounce = function( fn, delay, context ) {
14                 var timeout;
15                 return function() {
16                         var args = arguments;
17
18                         context = context || this;
19
20                         clearTimeout( timeout );
21                         timeout = setTimeout( function() {
22                                 timeout = null;
23                                 fn.apply( context, args );
24                         }, delay );
25                 };
26         };
27
28         /**
29          * @constructor
30          * @augments wp.customize.Messenger
31          * @augments wp.customize.Class
32          * @mixes wp.customize.Events
33          */
34         api.Preview = api.Messenger.extend({
35                 /**
36                  * @param {string} url The URL of preview frame
37                  */
38                 initialize: function( params, options ) {
39                         var self = this;
40
41                         api.Messenger.prototype.initialize.call( this, params, options );
42
43                         this.body = $( document.body );
44                         this.body.on( 'click.preview', 'a', function( event ) {
45                                 event.preventDefault();
46                                 self.send( 'scroll', 0 );
47                                 self.send( 'url', $(this).prop('href') );
48                         });
49
50                         // You cannot submit forms.
51                         // @todo: Allow form submissions by mixing $_POST data with the customize setting $_POST data.
52                         this.body.on( 'submit.preview', 'form', function( event ) {
53                                 event.preventDefault();
54                         });
55
56                         this.window = $( window );
57                         this.window.on( 'scroll.preview', debounce( function() {
58                                 self.send( 'scroll', self.window.scrollTop() );
59                         }, 200 ));
60
61                         this.bind( 'scroll', function( distance ) {
62                                 self.window.scrollTop( distance );
63                         });
64                 }
65         });
66
67         $( function() {
68                 api.settings = window._wpCustomizeSettings;
69                 if ( ! api.settings )
70                         return;
71
72                 var bg;
73
74                 api.preview = new api.Preview({
75                         url: window.location.href,
76                         channel: api.settings.channel
77                 });
78
79                 api.preview.bind( 'settings', function( values ) {
80                         $.each( values, function( id, value ) {
81                                 if ( api.has( id ) )
82                                         api( id ).set( value );
83                                 else
84                                         api.create( id, value );
85                         });
86                 });
87
88                 api.preview.trigger( 'settings', api.settings.values );
89
90                 api.preview.bind( 'setting', function( args ) {
91                         var value;
92
93                         args = args.slice();
94
95                         if ( value = api( args.shift() ) )
96                                 value.set.apply( value, args );
97                 });
98
99                 api.preview.bind( 'sync', function( events ) {
100                         $.each( events, function( event, args ) {
101                                 api.preview.trigger( event, args );
102                         });
103                         api.preview.send( 'synced' );
104                 });
105
106                 api.preview.bind( 'active', function() {
107                         if ( api.settings.nonce ) {
108                                 api.preview.send( 'nonce', api.settings.nonce );
109                         }
110
111                         api.preview.send( 'documentTitle', document.title );
112                 });
113
114                 /*
115                  * Send a message to the parent customize frame with a list of which
116                  * containers and controls are active.
117                  */
118                 api.preview.send( 'ready', {
119                         activePanels: api.settings.activePanels,
120                         activeSections: api.settings.activeSections,
121                         activeControls: api.settings.activeControls
122                 } );
123
124                 // Display a loading indicator when preview is reloading, and remove on failure.
125                 api.preview.bind( 'loading-initiated', function () {
126                         $( 'body' ).addClass( 'wp-customizer-unloading' );
127                 });
128                 api.preview.bind( 'loading-failed', function () {
129                         $( 'body' ).removeClass( 'wp-customizer-unloading' );
130                 });
131
132                 /* Custom Backgrounds */
133                 bg = $.map(['color', 'image', 'position_x', 'repeat', 'attachment'], function( prop ) {
134                         return 'background_' + prop;
135                 });
136
137                 api.when.apply( api, bg ).done( function( color, image, position_x, repeat, attachment ) {
138                         var body = $(document.body),
139                                 head = $('head'),
140                                 style = $('#custom-background-css'),
141                                 update;
142
143                         update = function() {
144                                 var css = '';
145
146                                 // The body will support custom backgrounds if either
147                                 // the color or image are set.
148                                 //
149                                 // See get_body_class() in /wp-includes/post-template.php
150                                 body.toggleClass( 'custom-background', !! ( color() || image() ) );
151
152                                 if ( color() )
153                                         css += 'background-color: ' + color() + ';';
154
155                                 if ( image() ) {
156                                         css += 'background-image: url("' + image() + '");';
157                                         css += 'background-position: top ' + position_x() + ';';
158                                         css += 'background-repeat: ' + repeat() + ';';
159                                         css += 'background-attachment: ' + attachment() + ';';
160                                 }
161
162                                 // Refresh the stylesheet by removing and recreating it.
163                                 style.remove();
164                                 style = $('<style type="text/css" id="custom-background-css">body.custom-background { ' + css + ' }</style>').appendTo( head );
165                         };
166
167                         $.each( arguments, function() {
168                                 this.bind( update );
169                         });
170                 });
171
172                 api.trigger( 'preview-ready' );
173         });
174
175 })( wp, jQuery );