X-Git-Url: https://scripts.mit.edu/gitweb/autoinstalls/wordpress.git/blobdiff_plain/0461a5f2e55c8d5f1fde96ca2e83117152573c7d..refs/tags/wordpress-4.0:/wp-admin/js/customize-controls.js diff --git a/wp-admin/js/customize-controls.js b/wp-admin/js/customize-controls.js index 4bfc252a..85b171db 100644 --- a/wp-admin/js/customize-controls.js +++ b/wp-admin/js/customize-controls.js @@ -3,6 +3,10 @@ var api = wp.customize; /** + * @constructor + * @augments wp.customize.Value + * @augments wp.customize.Class + * * @param options * - previewer - The Previewer instance to sync with. * - transport - The transport to use for previewing. Supports 'refresh' and 'postMessage'. @@ -26,6 +30,10 @@ } }); + /** + * @constructor + * @augments wp.customize.Class + */ api.Control = api.Class.extend({ initialize: function( id, options ) { var control = this, @@ -37,6 +45,7 @@ this.id = id; this.selector = '#customize-control-' + id.replace( /\]/g, '' ).replace( /\[/g, '-' ); this.container = $( this.selector ); + this.active = new api.Value( this.params.active ); settings = $.map( this.params.settings, function( value ) { return value; @@ -79,10 +88,33 @@ element.set( setting() ); }); }); + + control.active.bind( function ( active ) { + control.toggle( active ); + } ); + control.toggle( control.active() ); }, + /** + * @abstract + */ ready: function() {}, + /** + * Callback for change to the control's active state. + * + * Override function for custom behavior for the control being active/inactive. + * + * @param {Boolean} active + */ + toggle: function ( active ) { + if ( active ) { + this.container.slideDown(); + } else { + this.container.slideUp(); + } + }, + dropdownInit: function() { var control = this, statuses = this.container.find('.dropdown-status'), @@ -120,6 +152,11 @@ } }); + /** + * @constructor + * @augments wp.customize.Control + * @augments wp.customize.Class + */ api.ColorControl = api.Control.extend({ ready: function() { var control = this, @@ -136,6 +173,11 @@ } }); + /** + * @constructor + * @augments wp.customize.Control + * @augments wp.customize.Class + */ api.UploadControl = api.Control.extend({ ready: function() { var control = this; @@ -189,6 +231,12 @@ } }); + /** + * @constructor + * @augments wp.customize.UploadControl + * @augments wp.customize.Control + * @augments wp.customize.Class + */ api.ImageControl = api.UploadControl.extend({ ready: function() { var control = this, @@ -307,6 +355,11 @@ } }); + /** + * @constructor + * @augments wp.customize.Control + * @augments wp.customize.Class + */ api.HeaderControl = api.Control.extend({ ready: function() { this.btnRemove = $('#customize-control-header_image .actions .remove'); @@ -523,6 +576,12 @@ // Create the collection of Control objects. api.control = new api.Values({ defaultConstructor: api.Control }); + /** + * @constructor + * @augments wp.customize.Messenger + * @augments wp.customize.Class + * @mixes wp.customize.Events + */ api.PreviewFrame = api.Messenger.extend({ sensitivity: 2000, @@ -563,6 +622,19 @@ this.bind( 'ready', this._ready ); + this.bind( 'ready', function ( data ) { + if ( ! data || ! data.activeControls ) { + return; + } + + $.each( data.activeControls, function ( id, active ) { + var control = api.control( id ); + if ( control ) { + control.active( active ); + } + } ); + } ); + this.request = $.ajax( this.previewUrl(), { type: 'POST', data: this.query, @@ -680,11 +752,22 @@ (function(){ var uuid = 0; + /** + * Create a universally unique identifier. + * + * @return {int} + */ api.PreviewFrame.uuid = function() { return 'preview-' + uuid++; }; }()); + /** + * @constructor + * @augments wp.customize.Messenger + * @augments wp.customize.Class + * @mixes wp.customize.Events + */ api.Previewer = api.Messenger.extend({ refreshBuffer: 250, @@ -890,10 +973,6 @@ } }); - /* ===================================================================== - * Ready. - * ===================================================================== */ - api.controlConstructor = { color: api.ColorControl, upload: api.UploadControl, @@ -913,9 +992,12 @@ if ( ! $.support.postMessage || ( ! $.support.cors && api.settings.isCrossDomain ) ) return window.location = api.settings.url.fallback; - var previewer, parent, topFocus, + var parent, topFocus, body = $( document.body ), - overlay = body.children('.wp-full-overlay'); + overlay = body.children( '.wp-full-overlay' ), + title = $( '#customize-info .theme-name.site-title' ), + closeBtn = $( '.customize-controls-close' ), + saveBtn = $( '#save' ); // Prevent the form from saving when enter is pressed on an input or select element. $('#customize-controls').on( 'keydown', function( e ) { @@ -928,7 +1010,7 @@ }); // Initialize Previewer - previewer = new api.Previewer({ + api.previewer = new api.Previewer({ container: '#customize-preview', form: '#customize-controls', previewUrl: api.settings.url.preview, @@ -1005,14 +1087,14 @@ }); // Refresh the nonces if the preview sends updated nonces over. - previewer.bind( 'nonce', function( nonce ) { + api.previewer.bind( 'nonce', function( nonce ) { $.extend( this.nonce, nonce ); }); $.each( api.settings.settings, function( id, data ) { api.create( id, id, data.value, { transport: data.transport, - previewer: previewer + previewer: api.previewer } ); }); @@ -1022,15 +1104,16 @@ control = api.control.add( id, new constructor( id, { params: data, - previewer: previewer + previewer: api.previewer } ) ); }); // Check if preview url is valid and load the preview frame. - if ( previewer.previewUrl() ) - previewer.refresh(); - else - previewer.previewUrl( api.settings.url.home ); + if ( api.previewer.previewUrl() ) { + api.previewer.refresh(); + } else { + api.previewer.previewUrl( api.settings.url.home ); + } // Save and activated states (function() { @@ -1040,20 +1123,17 @@ processing = state.create( 'processing' ); state.bind( 'change', function() { - var save = $('#save'), - back = $('.back'); - if ( ! activated() ) { - save.val( api.l10n.activate ).prop( 'disabled', false ); - back.text( api.l10n.cancel ); + saveBtn.val( api.l10n.activate ).prop( 'disabled', false ); + closeBtn.find( '.screen-reader-text' ).text( api.l10n.cancel ); } else if ( saved() ) { - save.val( api.l10n.saved ).prop( 'disabled', true ); - back.text( api.l10n.close ); + saveBtn.val( api.l10n.saved ).prop( 'disabled', true ); + closeBtn.find( '.screen-reader-text' ).text( api.l10n.close ); } else { - save.val( api.l10n.save ).prop( 'disabled', false ); - back.text( api.l10n.cancel ); + saveBtn.val( api.l10n.save ).prop( 'disabled', false ); + closeBtn.find( '.screen-reader-text' ).text( api.l10n.cancel ); } }); @@ -1081,18 +1161,18 @@ }()); // Button bindings. - $('#save').click( function( event ) { - previewer.save(); + saveBtn.click( function( event ) { + api.previewer.save(); event.preventDefault(); }).keydown( function( event ) { if ( 9 === event.which ) // tab return; if ( 13 === event.which ) // enter - previewer.save(); + api.previewer.save(); event.preventDefault(); }); - $('.back').keydown( function( event ) { + closeBtn.keydown( function( event ) { if ( 9 === event.which ) // tab return; if ( 13 === event.which ) // enter @@ -1113,6 +1193,13 @@ event.preventDefault(); }); + // Bind site title display to the corresponding field. + if ( title.length ) { + $( '#customize-control-blogname input' ).on( 'input', function() { + title.text( this.value ); + } ); + } + // Create a potential postMessage connection with the parent frame. parent = new api.Messenger({ url: api.settings.url.parent, @@ -1122,16 +1209,25 @@ // If we receive a 'back' event, we're inside an iframe. // Send any clicks to the 'Return' link to the parent page. parent.bind( 'back', function() { - $('.back').on( 'click.back', function( event ) { + closeBtn.on( 'click.customize-controls-close', function( event ) { event.preventDefault(); parent.send( 'close' ); }); }); + // Prompt user with AYS dialog if leaving the Customizer with unsaved changes + $( window ).on( 'beforeunload', function () { + if ( ! api.state( 'saved' )() ) { + return api.l10n.saveAlert; + } + } ); + // Pass events through to the parent. - api.bind( 'saved', function() { - parent.send( 'saved' ); - }); + $.each( [ 'saved', 'change' ], function ( i, event ) { + api.bind( event, function() { + parent.send( event ); + }); + } ); // When activated, let the loader handle redirecting the page. // If no loader exists, redirect the page ourselves (if a url exists). @@ -1198,7 +1294,7 @@ api.trigger( 'ready' ); // Make sure left column gets focus - topFocus = $('.back'); + topFocus = closeBtn; topFocus.focus(); setTimeout(function () { topFocus.focus();