1 (function( exports, $ ){
2 var api = wp.customize;
6 * - previewer - The Previewer instance to sync with.
7 * - transport - The transport to use for previewing. Supports 'refresh' and 'postMessage'.
9 api.Setting = api.Value.extend({
10 initialize: function( id, value, options ) {
13 api.Value.prototype.initialize.call( this, value, options );
16 this.transport = this.transport || 'refresh';
18 this.bind( this.preview );
21 switch ( this.transport ) {
23 return this.previewer.refresh();
25 return this.previewer.send( 'setting', [ this.id, this() ] );
30 api.Control = api.Class.extend({
31 initialize: function( id, options ) {
33 nodes, radios, settings;
36 $.extend( this, options || {} );
39 this.selector = '#customize-control-' + id.replace( ']', '' ).replace( '[', '-' );
40 this.container = $( this.selector );
42 settings = $.map( this.params.settings, function( value ) {
46 api.apply( api, settings.concat( function() {
49 control.settings = {};
50 for ( key in control.params.settings ) {
51 control.settings[ key ] = api( control.params.settings[ key ] );
54 control.setting = control.settings['default'] || null;
58 control.elements = [];
60 nodes = this.container.find('[data-customize-setting-link]');
63 nodes.each( function() {
67 if ( node.is(':radio') ) {
68 name = node.prop('name');
72 radios[ name ] = true;
73 node = nodes.filter( '[name="' + name + '"]' );
76 api( node.data('customizeSettingLink'), function( setting ) {
77 var element = new api.Element( node );
78 control.elements.push( element );
79 element.sync( setting );
80 element.set( setting() );
87 dropdownInit: function() {
89 statuses = this.container.find('.dropdown-status'),
91 update = function( to ) {
92 if ( typeof to === 'string' && params.statuses && params.statuses[ to ] )
93 statuses.html( params.statuses[ to ] ).show();
98 // Support the .dropdown class to open/close complex elements
99 this.container.on( 'click', '.dropdown', function( event ) {
100 event.preventDefault();
101 control.container.toggleClass('open');
104 this.setting.bind( update );
105 update( this.setting() );
109 api.ColorControl = api.Control.extend({
112 rhex, spot, input, text, update;
114 rhex = /^#([A-Fa-f0-9]{3}){0,2}$/;
115 spot = this.container.find('.dropdown-content');
116 input = new api.Element( this.container.find('.color-picker-hex') );
117 update = function( color ) {
118 spot.css( 'background', color );
119 control.farbtastic.setColor( color );
122 this.farbtastic = $.farbtastic( this.container.find('.farbtastic-placeholder'), control.setting.set );
124 // Only pass through values that are valid hexes/empty.
125 input.sync( this.setting ).validate = function( to ) {
126 return rhex.test( to ) ? to : null;
129 this.setting.bind( update );
130 update( this.setting() );
136 api.UploadControl = api.Control.extend({
140 this.params.removed = this.params.removed || '';
142 this.success = $.proxy( this.success, this );
144 this.uploader = $.extend({
145 container: this.container,
146 browser: this.container.find('.upload'),
147 dropzone: this.container.find('.upload-dropzone'),
148 success: this.success
149 }, this.uploader || {} );
151 if ( this.uploader.supported ) {
152 if ( control.params.context )
153 control.uploader.param( 'post_data[context]', this.params.context );
155 control.uploader.param( 'post_data[theme]', api.settings.theme.stylesheet );
158 this.uploader = new wp.Uploader( this.uploader );
160 this.remover = this.container.find('.remove');
161 this.remover.click( function( event ) {
162 control.setting.set( control.params.removed );
163 event.preventDefault();
166 this.removerVisibility = $.proxy( this.removerVisibility, this );
167 this.setting.bind( this.removerVisibility );
168 this.removerVisibility( this.setting.get() );
170 success: function( attachment ) {
171 this.setting.set( attachment.url );
173 removerVisibility: function( to ) {
174 this.remover.toggle( to != this.params.removed );
178 api.ImageControl = api.UploadControl.extend({
184 init: function( up ) {
185 var fallback, button;
187 if ( this.supports.dragdrop )
190 // Maintain references while wrapping the fallback button.
191 fallback = control.container.find( '.upload-fallback' );
192 button = fallback.children().detach();
194 this.browser.detach().empty().append( button );
195 fallback.append( this.browser ).show();
199 api.UploadControl.prototype.ready.call( this );
201 this.thumbnail = this.container.find('.preview-thumbnail img');
202 this.thumbnailSrc = $.proxy( this.thumbnailSrc, this );
203 this.setting.bind( this.thumbnailSrc );
205 this.library = this.container.find('.library');
207 // Generate tab objects
209 panels = this.library.find('.library-content');
211 this.library.children('ul').children('li').each( function() {
213 id = link.data('customizeTab'),
214 panel = panels.filter('[data-customize-tab="' + id + '"]');
216 control.tabs[ id ] = {
217 both: link.add( panel ),
224 this.selected = this.tabs[ panels.first().data('customizeTab') ];
225 this.selected.both.addClass('library-selected');
227 // Bind tab switch events
228 this.library.children('ul').on( 'click', 'li', function( event ) {
229 var id = $(this).data('customizeTab'),
230 tab = control.tabs[ id ];
232 event.preventDefault();
234 if ( tab.link.hasClass('library-selected') )
237 control.selected.both.removeClass('library-selected');
238 control.selected = tab;
239 control.selected.both.addClass('library-selected');
242 // Bind events to switch image urls.
243 this.library.on( 'click', 'a', function( event ) {
244 var value = $(this).data('customizeImageValue');
247 control.setting.set( value );
248 event.preventDefault();
252 if ( this.tabs.uploaded ) {
253 this.tabs.uploaded.target = this.library.find('.uploaded-target');
254 if ( ! this.tabs.uploaded.panel.find('.thumbnail').length )
255 this.tabs.uploaded.both.addClass('hidden');
260 success: function( attachment ) {
261 api.UploadControl.prototype.success.call( this, attachment );
263 // Add the uploaded image to the uploaded tab.
264 if ( this.tabs.uploaded && this.tabs.uploaded.target.length ) {
265 this.tabs.uploaded.both.removeClass('hidden');
267 attachment.element = $( '<a href="#" class="thumbnail"></a>' )
268 .data( 'customizeImageValue', attachment.url )
269 .append( '<img src="' + attachment.url+ '" />' )
270 .appendTo( this.tabs.uploaded.target );
273 thumbnailSrc: function( to ) {
274 if ( /^(https?:)?\/\//.test( to ) )
275 this.thumbnail.prop( 'src', to ).show();
277 this.thumbnail.hide();
281 // Change objects contained within the main customize object to Settings.
282 api.defaultConstructor = api.Setting;
284 // Create the collection of Control objects.
285 api.control = new api.Values({ defaultConstructor: api.Control });
287 api.PreviewFrame = api.Messenger.extend({
290 initialize: function( params, options ) {
291 var deferred = $.Deferred(),
294 // This is the promise object.
295 deferred.promise( this );
297 this.previewer = params.previewer;
299 $.extend( params, { channel: api.PreviewFrame.uuid() });
301 api.Messenger.prototype.initialize.call( this, params, options );
303 this.add( 'previewUrl', params.previewUrl );
305 this.query = $.extend( params.query || {}, { customize_messenger_channel: this.channel() });
307 this.run( deferred );
310 run: function( deferred ) {
316 this.unbind( 'ready', this._ready );
318 this._ready = function() {
322 deferred.resolveWith( self );
325 this.bind( 'ready', this._ready );
327 this.request = $.ajax( this.previewUrl(), {
331 withCredentials: true
335 this.request.fail( function() {
336 deferred.rejectWith( self, [ 'request failure' ] );
339 this.request.done( function( response ) {
340 var location = self.request.getResponseHeader('Location'),
341 signature = 'WP_CUSTOMIZER_SIGNATURE',
344 // Check if the location response header differs from the current URL.
345 // If so, the request was redirected; try loading the requested page.
346 if ( location && location != self.previewUrl() ) {
347 deferred.rejectWith( self, [ 'redirect', location ] );
351 // Check if the user is not logged in.
352 if ( '0' === response ) {
353 self.login( deferred );
357 // Check for cheaters.
358 if ( '-1' === response ) {
359 deferred.rejectWith( self, [ 'cheatin' ] );
363 // Check for a signature in the request.
364 index = response.lastIndexOf( signature );
365 if ( -1 === index || index < response.lastIndexOf('</html>') ) {
366 deferred.rejectWith( self, [ 'unsigned' ] );
370 // Strip the signature from the request.
371 response = response.slice( 0, index ) + response.slice( index + signature.length );
373 // Create the iframe and inject the html content.
374 self.iframe = $('<iframe />').appendTo( self.previewer.container );
376 // Bind load event after the iframe has been added to the page;
377 // otherwise it will fire when injected into the DOM.
378 self.iframe.one( 'load', function() {
382 deferred.resolveWith( self );
384 setTimeout( function() {
385 deferred.rejectWith( self, [ 'ready timeout' ] );
386 }, self.sensitivity );
390 self.targetWindow( self.iframe[0].contentWindow );
392 self.targetWindow().document.open();
393 self.targetWindow().document.write( response );
394 self.targetWindow().document.close();
398 login: function( deferred ) {
402 reject = function() {
403 deferred.rejectWith( self, [ 'logged out' ] );
406 if ( this.triedLogin )
409 // Check if we have an admin cookie.
410 $.get( api.settings.url.ajax, {
412 }).fail( reject ).done( function( response ) {
415 if ( '1' !== response )
418 iframe = $('<iframe src="' + self.previewUrl() + '" />').hide();
419 iframe.appendTo( self.previewer.container );
420 iframe.load( function() {
421 self.triedLogin = true;
424 self.run( deferred );
429 destroy: function() {
430 api.Messenger.prototype.destroy.call( this );
431 this.request.abort();
434 this.iframe.remove();
438 delete this.targetWindow;
444 api.PreviewFrame.uuid = function() {
445 return 'preview-' + uuid++;
449 api.Previewer = api.Messenger.extend({
454 * - container - a selector or jQuery element
455 * - previewUrl - the URL of preview frame
457 initialize: function( params, options ) {
462 $.extend( this, options || {} );
465 * Wrap this.refresh to prevent it from hammering the servers:
467 * If refresh is called once and no other refresh requests are
468 * loading, trigger the request immediately.
470 * If refresh is called while another refresh request is loading,
471 * debounce the refresh requests:
472 * 1. Stop the loading request (as it is instantly outdated).
473 * 2. Trigger the new request once refresh hasn't been called for
474 * self.refreshBuffer milliseconds.
476 this.refresh = (function( self ) {
477 var refresh = self.refresh,
478 callback = function() {
480 refresh.call( self );
485 if ( typeof timeout !== 'number' ) {
486 if ( self.loading ) {
493 clearTimeout( timeout );
494 timeout = setTimeout( callback, self.refreshBuffer );
498 this.container = api.ensure( params.container );
499 this.allowedUrls = params.allowedUrls;
501 params.url = window.location.href;
503 api.Messenger.prototype.initialize.call( this, params );
505 this.add( 'scheme', this.origin() ).link( this.origin ).setter( function( to ) {
506 var match = to.match( rscheme );
507 return match ? match[0] : '';
510 // Limit the URL to internal, front-end links.
512 // If the frontend and the admin are served from the same domain, load the
513 // preview over ssl if the customizer is being loaded over ssl. This avoids
514 // insecure content warnings. This is not attempted if the admin and frontend
515 // are on different domains to avoid the case where the frontend doesn't have
518 this.add( 'previewUrl', params.previewUrl ).setter( function( to ) {
521 // Check for URLs that include "/wp-admin/" or end in "/wp-admin".
522 // Strip hashes and query strings before testing.
523 if ( /\/wp-admin(\/|$)/.test( to.replace(/[#?].*$/, '') ) )
526 // Attempt to match the URL to the control frame's scheme
527 // and check if it's allowed. If not, try the original URL.
528 $.each([ to.replace( rscheme, self.scheme() ), to ], function( i, url ) {
529 $.each( self.allowedUrls, function( i, allowed ) {
530 if ( 0 === url.indexOf( allowed ) ) {
539 // If we found a matching result, return it. If not, bail.
540 return result ? result : null;
543 // Refresh the preview when the URL is changed (but not yet).
544 this.previewUrl.bind( this.refresh );
547 this.bind( 'scroll', function( distance ) {
548 this.scroll = distance;
551 // Update the URL when the iframe sends a URL message.
552 this.bind( 'url', this.previewUrl );
555 query: function() {},
558 if ( this.loading ) {
559 this.loading.destroy();
564 refresh: function() {
569 this.loading = new api.PreviewFrame({
571 previewUrl: this.previewUrl(),
572 query: this.query() || {},
576 this.loading.done( function() {
577 // 'this' is the loading frame
578 this.bind( 'synced', function() {
580 self.preview.destroy();
584 self.targetWindow( this.targetWindow() );
585 self.channel( this.channel() );
594 this.loading.fail( function( reason, location ) {
595 if ( 'redirect' === reason && location )
596 self.previewUrl( location );
598 if ( 'logged out' === reason ) {
599 if ( self.preview ) {
600 self.preview.destroy();
604 self.login().done( self.refresh );
607 if ( 'cheatin' === reason )
613 var previewer = this,
614 deferred, messenger, iframe;
619 deferred = $.Deferred();
620 this._login = deferred.promise();
622 messenger = new api.Messenger({
624 url: api.settings.url.login
627 iframe = $('<iframe src="' + api.settings.url.login + '" />').appendTo( this.container );
629 messenger.targetWindow( iframe[0].contentWindow );
631 messenger.bind( 'login', function() {
634 delete previewer._login;
641 cheatin: function() {
642 $( document.body ).empty().addClass('cheatin').append( '<p>' + api.l10n.cheatin + '</p>' );
646 /* =====================================================================
648 * ===================================================================== */
650 api.controlConstructor = {
651 color: api.ColorControl,
652 upload: api.UploadControl,
653 image: api.ImageControl
657 api.settings = window._wpCustomizeSettings;
658 api.l10n = window._wpCustomizeControlsL10n;
660 // Check if we can run the customizer.
661 if ( ! api.settings )
664 // Redirect to the fallback preview if any incompatibilities are found.
665 if ( ! $.support.postMessage || ( ! $.support.cors && api.settings.isCrossDomain ) )
666 return window.location = api.settings.url.fallback;
668 var body = $( document.body ),
669 overlay = body.children('.wp-full-overlay'),
670 query, previewer, parent;
672 // Prevent the form from saving when enter is pressed.
673 $('#customize-controls').on( 'keydown', function( e ) {
674 if ( $( e.target ).is('textarea') )
677 if ( 13 === e.which ) // Enter
681 // Initialize Previewer
682 previewer = new api.Previewer({
683 container: '#customize-preview',
684 form: '#customize-controls',
685 previewUrl: api.settings.url.preview,
686 allowedUrls: api.settings.url.allowed
691 theme: api.settings.theme.stylesheet,
692 customized: JSON.stringify( api.get() )
696 nonce: $('#_wpnonce').val(),
700 query = $.extend( this.query(), {
701 action: 'customize_save',
704 request = $.post( api.settings.url.ajax, query );
706 api.trigger( 'save', request );
708 body.addClass('saving');
710 request.always( function() {
711 body.removeClass('saving');
714 request.done( function( response ) {
715 // Check if the user is logged out.
716 if ( '0' === response ) {
717 self.preview.iframe.hide();
718 self.login().done( function() {
720 self.preview.iframe.show();
725 // Check for cheaters.
726 if ( '-1' === response ) {
731 api.trigger( 'saved' );
736 $.each( api.settings.settings, function( id, data ) {
737 api.create( id, id, data.value, {
738 transport: data.transport,
743 $.each( api.settings.controls, function( id, data ) {
744 var constructor = api.controlConstructor[ data.type ] || api.Control,
747 control = api.control.add( id, new constructor( id, {
753 // Check if preview url is valid and load the preview frame.
754 if ( previewer.previewUrl() )
757 previewer.previewUrl( api.settings.url.home );
759 // Save and activated states
761 var state = new api.Values(),
762 saved = state.create('saved'),
763 activated = state.create('activated');
765 state.bind( 'change', function() {
766 var save = $('#save'),
769 if ( ! activated() ) {
770 save.val( api.l10n.activate ).prop( 'disabled', false );
771 back.text( api.l10n.cancel );
773 } else if ( saved() ) {
774 save.val( api.l10n.saved ).prop( 'disabled', true );
775 back.text( api.l10n.close );
778 save.val( api.l10n.save ).prop( 'disabled', false );
779 back.text( api.l10n.cancel );
783 // Set default states.
785 activated( api.settings.theme.active );
787 api.bind( 'change', function() {
788 state('saved').set( false );
791 api.bind( 'saved', function() {
792 state('saved').set( true );
793 state('activated').set( true );
796 activated.bind( function( to ) {
798 api.trigger( 'activated' );
801 // Expose states to the API.
805 // Temporary accordion code.
806 $('.customize-section-title').click( function( event ) {
807 var clicked = $( this ).parents( '.customize-section' );
809 if ( clicked.hasClass('cannot-expand') )
812 $( '.customize-section' ).not( clicked ).removeClass( 'open' );
813 clicked.toggleClass( 'open' );
814 event.preventDefault();
818 $('#save').click( function( event ) {
820 event.preventDefault();
823 $('.collapse-sidebar').click( function( event ) {
824 overlay.toggleClass( 'collapsed' ).toggleClass( 'expanded' );
825 event.preventDefault();
828 // Create a potential postMessage connection with the parent frame.
829 parent = new api.Messenger({
830 url: api.settings.url.parent,
834 // If we receive a 'back' event, we're inside an iframe.
835 // Send any clicks to the 'Return' link to the parent page.
836 parent.bind( 'back', function() {
837 $('.back').on( 'click.back', function( event ) {
838 event.preventDefault();
839 parent.send( 'close' );
843 // Pass events through to the parent.
844 api.bind( 'saved', function() {
845 parent.send( 'saved' );
848 // When activated, let the loader handle redirecting the page.
849 // If no loader exists, redirect the page ourselves (if a url exists).
850 api.bind( 'activated', function() {
851 if ( parent.targetWindow() )
852 parent.send( 'activated', api.settings.url.activated );
853 else if ( api.settings.url.activated )
854 window.location = api.settings.url.activated;
857 // Initialize the connection with the parent frame.
858 parent.send( 'ready' );
860 // Control visibility for default controls
862 'background_image': {
863 controls: [ 'background_repeat', 'background_position_x', 'background_attachment' ],
864 callback: function( to ) { return !! to }
867 controls: [ 'page_on_front', 'page_for_posts' ],
868 callback: function( to ) { return 'page' === to }
870 'header_textcolor': {
871 controls: [ 'header_textcolor' ],
872 callback: function( to ) { return 'blank' !== to }
874 }, function( settingId, o ) {
875 api( settingId, function( setting ) {
876 $.each( o.controls, function( i, controlId ) {
877 api.control( controlId, function( control ) {
878 var visibility = function( to ) {
879 control.container.toggle( o.callback( to ) );
882 visibility( setting.get() );
883 setting.bind( visibility );
889 // Juggle the two controls that use header_textcolor
890 api.control( 'display_header_text', function( control ) {
893 control.elements[0].unsync( api( 'header_textcolor' ) );
895 control.element = new api.Element( control.container.find('input') );
896 control.element.set( 'blank' !== control.setting() );
898 control.element.bind( function( to ) {
900 last = api( 'header_textcolor' ).get();
902 control.setting.set( to ? last : 'blank' );
905 control.setting.bind( function( to ) {
906 control.element.set( 'blank' !== to );
910 // Handle header image data
911 api.control( 'header_image', function( control ) {
912 control.setting.bind( function( to ) {
913 if ( to === control.params.removed )
914 control.settings.data.set( false );
917 control.library.on( 'click', 'a', function( event ) {
918 control.settings.data.set( $(this).data('customizeHeaderImageData') );
921 control.uploader.success = function( attachment ) {
924 api.ImageControl.prototype.success.call( control, attachment );
927 attachment_id: attachment.id,
929 thumbnail_url: attachment.url,
930 height: attachment.meta.height,
931 width: attachment.meta.width
934 attachment.element.data( 'customizeHeaderImageData', data );
935 control.settings.data.set( data );
939 api.trigger( 'ready' );