wp/wp-includes/js/customize-preview.js
author ymh <ymh.work@gmail.com>
Tue, 09 Jun 2015 03:35:32 +0200
changeset 5 5e2f62d02dcd
parent 0 d970ebf37754
child 7 cf61fcea0001
permissions -rw-r--r--
upgrade wordpress + plugins
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
     1
(function( exports, $ ){
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
     2
	var api = wp.customize,
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
     3
		debounce;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
     4
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
     5
	/**
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
     6
	 * Returns a debounced version of the function.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
     7
	 *
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
     8
	 * @todo Require Underscore.js for this file and retire this.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
     9
	 */
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    10
	debounce = function( fn, delay, context ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    11
		var timeout;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    12
		return function() {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    13
			var args = arguments;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    14
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    15
			context = context || this;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    16
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    17
			clearTimeout( timeout );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    18
			timeout = setTimeout( function() {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    19
				timeout = null;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    20
				fn.apply( context, args );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    21
			}, delay );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    22
		};
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    23
	};
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    24
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
    25
	/**
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
    26
	 * @constructor
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
    27
	 * @augments wp.customize.Messenger
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
    28
	 * @augments wp.customize.Class
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
    29
	 * @mixes wp.customize.Events
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
    30
	 */
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    31
	api.Preview = api.Messenger.extend({
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    32
		/**
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    33
		 * Requires params:
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    34
		 *  - url    - the URL of preview frame
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    35
		 */
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    36
		initialize: function( params, options ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    37
			var self = this;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    38
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    39
			api.Messenger.prototype.initialize.call( this, params, options );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    40
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    41
			this.body = $( document.body );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    42
			this.body.on( 'click.preview', 'a', function( event ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    43
				event.preventDefault();
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    44
				self.send( 'scroll', 0 );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    45
				self.send( 'url', $(this).prop('href') );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    46
			});
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    47
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    48
			// You cannot submit forms.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    49
			// @todo: Allow form submissions by mixing $_POST data with the customize setting $_POST data.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    50
			this.body.on( 'submit.preview', 'form', function( event ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    51
				event.preventDefault();
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    52
			});
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    53
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    54
			this.window = $( window );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    55
			this.window.on( 'scroll.preview', debounce( function() {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    56
				self.send( 'scroll', self.window.scrollTop() );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    57
			}, 200 ));
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    58
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    59
			this.bind( 'scroll', function( distance ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    60
				self.window.scrollTop( distance );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    61
			});
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    62
		}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    63
	});
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    64
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    65
	$( function() {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    66
		api.settings = window._wpCustomizeSettings;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    67
		if ( ! api.settings )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    68
			return;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    69
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
    70
		var bg;
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    71
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
    72
		api.preview = new api.Preview({
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    73
			url: window.location.href,
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    74
			channel: api.settings.channel
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    75
		});
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    76
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
    77
		api.preview.bind( 'settings', function( values ) {
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    78
			$.each( values, function( id, value ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    79
				if ( api.has( id ) )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    80
					api( id ).set( value );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    81
				else
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    82
					api.create( id, value );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    83
			});
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    84
		});
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    85
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
    86
		api.preview.trigger( 'settings', api.settings.values );
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    87
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
    88
		api.preview.bind( 'setting', function( args ) {
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    89
			var value;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    90
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    91
			args = args.slice();
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    92
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    93
			if ( value = api( args.shift() ) )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    94
				value.set.apply( value, args );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    95
		});
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    96
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
    97
		api.preview.bind( 'sync', function( events ) {
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    98
			$.each( events, function( event, args ) {
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
    99
				api.preview.trigger( event, args );
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   100
			});
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   101
			api.preview.send( 'synced' );
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   102
		});
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   103
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   104
		api.preview.bind( 'active', function() {
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   105
			if ( api.settings.nonce ) {
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   106
				api.preview.send( 'nonce', api.settings.nonce );
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   107
			}
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   108
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   109
			api.preview.send( 'documentTitle', document.title );
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   110
		});
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   111
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   112
		api.preview.send( 'ready', {
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   113
			activePanels: api.settings.activePanels,
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   114
			activeSections: api.settings.activeSections,
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   115
			activeControls: api.settings.activeControls
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   116
		} );
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   117
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   118
		// Display a loading indicator when preview is reloading, and remove on failure.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   119
		api.preview.bind( 'loading-initiated', function () {
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   120
			$( 'body' ).addClass( 'wp-customizer-unloading' );
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   121
			$( 'html' ).prop( 'title', api.settings.l10n.loading );
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   122
		});
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   123
		api.preview.bind( 'loading-failed', function () {
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   124
			$( 'body' ).removeClass( 'wp-customizer-unloading' );
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   125
			$( 'html' ).prop( 'title', '' );
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   126
		});
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   127
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   128
		/* Custom Backgrounds */
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   129
		bg = $.map(['color', 'image', 'position_x', 'repeat', 'attachment'], function( prop ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   130
			return 'background_' + prop;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   131
		});
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   132
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   133
		api.when.apply( api, bg ).done( function( color, image, position_x, repeat, attachment ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   134
			var body = $(document.body),
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   135
				head = $('head'),
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   136
				style = $('#custom-background-css'),
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   137
				update;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   138
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   139
			update = function() {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   140
				var css = '';
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   141
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   142
				// The body will support custom backgrounds if either
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   143
				// the color or image are set.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   144
				//
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   145
				// See get_body_class() in /wp-includes/post-template.php
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   146
				body.toggleClass( 'custom-background', !! ( color() || image() ) );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   147
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   148
				if ( color() )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   149
					css += 'background-color: ' + color() + ';';
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   150
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   151
				if ( image() ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   152
					css += 'background-image: url("' + image() + '");';
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   153
					css += 'background-position: top ' + position_x() + ';';
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   154
					css += 'background-repeat: ' + repeat() + ';';
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   155
					css += 'background-attachment: ' + attachment() + ';';
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   156
				}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   157
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   158
				// Refresh the stylesheet by removing and recreating it.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   159
				style.remove();
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   160
				style = $('<style type="text/css" id="custom-background-css">body.custom-background { ' + css + ' }</style>').appendTo( head );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   161
			};
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   162
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   163
			$.each( arguments, function() {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   164
				this.bind( update );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   165
			});
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   166
		});
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   167
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   168
		api.trigger( 'preview-ready' );
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   169
	});
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   170
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   171
})( wp, jQuery );