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
This commit is contained in:
koopersmith 2012-05-15 21:28:21 +00:00
parent 9c3ce86280
commit edc5274b20
1 changed files with 39 additions and 15 deletions

View File

@ -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.
* ===================================================================== */