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