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