1
/* globals _wpCustomizeHeader, _wpMediaViewsL10n */
2
(function( exports, $ ){
3
var api = wp.customize;
7
* @augments wp.customize.Value
8
* @augments wp.customize.Class
11
* - previewer - The Previewer instance to sync with.
12
* - transport - The transport to use for previewing. Supports 'refresh' and 'postMessage'.
14
api.Setting = api.Value.extend({
15
initialize: function( id, value, options ) {
16
api.Value.prototype.initialize.call( this, value, options );
19
this.transport = this.transport || 'refresh';
21
this.bind( this.preview );
24
switch ( this.transport ) {
26
return this.previewer.refresh();
28
return this.previewer.send( 'setting', [ this.id, this() ] );
35
* @augments wp.customize.Class
37
api.Control = api.Class.extend({
38
initialize: function( id, options ) {
40
nodes, radios, settings;
43
$.extend( this, options || {} );
46
this.selector = '#customize-control-' + id.replace( /\]/g, '' ).replace( /\[/g, '-' );
47
this.container = $( this.selector );
48
this.active = new api.Value( this.params.active );
50
settings = $.map( this.params.settings, function( value ) {
54
api.apply( api, settings.concat( function() {
57
control.settings = {};
58
for ( key in control.params.settings ) {
59
control.settings[ key ] = api( control.params.settings[ key ] );
62
control.setting = control.settings['default'] || null;
66
control.elements = [];
68
nodes = this.container.find('[data-customize-setting-link]');
71
nodes.each( function() {
75
if ( node.is(':radio') ) {
76
name = node.prop('name');
80
radios[ name ] = true;
81
node = nodes.filter( '[name="' + name + '"]' );
84
api( node.data('customizeSettingLink'), function( setting ) {
85
var element = new api.Element( node );
86
control.elements.push( element );
87
element.sync( setting );
88
element.set( setting() );
92
control.active.bind( function ( active ) {
93
control.toggle( active );
95
control.toggle( control.active() );
101
ready: function() {},
104
* Callback for change to the control's active state.
106
* Override function for custom behavior for the control being active/inactive.
108
* @param {Boolean} active
110
toggle: function ( active ) {
112
this.container.slideDown();
114
this.container.slideUp();
118
dropdownInit: function() {
120
statuses = this.container.find('.dropdown-status'),
121
params = this.params,
122
toggleFreeze = false,
123
update = function( to ) {
124
if ( typeof to === 'string' && params.statuses && params.statuses[ to ] )
125
statuses.html( params.statuses[ to ] ).show();
130
// Support the .dropdown class to open/close complex elements
131
this.container.on( 'click keydown', '.dropdown', function( event ) {
132
if ( event.type === 'keydown' && 13 !== event.which ) // enter
135
event.preventDefault();
138
control.container.toggleClass('open');
140
if ( control.container.hasClass('open') )
141
control.container.parent().parent().find('li.library-selected').focus();
143
// Don't want to fire focus and click at same time
145
setTimeout(function () {
146
toggleFreeze = false;
150
this.setting.bind( update );
151
update( this.setting() );
157
* @augments wp.customize.Control
158
* @augments wp.customize.Class
160
api.ColorControl = api.Control.extend({
163
picker = this.container.find('.color-picker-hex');
165
picker.val( control.setting() ).wpColorPicker({
167
control.setting.set( picker.wpColorPicker('color') );
170
control.setting.set( false );
178
* @augments wp.customize.Control
179
* @augments wp.customize.Class
181
api.UploadControl = api.Control.extend({
185
this.params.removed = this.params.removed || '';
187
this.success = $.proxy( this.success, this );
189
this.uploader = $.extend({
190
container: this.container,
191
browser: this.container.find('.upload'),
192
dropzone: this.container.find('.upload-dropzone'),
193
success: this.success,
196
}, this.uploader || {} );
198
if ( control.params.extensions ) {
199
control.uploader.plupload.filters = [{
200
title: api.l10n.allowedFiles,
201
extensions: control.params.extensions
205
if ( control.params.context )
206
control.uploader.params['post_data[context]'] = this.params.context;
208
if ( api.settings.theme.stylesheet )
209
control.uploader.params['post_data[theme]'] = api.settings.theme.stylesheet;
211
this.uploader = new wp.Uploader( this.uploader );
213
this.remover = this.container.find('.remove');
214
this.remover.on( 'click keydown', function( event ) {
215
if ( event.type === 'keydown' && 13 !== event.which ) // enter
218
control.setting.set( control.params.removed );
219
event.preventDefault();
222
this.removerVisibility = $.proxy( this.removerVisibility, this );
223
this.setting.bind( this.removerVisibility );
224
this.removerVisibility( this.setting.get() );
226
success: function( attachment ) {
227
this.setting.set( attachment.get('url') );
229
removerVisibility: function( to ) {
230
this.remover.toggle( to != this.params.removed );
236
* @augments wp.customize.UploadControl
237
* @augments wp.customize.Control
238
* @augments wp.customize.Class
240
api.ImageControl = api.UploadControl.extend({
247
var fallback, button;
249
if ( this.supports.dragdrop )
252
// Maintain references while wrapping the fallback button.
253
fallback = control.container.find( '.upload-fallback' );
254
button = fallback.children().detach();
256
this.browser.detach().empty().append( button );
257
fallback.append( this.browser ).show();
261
api.UploadControl.prototype.ready.call( this );
263
this.thumbnail = this.container.find('.preview-thumbnail img');
264
this.thumbnailSrc = $.proxy( this.thumbnailSrc, this );
265
this.setting.bind( this.thumbnailSrc );
267
this.library = this.container.find('.library');
269
// Generate tab objects
271
panels = this.library.find('.library-content');
273
this.library.children('ul').children('li').each( function() {
275
id = link.data('customizeTab'),
276
panel = panels.filter('[data-customize-tab="' + id + '"]');
278
control.tabs[ id ] = {
279
both: link.add( panel ),
285
// Bind tab switch events
286
this.library.children('ul').on( 'click keydown', 'li', function( event ) {
287
if ( event.type === 'keydown' && 13 !== event.which ) // enter
290
var id = $(this).data('customizeTab'),
291
tab = control.tabs[ id ];
293
event.preventDefault();
295
if ( tab.link.hasClass('library-selected') )
298
control.selected.both.removeClass('library-selected');
299
control.selected = tab;
300
control.selected.both.addClass('library-selected');
303
// Bind events to switch image urls.
304
this.library.on( 'click keydown', 'a', function( event ) {
305
if ( event.type === 'keydown' && 13 !== event.which ) // enter
308
var value = $(this).data('customizeImageValue');
311
control.setting.set( value );
312
event.preventDefault();
316
if ( this.tabs.uploaded ) {
317
this.tabs.uploaded.target = this.library.find('.uploaded-target');
318
if ( ! this.tabs.uploaded.panel.find('.thumbnail').length )
319
this.tabs.uploaded.both.addClass('hidden');
323
panels.each( function() {
324
var tab = control.tabs[ $(this).data('customizeTab') ];
326
// Select the first visible tab.
327
if ( ! tab.link.hasClass('hidden') ) {
328
control.selected = tab;
329
tab.both.addClass('library-selected');
336
success: function( attachment ) {
337
api.UploadControl.prototype.success.call( this, attachment );
339
// Add the uploaded image to the uploaded tab.
340
if ( this.tabs.uploaded && this.tabs.uploaded.target.length ) {
341
this.tabs.uploaded.both.removeClass('hidden');
343
// @todo: Do NOT store this on the attachment model. That is bad.
344
attachment.element = $( '<a href="#" class="thumbnail"></a>' )
345
.data( 'customizeImageValue', attachment.get('url') )
346
.append( '<img src="' + attachment.get('url')+ '" />' )
347
.appendTo( this.tabs.uploaded.target );
350
thumbnailSrc: function( to ) {
351
if ( /^(https?:)?\/\//.test( to ) )
352
this.thumbnail.prop( 'src', to ).show();
354
this.thumbnail.hide();
360
* @augments wp.customize.Control
361
* @augments wp.customize.Class
363
api.HeaderControl = api.Control.extend({
365
this.btnRemove = $('#customize-control-header_image .actions .remove');
366
this.btnNew = $('#customize-control-header_image .actions .new');
368
_.bindAll(this, 'openMedia', 'removeImage');
370
this.btnNew.on( 'click', this.openMedia );
371
this.btnRemove.on( 'click', this.removeImage );
373
api.HeaderTool.currentHeader = new api.HeaderTool.ImageModel();
375
new api.HeaderTool.CurrentView({
376
model: api.HeaderTool.currentHeader,
377
el: '.current .container'
380
new api.HeaderTool.ChoiceListView({
381
collection: api.HeaderTool.UploadsList = new api.HeaderTool.ChoiceList(),
382
el: '.choices .uploaded .list'
385
new api.HeaderTool.ChoiceListView({
386
collection: api.HeaderTool.DefaultsList = new api.HeaderTool.DefaultsList(),
387
el: '.choices .default .list'
390
api.HeaderTool.combinedList = api.HeaderTool.CombinedList = new api.HeaderTool.CombinedList([
391
api.HeaderTool.UploadsList,
392
api.HeaderTool.DefaultsList
397
* Returns a set of options, computed from the attached image data and
398
* theme-specific data, to be fed to the imgAreaSelect plugin in
399
* wp.media.view.Cropper.
401
* @param {wp.media.model.Attachment} attachment
402
* @param {wp.media.controller.Cropper} controller
403
* @returns {Object} Options
405
calculateImageSelectOptions: function(attachment, controller) {
406
var xInit = parseInt(_wpCustomizeHeader.data.width, 10),
407
yInit = parseInt(_wpCustomizeHeader.data.height, 10),
408
flexWidth = !! parseInt(_wpCustomizeHeader.data['flex-width'], 10),
409
flexHeight = !! parseInt(_wpCustomizeHeader.data['flex-height'], 10),
410
ratio, xImg, yImg, realHeight, realWidth,
413
realWidth = attachment.get('width');
414
realHeight = attachment.get('height');
416
this.headerImage = new api.HeaderTool.ImageModel();
417
this.headerImage.set({
420
themeFlexWidth: flexWidth,
421
themeFlexHeight: flexHeight,
422
imageWidth: realWidth,
423
imageHeight: realHeight
426
controller.set( 'canSkipCrop', ! this.headerImage.shouldBeCropped() );
428
ratio = xInit / yInit;
432
if ( xImg / yImg > ratio ) {
434
xInit = yInit * ratio;
437
yInit = xInit / ratio;
445
imageWidth: realWidth,
446
imageHeight: realHeight,
453
if (flexHeight === false && flexWidth === false) {
454
imgSelectOptions.aspectRatio = xInit + ':' + yInit;
456
if (flexHeight === false ) {
457
imgSelectOptions.maxHeight = yInit;
459
if (flexWidth === false ) {
460
imgSelectOptions.maxWidth = xInit;
463
return imgSelectOptions;
467
* Sets up and opens the Media Manager in order to select an image.
468
* Depending on both the size of the image and the properties of the
469
* current theme, a cropping step after selection may be required or
472
* @param {event} event
474
openMedia: function(event) {
475
var l10n = _wpMediaViewsL10n;
477
event.preventDefault();
479
this.frame = wp.media({
481
text: l10n.selectAndCrop,
485
new wp.media.controller.Library({
486
title: l10n.chooseImage,
487
library: wp.media.query({ type: 'image' }),
490
suggestedWidth: _wpCustomizeHeader.data.width,
491
suggestedHeight: _wpCustomizeHeader.data.height
493
new wp.media.controller.Cropper({
494
imgSelectOptions: this.calculateImageSelectOptions
499
this.frame.on('select', this.onSelect, this);
500
this.frame.on('cropped', this.onCropped, this);
501
this.frame.on('skippedcrop', this.onSkippedCrop, this);
506
onSelect: function() {
507
this.frame.setState('cropper');
509
onCropped: function(croppedImage) {
510
var url = croppedImage.post_content,
511
attachmentId = croppedImage.attachment_id,
512
w = croppedImage.width,
513
h = croppedImage.height;
514
this.setImageFromURL(url, attachmentId, w, h);
516
onSkippedCrop: function(selection) {
517
var url = selection.get('url'),
518
w = selection.get('width'),
519
h = selection.get('height');
520
this.setImageFromURL(url, selection.id, w, h);
524
* Creates a new wp.customize.HeaderTool.ImageModel from provided
525
* header image data and inserts it into the user-uploaded headers
528
* @param {String} url
529
* @param {Number} attachmentId
530
* @param {Number} width
531
* @param {Number} height
533
setImageFromURL: function(url, attachmentId, width, height) {
534
var choice, data = {};
537
data.thumbnail_url = url;
538
data.timestamp = _.now();
541
data.attachment_id = attachmentId;
549
data.height = height;
552
choice = new api.HeaderTool.ImageModel({
554
choice: url.split('/').pop()
556
api.HeaderTool.UploadsList.add(choice);
557
api.HeaderTool.currentHeader.set(choice.toJSON());
559
choice.importImage();
563
* Triggers the necessary events to deselect an image which was set as
564
* the currently selected one.
566
removeImage: function() {
567
api.HeaderTool.currentHeader.trigger('hide');
568
api.HeaderTool.CombinedList.trigger('control:removeImage');
573
// Change objects contained within the main customize object to Settings.
574
api.defaultConstructor = api.Setting;
576
// Create the collection of Control objects.
577
api.control = new api.Values({ defaultConstructor: api.Control });
581
* @augments wp.customize.Messenger
582
* @augments wp.customize.Class
583
* @mixes wp.customize.Events
585
api.PreviewFrame = api.Messenger.extend({
588
initialize: function( params, options ) {
589
var deferred = $.Deferred();
591
// This is the promise object.
592
deferred.promise( this );
594
this.container = params.container;
595
this.signature = params.signature;
597
$.extend( params, { channel: api.PreviewFrame.uuid() });
599
api.Messenger.prototype.initialize.call( this, params, options );
601
this.add( 'previewUrl', params.previewUrl );
603
this.query = $.extend( params.query || {}, { customize_messenger_channel: this.channel() });
605
this.run( deferred );
608
run: function( deferred ) {
614
this.unbind( 'ready', this._ready );
616
this._ready = function() {
620
deferred.resolveWith( self );
623
this.bind( 'ready', this._ready );
625
this.bind( 'ready', function ( data ) {
626
if ( ! data || ! data.activeControls ) {
630
$.each( data.activeControls, function ( id, active ) {
631
var control = api.control( id );
633
control.active( active );
638
this.request = $.ajax( this.previewUrl(), {
642
withCredentials: true
646
this.request.fail( function() {
647
deferred.rejectWith( self, [ 'request failure' ] );
650
this.request.done( function( response ) {
651
var location = self.request.getResponseHeader('Location'),
652
signature = self.signature,
655
// Check if the location response header differs from the current URL.
656
// If so, the request was redirected; try loading the requested page.
657
if ( location && location != self.previewUrl() ) {
658
deferred.rejectWith( self, [ 'redirect', location ] );
662
// Check if the user is not logged in.
663
if ( '0' === response ) {
664
self.login( deferred );
668
// Check for cheaters.
669
if ( '-1' === response ) {
670
deferred.rejectWith( self, [ 'cheatin' ] );
674
// Check for a signature in the request.
675
index = response.lastIndexOf( signature );
676
if ( -1 === index || index < response.lastIndexOf('</html>') ) {
677
deferred.rejectWith( self, [ 'unsigned' ] );
681
// Strip the signature from the request.
682
response = response.slice( 0, index ) + response.slice( index + signature.length );
684
// Create the iframe and inject the html content.
685
self.iframe = $('<iframe />').appendTo( self.container );
687
// Bind load event after the iframe has been added to the page;
688
// otherwise it will fire when injected into the DOM.
689
self.iframe.one( 'load', function() {
693
deferred.resolveWith( self );
695
setTimeout( function() {
696
deferred.rejectWith( self, [ 'ready timeout' ] );
697
}, self.sensitivity );
701
self.targetWindow( self.iframe[0].contentWindow );
703
self.targetWindow().document.open();
704
self.targetWindow().document.write( response );
705
self.targetWindow().document.close();
709
login: function( deferred ) {
713
reject = function() {
714
deferred.rejectWith( self, [ 'logged out' ] );
717
if ( this.triedLogin )
720
// Check if we have an admin cookie.
721
$.get( api.settings.url.ajax, {
723
}).fail( reject ).done( function( response ) {
726
if ( '1' !== response )
729
iframe = $('<iframe src="' + self.previewUrl() + '" />').hide();
730
iframe.appendTo( self.container );
731
iframe.load( function() {
732
self.triedLogin = true;
735
self.run( deferred );
740
destroy: function() {
741
api.Messenger.prototype.destroy.call( this );
742
this.request.abort();
745
this.iframe.remove();
749
delete this.targetWindow;
756
* Create a universally unique identifier.
760
api.PreviewFrame.uuid = function() {
761
return 'preview-' + uuid++;
767
* @augments wp.customize.Messenger
768
* @augments wp.customize.Class
769
* @mixes wp.customize.Events
771
api.Previewer = api.Messenger.extend({
776
* - container - a selector or jQuery element
777
* - previewUrl - the URL of preview frame
779
initialize: function( params, options ) {
783
$.extend( this, options || {} );
786
* Wrap this.refresh to prevent it from hammering the servers:
788
* If refresh is called once and no other refresh requests are
789
* loading, trigger the request immediately.
791
* If refresh is called while another refresh request is loading,
792
* debounce the refresh requests:
793
* 1. Stop the loading request (as it is instantly outdated).
794
* 2. Trigger the new request once refresh hasn't been called for
795
* self.refreshBuffer milliseconds.
797
this.refresh = (function( self ) {
798
var refresh = self.refresh,
799
callback = function() {
801
refresh.call( self );
806
if ( typeof timeout !== 'number' ) {
807
if ( self.loading ) {
814
clearTimeout( timeout );
815
timeout = setTimeout( callback, self.refreshBuffer );
819
this.container = api.ensure( params.container );
820
this.allowedUrls = params.allowedUrls;
821
this.signature = params.signature;
823
params.url = window.location.href;
825
api.Messenger.prototype.initialize.call( this, params );
827
this.add( 'scheme', this.origin() ).link( this.origin ).setter( function( to ) {
828
var match = to.match( rscheme );
829
return match ? match[0] : '';
832
// Limit the URL to internal, front-end links.
834
// If the frontend and the admin are served from the same domain, load the
835
// preview over ssl if the customizer is being loaded over ssl. This avoids
836
// insecure content warnings. This is not attempted if the admin and frontend
837
// are on different domains to avoid the case where the frontend doesn't have
840
this.add( 'previewUrl', params.previewUrl ).setter( function( to ) {
843
// Check for URLs that include "/wp-admin/" or end in "/wp-admin".
844
// Strip hashes and query strings before testing.
845
if ( /\/wp-admin(\/|$)/.test( to.replace( /[#?].*$/, '' ) ) )
848
// Attempt to match the URL to the control frame's scheme
849
// and check if it's allowed. If not, try the original URL.
850
$.each([ to.replace( rscheme, self.scheme() ), to ], function( i, url ) {
851
$.each( self.allowedUrls, function( i, allowed ) {
854
allowed = allowed.replace( /\/+$/, '' );
855
path = url.replace( allowed, '' );
857
if ( 0 === url.indexOf( allowed ) && /^([/#?]|$)/.test( path ) ) {
866
// If we found a matching result, return it. If not, bail.
867
return result ? result : null;
870
// Refresh the preview when the URL is changed (but not yet).
871
this.previewUrl.bind( this.refresh );
874
this.bind( 'scroll', function( distance ) {
875
this.scroll = distance;
878
// Update the URL when the iframe sends a URL message.
879
this.bind( 'url', this.previewUrl );
882
query: function() {},
885
if ( this.loading ) {
886
this.loading.destroy();
891
refresh: function() {
896
this.loading = new api.PreviewFrame({
898
previewUrl: this.previewUrl(),
899
query: this.query() || {},
900
container: this.container,
901
signature: this.signature
904
this.loading.done( function() {
905
// 'this' is the loading frame
906
this.bind( 'synced', function() {
908
self.preview.destroy();
912
self.targetWindow( this.targetWindow() );
913
self.channel( this.channel() );
915
self.send( 'active' );
924
this.loading.fail( function( reason, location ) {
925
if ( 'redirect' === reason && location )
926
self.previewUrl( location );
928
if ( 'logged out' === reason ) {
929
if ( self.preview ) {
930
self.preview.destroy();
934
self.login().done( self.refresh );
937
if ( 'cheatin' === reason )
943
var previewer = this,
944
deferred, messenger, iframe;
949
deferred = $.Deferred();
950
this._login = deferred.promise();
952
messenger = new api.Messenger({
954
url: api.settings.url.login
957
iframe = $('<iframe src="' + api.settings.url.login + '" />').appendTo( this.container );
959
messenger.targetWindow( iframe[0].contentWindow );
961
messenger.bind( 'login', function() {
964
delete previewer._login;
971
cheatin: function() {
972
$( document.body ).empty().addClass('cheatin').append( '<p>' + api.l10n.cheatin + '</p>' );
976
api.controlConstructor = {
977
color: api.ColorControl,
978
upload: api.UploadControl,
979
image: api.ImageControl,
980
header: api.HeaderControl
984
api.settings = window._wpCustomizeSettings;
985
api.l10n = window._wpCustomizeControlsL10n;
987
// Check if we can run the customizer.
988
if ( ! api.settings )
991
// Redirect to the fallback preview if any incompatibilities are found.
992
if ( ! $.support.postMessage || ( ! $.support.cors && api.settings.isCrossDomain ) )
993
return window.location = api.settings.url.fallback;
995
var parent, topFocus,
996
body = $( document.body ),
997
overlay = body.children( '.wp-full-overlay' ),
998
title = $( '#customize-info .theme-name.site-title' ),
999
closeBtn = $( '.customize-controls-close' ),
1000
saveBtn = $( '#save' );
1002
// Prevent the form from saving when enter is pressed on an input or select element.
1003
$('#customize-controls').on( 'keydown', function( e ) {
1004
var isEnter = ( 13 === e.which ),
1005
$el = $( e.target );
1007
if ( isEnter && ( $el.is( 'input:not([type=button])' ) || $el.is( 'select' ) ) ) {
1012
// Initialize Previewer
1013
api.previewer = new api.Previewer({
1014
container: '#customize-preview',
1015
form: '#customize-controls',
1016
previewUrl: api.settings.url.preview,
1017
allowedUrls: api.settings.url.allowed,
1018
signature: 'WP_CUSTOMIZER_SIGNATURE'
1021
nonce: api.settings.nonce,
1026
theme: api.settings.theme.stylesheet,
1027
customized: JSON.stringify( api.get() ),
1028
nonce: this.nonce.preview
1034
query = $.extend( this.query(), {
1035
action: 'customize_save',
1036
nonce: this.nonce.save
1038
processing = api.state( 'processing' ),
1039
submitWhenDoneProcessing,
1042
body.addClass( 'saving' );
1044
submit = function () {
1045
var request = $.post( api.settings.url.ajax, query );
1047
api.trigger( 'save', request );
1049
request.always( function () {
1050
body.removeClass( 'saving' );
1053
request.done( function( response ) {
1054
// Check if the user is logged out.
1055
if ( '0' === response ) {
1056
self.preview.iframe.hide();
1057
self.login().done( function() {
1059
self.preview.iframe.show();
1064
// Check for cheaters.
1065
if ( '-1' === response ) {
1070
api.trigger( 'saved' );
1074
if ( 0 === processing() ) {
1077
submitWhenDoneProcessing = function () {
1078
if ( 0 === processing() ) {
1079
api.state.unbind( 'change', submitWhenDoneProcessing );
1083
api.state.bind( 'change', submitWhenDoneProcessing );
1089
// Refresh the nonces if the preview sends updated nonces over.
1090
api.previewer.bind( 'nonce', function( nonce ) {
1091
$.extend( this.nonce, nonce );
1094
$.each( api.settings.settings, function( id, data ) {
1095
api.create( id, id, data.value, {
1096
transport: data.transport,
1097
previewer: api.previewer
1101
$.each( api.settings.controls, function( id, data ) {
1102
var constructor = api.controlConstructor[ data.type ] || api.Control,
1105
control = api.control.add( id, new constructor( id, {
1107
previewer: api.previewer
1111
// Check if preview url is valid and load the preview frame.
1112
if ( api.previewer.previewUrl() ) {
1113
api.previewer.refresh();
1115
api.previewer.previewUrl( api.settings.url.home );
1118
// Save and activated states
1120
var state = new api.Values(),
1121
saved = state.create( 'saved' ),
1122
activated = state.create( 'activated' ),
1123
processing = state.create( 'processing' );
1125
state.bind( 'change', function() {
1126
if ( ! activated() ) {
1127
saveBtn.val( api.l10n.activate ).prop( 'disabled', false );
1128
closeBtn.find( '.screen-reader-text' ).text( api.l10n.cancel );
1130
} else if ( saved() ) {
1131
saveBtn.val( api.l10n.saved ).prop( 'disabled', true );
1132
closeBtn.find( '.screen-reader-text' ).text( api.l10n.close );
1135
saveBtn.val( api.l10n.save ).prop( 'disabled', false );
1136
closeBtn.find( '.screen-reader-text' ).text( api.l10n.cancel );
1140
// Set default states.
1142
activated( api.settings.theme.active );
1145
api.bind( 'change', function() {
1146
state('saved').set( false );
1149
api.bind( 'saved', function() {
1150
state('saved').set( true );
1151
state('activated').set( true );
1154
activated.bind( function( to ) {
1156
api.trigger( 'activated' );
1159
// Expose states to the API.
1164
saveBtn.click( function( event ) {
1165
api.previewer.save();
1166
event.preventDefault();
1167
}).keydown( function( event ) {
1168
if ( 9 === event.which ) // tab
1170
if ( 13 === event.which ) // enter
1171
api.previewer.save();
1172
event.preventDefault();
1175
closeBtn.keydown( function( event ) {
1176
if ( 9 === event.which ) // tab
1178
if ( 13 === event.which ) // enter
1180
event.preventDefault();
1183
$('.upload-dropzone a.upload').keydown( function( event ) {
1184
if ( 13 === event.which ) // enter
1188
$('.collapse-sidebar').on( 'click keydown', function( event ) {
1189
if ( event.type === 'keydown' && 13 !== event.which ) // enter
1192
overlay.toggleClass( 'collapsed' ).toggleClass( 'expanded' );
1193
event.preventDefault();
1196
// Bind site title display to the corresponding field.
1197
if ( title.length ) {
1198
$( '#customize-control-blogname input' ).on( 'input', function() {
1199
title.text( this.value );
1203
// Create a potential postMessage connection with the parent frame.
1204
parent = new api.Messenger({
1205
url: api.settings.url.parent,
1209
// If we receive a 'back' event, we're inside an iframe.
1210
// Send any clicks to the 'Return' link to the parent page.
1211
parent.bind( 'back', function() {
1212
closeBtn.on( 'click.customize-controls-close', function( event ) {
1213
event.preventDefault();
1214
parent.send( 'close' );
1218
// Prompt user with AYS dialog if leaving the Customizer with unsaved changes
1219
$( window ).on( 'beforeunload', function () {
1220
if ( ! api.state( 'saved' )() ) {
1221
return api.l10n.saveAlert;
1225
// Pass events through to the parent.
1226
$.each( [ 'saved', 'change' ], function ( i, event ) {
1227
api.bind( event, function() {
1228
parent.send( event );
1232
// When activated, let the loader handle redirecting the page.
1233
// If no loader exists, redirect the page ourselves (if a url exists).
1234
api.bind( 'activated', function() {
1235
if ( parent.targetWindow() )
1236
parent.send( 'activated', api.settings.url.activated );
1237
else if ( api.settings.url.activated )
1238
window.location = api.settings.url.activated;
1241
// Initialize the connection with the parent frame.
1242
parent.send( 'ready' );
1244
// Control visibility for default controls
1246
'background_image': {
1247
controls: [ 'background_repeat', 'background_position_x', 'background_attachment' ],
1248
callback: function( to ) { return !! to; }
1251
controls: [ 'page_on_front', 'page_for_posts' ],
1252
callback: function( to ) { return 'page' === to; }
1254
'header_textcolor': {
1255
controls: [ 'header_textcolor' ],
1256
callback: function( to ) { return 'blank' !== to; }
1258
}, function( settingId, o ) {
1259
api( settingId, function( setting ) {
1260
$.each( o.controls, function( i, controlId ) {
1261
api.control( controlId, function( control ) {
1262
var visibility = function( to ) {
1263
control.container.toggle( o.callback( to ) );
1266
visibility( setting.get() );
1267
setting.bind( visibility );
1273
// Juggle the two controls that use header_textcolor
1274
api.control( 'display_header_text', function( control ) {
1277
control.elements[0].unsync( api( 'header_textcolor' ) );
1279
control.element = new api.Element( control.container.find('input') );
1280
control.element.set( 'blank' !== control.setting() );
1282
control.element.bind( function( to ) {
1284
last = api( 'header_textcolor' ).get();
1286
control.setting.set( to ? last : 'blank' );
1289
control.setting.bind( function( to ) {
1290
control.element.set( 'blank' !== to );
1294
api.trigger( 'ready' );
1296
// Make sure left column gets focus
1297
topFocus = closeBtn;
1299
setTimeout(function () {