1 /* globals _wpCustomizeHeader, _wpMediaViewsL10n */
2 (function( exports, $ ){
3 var api = wp.customize;
7 * - previewer - The Previewer instance to sync with.
8 * - transport - The transport to use for previewing. Supports 'refresh' and 'postMessage'.
10 api.Setting = api.Value.extend({
11 initialize: function( id, value, options ) {
12 api.Value.prototype.initialize.call( this, value, options );
15 this.transport = this.transport || 'refresh';
17 this.bind( this.preview );
20 switch ( this.transport ) {
22 return this.previewer.refresh();
24 return this.previewer.send( 'setting', [ this.id, this() ] );
29 api.Control = api.Class.extend({
30 initialize: function( id, options ) {
32 nodes, radios, settings;
35 $.extend( this, options || {} );
38 this.selector = '#customize-control-' + id.replace( /\]/g, '' ).replace( /\[/g, '-' );
39 this.container = $( this.selector );
41 settings = $.map( this.params.settings, function( value ) {
45 api.apply( api, settings.concat( function() {
48 control.settings = {};
49 for ( key in control.params.settings ) {
50 control.settings[ key ] = api( control.params.settings[ key ] );
53 control.setting = control.settings['default'] || null;
57 control.elements = [];
59 nodes = this.container.find('[data-customize-setting-link]');
62 nodes.each( function() {
66 if ( node.is(':radio') ) {
67 name = node.prop('name');
71 radios[ name ] = true;
72 node = nodes.filter( '[name="' + name + '"]' );
75 api( node.data('customizeSettingLink'), function( setting ) {
76 var element = new api.Element( node );
77 control.elements.push( element );
78 element.sync( setting );
79 element.set( setting() );
86 dropdownInit: function() {
88 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 keydown', '.dropdown', function( event ) {
100 if ( event.type === 'keydown' && 13 !== event.which ) // enter
103 event.preventDefault();
106 control.container.toggleClass('open');
108 if ( control.container.hasClass('open') )
109 control.container.parent().parent().find('li.library-selected').focus();
111 // Don't want to fire focus and click at same time
113 setTimeout(function () {
114 toggleFreeze = false;
118 this.setting.bind( update );
119 update( this.setting() );
123 api.ColorControl = api.Control.extend({
126 picker = this.container.find('.color-picker-hex');
128 picker.val( control.setting() ).wpColorPicker({
130 control.setting.set( picker.wpColorPicker('color') );
133 control.setting.set( false );
139 api.UploadControl = api.Control.extend({
143 this.params.removed = this.params.removed || '';
145 this.success = $.proxy( this.success, this );
147 this.uploader = $.extend({
148 container: this.container,
149 browser: this.container.find('.upload'),
150 dropzone: this.container.find('.upload-dropzone'),
151 success: this.success,
154 }, this.uploader || {} );
156 if ( control.params.extensions ) {
157 control.uploader.plupload.filters = [{
158 title: api.l10n.allowedFiles,
159 extensions: control.params.extensions
163 if ( control.params.context )
164 control.uploader.params['post_data[context]'] = this.params.context;
166 if ( api.settings.theme.stylesheet )
167 control.uploader.params['post_data[theme]'] = api.settings.theme.stylesheet;
169 this.uploader = new wp.Uploader( this.uploader );
171 this.remover = this.container.find('.remove');
172 this.remover.on( 'click keydown', function( event ) {
173 if ( event.type === 'keydown' && 13 !== event.which ) // enter
176 control.setting.set( control.params.removed );
177 event.preventDefault();
180 this.removerVisibility = $.proxy( this.removerVisibility, this );
181 this.setting.bind( this.removerVisibility );
182 this.removerVisibility( this.setting.get() );
184 success: function( attachment ) {
185 this.setting.set( attachment.get('url') );
187 removerVisibility: function( to ) {
188 this.remover.toggle( to != this.params.removed );
192 api.ImageControl = api.UploadControl.extend({
199 var fallback, button;
201 if ( this.supports.dragdrop )
204 // Maintain references while wrapping the fallback button.
205 fallback = control.container.find( '.upload-fallback' );
206 button = fallback.children().detach();
208 this.browser.detach().empty().append( button );
209 fallback.append( this.browser ).show();
213 api.UploadControl.prototype.ready.call( this );
215 this.thumbnail = this.container.find('.preview-thumbnail img');
216 this.thumbnailSrc = $.proxy( this.thumbnailSrc, this );
217 this.setting.bind( this.thumbnailSrc );
219 this.library = this.container.find('.library');
221 // Generate tab objects
223 panels = this.library.find('.library-content');
225 this.library.children('ul').children('li').each( function() {
227 id = link.data('customizeTab'),
228 panel = panels.filter('[data-customize-tab="' + id + '"]');
230 control.tabs[ id ] = {
231 both: link.add( panel ),
237 // Bind tab switch events
238 this.library.children('ul').on( 'click keydown', 'li', function( event ) {
239 if ( event.type === 'keydown' && 13 !== event.which ) // enter
242 var id = $(this).data('customizeTab'),
243 tab = control.tabs[ id ];
245 event.preventDefault();
247 if ( tab.link.hasClass('library-selected') )
250 control.selected.both.removeClass('library-selected');
251 control.selected = tab;
252 control.selected.both.addClass('library-selected');
255 // Bind events to switch image urls.
256 this.library.on( 'click keydown', 'a', function( event ) {
257 if ( event.type === 'keydown' && 13 !== event.which ) // enter
260 var value = $(this).data('customizeImageValue');
263 control.setting.set( value );
264 event.preventDefault();
268 if ( this.tabs.uploaded ) {
269 this.tabs.uploaded.target = this.library.find('.uploaded-target');
270 if ( ! this.tabs.uploaded.panel.find('.thumbnail').length )
271 this.tabs.uploaded.both.addClass('hidden');
275 panels.each( function() {
276 var tab = control.tabs[ $(this).data('customizeTab') ];
278 // Select the first visible tab.
279 if ( ! tab.link.hasClass('hidden') ) {
280 control.selected = tab;
281 tab.both.addClass('library-selected');
288 success: function( attachment ) {
289 api.UploadControl.prototype.success.call( this, attachment );
291 // Add the uploaded image to the uploaded tab.
292 if ( this.tabs.uploaded && this.tabs.uploaded.target.length ) {
293 this.tabs.uploaded.both.removeClass('hidden');
295 // @todo: Do NOT store this on the attachment model. That is bad.
296 attachment.element = $( '<a href="#" class="thumbnail"></a>' )
297 .data( 'customizeImageValue', attachment.get('url') )
298 .append( '<img src="' + attachment.get('url')+ '" />' )
299 .appendTo( this.tabs.uploaded.target );
302 thumbnailSrc: function( to ) {
303 if ( /^(https?:)?\/\//.test( to ) )
304 this.thumbnail.prop( 'src', to ).show();
306 this.thumbnail.hide();
310 api.HeaderControl = api.Control.extend({
312 this.btnRemove = $('.actions .remove');
313 this.btnNew = $('.actions .new');
315 _.bindAll(this, 'openMedia', 'removeImage');
317 this.btnNew.on( 'click', this.openMedia );
318 this.btnRemove.on( 'click', this.removeImage );
320 api.HeaderTool.currentHeader = new api.HeaderTool.ImageModel();
322 new api.HeaderTool.CurrentView({
323 model: api.HeaderTool.currentHeader,
324 el: '.current .container'
327 new api.HeaderTool.ChoiceListView({
328 collection: api.HeaderTool.UploadsList = new api.HeaderTool.ChoiceList(),
329 el: '.choices .uploaded .list'
332 new api.HeaderTool.ChoiceListView({
333 collection: api.HeaderTool.DefaultsList = new api.HeaderTool.DefaultsList(),
334 el: '.choices .default .list'
337 api.HeaderTool.combinedList = api.HeaderTool.CombinedList = new api.HeaderTool.CombinedList([
338 api.HeaderTool.UploadsList,
339 api.HeaderTool.DefaultsList
344 * Returns a set of options, computed from the attached image data and
345 * theme-specific data, to be fed to the imgAreaSelect plugin in
346 * wp.media.view.Cropper.
348 * @param {wp.media.model.Attachment} attachment
349 * @param {wp.media.controller.Cropper} controller
350 * @returns {Object} Options
352 calculateImageSelectOptions: function(attachment, controller) {
353 var xInit = parseInt(_wpCustomizeHeader.data.width, 10),
354 yInit = parseInt(_wpCustomizeHeader.data.height, 10),
355 flexWidth = !! parseInt(_wpCustomizeHeader.data['flex-width'], 10),
356 flexHeight = !! parseInt(_wpCustomizeHeader.data['flex-height'], 10),
357 ratio, xImg, yImg, realHeight, realWidth,
360 realWidth = attachment.get('width');
361 realHeight = attachment.get('height');
363 this.headerImage = new api.HeaderTool.ImageModel();
364 this.headerImage.set({
367 themeFlexWidth: flexWidth,
368 themeFlexHeight: flexHeight,
369 imageWidth: realWidth,
370 imageHeight: realHeight
373 controller.set( 'canSkipCrop', ! this.headerImage.shouldBeCropped() );
375 ratio = xInit / yInit;
379 if ( xImg / yImg > ratio ) {
381 xInit = yInit * ratio;
384 yInit = xInit / ratio;
392 imageWidth: realWidth,
393 imageHeight: realHeight,
400 if (flexHeight === false && flexWidth === false) {
401 imgSelectOptions.aspectRatio = xInit + ':' + yInit;
403 if (flexHeight === false ) {
404 imgSelectOptions.maxHeight = yInit;
406 if (flexWidth === false ) {
407 imgSelectOptions.maxWidth = xInit;
410 return imgSelectOptions;
414 * Sets up and opens the Media Manager in order to select an image.
415 * Depending on both the size of the image and the properties of the
416 * current theme, a cropping step after selection may be required or
419 * @param {event} event
421 openMedia: function(event) {
422 var l10n = _wpMediaViewsL10n;
424 event.preventDefault();
426 this.frame = wp.media({
428 text: l10n.selectAndCrop,
432 new wp.media.controller.Library({
433 title: l10n.chooseImage,
434 library: wp.media.query({ type: 'image' }),
437 suggestedWidth: _wpCustomizeHeader.data.width,
438 suggestedHeight: _wpCustomizeHeader.data.height
440 new wp.media.controller.Cropper({
441 imgSelectOptions: this.calculateImageSelectOptions
446 this.frame.on('select', this.onSelect, this);
447 this.frame.on('cropped', this.onCropped, this);
448 this.frame.on('skippedcrop', this.onSkippedCrop, this);
453 onSelect: function() {
454 this.frame.setState('cropper');
456 onCropped: function(croppedImage) {
457 var url = croppedImage.post_content,
458 attachmentId = croppedImage.attachment_id,
459 w = croppedImage.width,
460 h = croppedImage.height;
461 this.setImageFromURL(url, attachmentId, w, h);
463 onSkippedCrop: function(selection) {
464 var url = selection.get('url'),
465 w = selection.get('width'),
466 h = selection.get('height');
467 this.setImageFromURL(url, selection.id, w, h);
471 * Creates a new wp.customize.HeaderTool.ImageModel from provided
472 * header image data and inserts it into the user-uploaded headers
475 * @param {String} url
476 * @param {Number} attachmentId
477 * @param {Number} width
478 * @param {Number} height
480 setImageFromURL: function(url, attachmentId, width, height) {
481 var choice, data = {};
484 data.thumbnail_url = url;
485 data.timestamp = _.now();
488 data.attachment_id = attachmentId;
496 data.height = height;
499 choice = new api.HeaderTool.ImageModel({
501 choice: url.split('/').pop()
503 api.HeaderTool.UploadsList.add(choice);
504 api.HeaderTool.currentHeader.set(choice.toJSON());
506 choice.importImage();
510 * Triggers the necessary events to deselect an image which was set as
511 * the currently selected one.
513 removeImage: function() {
514 api.HeaderTool.currentHeader.trigger('hide');
515 api.HeaderTool.CombinedList.trigger('control:removeImage');
520 // Change objects contained within the main customize object to Settings.
521 api.defaultConstructor = api.Setting;
523 // Create the collection of Control objects.
524 api.control = new api.Values({ defaultConstructor: api.Control });
526 api.PreviewFrame = api.Messenger.extend({
529 initialize: function( params, options ) {
530 var deferred = $.Deferred();
532 // This is the promise object.
533 deferred.promise( this );
535 this.container = params.container;
536 this.signature = params.signature;
538 $.extend( params, { channel: api.PreviewFrame.uuid() });
540 api.Messenger.prototype.initialize.call( this, params, options );
542 this.add( 'previewUrl', params.previewUrl );
544 this.query = $.extend( params.query || {}, { customize_messenger_channel: this.channel() });
546 this.run( deferred );
549 run: function( deferred ) {
555 this.unbind( 'ready', this._ready );
557 this._ready = function() {
561 deferred.resolveWith( self );
564 this.bind( 'ready', this._ready );
566 this.request = $.ajax( this.previewUrl(), {
570 withCredentials: true
574 this.request.fail( function() {
575 deferred.rejectWith( self, [ 'request failure' ] );
578 this.request.done( function( response ) {
579 var location = self.request.getResponseHeader('Location'),
580 signature = self.signature,
583 // Check if the location response header differs from the current URL.
584 // If so, the request was redirected; try loading the requested page.
585 if ( location && location != self.previewUrl() ) {
586 deferred.rejectWith( self, [ 'redirect', location ] );
590 // Check if the user is not logged in.
591 if ( '0' === response ) {
592 self.login( deferred );
596 // Check for cheaters.
597 if ( '-1' === response ) {
598 deferred.rejectWith( self, [ 'cheatin' ] );
602 // Check for a signature in the request.
603 index = response.lastIndexOf( signature );
604 if ( -1 === index || index < response.lastIndexOf('</html>') ) {
605 deferred.rejectWith( self, [ 'unsigned' ] );
609 // Strip the signature from the request.
610 response = response.slice( 0, index ) + response.slice( index + signature.length );
612 // Create the iframe and inject the html content.
613 self.iframe = $('<iframe />').appendTo( self.container );
615 // Bind load event after the iframe has been added to the page;
616 // otherwise it will fire when injected into the DOM.
617 self.iframe.one( 'load', function() {
621 deferred.resolveWith( self );
623 setTimeout( function() {
624 deferred.rejectWith( self, [ 'ready timeout' ] );
625 }, self.sensitivity );
629 self.targetWindow( self.iframe[0].contentWindow );
631 self.targetWindow().document.open();
632 self.targetWindow().document.write( response );
633 self.targetWindow().document.close();
637 login: function( deferred ) {
641 reject = function() {
642 deferred.rejectWith( self, [ 'logged out' ] );
645 if ( this.triedLogin )
648 // Check if we have an admin cookie.
649 $.get( api.settings.url.ajax, {
651 }).fail( reject ).done( function( response ) {
654 if ( '1' !== response )
657 iframe = $('<iframe src="' + self.previewUrl() + '" />').hide();
658 iframe.appendTo( self.container );
659 iframe.load( function() {
660 self.triedLogin = true;
663 self.run( deferred );
668 destroy: function() {
669 api.Messenger.prototype.destroy.call( this );
670 this.request.abort();
673 this.iframe.remove();
677 delete this.targetWindow;
683 api.PreviewFrame.uuid = function() {
684 return 'preview-' + uuid++;
688 api.Previewer = api.Messenger.extend({
693 * - container - a selector or jQuery element
694 * - previewUrl - the URL of preview frame
696 initialize: function( params, options ) {
700 $.extend( this, options || {} );
703 * Wrap this.refresh to prevent it from hammering the servers:
705 * If refresh is called once and no other refresh requests are
706 * loading, trigger the request immediately.
708 * If refresh is called while another refresh request is loading,
709 * debounce the refresh requests:
710 * 1. Stop the loading request (as it is instantly outdated).
711 * 2. Trigger the new request once refresh hasn't been called for
712 * self.refreshBuffer milliseconds.
714 this.refresh = (function( self ) {
715 var refresh = self.refresh,
716 callback = function() {
718 refresh.call( self );
723 if ( typeof timeout !== 'number' ) {
724 if ( self.loading ) {
731 clearTimeout( timeout );
732 timeout = setTimeout( callback, self.refreshBuffer );
736 this.container = api.ensure( params.container );
737 this.allowedUrls = params.allowedUrls;
738 this.signature = params.signature;
740 params.url = window.location.href;
742 api.Messenger.prototype.initialize.call( this, params );
744 this.add( 'scheme', this.origin() ).link( this.origin ).setter( function( to ) {
745 var match = to.match( rscheme );
746 return match ? match[0] : '';
749 // Limit the URL to internal, front-end links.
751 // If the frontend and the admin are served from the same domain, load the
752 // preview over ssl if the customizer is being loaded over ssl. This avoids
753 // insecure content warnings. This is not attempted if the admin and frontend
754 // are on different domains to avoid the case where the frontend doesn't have
757 this.add( 'previewUrl', params.previewUrl ).setter( function( to ) {
760 // Check for URLs that include "/wp-admin/" or end in "/wp-admin".
761 // Strip hashes and query strings before testing.
762 if ( /\/wp-admin(\/|$)/.test( to.replace( /[#?].*$/, '' ) ) )
765 // Attempt to match the URL to the control frame's scheme
766 // and check if it's allowed. If not, try the original URL.
767 $.each([ to.replace( rscheme, self.scheme() ), to ], function( i, url ) {
768 $.each( self.allowedUrls, function( i, allowed ) {
771 allowed = allowed.replace( /\/+$/, '' );
772 path = url.replace( allowed, '' );
774 if ( 0 === url.indexOf( allowed ) && /^([/#?]|$)/.test( path ) ) {
783 // If we found a matching result, return it. If not, bail.
784 return result ? result : null;
787 // Refresh the preview when the URL is changed (but not yet).
788 this.previewUrl.bind( this.refresh );
791 this.bind( 'scroll', function( distance ) {
792 this.scroll = distance;
795 // Update the URL when the iframe sends a URL message.
796 this.bind( 'url', this.previewUrl );
799 query: function() {},
802 if ( this.loading ) {
803 this.loading.destroy();
808 refresh: function() {
813 this.loading = new api.PreviewFrame({
815 previewUrl: this.previewUrl(),
816 query: this.query() || {},
817 container: this.container,
818 signature: this.signature
821 this.loading.done( function() {
822 // 'this' is the loading frame
823 this.bind( 'synced', function() {
825 self.preview.destroy();
829 self.targetWindow( this.targetWindow() );
830 self.channel( this.channel() );
832 self.send( 'active' );
841 this.loading.fail( function( reason, location ) {
842 if ( 'redirect' === reason && location )
843 self.previewUrl( location );
845 if ( 'logged out' === reason ) {
846 if ( self.preview ) {
847 self.preview.destroy();
851 self.login().done( self.refresh );
854 if ( 'cheatin' === reason )
860 var previewer = this,
861 deferred, messenger, iframe;
866 deferred = $.Deferred();
867 this._login = deferred.promise();
869 messenger = new api.Messenger({
871 url: api.settings.url.login
874 iframe = $('<iframe src="' + api.settings.url.login + '" />').appendTo( this.container );
876 messenger.targetWindow( iframe[0].contentWindow );
878 messenger.bind( 'login', function() {
881 delete previewer._login;
888 cheatin: function() {
889 $( document.body ).empty().addClass('cheatin').append( '<p>' + api.l10n.cheatin + '</p>' );
893 /* =====================================================================
895 * ===================================================================== */
897 api.controlConstructor = {
898 color: api.ColorControl,
899 upload: api.UploadControl,
900 image: api.ImageControl,
901 header: api.HeaderControl
905 api.settings = window._wpCustomizeSettings;
906 api.l10n = window._wpCustomizeControlsL10n;
908 // Check if we can run the customizer.
909 if ( ! api.settings )
912 // Redirect to the fallback preview if any incompatibilities are found.
913 if ( ! $.support.postMessage || ( ! $.support.cors && api.settings.isCrossDomain ) )
914 return window.location = api.settings.url.fallback;
916 var previewer, parent, topFocus,
917 body = $( document.body ),
918 overlay = body.children('.wp-full-overlay');
920 // Prevent the form from saving when enter is pressed on an input or select element.
921 $('#customize-controls').on( 'keydown', function( e ) {
922 var isEnter = ( 13 === e.which ),
925 if ( isEnter && ( $el.is( 'input:not([type=button])' ) || $el.is( 'select' ) ) ) {
930 // Initialize Previewer
931 previewer = new api.Previewer({
932 container: '#customize-preview',
933 form: '#customize-controls',
934 previewUrl: api.settings.url.preview,
935 allowedUrls: api.settings.url.allowed,
936 signature: 'WP_CUSTOMIZER_SIGNATURE'
939 nonce: api.settings.nonce,
944 theme: api.settings.theme.stylesheet,
945 customized: JSON.stringify( api.get() ),
946 nonce: this.nonce.preview
952 query = $.extend( this.query(), {
953 action: 'customize_save',
954 nonce: this.nonce.save
956 processing = api.state( 'processing' ),
957 submitWhenDoneProcessing,
960 body.addClass( 'saving' );
962 submit = function () {
963 var request = $.post( api.settings.url.ajax, query );
965 api.trigger( 'save', request );
967 request.always( function () {
968 body.removeClass( 'saving' );
971 request.done( function( response ) {
972 // Check if the user is logged out.
973 if ( '0' === response ) {
974 self.preview.iframe.hide();
975 self.login().done( function() {
977 self.preview.iframe.show();
982 // Check for cheaters.
983 if ( '-1' === response ) {
988 api.trigger( 'saved' );
992 if ( 0 === processing() ) {
995 submitWhenDoneProcessing = function () {
996 if ( 0 === processing() ) {
997 api.state.unbind( 'change', submitWhenDoneProcessing );
1001 api.state.bind( 'change', submitWhenDoneProcessing );
1007 // Refresh the nonces if the preview sends updated nonces over.
1008 previewer.bind( 'nonce', function( nonce ) {
1009 $.extend( this.nonce, nonce );
1012 $.each( api.settings.settings, function( id, data ) {
1013 api.create( id, id, data.value, {
1014 transport: data.transport,
1015 previewer: previewer
1019 $.each( api.settings.controls, function( id, data ) {
1020 var constructor = api.controlConstructor[ data.type ] || api.Control,
1023 control = api.control.add( id, new constructor( id, {
1025 previewer: previewer
1029 // Check if preview url is valid and load the preview frame.
1030 if ( previewer.previewUrl() )
1031 previewer.refresh();
1033 previewer.previewUrl( api.settings.url.home );
1035 // Save and activated states
1037 var state = new api.Values(),
1038 saved = state.create( 'saved' ),
1039 activated = state.create( 'activated' ),
1040 processing = state.create( 'processing' );
1042 state.bind( 'change', function() {
1043 var save = $('#save'),
1046 if ( ! activated() ) {
1047 save.val( api.l10n.activate ).prop( 'disabled', false );
1048 back.text( api.l10n.cancel );
1050 } else if ( saved() ) {
1051 save.val( api.l10n.saved ).prop( 'disabled', true );
1052 back.text( api.l10n.close );
1055 save.val( api.l10n.save ).prop( 'disabled', false );
1056 back.text( api.l10n.cancel );
1060 // Set default states.
1062 activated( api.settings.theme.active );
1065 api.bind( 'change', function() {
1066 state('saved').set( false );
1069 api.bind( 'saved', function() {
1070 state('saved').set( true );
1071 state('activated').set( true );
1074 activated.bind( function( to ) {
1076 api.trigger( 'activated' );
1079 // Expose states to the API.
1084 $('#save').click( function( event ) {
1086 event.preventDefault();
1087 }).keydown( function( event ) {
1088 if ( 9 === event.which ) // tab
1090 if ( 13 === event.which ) // enter
1092 event.preventDefault();
1095 $('.back').keydown( function( event ) {
1096 if ( 9 === event.which ) // tab
1098 if ( 13 === event.which ) // enter
1100 event.preventDefault();
1103 $('.upload-dropzone a.upload').keydown( function( event ) {
1104 if ( 13 === event.which ) // enter
1108 $('.collapse-sidebar').on( 'click keydown', function( event ) {
1109 if ( event.type === 'keydown' && 13 !== event.which ) // enter
1112 overlay.toggleClass( 'collapsed' ).toggleClass( 'expanded' );
1113 event.preventDefault();
1116 // Create a potential postMessage connection with the parent frame.
1117 parent = new api.Messenger({
1118 url: api.settings.url.parent,
1122 // If we receive a 'back' event, we're inside an iframe.
1123 // Send any clicks to the 'Return' link to the parent page.
1124 parent.bind( 'back', function() {
1125 $('.back').on( 'click.back', function( event ) {
1126 event.preventDefault();
1127 parent.send( 'close' );
1131 // Pass events through to the parent.
1132 api.bind( 'saved', function() {
1133 parent.send( 'saved' );
1136 // When activated, let the loader handle redirecting the page.
1137 // If no loader exists, redirect the page ourselves (if a url exists).
1138 api.bind( 'activated', function() {
1139 if ( parent.targetWindow() )
1140 parent.send( 'activated', api.settings.url.activated );
1141 else if ( api.settings.url.activated )
1142 window.location = api.settings.url.activated;
1145 // Initialize the connection with the parent frame.
1146 parent.send( 'ready' );
1148 // Control visibility for default controls
1150 'background_image': {
1151 controls: [ 'background_repeat', 'background_position_x', 'background_attachment' ],
1152 callback: function( to ) { return !! to; }
1155 controls: [ 'page_on_front', 'page_for_posts' ],
1156 callback: function( to ) { return 'page' === to; }
1158 'header_textcolor': {
1159 controls: [ 'header_textcolor' ],
1160 callback: function( to ) { return 'blank' !== to; }
1162 }, function( settingId, o ) {
1163 api( settingId, function( setting ) {
1164 $.each( o.controls, function( i, controlId ) {
1165 api.control( controlId, function( control ) {
1166 var visibility = function( to ) {
1167 control.container.toggle( o.callback( to ) );
1170 visibility( setting.get() );
1171 setting.bind( visibility );
1177 // Juggle the two controls that use header_textcolor
1178 api.control( 'display_header_text', function( control ) {
1181 control.elements[0].unsync( api( 'header_textcolor' ) );
1183 control.element = new api.Element( control.container.find('input') );
1184 control.element.set( 'blank' !== control.setting() );
1186 control.element.bind( function( to ) {
1188 last = api( 'header_textcolor' ).get();
1190 control.setting.set( to ? last : 'blank' );
1193 control.setting.bind( function( to ) {
1194 control.element.set( 'blank' !== to );
1198 api.trigger( 'ready' );
1200 // Make sure left column gets focus
1201 topFocus = $('.back');
1203 setTimeout(function () {