Customizer: Move private helper functions to wp.customize.utils so they can be unit tested.

Includes unit tests.

props ryankienstra, westonruter.
see #28709.
Built from https://develop.svn.wordpress.org/trunk@30716


git-svn-id: http://core.svn.wordpress.org/trunk@30706 1a063a9b-81f0-0310-95a4-ce76da25c4cd
This commit is contained in:
Dominik Schilling 2014-12-02 22:56:45 +00:00
parent 2c25a4947f
commit 858a51273a
3 changed files with 53 additions and 51 deletions

View File

@ -1,8 +1,6 @@
/* globals _wpCustomizeHeader, _wpMediaViewsL10n */
(function( exports, $ ){
var bubbleChildValueChanges, Container, focus, isKeydownButNotEnterEvent, areElementListsEqual, prioritySort, api = wp.customize;
// @todo Move private helper functions to wp.customize.utils so they can be unit tested
var Container, focus, api = wp.customize;
/**
* @class
@ -32,13 +30,18 @@
}
});
/**
* Utility function namespace
*/
api.utils = {};
/**
* Watch all changes to Value properties, and bubble changes to parent Values instance
*
* @param {wp.customize.Class} instance
* @param {Array} properties The names of the Value instances to watch.
*/
bubbleChildValueChanges = function ( instance, properties ) {
api.utils.bubbleChildValueChanges = function ( instance, properties ) {
$.each( properties, function ( i, key ) {
instance[ key ].bind( function ( to, from ) {
if ( instance.parent && to !== from ) {
@ -86,7 +89,7 @@
* @param {(wp.customize.Panel|wp.customize.Section|wp.customize.Control)} b
* @returns {Number}
*/
prioritySort = function ( a, b ) {
api.utils.prioritySort = function ( a, b ) {
if ( a.priority() === b.priority() && typeof a.params.instanceNumber === 'number' && typeof b.params.instanceNumber === 'number' ) {
return a.params.instanceNumber - b.params.instanceNumber;
} else {
@ -100,7 +103,7 @@
* @param {jQuery.Event} event
* @returns {boolean}
*/
isKeydownButNotEnterEvent = function ( event ) {
api.utils.isKeydownButNotEnterEvent = function ( event ) {
return ( 'keydown' === event.type && 13 !== event.which );
};
@ -111,7 +114,7 @@
* @param {Array|jQuery} listB
* @returns {boolean}
*/
areElementListsEqual = function ( listA, listB ) {
api.utils.areElementListsEqual = function ( listA, listB ) {
var equal = (
listA.length === listB.length && // if lists are different lengths, then naturally they are not equal
-1 === _.map( // are there any false values in the list returned by map?
@ -142,7 +145,7 @@
container.container = $( container.params.content );
container.deferred = {
ready: new $.Deferred()
embedded: new $.Deferred()
};
container.priority = new api.Value();
container.active = new api.Value();
@ -155,22 +158,20 @@
args = $.extend( {}, container.defaultActiveArguments, args );
active = ( active && container.isContextuallyActive() );
container.onChangeActive( active, args );
// @todo trigger 'activated' and 'deactivated' events based on the expanded param?
});
container.expanded.bind( function ( expanded ) {
var args = container.expandedArgumentsQueue.shift();
args = $.extend( {}, container.defaultExpandedArguments, args );
container.onChangeExpanded( expanded, args );
// @todo trigger 'expanded' and 'collapsed' events based on the expanded param?
});
container.attachEvents();
bubbleChildValueChanges( container, [ 'priority', 'active' ] );
api.utils.bubbleChildValueChanges( container, [ 'priority', 'active' ] );
container.priority.set( isNaN( container.params.priority ) ? 100 : container.params.priority );
container.active.set( container.params.active );
container.expanded.set( false ); // @todo True if deeplinking?
container.expanded.set( false );
},
/**
@ -193,7 +194,7 @@
children.push( child );
}
} );
children.sort( prioritySort );
children.sort( api.utils.prioritySort );
return children;
},
@ -202,7 +203,7 @@
* @abstract
*/
isContextuallyActive: function () {
throw new Error( 'Must override with subclass.' );
throw new Error( 'Container.isContextuallyActive() must be overridden in a subclass.' );
},
/**
@ -335,10 +336,10 @@
$( section.container ).toggleClass( 'control-subsection', !! id );
});
section.panel.set( section.params.panel || '' );
bubbleChildValueChanges( section, [ 'panel' ] );
api.utils.bubbleChildValueChanges( section, [ 'panel' ] );
section.embed();
section.deferred.ready.done( function () {
section.deferred.embedded.done( function () {
section.ready();
});
},
@ -356,12 +357,12 @@
// The panel has been supplied, so wait until the panel object is registered
api.panel( panelId, function ( panel ) {
// The panel has been registered, wait for it to become ready/initialized
panel.deferred.ready.done( function () {
panel.deferred.embedded.done( function () {
parentContainer = panel.container.find( 'ul:first' );
if ( ! section.container.parent().is( parentContainer ) ) {
parentContainer.append( section.container );
}
section.deferred.ready.resolve(); // @todo Better to use `embedded` instead of `ready`
section.deferred.embedded.resolve();
});
} );
} else {
@ -370,7 +371,7 @@
if ( ! section.container.parent().is( parentContainer ) ) {
parentContainer.append( section.container );
}
section.deferred.ready.resolve();
section.deferred.embedded.resolve();
}
};
section.panel.bind( inject );
@ -385,7 +386,7 @@
// Expand/Collapse accordion sections on click.
section.container.find( '.accordion-section-title' ).on( 'click keydown', function( event ) {
if ( isKeydownButNotEnterEvent( event ) ) {
if ( api.utils.isKeydownButNotEnterEvent( event ) ) {
return;
}
event.preventDefault(); // Keep this AFTER the key filter above
@ -479,7 +480,7 @@
var panel = this;
Container.prototype.initialize.call( panel, id, options );
panel.embed();
panel.deferred.ready.done( function () {
panel.deferred.embedded.done( function () {
panel.ready();
});
},
@ -494,7 +495,7 @@
if ( ! panel.container.parent().is( parentContainer ) ) {
parentContainer.append( panel.container );
}
panel.deferred.ready.resolve();
panel.deferred.embedded.resolve();
},
/**
@ -505,7 +506,7 @@
// Expand/Collapse accordion sections on click.
panel.container.find( '.accordion-section-title' ).on( 'click keydown', function( event ) {
if ( isKeydownButNotEnterEvent( event ) ) {
if ( api.utils.isKeydownButNotEnterEvent( event ) ) {
return;
}
event.preventDefault(); // Keep this AFTER the key filter above
@ -518,7 +519,7 @@
meta = panel.container.find( '.panel-meta:first' );
meta.find( '> .accordion-section-title' ).on( 'click keydown', function( event ) {
if ( isKeydownButNotEnterEvent( event ) ) {
if ( api.utils.isKeydownButNotEnterEvent( event ) ) {
return;
}
event.preventDefault(); // Keep this AFTER the key filter above
@ -676,7 +677,7 @@
control.container = control.params.content ? $( control.params.content ) : $( control.selector );
control.deferred = {
ready: new $.Deferred()
embedded: new $.Deferred()
};
control.section = new api.Value();
control.priority = new api.Value();
@ -720,7 +721,7 @@
control.priority.set( isNaN( control.params.priority ) ? 10 : control.params.priority );
control.active.set( control.params.active );
bubbleChildValueChanges( control, [ 'section', 'priority', 'active' ] );
api.utils.bubbleChildValueChanges( control, [ 'section', 'priority', 'active' ] );
// Associate this control with its settings when they are created
settings = $.map( control.params.settings, function( value ) {
@ -739,7 +740,7 @@
control.embed();
}) );
control.deferred.ready.done( function () {
control.deferred.embedded.done( function () {
control.ready();
});
},
@ -760,13 +761,13 @@
// Wait for the section to be registered
api.section( sectionId, function ( section ) {
// Wait for the section to be ready/initialized
section.deferred.ready.done( function () {
section.deferred.embedded.done( function () {
parentContainer = section.container.find( 'ul:first' );
if ( ! control.container.parent().is( parentContainer ) ) {
parentContainer.append( control.container );
control.renderContent();
}
control.deferred.ready.resolve(); // @todo Better to use `embedded` instead of `ready`
control.deferred.embedded.resolve();
});
});
};
@ -852,7 +853,7 @@
// Support the .dropdown class to open/close complex elements
this.container.on( 'click keydown', '.dropdown', function( event ) {
if ( isKeydownButNotEnterEvent( event ) ) {
if ( api.utils.isKeydownButNotEnterEvent( event ) ) {
return;
}
@ -954,7 +955,7 @@
* Open the media modal.
*/
openFrame: function( event ) {
if ( event.type === 'keydown' && 13 !== event.which ) { // enter
if ( api.utils.isKeydownButNotEnterEvent( event ) ) {
return;
}
@ -1008,7 +1009,7 @@
* Reset the setting to the default value.
*/
restoreDefault: function( event ) {
if ( event.type === 'keydown' && 13 !== event.which ) { // enter
if ( api.utils.isKeydownButNotEnterEvent( event ) ) {
return;
}
event.preventDefault();
@ -1023,7 +1024,7 @@
* @param {object} event jQuery Event object
*/
removeFile: function( event ) {
if ( event.type === 'keydown' && 13 !== event.which ) { // enter
if ( api.utils.isKeydownButNotEnterEvent( event ) ) {
return;
}
event.preventDefault();
@ -1742,8 +1743,9 @@
api.l10n = window._wpCustomizeControlsL10n;
// Check if we can run the Customizer.
if ( ! api.settings )
if ( ! api.settings ) {
return;
}
// Redirect to the fallback preview if any incompatibilities are found.
if ( ! $.support.postMessage || ( ! $.support.cors && api.settings.isCrossDomain ) )
@ -1768,7 +1770,7 @@
// Expand/Collapse the main customizer customize info
$( '#customize-info' ).find( '> .accordion-section-title' ).on( 'click keydown', function( event ) {
if ( isKeydownButNotEnterEvent( event ) ) {
if ( api.utils.isKeydownButNotEnterEvent( event ) ) {
return;
}
event.preventDefault(); // Keep this AFTER the key filter above
@ -1930,7 +1932,7 @@
if ( id && api[ type ]( id ) ) {
instance = api[ type ]( id );
// Wait until the element is embedded in the DOM
instance.deferred.ready.done( function () {
instance.deferred.embedded.done( function () {
// Wait until the preview has activated and so active panels, sections, controls have been set
api.previewer.deferred.active.done( function () {
instance.focus();
@ -1956,7 +1958,7 @@
sectionContainers = _.pluck( sections, 'container' );
rootNodes.push( panel );
appendContainer = panel.container.find( 'ul:first' );
if ( ! areElementListsEqual( sectionContainers, appendContainer.children( '[id]' ) ) ) {
if ( ! api.utils.areElementListsEqual( sectionContainers, appendContainer.children( '[id]' ) ) ) {
_( sections ).each( function ( section ) {
appendContainer.append( section.container );
} );
@ -1972,7 +1974,7 @@
rootNodes.push( section );
}
appendContainer = section.container.find( 'ul:first' );
if ( ! areElementListsEqual( controlContainers, appendContainer.children( '[id]' ) ) ) {
if ( ! api.utils.areElementListsEqual( controlContainers, appendContainer.children( '[id]' ) ) ) {
_( controls ).each( function ( control ) {
appendContainer.append( control.container );
} );
@ -1981,10 +1983,10 @@
} );
// Sort the root panels and sections
rootNodes.sort( prioritySort );
rootNodes.sort( api.utils.prioritySort );
rootContainers = _.pluck( rootNodes, 'container' );
appendContainer = $( '#customize-theme-controls' ).children( 'ul' ); // @todo This should be defined elsewhere, and to be configurable
if ( ! areElementListsEqual( rootContainers, appendContainer.children() ) ) {
if ( ! api.utils.areElementListsEqual( rootContainers, appendContainer.children() ) ) {
_( rootNodes ).each( function ( rootNode ) {
appendContainer.append( rootNode.container );
} );
@ -2080,7 +2082,7 @@
// Go back to the top-level Customizer accordion.
$( '#customize-header-actions' ).on( 'click keydown', '.control-panel-back', function( event ) {
if ( isKeydownButNotEnterEvent( event ) ) {
if ( api.utils.isKeydownButNotEnterEvent( event ) ) {
return;
}
@ -2099,7 +2101,7 @@
});
$('.collapse-sidebar').on( 'click keydown', function( event ) {
if ( isKeydownButNotEnterEvent( event ) ) {
if ( api.utils.isKeydownButNotEnterEvent( event ) ) {
return;
}

File diff suppressed because one or more lines are too long

View File

@ -4,7 +4,7 @@
*
* @global string $wp_version
*/
$wp_version = '4.1-beta2-30715';
$wp_version = '4.1-beta2-30716';
/**
* Holds the WordPress DB revision, increments when changes are made to the WordPress DB schema.