From edc5274b2042d07275efd38f7e7a8087a2e36413 Mon Sep 17 00:00:00 2001 From: koopersmith Date: Tue, 15 May 2012 21:28:21 +0000 Subject: [PATCH] Theme Customizer: Add an Events mixin to wp.customize, used by default in wp.customize.Messenger. see #19910. git-svn-id: http://core.svn.wordpress.org/trunk@20795 1a063a9b-81f0-0310-95a4-ce76da25c4cd --- wp-includes/js/customize-base.dev.js | 54 ++++++++++++++++++++-------- 1 file changed, 39 insertions(+), 15 deletions(-) diff --git a/wp-includes/js/customize-base.dev.js b/wp-includes/js/customize-base.dev.js index 84e641f95..4e79aa5c4 100644 --- a/wp-includes/js/customize-base.dev.js +++ b/wp-includes/js/customize-base.dev.js @@ -120,7 +120,29 @@ if ( typeof wp === 'undefined' ) api.Class.extend = extend; /* ===================================================================== - * Light two-way binding. + * Events mixin. + * ===================================================================== */ + + api.Events = { + trigger: function( id ) { + if ( this.topics && this.topics[ id ] ) + this.topics[ id ].fireWith( this, slice.call( arguments, 1 ) ); + }, + + bind: function( id, callback ) { + this.topics = this.topics || {}; + this.topics[ id ] = this.topics[ id ] || $.Callbacks(); + this.topics[ id ].add.apply( this.topics[ id ], slice.call( arguments, 1 ) ); + }, + + unbind: function( id, callback ) { + if ( this.topics && this.topics[ id ] ) + this.topics[ id ].remove.apply( this.topics[ id ], slice.call( arguments, 1 ) ); + } + }; + + /* ===================================================================== + * Observable values that support two-way binding. * ===================================================================== */ api.Value = api.Class.extend({ @@ -227,6 +249,10 @@ if ( typeof wp === 'undefined' ) } }); + /* ===================================================================== + * A collection of observable values. + * ===================================================================== */ + api.Values = api.Class.extend({ defaultConstructor: api.Value, @@ -344,6 +370,13 @@ if ( typeof wp === 'undefined' ) }; }); + + /* ===================================================================== + * An observable value that syncs with an element. + * + * Handles inputs, selects, and textareas by default. + * ===================================================================== */ + api.ensure = function( element ) { return typeof element == 'string' ? $( element ) : element; }; @@ -448,8 +481,6 @@ if ( typeof wp === 'undefined' ) return to.replace( /([^:]+:\/\/[^\/]+).*/, '$1' ); }); - this.topics = {}; - this.receive = $.proxy( this.receive, this ); $( window ).on( 'message', this.receive ); }, @@ -469,8 +500,8 @@ if ( typeof wp === 'undefined' ) message = JSON.parse( event.data ); - if ( message && message.id && typeof message.data !== 'undefined' && this.topics[ message.id ] ) - this.topics[ message.id ].fireWith( this, [ message.data ]); + if ( message && message.id && typeof message.data !== 'undefined' ) + this.trigger( message.id, message.data ); }, send: function( id, data ) { @@ -483,19 +514,12 @@ if ( typeof wp === 'undefined' ) message = JSON.stringify({ id: id, data: data }); this.targetWindow().postMessage( message, this.origin() ); - }, - - bind: function( id, callback ) { - var topic = this.topics[ id ] || ( this.topics[ id ] = $.Callbacks() ); - topic.add( callback ); - }, - - unbind: function( id, callback ) { - if ( this.topics[ id ] ) - this.topics[ id ].remove( callback ); } }); + // Add the Events mixin to api.Messenger. + $.extend( api.Messenger.prototype, api.Events ); + /* ===================================================================== * Core customize object. * ===================================================================== */